Skip to content

Commit

Permalink
Merge pull request #6 from kurosakishigure/feature/cd
Browse files Browse the repository at this point in the history
feat: Support binary artifact upload
  • Loading branch information
kurosakishigure authored Dec 4, 2024
2 parents 74a4151 + 26d925c commit 93b335e
Show file tree
Hide file tree
Showing 3 changed files with 258 additions and 1 deletion.
93 changes: 93 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
name: CD

on:
push:
tags: [ '[0-9]+.[0-9]+.[0-9]+', '[0-9]+.[0-9]+.[0-9]+-canary.[0-9]+' ]

permissions:
contents: write

jobs:
publish:
name: Publishing for ${{ matrix.job.os }}
runs-on: ${{ matrix.job.os }}
strategy:
matrix:
job:
- os: macos-latest
target: x86_64-apple-darwin
use-cross: false
- os: macos-latest
target: aarch64-apple-darwin
use-cross: false
- os: windows-latest
target: x86_64-pc-windows-msvc
use-cross: false
- os: ubuntu-20.04
target: x86_64-unknown-linux-gnu
use-cross: false
- os: ubuntu-latest
target: x86_64-unknown-linux-musl
use-cross: true
- os: ubuntu-20.04
target: i686-unknown-linux-gnu
use-cross: true
- os: ubuntu-20.04
target: arm-unknown-linux-gnueabihf
use-cross: true
- os: ubuntu-20.04
target: aarch64-unknown-linux-gnu
use-cross: true

steps:
- name: Installing Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
targets: ${{ matrix.job.target }}
- name: Checkout repository
uses: actions/checkout@v3
- name: Install cross
if: matrix.job.use-cross
uses: taiki-e/install-action@v2
with:
tool: cross
- name: Cargo build
env:
MACOSX_DEPLOYMENT_TARGET: 10.7
shell: bash
run: |
if [[ "${{ matrix.job.use-cross }}" == "true" ]]; then
cross build --release --target ${{ matrix.job.target }}
else
cargo build --release --target ${{ matrix.job.target }}
fi
- name: Install required dependencies
shell: bash
run: |
if [[ ${{ matrix.job.target }} == arm-unknown-linux-gnueabihf ]]; then
sudo apt update
sudo apt-get install -y binutils-arm-linux-gnueabihf
fi
if [[ ${{ matrix.job.target }} == aarch64-unknown-linux-gnu ]]; then
sudo apt update
sudo apt-get install -y binutils-aarch64-linux-gnu
fi
- name: Packaging final binary
shell: bash
env:
TARGET: ${{ matrix.job.target }}
PROJECT_NAME: katharsis
PACKAGE_NAME: katharsis
OS_NAME: ${{ matrix.job.os }}
run: ./etc/cd/before_deploy.sh

- name: Releasing assets
uses: softprops/action-gh-release@v1
with:
files: |
katharsis-*-${{ matrix.job.target }}.*
katharsis*.deb
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: CI

on:
push:
tags: [ 'v[0-9]+.[0-9]+.[0-9]+', 'v[0-9]+.[0-9]+.[0-9]+-canary.[0-9]+' ]
tags: [ '[0-9]+.[0-9]+.[0-9]+', '[0-9]+.[0-9]+.[0-9]+-canary.[0-9]+' ]
pull_request:
branches: [ 'canary' ]

Expand Down
164 changes: 164 additions & 0 deletions etc/cd/before_deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
#!/usr/bin/env bash

set -ex

