Release Build and Publish #33
This file contains hidden or 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 Build and Publish | |
permissions: | |
contents: write | |
pull-requests: read | |
packages: write | |
on: | |
push: | |
tags: | |
- "v*" | |
pull_request: | |
paths: | |
- "**/*.rs" | |
- "**/Cargo.toml" | |
- "Cargo.lock" | |
- "flake.nix" | |
workflow_dispatch: | |
inputs: | |
release_name: | |
description: "Name of release (optional)" | |
required: false | |
default: "" | |
create_release: | |
description: "Create a GitHub release? (true/false)" | |
required: false | |
default: "false" | |
create_binaries: | |
description: "Create a Binaries? (true/false)" | |
required: false | |
default: "false" | |
create_images: | |
description: "Create a Docker Images? (true/false)" | |
required: false | |
default: "false" | |
jobs: | |
setup: | |
runs-on: ubuntu-latest | |
outputs: | |
version: ${{ steps.set-version.outputs.version }} | |
steps: | |
- name: Extract version | |
id: set-version | |
run: | | |
if [ -n "${{ github.event.inputs.release_name }}" ]; then | |
VERSION="${{ github.event.inputs.release_name }}" | |
elif [[ "${GITHUB_REF}" == refs/tags/v* ]]; then | |
VERSION="${GITHUB_REF#refs/tags/v}" | |
elif [[ "${GITHUB_REF}" == refs/tags/* ]]; then | |
VERSION="${GITHUB_REF#refs/tags/}" | |
else | |
VERSION="${GITHUB_REF_NAME}" | |
fi | |
echo "VERSION=$VERSION" | |
echo "version=$VERSION" >> $GITHUB_OUTPUT | |
generate-matrix: | |
runs-on: ubuntu-latest | |
outputs: | |
matrix: ${{ steps.generate-matrix.outputs.matrix }} | |
arch_list: ${{ steps.generate-arch-list.outputs.arch_list }} | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Install Nix | |
uses: cachix/install-nix-action@v30 | |
with: | |
nix_path: nixpkgs=channel:nixos-unstable | |
github_access_token: ${{ secrets.GITHUB_TOKEN }} | |
extra_nix_config: | | |
experimental-features = nix-command flakes | |
- name: Generate matrix | |
id: generate-matrix | |
run: | | |
MATRIX=$(nix run .#matrix --quiet) | |
echo "Generated Matrix:" | |
echo "$MATRIX" | |
echo "matrix=$MATRIX" >> $GITHUB_OUTPUT | |
- name: Generate Arch List | |
id: generate-arch-list | |
run: | | |
ARCH_LIST=$(nix run .#archs --quiet) | |
echo "Generated Archs:" | |
echo "$ARCH_LIST" | |
echo "arch_list=$ARCH_LIST" >> $GITHUB_OUTPUT | |
build: | |
runs-on: ubuntu-latest | |
needs: [setup, generate-matrix] | |
env: | |
VERSION: ${{ needs.setup.outputs.version }} | |
if: ${{ github.event.inputs.create_binaries == 'true' || (github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')) }} | |
strategy: | |
fail-fast: false | |
matrix: | |
include: ${{ fromJson(needs.generate-matrix.outputs.matrix) }} | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Install Nix | |
uses: cachix/install-nix-action@v30 | |
with: | |
nix_path: nixpkgs=channel:nixos-unstable | |
github_access_token: ${{ secrets.GITHUB_TOKEN }} | |
extra_nix_config: | | |
experimental-features = nix-command flakes | |
- name: Set up artifact name | |
run: | | |
echo "ARTIFACT_NAME=grhooks_${{ env.VERSION }}_${{ matrix.os }}_${{ matrix.arch }}.${{ matrix.format }}" >> $GITHUB_ENV | |
- name: Build package | |
run: | | |
echo "Building ${{ matrix.package }} for ${{ matrix.arch }}-${{ matrix.os }}..." | |
nix bundle --bundler "${{ matrix.bundler }}" ".#${{ matrix.package }}" --out-link result | |
mkdir -p dist | |
cp result/*${{ matrix.format }} dist/${{ env.ARTIFACT_NAME }} | |
- name: Upload Artifact | |
uses: actions/upload-artifact@v4 | |
with: | |
name: ${{ env.ARTIFACT_NAME }} | |
path: dist/* | |
create-release: | |
runs-on: ubuntu-latest | |
needs: [setup, build] | |
if: ${{ github.event.inputs.create_release == 'true' || (github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')) }} | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Generate changelog | |
uses: orhun/git-cliff-action@v4 | |
id: git-cliff | |
with: | |
config: cliff.toml | |
args: --latest | |
env: | |
OUTPUT: CHANGES.md | |
GITHUB_REPO: ${{ github.repository }} | |
- name: Get release name | |
id: release_name | |
run: | | |
if [ -n "${{ github.event.inputs.release_name }}" ]; then | |
echo "RELEASE_NAME=${{ github.event.inputs.release_name }}" >> $GITHUB_OUTPUT | |
else | |
echo "RELEASE_NAME=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT | |
fi | |
- name: Download Artifacts | |
uses: actions/download-artifact@v4 | |
with: | |
path: artifacts | |
merge-multiple: true | |
- name: Create Release | |
uses: softprops/action-gh-release@v2 | |
with: | |
make_latest: true | |
prerelease: ${{ steps.release_name.outputs.release_name != '' && contains(steps.release_name.outputs.release_name, 'a') }} | |
tag_name: ${{ needs.setup.outputs.version }} | |
name: ${{ needs.setup.outputs.version }} | |
body: ${{ steps.git-cliff.outputs.content }} | |
files: | | |
artifacts/* | |
CHANGES.md | |
docker-build: | |
runs-on: ubuntu-latest | |
needs: [setup, generate-matrix] | |
env: | |
VERSION: ${{ needs.setup.outputs.version }} | |
if: ${{ github.event.inputs.create_images == 'true' || (github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')) }} | |
strategy: | |
fail-fast: false | |
matrix: | |
include: ${{ fromJson(needs.generate-matrix.outputs.arch_list) }} | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Set Repository Lowercase | |
run: echo "REPOSITORY=${GITHUB_REPOSITORY,,}" >> $GITHUB_ENV | |
- name: Install Nix | |
uses: cachix/install-nix-action@v30 | |
with: | |
nix_path: nixpkgs=channel:nixos-unstable | |
github_access_token: ${{ secrets.GITHUB_TOKEN }} | |
extra_nix_config: | | |
experimental-features = nix-command flakes | |
- name: Log in to GHCR | |
uses: docker/login-action@v3 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Build individual images with Nix | |
run: | | |
nix build .#image-${{ matrix.arch }} | |
docker load < ./result | |
docker tag grhooks:${{ env.VERSION }} ghcr.io/${{ env.REPOSITORY }}:${{ env.VERSION }}-${{ matrix.arch }} | |
- name: Push image | |
run: | | |
docker push ghcr.io/${{ env.REPOSITORY }}:${{ env.VERSION }}-${{ matrix.arch }} | |
docker-publish: | |
runs-on: ubuntu-latest | |
needs: [setup, generate-matrix, docker-build] | |
env: | |
VERSION: ${{ needs.setup.outputs.version }} | |
ARCHS: ${{ needs.generate-matrix.outputs.arch_list }} | |
steps: | |
- name: Log in to GHCR | |
uses: docker/login-action@v3 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Set Repository Lowercase | |
run: echo "REPOSITORY=${GITHUB_REPOSITORY,,}" >> $GITHUB_ENV | |
- name: Create and push manifest | |
run: | | |
IMAGE=ghcr.io/${{ env.REPOSITORY }}:${{ env.VERSION }} | |
echo "Creating manifest for architectures: $ARCHS" | |
manifest_args="" | |
for arch in $(echo "$ARCHS" | jq -r '.[].arch'); do | |
manifest_args="$manifest_args --amend ghcr.io/${{ env.REPOSITORY }}:${{ env.VERSION }}-$arch" | |
done | |
echo "Running docker manifest create $IMAGE $manifest_args" | |
eval docker manifest create $IMAGE $manifest_args | |
docker manifest push $IMAGE |