ci: improve build and package step in release workflow #12
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Release | |
on: | |
push: | |
tags: | |
- "v*" | |
env: | |
CARGO_TERM_COLOR: always | |
CARGO_INCREMENTAL: 0 | |
CARGO_NET_RETRY: 10 | |
RUSTUP_MAX_RETRIES: 10 | |
RUST_BACKTRACE: short | |
PKG_CONFIG_ALLOW_CROSS: 1 | |
jobs: | |
# First run the same checks as in CI | |
check: | |
name: Check | |
uses: ./.github/workflows/ci.yml | |
build-deb: | |
name: Build Debian Package | |
needs: check | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: | |
target: | |
- armv7-unknown-linux-gnueabihf | |
- aarch64-unknown-linux-gnu | |
- x86_64-unknown-linux-gnu | |
include: | |
- target: armv7-unknown-linux-gnueabihf | |
arch: armhf | |
- target: aarch64-unknown-linux-gnu | |
arch: arm64 | |
- target: x86_64-unknown-linux-gnu | |
arch: amd64 | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Install common dependencies | |
run: | | |
sudo apt-get update | |
sudo apt-get install -y pkg-config | |
- name: Install target specific dependencies | |
if: matrix.target == 'x86_64-unknown-linux-gnu' | |
run: | | |
sudo apt-get update | |
sudo apt-get install -y libudev-dev dpkg-dev | |
- name: Set library path | |
if: matrix.target == 'x86_64-unknown-linux-gnu' | |
run: | | |
echo "LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH" >> $GITHUB_ENV | |
echo "LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:$LIBRARY_PATH" >> $GITHUB_ENV | |
- name: Install Rust toolchain | |
uses: dtolnay/rust-toolchain@stable | |
with: | |
targets: ${{ matrix.target }} | |
- name: Install cross | |
uses: taiki-e/install-action@cross | |
- name: Cache cargo | |
uses: actions/cache@v3 | |
with: | |
path: | | |
~/.cargo/registry | |
~/.cargo/git | |
target | |
key: ${{ runner.os }}-cargo-${{ matrix.target }}-${{ hashFiles('**/Cargo.lock') }} | |
restore-keys: | | |
${{ runner.os }}-cargo-${{ matrix.target }}- | |
- name: Cache cross | |
if: matrix.target != 'x86_64-unknown-linux-gnu' | |
uses: actions/cache@v3 | |
with: | |
path: ~/.cargo/.cross | |
key: ${{ runner.os }}-cross-${{ matrix.target }}-${{ hashFiles('Cross.toml') }} | |
restore-keys: | | |
${{ runner.os }}-cross-${{ matrix.target }}- | |
- name: Install cargo-deb | |
run: cargo install cargo-deb | |
- name: Build and package | |
env: | |
PKG_CONFIG_ALLOW_CROSS: "1" | |
run: | | |
if [ "${{ matrix.target }}" = "x86_64-unknown-linux-gnu" ]; then | |
# Native build | |
cargo build --release --target ${{ matrix.target }} | |
cargo deb --target ${{ matrix.target }} | |
else | |
# Cross compilation requires special paths | |
if [ "${{ matrix.target }}" = "armv7-unknown-linux-gnueabihf" ]; then | |
PKG_PATH="/usr/lib/arm-linux-gnueabihf/pkgconfig" | |
elif [ "${{ matrix.target }}" = "aarch64-unknown-linux-gnu" ]; then | |
PKG_PATH="/usr/lib/aarch64-linux-gnu/pkgconfig" | |
fi | |
# Vars will be passed to both commands thanks to passthrough in Cross.toml | |
export PKG_CONFIG_PATH="$PKG_PATH" | |
export PKG_CONFIG_SYSROOT_DIR="/usr" | |
export PKG_CONFIG_LIBDIR="$PKG_PATH" | |
cross build --release --target ${{ matrix.target }} | |
cargo deb --target ${{ matrix.target }} | |
fi | |
- name: Upload artifacts | |
uses: actions/upload-artifact@v4 | |
with: | |
name: deb-${{ matrix.arch }} | |
path: target/${{ matrix.target }}/debian/*.deb | |
build-arch: | |
name: Build Arch Package | |
needs: check | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: | |
target: | |
- x86_64-unknown-linux-gnu | |
- aarch64-unknown-linux-gnu | |
- armv7-unknown-linux-gnueabihf | |
include: | |
- target: x86_64-unknown-linux-gnu | |
arch: x86_64 | |
- target: aarch64-unknown-linux-gnu | |
arch: aarch64 | |
- target: armv7-unknown-linux-gnueabihf | |
arch: armv7h | |
container: | |
image: archlinux:base-devel | |
options: --privileged | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Install dependencies | |
run: | | |
# Update system | |
pacman -Syu --noconfirm | |
# Install common dependencies | |
pacman -S --noconfirm \ | |
git \ | |
rustup \ | |
cargo \ | |
pkg-config \ | |
libudev \ | |
systemd | |
- name: Setup Rust | |
run: | | |
rustup default stable | |
rustup target add ${{ matrix.target }} | |
- name: Install Rust toolchain | |
uses: dtolnay/rust-toolchain@stable | |
with: | |
targets: ${{ matrix.target }} | |
- name: Install cross | |
uses: taiki-e/install-action@cross | |
- name: Cache cargo | |
uses: actions/cache@v3 | |
with: | |
path: | | |
~/.cargo/registry | |
~/.cargo/git | |
target | |
key: ${{ runner.os }}-arch-cargo-${{ matrix.target }}-${{ hashFiles('**/Cargo.lock') }} | |
restore-keys: | | |
${{ runner.os }}-arch-cargo-${{ matrix.target }}- | |
- name: Cache cross | |
if: matrix.target != 'x86_64-unknown-linux-gnu' | |
uses: actions/cache@v3 | |
with: | |
path: ~/.cargo/.cross | |
key: ${{ runner.os }}-arch-cross-${{ matrix.target }}-${{ hashFiles('Cross.arch.toml') }} | |
restore-keys: | | |
${{ runner.os }}-arch-cross-${{ matrix.target }}- | |
- name: Build | |
env: | |
CROSS_CONFIG: Cross.arch.toml | |
run: | | |
if [ "${{ matrix.target }}" = "x86_64-unknown-linux-gnu" ]; then | |
cargo build --release --target ${{ matrix.target }} | |
else | |
cross build --release --target ${{ matrix.target }} | |
fi | |
- name: Prepare PKGBUILD | |
run: | | |
VERSION=$(grep -m1 'version =' Cargo.toml | cut -d '"' -f2) | |
sed -i "s/pkgver=.*/pkgver=$VERSION/" dist/arch/PKGBUILD | |
cp -r . "/tmp/modbus-relay" | |
cd /tmp | |
tar czf "modbus-relay.tar.gz" "modbus-relay" | |
- name: Build package | |
run: | | |
cd /tmp | |
mkdir -p pkg | |
cd pkg | |
mv ../modbus-relay.tar.gz . | |
cp /tmp/modbus-relay/dist/arch/PKGBUILD . | |
CARCH=${{ matrix.arch }} makepkg -s --noconfirm | |
- name: Upload artifacts | |
uses: actions/upload-artifact@v4 | |
with: | |
name: pkg-${{ matrix.arch }} | |
path: /tmp/pkg/*.pkg.tar.zst | |
create-release: | |
name: Create Release | |
needs: [build-deb, build-arch] | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
steps: | |
- uses: actions/checkout@v4 | |
- uses: actions/download-artifact@v4 | |
- name: Create Release | |
uses: softprops/action-gh-release@v1 | |
with: | |
files: deb-*/*.deb pkg-*/*.pkg.tar.zst | |
generate_release_notes: true |