name: Release
on:
push:
tags:
- 'v*'
env:
CARGO_TERM_COLOR: always
permissions:
contents: write
jobs:
build-binaries:
name: Build Binaries
runs-on: ${{ matrix.os }}
strategy:
matrix:
include:
- os: ubuntu-latest
target: x86_64-unknown-linux-gnu
suffix: linux-amd64
- os: ubuntu-latest
target: aarch64-unknown-linux-gnu
suffix: linux-arm64
use-cross: true
- os: macos-latest
target: x86_64-apple-darwin
suffix: macos-amd64
- os: macos-latest
target: aarch64-apple-darwin
suffix: macos-arm64
- os: windows-latest
target: x86_64-pc-windows-msvc
suffix: windows-amd64
extension: .exe
steps:
- uses: actions/checkout@v4
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
targets: ${{ matrix.target }}
- name: Install cross
if: matrix.use-cross
run: cargo install cross
- name: Build release binary
run: |
if [ "${{ matrix.use-cross }}" = "true" ]; then
cross build --release --target ${{ matrix.target }}
else
cargo build --release --target ${{ matrix.target }}
fi
shell: bash
- name: Package binary
run: |
cd target/${{ matrix.target }}/release
if [ "${{ matrix.os }}" = "windows-latest" ]; then
7z a ../../../sorting-race-${{ matrix.suffix }}.zip sorting-race.exe
echo "ASSET_PATH=sorting-race-${{ matrix.suffix }}.zip" >> $GITHUB_ENV
else
tar czf ../../../sorting-race-${{ matrix.suffix }}.tar.gz sorting-race
echo "ASSET_PATH=sorting-race-${{ matrix.suffix }}.tar.gz" >> $GITHUB_ENV
fi
cd -
shell: bash
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.suffix }}${{ matrix.extension }}
path: ${{ env.ASSET_PATH }}
create-release:
name: Create Release
runs-on: ubuntu-latest
needs: build-binaries
outputs:
release-id: ${{ steps.create-release.outputs.id }}
steps:
- uses: actions/checkout@v4
- name: Download all artifacts
uses: actions/download-artifact@v4
with:
path: ./artifacts
- name: Display structure of downloaded files
run: ls -R ./artifacts
- name: Create Release
id: create-release
run: |
# Collect all artifacts for upload
assets=""
for dir in artifacts/*/; do
for file in "$dir"*; do
if [ -f "$file" ]; then
assets="$assets $file"
fi
done
done
# Create release with all assets
gh release create ${{ github.ref_name }} \
--title "Release ${{ github.ref_name }}" \
--notes "Automated release for ${{ github.ref_name }}" \
--draft=false \
--prerelease=false \
$assets
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
publish-crate:
name: Publish to crates.io
runs-on: ubuntu-latest
needs: create-release
steps:
- uses: actions/checkout@v4
- name: Install Rust stable
uses: dtolnay/rust-toolchain@stable
- name: Publish to crates.io
run: cargo publish --token $CRATES_IO_TOKEN
env:
CRATES_IO_TOKEN: ${{ secrets.CRATES_IO_TOKEN }}
continue-on-error: true