Skip to content

Merge pull request #40 from thomasschafer/tschafer-use-parallel-walker #7

Merge pull request #40 from thomasschafer/tschafer-use-parallel-walker

Merge pull request #40 from thomasschafer/tschafer-use-parallel-walker #7

Workflow file for this run

name: Release
on:
push:
branches: [ "main" ]
paths:
- 'Cargo.toml'
workflow_dispatch:
permissions:
contents: write
jobs:
prepare-release:
name: Prepare release
runs-on: ubuntu-latest
outputs:
version: ${{ steps.get-package-info.outputs.version }}
should_release: ${{ steps.check-version.outputs.exists == 'false' }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get package info from Cargo.toml
id: get-package-info
run: |
version=$(grep -m1 '^version = ' Cargo.toml | cut -d '"' -f2)
package_name=$(grep -m1 '^name = ' Cargo.toml | cut -d '"' -f2)
echo "version=${version}" >> $GITHUB_OUTPUT
echo "package_name=${package_name}" >> $GITHUB_OUTPUT
- name: Check if version exists on crates.io
id: check-version
run: |
version="${{ steps.get-package-info.outputs.version }}"
package_name="${{ steps.get-package-info.outputs.package_name }}"
if cargo search "${package_name}" | grep -q "^${package_name} = \"${version}\""; then
echo "exists=true" >> $GITHUB_OUTPUT
else
echo "exists=false" >> $GITHUB_OUTPUT
fi
- name: Create release
if: steps.check-version.outputs.exists == 'false'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
version="${{ steps.get-package-info.outputs.version }}"
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git tag -a "v${version}" -m "Release v${version}"
git push origin "v${version}"
gh release create "v${version}" \
--title "v${version}" \
--generate-notes \
--draft # Make it draft until binaries are uploaded
build-and-upload:
needs: prepare-release
if: needs.prepare-release.outputs.should_release == 'true'
name: ${{ matrix.target }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
include:
- os: ubuntu-latest
target: x86_64-unknown-linux-musl
use-cross: true
- os: ubuntu-latest
target: aarch64-unknown-linux-musl
use-cross: true
- os: macos-13
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
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
targets: ${{ matrix.target }}
- name: Handle Rust dependencies caching
uses: Swatinem/rust-cache@v2
with:
key: v1-${{ matrix.target }}
- name: Build binary
uses: clechasseur/rs-cargo@v2
with:
command: build
args: --release --target ${{ matrix.target }}
use-cross: ${{ matrix.use-cross }}
- name: Get binary name from Cargo.toml
shell: bash
run: |
BINARY_NAME=$(grep -m1 '^name' Cargo.toml | cut -d'"' -f2 | cut -d"'" -f2)
echo "BINARY_NAME=$BINARY_NAME" >> $GITHUB_ENV
- name: Create release archive
shell: bash
run: |
cd target/${{ matrix.target }}/release
VERSION="${{ needs.prepare-release.outputs.version }}"
if [ "${{ runner.os }}" = "Windows" ]; then
ARCHIVE="${{ env.BINARY_NAME }}-v${VERSION}-${{ matrix.target }}.zip"
mv "${{ env.BINARY_NAME }}.exe" "${{ env.BINARY_NAME }}-${{ matrix.target }}.exe"
7z a "$ARCHIVE" "${{ env.BINARY_NAME }}-${{ matrix.target }}.exe"
else
ARCHIVE="${{ env.BINARY_NAME }}-v${VERSION}-${{ matrix.target }}.tar.gz"
mv "${{ env.BINARY_NAME }}" "${{ env.BINARY_NAME }}-${{ matrix.target }}"
tar -czvf "$ARCHIVE" "${{ env.BINARY_NAME }}-${{ matrix.target }}"
fi
# Generate checksums
openssl dgst -r -sha256 -out "$ARCHIVE.sha256" "$ARCHIVE"
openssl dgst -r -sha512 -out "$ARCHIVE.sha512" "$ARCHIVE"
echo "ASSET=$ARCHIVE" >> $GITHUB_ENV
- name: Verify binary
shell: bash
run: |
cd target/${{ matrix.target }}/release
case "${{ matrix.target }}" in
*windows*)
7z x -y "$ASSET"
./${{ env.BINARY_NAME }}-${{ matrix.target }}.exe --version ;;
aarch64*)
echo "Can't test an ARM binary on a AMD64 runner" ;;
*)
tar -xvzf "$ASSET"
./${{ env.BINARY_NAME }}-${{ matrix.target }} --version ;;
esac
- name: Upload to release
uses: softprops/action-gh-release@v1
with:
tag_name: v${{ needs.prepare-release.outputs.version }}
files: |
target/${{ matrix.target }}/release/${{ env.ASSET }}
target/${{ matrix.target }}/release/${{ env.ASSET }}.sha256
target/${{ matrix.target }}/release/${{ env.ASSET }}.sha512
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
publish:
needs: [prepare-release, build-and-upload]
if: needs.prepare-release.outputs.should_release == 'true'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
- name: Rust cache
uses: Swatinem/rust-cache@v2
- name: Publish to crates.io
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
run: cargo publish
- name: Publish release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh release edit "v${{ needs.prepare-release.outputs.version }}" --draft=false