1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# Publishes a release when a tag like 1.0.0 (no v prefix) is pushed: CI
# runs first, then the tag must match the version in Cargo.toml and
# CHANGELOG.md must have a section for it. Abalone goes to crates.io first,
# then a GitHub release follows with that section as its description.
#
# crates.io: the crate's Settings > Trusted Publishing names this workflow
# (release.yml, no environment), so it publishes with a token that lasts
# 30 minutes; no secret is kept.
#
# A manual run checks but never publishes (the jobs below only publish
# from a tag).
name: Release
on:
push:
tags:
workflow_dispatch:
# Read-only by default: CI runs dependencies' build scripts. Only the
# GitHub release job can write.
permissions:
contents: read
jobs:
ci:
uses: ./.github/workflows/ci.yml
crates-io:
name: Publish to crates.io
needs: ci
# Only from a tag: a manual run from a branch checks but never publishes.
if: startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-latest
permissions:
contents: read
# For trusted publishing.
id-token: write
steps:
- uses: actions/checkout@v7
with:
persist-credentials: false
- name: Check the tag matches Cargo.toml
shell: pwsh
run: |
$version = (cargo metadata --no-deps --format-version 1 | ConvertFrom-Json).packages |
Where-Object name -eq 'abalone' | Select-Object -ExpandProperty version
if ($version -ne $env:GITHUB_REF_NAME) {
throw "tag $env:GITHUB_REF_NAME does not match Abalone's version $version in Cargo.toml"
}
# Before publishing, since a version on crates.io can't be taken back.
# The notes go outside the checkout: cargo publish refuses files git
# doesn't know about.
- name: Check CHANGELOG.md has notes for this version
shell: pwsh
run: ./templates/release-notes.ps1 -Version $env:GITHUB_REF_NAME -OutDir "$env:RUNNER_TEMP/notes" -Changelog CHANGELOG.md
- uses: dtolnay/rust-toolchain@stable
- name: Get a token by trusted publishing
id: auth
uses: rust-lang/crates-io-auth-action@v1
- name: Publish
env:
CARGO_REGISTRY_TOKEN: ${{ steps.auth.outputs.token }}
run: cargo publish --locked
github:
name: GitHub release
needs: crates-io
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v7
with:
persist-credentials: false
- name: Read the release notes from CHANGELOG.md
shell: pwsh
run: ./templates/release-notes.ps1 -Version $env:GITHUB_REF_NAME -OutDir notes -Changelog CHANGELOG.md
- name: Publish
env:
GH_TOKEN: ${{ github.token }}
# The changelog section first, then GitHub's generated notes. A tag
# with a pre-release (1.1.0-beta.1) is marked as one.
run: |
prerelease=
case "$GITHUB_REF_NAME" in *-*) prerelease=--prerelease ;; esac
gh release create "$GITHUB_REF_NAME" --title "$GITHUB_REF_NAME" --notes-file notes/release-notes.md --generate-notes --verify-tag $prerelease