name: Rust
on:
push:
branches: [ "main" ]
paths-ignore:
- 'README.md'
- 'LICENSE'
pull_request:
branches: [ "main" ]
paths-ignore:
- 'README.md'
- 'LICENSE'
env:
CARGO_TERM_COLOR: always
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
build:
name: Build and Release
if: "!contains(github.event.head_commit.message, '[skip-ci]')"
strategy:
matrix:
include:
- os: ubuntu-latest
target: x86_64-unknown-linux-gnu
artifact_suffix: linux
- os: macos-latest
target: x86_64-apple-darwin
artifact_suffix: macos-intel
- os: macos-latest
target: aarch64-apple-darwin
artifact_suffix: macos-apple-silicon
- os: windows-latest
target: x86_64-pc-windows-msvc
artifact_suffix: windows.exe
runs-on: ${{ matrix.os }}
outputs:
binary_name: ${{ steps.binary_name.outputs.BINARY_NAME }}
version: ${{ steps.package_version.outputs.VERSION }}
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
target: ${{ matrix.target }}
- name: Cache Dependencies
uses: Swatinem/rust-cache@v2
with:
key: rust-${{ matrix.target }}-${{ hashFiles('**/Cargo.lock') }}
shared-key: rust-${{ matrix.target }}
cache-on-failure: true
- name: Build Release Binary
run: cargo build --release --locked --target ${{ matrix.target }}
- name: Extract Binary Name
id: binary_name
shell: bash
run: |
BINARY_NAME=$(cargo metadata --format-version=1 --no-deps | jq -r '.packages[0].targets[] | select(.kind[] == "bin") | .name')
echo "BINARY_NAME=${BINARY_NAME}" >> $GITHUB_ENV
echo "BINARY_NAME=${BINARY_NAME}" >> $GITHUB_OUTPUT
- name: Extract Package Version
id: package_version
shell: bash
run: |
VERSION=$(cargo metadata --format-version=1 --no-deps | jq -r '.packages[0].version')
echo "VERSION=${VERSION}" >> $GITHUB_ENV
echo "VERSION=${VERSION}" >> $GITHUB_OUTPUT
- name: Verify Binary Existence
shell: bash
run: |
if [[ "${{ matrix.os }}" == "windows-latest" ]]; then
[[ -f target/${{ matrix.target }}/release/${{ env.BINARY_NAME }}.exe ]] || { echo "Binary not found on Windows"; exit 1; }
else
[[ -f target/${{ matrix.target }}/release/${{ env.BINARY_NAME }} ]] || { echo "Binary not found on Unix-based OS"; exit 1; }
fi
- name: Archive Binary (Original Artifacts)
shell: bash
run: |
mkdir -p artifacts
if [[ "${{ matrix.os }}" == "windows-latest" ]]; then
cp target/${{ matrix.target }}/release/${{ env.BINARY_NAME }}.exe artifacts/${{ env.BINARY_NAME }}_${{ matrix.artifact_suffix }}
else
cp target/${{ matrix.target }}/release/${{ env.BINARY_NAME }} artifacts/${{ env.BINARY_NAME }}_${{ matrix.artifact_suffix }}
fi
- name: Upload Build Artifact (Original)
uses: actions/upload-artifact@v4
with:
name: ${{ env.BINARY_NAME }}-${{ matrix.artifact_suffix }}
path: artifacts/${{ env.BINARY_NAME }}_${{ matrix.artifact_suffix }}
- name: Prepare Archive (cargo binstall)
shell: bash
run: |
mkdir -p artifacts_binstall
if [[ "${{ matrix.os }}" == "windows-latest" ]]; then
cp target/${{ matrix.target }}/release/${{ env.BINARY_NAME }}.exe artifacts_binstall/typfont.exe
powershell -Command "Compress-Archive -Path artifacts_binstall/typfont.exe -DestinationPath artifacts_binstall/typfont-v${{ env.VERSION }}-${{ matrix.target }}.zip"
else
cp target/${{ matrix.target }}/release/${{ env.BINARY_NAME }} artifacts_binstall/typfont
tar -czvf artifacts_binstall/typfont-v${{ env.VERSION }}-${{ matrix.target }}.tar.gz -C artifacts_binstall typfont
fi
- name: Upload Build Artifact (cargo binstall)
uses: actions/upload-artifact@v4
with:
name: typfont-v${{ env.VERSION }}-${{ matrix.target }}
path: |
artifacts_binstall/typfont-v${{ env.VERSION }}-${{ matrix.target }}.*
release:
name: Create GitHub Release
if: github.ref == 'refs/heads/main'
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Determine Tag Name
id: determine_tag
run: |
VERSION=$(cargo metadata --format-version=1 --no-deps | jq -r '.packages[0].version')
TIMESTAMP=$(date +%Y%m%d%H%M%S)
# Check if the commit message contains [fixed-tag]
if git log -1 --pretty=%B | grep '\[fixed-tag\]'; then
TAG="v${VERSION}"
PRERELEASE=false
else
TAG="v${VERSION}+${TIMESTAMP}"
PRERELEASE=true
fi
echo "TAG=${TAG}" >> $GITHUB_ENV
echo "PRERELEASE=${PRERELEASE}" >> $GITHUB_ENV
git tag ${TAG}
git push origin ${TAG}
shell: bash
- name: Download Artifacts
uses: actions/download-artifact@v4
with:
path: artifacts
- name: Flatten Artifacts
run: |
mkdir -p release_artifacts
find artifacts -type f -exec cp {} release_artifacts/ \;
- name: Upload Binaries to Release
uses: softprops/action-gh-release@v2
with:
files: |
release_artifacts/${{ needs.build.outputs.binary_name }}_linux
release_artifacts/${{ needs.build.outputs.binary_name }}_macos-apple-silicon
release_artifacts/${{ needs.build.outputs.binary_name }}_macos-intel
release_artifacts/${{ needs.build.outputs.binary_name }}_windows.exe
release_artifacts/typfont-v${{ needs.build.outputs.version }}-x86_64-unknown-linux-gnu.tar.gz
release_artifacts/typfont-v${{ needs.build.outputs.version }}-x86_64-apple-darwin.tar.gz
release_artifacts/typfont-v${{ needs.build.outputs.version }}-aarch64-apple-darwin.tar.gz
release_artifacts/typfont-v${{ needs.build.outputs.version }}-x86_64-pc-windows-msvc.zip
tag_name: ${{ env.TAG }}
prerelease: ${{ env.PRERELEASE }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}