pack() {
local tempdir
local out_dir
local package_name
local gcc_prefix

tempdir=$(mktemp -d 2>/dev/null || mktemp -d -t tmp)
out_dir=$(pwd)
package_name="$PROJECT_NAME-${GITHUB_REF/refs\/tags\//}-$TARGET"

if [[ $TARGET == "arm-unknown-linux-gnueabihf" ]]; then
gcc_prefix="arm-linux-gnueabihf-"
elif [[ $TARGET == "aarch64-unknown-linux-gnu" ]]; then
gcc_prefix="aarch64-linux-gnu-"
else
gcc_prefix=""
fi

mkdir "$tempdir/$package_name"

cp "target/$TARGET/release/$PROJECT_NAME" "$tempdir/$package_name/"
if [ "$OS_NAME" != windows-latest ]; then
"${gcc_prefix}"strip "$tempdir/$package_name/$PROJECT_NAME"
fi

cp README.md "$tempdir/$package_name"
cp LICENSE "$tempdir/$package_name"

pushd "$tempdir"
if [ "$OS_NAME" = windows-latest ]; then
7z a "$out_dir/$package_name.zip" "$package_name"/*
else
tar czf "$out_dir/$package_name.tar.gz" "$package_name"/*
fi
popd
rm -r "$tempdir"
}

make_deb() {
local tempdir
local architecture
local version
local dpkgname
local conflictname
local gcc_prefix
local homepage
local maintainer

homepage="https://github.com/kurosakishigure/katharsis"
maintainer="Shigure Kurosaki <[email protected]>"
copyright_years="2024 - "$(date "+%Y")

case $TARGET in
x86_64*)
architecture=amd64
gcc_prefix=""
library_dir=""
;;
i686*)
architecture=i386
gcc_prefix=""
library_dir=""
;;
aarch64*)
architecture=arm64
gcc_prefix="aarch64-linux-gnu-"
library_dir="-l/usr/aarch64-linux-gnu/lib"
;;
arm*hf)
architecture=armhf
gcc_prefix="arm-linux-gnueabihf-"
library_dir="-l/usr/arm-linux-gnueabihf/lib"
;;
*)
echo "make_deb: skipping target '${TARGET}'" >&2
return 0
;;
esac
version=${GITHUB_REF/refs\/tags\//}

if [[ $TARGET = *musl* ]]; then
dpkgname=$PACKAGE_NAME-musl
conflictname=$PROJECT_NAME
else
dpkgname=$PACKAGE_NAME
conflictname=$PROJECT_NAME-musl
fi

tempdir=$(mktemp -d 2>/dev/null || mktemp -d -t tmp)

install -Dm755 "target/$TARGET/release/$PROJECT_NAME" "$tempdir/usr/bin/$PROJECT_NAME"
"${gcc_prefix}"strip "$tempdir/usr/bin/$PROJECT_NAME"

mkdir "./debian"
touch "./debian/control"
depends="$(dpkg-shlibdeps $library_dir -O "$tempdir/usr/bin/$PROJECT_NAME" 2> /dev/null | sed 's/^shlibs:Depends=//')"
rm -rf "./debian"

install -Dm644 README.md "$tempdir/usr/share/doc/$dpkgname/README.md"
cat > "$tempdir/usr/share/doc/$dpkgname/copyright" <<EOF
Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Upstream-Name: $PROJECT_NAME
Source: $homepage
Files: *
Copyright: $copyright_years $maintainer
License: MIT
License: MIT
Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without
limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following
conditions:
.
The above copyright notice and this permission notice
shall be included in all copies or substantial portions
of the Software.
.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
EOF
chmod 644 "$tempdir/usr/share/doc/$dpkgname/copyright"

mkdir "$tempdir/DEBIAN"
cat > "$tempdir/DEBIAN/control" <<EOF
Package: $dpkgname
Version: $version
Section: utils
Priority: optional
Maintainer: Shigure Kurosaki <[email protected]>
Architecture: $architecture
Depends: $depends
Conflicts: $conflictname
Description: CLI tool for generating RSS feeds.
EOF

fakeroot dpkg-deb -Zxz --build "$tempdir" "${dpkgname}_${version}_${architecture}.deb"
}


main() {
pack
if [[ $TARGET = *linux* ]]; then
make_deb
fi
}

main

0 comments on commit 93b335e

Please sign in to comment.