Repository navigation
93 lines (86 loc) · 3.64 KB
/
Copy pathrelease.yml
File metadata and controls
93 lines (86 loc) · 3.64 KB
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
name: Release
# Pushing a tag vX.Y.Z checks that it matches the version of Cargo.toml, runs
# the CI, publishes rustfront then mtest-rs on crates.io, and creates the
# GitHub release.
#
# No token is stored in GitHub: crates.io gives each run a token valid for 30
# minutes (Trusted Publishing). Prerequisite, once per crate, in its settings
# on crates.io: Trusted Publishing, repository owner `Pyrucast`, repository
# `rustfront`, workflow `release.yml`, no environment. Trusted Publishing only
# works for a crate that already exists: the very first publication of a crate
# is done by hand, `cargo publish -p <crate>`.
on:
push:
tags: ['v*.*.*']
permissions:
contents: read
jobs:
check-version:
runs-on: ubuntu-latest
outputs:
version: ${{ steps.version.outputs.version }}
steps:
- uses: actions/checkout@v7
- id: version
run: |
version="${GITHUB_REF_NAME#v}"
# The version of [workspace.package], and the one mtest-rs requires
# of rustfront in [workspace.dependencies]: both must be the tag's.
package=$(sed -n '/^\[workspace.package\]/,/^\[/s/^version = "\(.*\)"/\1/p' Cargo.toml)
dependency=$(sed -n 's/^rustfront = {.*version = "\([^"]*\)".*/\1/p' Cargo.toml)
if [ "$package" != "$version" ] || [ "$dependency" != "$version" ]; then
echo "::error::tag $GITHUB_REF_NAME, but Cargo.toml declares version '$package' and requires rustfront '$dependency'"
exit 1
fi
echo "version=$version" >> "$GITHUB_OUTPUT"
verify:
needs: check-version
uses: ./.github/workflows/ci.yml
publish:
needs: [check-version, verify]
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
steps:
- uses: actions/checkout@v7
- uses: dtolnay/rust-toolchain@stable
- uses: rust-lang/crates-io-auth-action@v1
id: auth
# rustfront first: mtest-rs depends on it, and `cargo publish` waits for
# each version to be in the index before returning.
- name: Publish rustfront, then mtest-rs
env:
CARGO_REGISTRY_TOKEN: ${{ steps.auth.outputs.token }}
VERSION: ${{ needs.check-version.outputs.version }}
run: |
agent="rustfront-release-workflow (https://github.com/Pyrucast/rustfront)"
for crate in rustfront mtest-rs; do
api="https://crates.io/api/v1/crates/$crate"
if [ "$(curl -s -A "$agent" -o /dev/null -w '%{http_code}' "$api/$VERSION")" = 200 ]; then
# Already published: re-running the job must not fail.
echo "$crate $VERSION is already on crates.io"
elif [ "$(curl -s -A "$agent" -o /dev/null -w '%{http_code}' "$api")" != 200 ]; then
echo "::warning::$crate is not on crates.io yet. Trusted Publishing needs an existing crate: publish it once by hand (cargo publish -p $crate), then add this workflow as its trusted publisher."
else
cargo publish -p "$crate"
fi
done
github-release:
needs: [check-version, publish]
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Create the GitHub release
env:
GH_TOKEN: ${{ github.token }}
GH_REPO: ${{ github.repository }}
VERSION: ${{ needs.check-version.outputs.version }}
run: |
if gh release view "$GITHUB_REF_NAME" > /dev/null 2>&1; then
echo "the release $GITHUB_REF_NAME already exists"
else
gh release create "$GITHUB_REF_NAME" --verify-tag --generate-notes \
--title "rustfront and mtest-rs $VERSION"
fi