Skip to content

Bump Polaris to v0.3.0 (#29) #5

Bump Polaris to v0.3.0 (#29)

Bump Polaris to v0.3.0 (#29) #5

Workflow file for this run

name: release
on:
push:
branches:
- main
tags:
- "v*.*.*"
paths:
- Cargo.toml
workflow_dispatch:
inputs:
tag:
description: Release tag to build and publish, for example v0.1.0
required: true
type: string
permissions:
contents: write
env:
CARGO_TERM_COLOR: always
jobs:
prepare:
runs-on: ubuntu-24.04
outputs:
create_tag: ${{ steps.plan.outputs.create_tag }}
release_sha: ${{ steps.plan.outputs.release_sha }}
should_release: ${{ steps.plan.outputs.should_release }}
source_ref: ${{ steps.plan.outputs.source_ref }}
tag: ${{ steps.plan.outputs.tag }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Determine release plan
id: plan
shell: bash
run: |
set -euo pipefail
event_name="${GITHUB_EVENT_NAME}"
ref_type="${GITHUB_REF_TYPE:-}"
before_sha="${{ github.event.before }}"
should_release="false"
create_tag="false"
release_sha="${GITHUB_SHA}"
source_ref="${GITHUB_SHA}"
tag=""
if [[ "${event_name}" == "workflow_dispatch" ]]; then
tag="${{ inputs.tag }}"
should_release="true"
elif [[ "${ref_type}" == "tag" ]]; then
tag="${GITHUB_REF_NAME}"
should_release="true"
source_ref="${tag}"
else
current_version="$(sed -n 's/^version = "\(.*\)"/\1/p' Cargo.toml | head -n1)"
previous_version=""
if [[ -z "${current_version}" ]]; then
echo "Could not determine crate version from Cargo.toml." >&2
exit 1
fi
if [[ -n "${before_sha}" && "${before_sha}" != "0000000000000000000000000000000000000000" ]]; then
previous_version="$(
git show "${before_sha}:Cargo.toml" 2>/dev/null \
| sed -n 's/^version = "\(.*\)"/\1/p' \
| head -n1 || true
)"
fi
if [[ "${previous_version}" != "${current_version}" ]]; then
tag="v${current_version}"
should_release="true"
create_tag="true"
fi
fi
if [[ "${should_release}" == "true" ]]; then
if [[ -z "${tag}" ]]; then
echo "Release tag is empty." >&2
exit 1
fi
if [[ ! "${tag}" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "Release tag must match vMAJOR.MINOR.PATCH." >&2
exit 1
fi
if git rev-parse -q --verify "refs/tags/${tag}" >/dev/null; then
existing_sha="$(git rev-list -n1 "${tag}")"
if [[ "${event_name}" == "workflow_dispatch" ]]; then
release_sha="${existing_sha}"
source_ref="${tag}"
elif [[ "${existing_sha}" != "${GITHUB_SHA}" ]]; then
echo "Tag ${tag} already exists at ${existing_sha}, not ${GITHUB_SHA}." >&2
exit 1
fi
create_tag="false"
fi
fi
echo "create_tag=${create_tag}" >>"${GITHUB_OUTPUT}"
echo "release_sha=${release_sha}" >>"${GITHUB_OUTPUT}"
echo "should_release=${should_release}" >>"${GITHUB_OUTPUT}"
echo "source_ref=${source_ref}" >>"${GITHUB_OUTPUT}"
echo "tag=${tag}" >>"${GITHUB_OUTPUT}"
validate:
needs: prepare
if: needs.prepare.outputs.should_release == 'true'
runs-on: ubuntu-24.04
outputs:
tag: ${{ steps.meta.outputs.tag }}
version: ${{ steps.meta.outputs.version }}
steps:
- uses: actions/checkout@v4
with:
ref: ${{ needs.prepare.outputs.source_ref }}
- name: Validate Cargo version matches tag
id: meta
shell: bash
run: |
set -euo pipefail
version="$(sed -n 's/^version = "\(.*\)"/\1/p' Cargo.toml | head -n1)"
tag="${{ needs.prepare.outputs.tag }}"
if [[ -z "${version}" ]]; then
echo "Could not determine crate version from Cargo.toml." >&2
exit 1
fi
if [[ "${tag}" != "v${version}" ]]; then
echo "Tag ${tag} does not match Cargo.toml version v${version}." >&2
exit 1
fi
echo "tag=${tag}" >>"${GITHUB_OUTPUT}"
echo "version=${version}" >>"${GITHUB_OUTPUT}"
- uses: dtolnay/rust-toolchain@stable
- name: Run test suite
run: cargo test
build:
needs:
- prepare
- validate
if: needs.prepare.outputs.should_release == 'true'
strategy:
fail-fast: false
matrix:
include:
- runner: ubuntu-24.04
target: x86_64-unknown-linux-gnu
- runner: ubuntu-24.04-arm
target: aarch64-unknown-linux-gnu
- runner: macos-15-intel
target: x86_64-apple-darwin
- runner: macos-14
target: aarch64-apple-darwin
runs-on: ${{ matrix.runner }}
steps:
- uses: actions/checkout@v4
with:
ref: ${{ needs.prepare.outputs.source_ref }}
- uses: dtolnay/rust-toolchain@stable
with:
targets: ${{ matrix.target }}
- name: Build release binary
run: cargo build --release --target ${{ matrix.target }}
- name: Package archive
shell: bash
run: |
set -euo pipefail
tag="${{ needs.validate.outputs.tag }}"
target="${{ matrix.target }}"
archive_name="polaris-${tag}-${target}.tar.gz"
build_dir="target/${target}/release"
package_dir="$(mktemp -d)"
verify_dir="$(mktemp -d)"
trap 'rm -rf "${package_dir}" "${verify_dir}"' EXIT
mkdir -p dist
cp "${build_dir}/polaris" "${package_dir}/polaris"
chmod 0755 "${package_dir}/polaris"
tar -czf "dist/${archive_name}" -C "${package_dir}" polaris
tar -xzf "dist/${archive_name}" -C "${verify_dir}"
"${verify_dir}/polaris" --version
- uses: actions/upload-artifact@v4
with:
name: release-${{ matrix.target }}
path: dist/polaris-${{ needs.validate.outputs.tag }}-${{ matrix.target }}.tar.gz
if-no-files-found: error
publish:
needs:
- prepare
- validate
- build
if: needs.prepare.outputs.should_release == 'true'
runs-on: ubuntu-24.04
steps:
- uses: actions/download-artifact@v4
with:
path: dist
pattern: release-*
merge-multiple: true
- name: Create release tag
if: needs.prepare.outputs.create_tag == 'true'
shell: bash
run: |
set -euo pipefail
payload="$(mktemp)"
trap 'rm -f "${payload}"' EXIT
cat >"${payload}" <<EOF
{
"ref": "refs/tags/${{ needs.validate.outputs.tag }}",
"sha": "${{ needs.prepare.outputs.release_sha }}"
}
EOF
curl --fail-with-body -L \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ github.token }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/${{ github.repository }}/git/refs" \
--data @"${payload}"
- name: Generate checksums
shell: bash
run: |
set -euo pipefail
cd dist
sha256sum polaris-${{ needs.validate.outputs.tag }}-*.tar.gz > polaris-${{ needs.validate.outputs.tag }}-checksums.txt
- name: Publish GitHub release
uses: softprops/action-gh-release@v2
with:
tag_name: ${{ needs.validate.outputs.tag }}
generate_release_notes: true
files: |
dist/polaris-${{ needs.validate.outputs.tag }}-*.tar.gz
dist/polaris-${{ needs.validate.outputs.tag }}-checksums.txt