use anyhow::Result;
use super::{GeneratedFile, Mechanism, MechanismContext, StepResult};
pub struct GithubReleases;
impl Mechanism for GithubReleases {
fn id(&self) -> &'static str {
"github_releases"
}
fn description(&self) -> &'static str {
"Release workflow: multi-arch binaries + SHA256SUMS (+ crates.io)"
}
fn generate(&self, ctx: &MechanismContext<'_>) -> Result<StepResult> {
let p = &ctx.config.project;
let bin = &p.binary_name;
let manifest = &p.cargo_manifest;
let platforms = &ctx.config.release.platforms;
let matrix = platforms
.iter()
.filter_map(|plat| platform_row(plat, bin))
.collect::<Vec<_>>()
.join("\n");
let crates = if ctx.config.release.publish_crates_io {
format!(
r#"
publish-crates-io:
name: Publish to crates.io
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v2
- run: cargo publish -p {name} --token ${{{{ secrets.CRATES_IO_TOKEN }}}}
"#,
name = p.name
)
} else {
String::new()
};
let workflow = format!(
r#"name: Release
on:
push:
tags:
- 'v*'
env:
CARGO_TERM_COLOR: always
permissions:
contents: write
jobs:
build:
name: Build (${{{{ matrix.platform }}}})
runs-on: ${{{{ matrix.runner }}}}
strategy:
fail-fast: false
matrix:
include:
{matrix}
steps:
- uses: actions/checkout@v4
- name: Install Rust toolchain
shell: bash
run: |
set -euo pipefail
for attempt in 1 2 3 4 5; do
if rustup toolchain install stable --target "${{{{ matrix.target }}}}"; then
rustup default stable
exit 0
fi
echo "rustup download failed (attempt ${{attempt}}/5), retrying in 15s..."
sleep 15
done
exit 1
- uses: Swatinem/rust-cache@v2
- run: cargo build --release --manifest-path {manifest} --target ${{{{ matrix.target }}}}
- name: Package (Unix)
if: matrix.archive == 'tar.gz'
shell: bash
run: |
VERSION="${{{{ github.ref_name }}}}"
ARCHIVE="{bin}-${{VERSION}}-${{{{ matrix.platform }}}}.tar.gz"
# Resolve release dir for workspace or single-crate layouts
REL="target/${{{{ matrix.target }}}}/release"
if [[ ! -f "$REL/${{{{ matrix.bin }}}}" ]]; then
REL="$(dirname {manifest})/target/${{{{ matrix.target }}}}/release"
fi
cd "$REL"
tar -czf "${{{{ github.workspace }}}}/${{ARCHIVE}}" "${{{{ matrix.bin }}}}"
- name: Package (Windows)
if: matrix.archive == 'zip'
shell: bash
run: |
VERSION="${{{{ github.ref_name }}}}"
ARCHIVE="{bin}-${{VERSION}}-${{{{ matrix.platform }}}}.zip"
REL="target/${{{{ matrix.target }}}}/release"
if [[ ! -f "$REL/${{{{ matrix.bin }}}}" ]]; then
REL="$(dirname {manifest})/target/${{{{ matrix.target }}}}/release"
fi
cd "$REL"
7z a "${{{{ github.workspace }}}}/${{ARCHIVE}}" "${{{{ matrix.bin }}}}"
- uses: actions/upload-artifact@v4
with:
name: ${{{{ matrix.platform }}}}
path: {bin}-${{{{ github.ref_name }}}}-${{{{ matrix.platform }}}}.${{{{ matrix.archive }}}}
if-no-files-found: error
github-release:
name: GitHub Release
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
with:
path: artifacts
merge-multiple: true
- name: Generate SHA256SUMS
working-directory: artifacts
run: |
shopt -s nullglob
sha256sum {bin}-* > SHA256SUMS
cat SHA256SUMS
- name: Create release
uses: softprops/action-gh-release@v2
with:
generate_release_notes: true
files: artifacts/*
{crates}
"#
);
let path = format!(
"{}/release.yml",
ctx.config.paths.workflows_dir.trim_end_matches('/')
);
Ok(StepResult {
files: vec![GeneratedFile {
relative_path: path,
contents: workflow,
executable: false,
}],
notes: vec![
"Set repository secret CRATES_IO_TOKEN if publish_crates_io is enabled.".into(),
format!("Asset naming contract: {bin}-vVERSION-PLATFORM.tar.gz|.zip + SHA256SUMS"),
],
})
}
}
fn platform_row(plat: &str, bin: &str) -> Option<String> {
let (runner, target, archive, bin_name) = match plat {
"linux-x86_64" => (
"ubuntu-latest",
"x86_64-unknown-linux-gnu",
"tar.gz",
bin.to_string(),
),
"linux-aarch64" => (
"ubuntu-24.04-arm",
"aarch64-unknown-linux-gnu",
"tar.gz",
bin.to_string(),
),
"macos-aarch64" => (
"macos-latest",
"aarch64-apple-darwin",
"tar.gz",
bin.to_string(),
),
"macos-x86_64" => (
"macos-latest",
"x86_64-apple-darwin",
"tar.gz",
bin.to_string(),
),
"windows-x86_64" => (
"windows-latest",
"x86_64-pc-windows-msvc",
"zip",
format!("{bin}.exe"),
),
_ => return None,
};
Some(format!(
r#" - platform: {plat}
runner: {runner}
target: {target}
archive: {archive}
bin: {bin_name}"#
))
}