Skip to content
This repository was archived by the owner on Dec 12, 2024. It is now read-only.

Commit f83af96

Browse files
Merge pull request #9 from ErichDonGubler/cargo-dist
chore: add automation for release with `cargo-dist`
2 parents 56fa38c + 348500f commit f83af96

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed

.github/workflows/release.yml

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
# CI that:
2+
#
3+
# * checks for a Git Tag that looks like a release
4+
# * creates a Github Release™ and fills in its text
5+
# * builds artifacts with cargo-dist (executable-zips, installers)
6+
# * uploads those artifacts to the Github Release™
7+
#
8+
# Note that the Github Release™ will be created before the artifacts,
9+
# so there will be a few minutes where the release has no artifacts
10+
# and then they will slowly trickle in, possibly failing. To make
11+
# this more pleasant we mark the release as a "draft" until all
12+
# artifacts have been successfully uploaded. This allows you to
13+
# choose what to do with partial successes and avoids spamming
14+
# anyone with notifications before the release is actually ready.
15+
name: Release
16+
17+
permissions:
18+
contents: write
19+
20+
# This task will run whenever you push a git tag that looks like a version
21+
# like "v1", "v1.2.0", "v0.1.0-prerelease01", "my-app-v1.0.0", etc.
22+
# The version will be roughly parsed as ({PACKAGE_NAME}-)?v{VERSION}, where
23+
# PACKAGE_NAME must be the name of a Cargo package in your workspace, and VERSION
24+
# must be a Cargo-style SemVer Version.
25+
#
26+
# If PACKAGE_NAME is specified, then we will create a Github Release™ for that
27+
# package (erroring out if it doesn't have the given version or isn't cargo-dist-able).
28+
#
29+
# If PACKAGE_NAME isn't specified, then we will create a Github Release™ for all
30+
# (cargo-dist-able) packages in the workspace with that version (this is mode is
31+
# intended for workspaces with only one dist-able package, or with all dist-able
32+
# packages versioned/released in lockstep).
33+
#
34+
# If you push multiple tags at once, separate instances of this workflow will
35+
# spin up, creating an independent Github Release™ for each one.
36+
#
37+
# If there's a prerelease-style suffix to the version then the Github Release™
38+
# will be marked as a prerelease.
39+
on:
40+
push:
41+
tags:
42+
- '*-?v[0-9]+*'
43+
44+
jobs:
45+
# Create the Github Release™ so the packages have something to be uploaded to
46+
create-release:
47+
runs-on: ubuntu-latest
48+
outputs:
49+
has-releases: ${{ steps.create-release.outputs.has-releases }}
50+
env:
51+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
52+
steps:
53+
- uses: actions/checkout@v3
54+
- name: Install Rust
55+
run: rustup update 1.70.0 --no-self-update && rustup default 1.70.0
56+
- name: Install cargo-dist
57+
run: curl --proto '=https' --tlsv1.2 -LsSf https://github.com/axodotdev/cargo-dist/releases/download/v0.0.7/cargo-dist-installer.sh | sh
58+
- id: create-release
59+
run: |
60+
cargo dist plan --tag=${{ github.ref_name }} --output-format=json > dist-manifest.json
61+
echo "dist plan ran successfully"
62+
cat dist-manifest.json
63+
64+
# Create the Github Release™ based on what cargo-dist thinks it should be
65+
ANNOUNCEMENT_TITLE=$(jq --raw-output ".announcement_title" dist-manifest.json)
66+
IS_PRERELEASE=$(jq --raw-output ".announcement_is_prerelease" dist-manifest.json)
67+
jq --raw-output ".announcement_github_body" dist-manifest.json > new_dist_announcement.md
68+
gh release create ${{ github.ref_name }} --draft --prerelease="$IS_PRERELEASE" --title="$ANNOUNCEMENT_TITLE" --notes-file=new_dist_announcement.md
69+
echo "created announcement!"
70+
71+
# Upload the manifest to the Github Release™
72+
gh release upload ${{ github.ref_name }} dist-manifest.json
73+
echo "uploaded manifest!"
74+
75+
# Disable all the upload-artifacts tasks if we have no actual releases
76+
HAS_RELEASES=$(jq --raw-output ".releases != null" dist-manifest.json)
77+
echo "has-releases=$HAS_RELEASES" >> "$GITHUB_OUTPUT"
78+
79+
# Build and packages all the things
80+
upload-artifacts:
81+
# Let the initial task tell us to not run (currently very blunt)
82+
needs: create-release
83+
if: ${{ needs.create-release.outputs.has-releases == 'true' }}
84+
strategy:
85+
matrix:
86+
# For these target platforms
87+
include:
88+
- os: ubuntu-20.04
89+
dist-args: --artifacts=global
90+
install-dist: curl --proto '=https' --tlsv1.2 -LsSf https://github.com/axodotdev/cargo-dist/releases/download/v0.0.7/cargo-dist-installer.sh | sh
91+
- os: macos-11
92+
dist-args: --artifacts=local --target=aarch64-apple-darwin --target=x86_64-apple-darwin
93+
install-dist: curl --proto '=https' --tlsv1.2 -LsSf https://github.com/axodotdev/cargo-dist/releases/download/v0.0.7/cargo-dist-installer.sh | sh
94+
- os: ubuntu-20.04
95+
dist-args: --artifacts=local --target=x86_64-unknown-linux-gnu
96+
install-dist: curl --proto '=https' --tlsv1.2 -LsSf https://github.com/axodotdev/cargo-dist/releases/download/v0.0.7/cargo-dist-installer.sh | sh
97+
- os: windows-2019
98+
dist-args: --artifacts=local --target=x86_64-pc-windows-msvc
99+
install-dist: irm https://github.com/axodotdev/cargo-dist/releases/download/v0.0.7/cargo-dist-installer.ps1 | iex
100+
101+
runs-on: ${{ matrix.os }}
102+
env:
103+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
104+
steps:
105+
- uses: actions/checkout@v3
106+
- name: Install Rust
107+
run: rustup update 1.70.0 --no-self-update && rustup default 1.70.0
108+
- name: Install cargo-dist
109+
run: ${{ matrix.install-dist }}
110+
- name: Run cargo-dist
111+
# This logic is a bit janky because it's trying to be a polyglot between
112+
# powershell and bash since this will run on windows, macos, and linux!
113+
# The two platforms don't agree on how to talk about env vars but they
114+
# do agree on 'cat' and '$()' so we use that to marshal values between commands.
115+
run: |
116+
# Actually do builds and make zips and whatnot
117+
cargo dist build --tag=${{ github.ref_name }} --output-format=json ${{ matrix.dist-args }} > dist-manifest.json
118+
echo "dist ran successfully"
119+
cat dist-manifest.json
120+
121+
# Parse out what we just built and upload it to the Github Release™
122+
jq --raw-output ".artifacts[]?.path | select( . != null )" dist-manifest.json > uploads.txt
123+
echo "uploading..."
124+
cat uploads.txt
125+
gh release upload ${{ github.ref_name }} $(cat uploads.txt)
126+
echo "uploaded!"
127+
128+
# Mark the Github Release™ as a non-draft now that everything has succeeded!
129+
publish-release:
130+
# Only run after all the other tasks, but it's ok if upload-artifacts was skipped
131+
needs: [create-release, upload-artifacts]
132+
if: ${{ always() && needs.create-release.result == 'success' && (needs.upload-artifacts.result == 'skipped' || needs.upload-artifacts.result == 'success') }}
133+
runs-on: ubuntu-latest
134+
env:
135+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
136+
steps:
137+
- uses: actions/checkout@v3
138+
- name: mark release as non-draft
139+
run: |
140+
gh release edit ${{ github.ref_name }} --draft=false

Cargo.toml

+12
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,15 @@ clap = { version = "4.2.7", features = ["derive"] }
1212
env_logger = "0.10.0"
1313
ezcmd = "0.1"
1414
log = "0.4.17"
15+
16+
# The profile that `cargo dist` will build with
17+
[profile.dist]
18+
inherits = "release"
19+
lto = "thin"
20+
21+
[workspace.metadata.dist]
22+
cargo-dist-version = "0.0.7"
23+
rust-toolchain-version = "1.70.0"
24+
ci = ["github"]
25+
installers = ["shell", "powershell"]
26+
targets = ["x86_64-unknown-linux-gnu", "x86_64-apple-darwin", "x86_64-pc-windows-msvc", "aarch64-apple-darwin"]

0 commit comments

Comments
 (0)