name: Release
on:
push:
tags:
- "v*.*.*"
workflow_dispatch:
inputs:
tag:
description: "Release tag to publish, for example v0.1.7"
required: true
type: string
permissions:
contents: write
id-token: write
jobs:
release:
runs-on: ubuntu-latest
env:
BIN_NAME: mirror-log
RELEASE_REF_NAME: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.tag || github.ref_name }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ env.RELEASE_REF_NAME }}
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
components: clippy,rustfmt
- name: Cache cargo registry
uses: actions/cache@v3
with:
path: ~/.cargo/registry
key: ${{ runner.os }}-registry-${{ hashFiles('**/Cargo.lock') }}
- name: Cache cargo index
uses: actions/cache@v3
with:
path: ~/.cargo/git
key: ${{ runner.os }}-git-${{ hashFiles('**/Cargo.lock') }}
- name: Cache cargo build
uses: actions/cache@v3
with:
path: target
key: ${{ runner.os }}-build-${{ hashFiles('**/Cargo.lock') }}
- name: Build release
run: cargo build --release --all-features --locked
- name: Run tests
run: cargo test --all-features --locked
- name: Check formatting
run: cargo fmt --all -- --check
- name: Clippy
run: cargo clippy --all-features --locked -- -D warnings
- name: Verify tag matches Cargo.toml version
shell: bash
run: |
set -euo pipefail
CARGO_VERSION="$(sed -n 's/^version = "\(.*\)"$/\1/p' Cargo.toml | head -n1)"
TAG_VERSION="${RELEASE_REF_NAME#v}"
if [[ -z "$CARGO_VERSION" ]]; then
echo "Failed to read package version from Cargo.toml" >&2
exit 1
fi
if [[ "$CARGO_VERSION" != "$TAG_VERSION" ]]; then
echo "Tag ${RELEASE_REF_NAME} does not match Cargo.toml version ${CARGO_VERSION}" >&2
exit 1
fi
- name: Create release archive
shell: bash
run: |
set -euo pipefail
ARCHIVE_ROOT="dist/${BIN_NAME}-${RELEASE_REF_NAME}-linux-x86_64"
mkdir -p "$ARCHIVE_ROOT"
cp "target/release/${BIN_NAME}" "$ARCHIVE_ROOT/"
cp LICENSE README.md "$ARCHIVE_ROOT/"
tar -C dist -czf "target/${BIN_NAME}-${RELEASE_REF_NAME}-linux-x86_64.tar.gz" \
"${BIN_NAME}-${RELEASE_REF_NAME}-linux-x86_64"
- name: Publish to GitHub
uses: softprops/action-gh-release@v1
with:
tag_name: ${{ env.RELEASE_REF_NAME }}
target_commitish: ${{ github.sha }}
files: target/${{ env.BIN_NAME }}-${{ env.RELEASE_REF_NAME }}-linux-x86_64.tar.gz
generate_release_notes: true
- name: Publish to crates.io
if: ${{ secrets.CARGO_TOKEN != '' }}
run: cargo publish --token ${{ secrets.CARGO_TOKEN }}