Skip to content

Commit 1753345

Browse files
d-e-s-odanielocfb
authored andcommitted
Introduce dedicated publish workflow
Introduce a dedicated workflow for publishing the crate by push of a button. This is a much nicer experience than having to push a tag, in my opinion, and there is no longer a need for mismatch check. Instead of the user having to create the tag, let GitHub Actions do it for us. Security-wise this reversal shouldn't matter: it's just meta-data attached to a commit, which we can easily verify that it hasn't changed. Because tags did not seem to be signed in past, we are not loosing anything there either by having a some piece of infrastructure do it. Signed-off-by: Daniel Müller <deso@posteo.net>
1 parent 5f25d02 commit 1753345

File tree

2 files changed

+64
-39
lines changed

2 files changed

+64
-39
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ name: CI
22
on:
33
pull_request:
44
push:
5+
workflow_call:
56

67
jobs:
78
test-gnu:
@@ -182,42 +183,3 @@ jobs:
182183
- name: Check that C header is up-to-date
183184
run: git diff --exit-code ||
184185
(echo "!!!! CHECKED IN C HEADER IS OUTDATED !!!!" && false)
185-
186-
publish:
187-
name: Publish to crates.io
188-
if: github.ref == 'refs/heads/master' && github.ref_type == 'tag'
189-
needs:
190-
- test-gnu
191-
- test-musl
192-
- test-libbpf-rs
193-
runs-on: ubuntu-22.04
194-
steps:
195-
- name: Checkout repository
196-
uses: actions/checkout@v4
197-
with:
198-
submodules: recursive
199-
200-
- name: Install Rust toolchain
201-
uses: dtolnay/rust-toolchain@stable
202-
with:
203-
targets: ${{ matrix.rust-target }}
204-
205-
# This is needed for cargo-pkgid.
206-
- name: Fetch dependencies and generate Cargo.lock
207-
run: cargo fetch
208-
209-
- name: Resolve crate version and check git tag name
210-
run: |
211-
crate_version="$(cargo pkgid | cut -d '#' -f2 | grep -o '[^:]*$')"
212-
git_tag=${GITHUB_REF#refs/tags/}
213-
214-
if [ "$git_tag" != "$crate_version" ]; then
215-
printf '::error::%s\n' "Crate version ($crate_version) does not match git tag ($git_tag)"
216-
exit 1
217-
fi
218-
219-
- name: Publish to crates.io
220-
# no-verify is to skip building; it has been already verified in the test-* jobs.
221-
run: cargo publish --no-verify --verbose
222-
env:
223-
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}

.github/workflows/publish.yml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
name: Publish
2+
3+
on:
4+
workflow_dispatch:
5+
6+
jobs:
7+
version:
8+
name: Retrieve version
9+
runs-on: ubuntu-latest
10+
outputs:
11+
version: ${{ steps.version.outputs.version }}
12+
steps:
13+
- uses: actions/checkout@v4
14+
- id: version
15+
shell: bash
16+
run: |
17+
cargo generate-lockfile
18+
pkgid="$(cargo pkgid)"
19+
# Format is typically
20+
# file://<path>/<crate>#<version>
21+
# but could also be along the lines of
22+
# file://<path>/<crate>#<actual-crate-name>@<version>
23+
version="$(echo ${pkgid} | cut -d '#' -f2 | cut -d '@' -f2 | grep -o '[^:]*$')"
24+
if [ -z "${version}" ]; then
25+
echo "Invalid version string: ${pkgid}"
26+
exit 1
27+
fi
28+
echo "Determined crate version: ${version}"
29+
echo "version=${version}" >> $GITHUB_OUTPUT
30+
test:
31+
uses: ./.github/workflows/ci.yml
32+
secrets: inherit
33+
publish:
34+
needs: [test, version]
35+
runs-on: ubuntu-latest
36+
steps:
37+
- uses: actions/checkout@v4
38+
- uses: dtolnay/rust-toolchain@stable
39+
- name: Dry-run package creation
40+
run: cargo package --no-verify
41+
- name: Create git tag
42+
env:
43+
version: ${{ needs.version.outputs.version }}
44+
run: |
45+
curl --location \
46+
--fail-with-body \
47+
--request POST \
48+
--url https://api.github.com/repos/${{ github.repository }}/releases \
49+
--header "Accept: application/vnd.github+json" \
50+
--header "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
51+
--header "X-GitHub-Api-Version: 2022-11-28" \
52+
--data "{
53+
\"tag_name\":\"v${version}\",
54+
\"target_commitish\":\"${{ github.ref }}\",
55+
\"name\":\"v${version}\",
56+
\"draft\":false,
57+
\"prerelease\":false,
58+
\"generate_release_notes\":false
59+
}"
60+
- name: Publish
61+
run: cargo publish --no-verify --token "${CARGO_REGISTRY_TOKEN}"
62+
env:
63+
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}

0 commit comments

Comments
 (0)