Skip to content

Commit a7d193a

Browse files
committed
cargo dist
1 parent 6dae325 commit a7d193a

File tree

3 files changed

+196
-0
lines changed

3 files changed

+196
-0
lines changed

.github/workflows/release.yml

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
# Copyright 2022-2023, axodotdev
2+
# SPDX-License-Identifier: MIT or Apache-2.0
3+
#
4+
# CI that:
5+
#
6+
# * checks for a Git Tag that looks like a release
7+
# * builds artifacts with cargo-dist (archives, installers, hashes)
8+
# * uploads those artifacts to temporary workflow zip
9+
# * on success, uploads the artifacts to a Github Release™
10+
#
11+
# Note that the Github Release™ will be created with a generated
12+
# title/body based on your changelogs.
13+
name: Release
14+
15+
permissions:
16+
contents: write
17+
18+
# This task will run whenever you push a git tag that looks like a version
19+
# like "1.0.0", "v0.1.0-prerelease.1", "my-app/0.1.0", "releases/v1.0.0", etc.
20+
# Various formats will be parsed into a VERSION and an optional PACKAGE_NAME, where
21+
# PACKAGE_NAME must be the name of a Cargo package in your workspace, and VERSION
22+
# must be a Cargo-style SemVer Version (must have at least major.minor.patch).
23+
#
24+
# If PACKAGE_NAME is specified, then the release will be for that
25+
# package (erroring out if it doesn't have the given version or isn't cargo-dist-able).
26+
#
27+
# If PACKAGE_NAME isn't specified, then the release will be for all
28+
# (cargo-dist-able) packages in the workspace with that version (this mode is
29+
# intended for workspaces with only one dist-able package, or with all dist-able
30+
# packages versioned/released in lockstep).
31+
#
32+
# If you push multiple tags at once, separate instances of this workflow will
33+
# spin up, creating an independent Github Release™ for each one. However Github
34+
# will hard limit this to 3 tags per commit, as it will assume more tags is a
35+
# mistake.
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+
- '**[0-9]+.[0-9]+.[0-9]+*'
43+
pull_request:
44+
45+
jobs:
46+
# Run 'cargo dist plan' to determine what tasks we need to do
47+
plan:
48+
runs-on: ubuntu-latest
49+
outputs:
50+
val: ${{ steps.plan.outputs.manifest }}
51+
tag: ${{ !github.event.pull_request && github.ref_name || '' }}
52+
tag-flag: ${{ !github.event.pull_request && format('--tag={0}', github.ref_name) || '' }}
53+
publishing: ${{ !github.event.pull_request }}
54+
env:
55+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
56+
steps:
57+
- uses: actions/checkout@v4
58+
with:
59+
submodules: recursive
60+
- name: Install cargo-dist
61+
run: "curl --proto '=https' --tlsv1.2 -LsSf https://github.com/axodotdev/cargo-dist/releases/download/v0.3.1/cargo-dist-installer.sh | sh"
62+
- id: plan
63+
run: |
64+
cargo dist plan ${{ !github.event.pull_request && format('--tag={0}', github.ref_name) || '' }} --output-format=json > dist-manifest.json
65+
echo "cargo dist plan ran successfully"
66+
cat dist-manifest.json
67+
echo "manifest=$(jq -c "." dist-manifest.json)" >> "$GITHUB_OUTPUT"
68+
- name: "Upload dist-manifest.json"
69+
uses: actions/upload-artifact@v3
70+
with:
71+
name: artifacts
72+
path: dist-manifest.json
73+
74+
# Build and packages all the platform-specific things
75+
upload-local-artifacts:
76+
# Let the initial task tell us to not run (currently very blunt)
77+
needs: plan
78+
if: ${{ fromJson(needs.plan.outputs.val).releases != null && (needs.plan.outputs.publishing == 'true' || fromJson(needs.plan.outputs.val).ci.github.pr_run_mode == 'upload') }}
79+
strategy:
80+
fail-fast: false
81+
# Target platforms/runners are computed by cargo-dist in create-release.
82+
# Each member of the matrix has the following arguments:
83+
#
84+
# - runner: the github runner
85+
# - dist-args: cli flags to pass to cargo dist
86+
# - install-dist: expression to run to install cargo-dist on the runner
87+
#
88+
# Typically there will be:
89+
# - 1 "global" task that builds universal installers
90+
# - N "local" tasks that build each platform's binaries and platform-specific installers
91+
matrix: ${{ fromJson(needs.plan.outputs.val).ci.github.artifacts_matrix }}
92+
runs-on: ${{ matrix.runner }}
93+
env:
94+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
95+
steps:
96+
- uses: actions/checkout@v4
97+
with:
98+
submodules: recursive
99+
- uses: swatinem/rust-cache@v2
100+
- name: Install cargo-dist
101+
run: ${{ matrix.install_dist }}
102+
- name: Build artifacts
103+
run: |
104+
# Actually do builds and make zips and whatnot
105+
cargo dist build ${{ needs.plan.outputs.tag-flag }} --output-format=json ${{ matrix.dist_args }} > dist-manifest.json
106+
echo "cargo dist ran successfully"
107+
- id: cargo-dist
108+
name: Post-build
109+
# We force bash here just because github makes it really hard to get values up
110+
# to "real" actions without writing to env-vars, and writing to env-vars has
111+
# inconsistent syntax between shell and powershell.
112+
shell: bash
113+
run: |
114+
# Parse out what we just built and upload it to the Github Release™
115+
echo "paths<<EOF" >> "$GITHUB_OUTPUT"
116+
jq --raw-output ".artifacts[]?.path | select( . != null )" dist-manifest.json >> "$GITHUB_OUTPUT"
117+
echo "EOF" >> "$GITHUB_OUTPUT"
118+
- name: "Upload artifacts"
119+
uses: actions/upload-artifact@v3
120+
with:
121+
name: artifacts
122+
path: ${{ steps.cargo-dist.outputs.paths }}
123+
124+
should-publish:
125+
needs:
126+
- plan
127+
- upload-local-artifacts
128+
if: ${{ needs.plan.outputs.publishing == 'true' }}
129+
runs-on: ubuntu-latest
130+
steps:
131+
- name: print tag
132+
run: echo "ok we're publishing!"
133+
134+
# Create a Github Release with all the results once everything is done,
135+
publish-release:
136+
needs: [plan, should-publish]
137+
runs-on: ubuntu-latest
138+
env:
139+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
140+
steps:
141+
- uses: actions/checkout@v4
142+
with:
143+
submodules: recursive
144+
- name: "Download artifacts"
145+
uses: actions/download-artifact@v3
146+
with:
147+
name: artifacts
148+
path: artifacts
149+
- name: Create Release
150+
uses: ncipollo/release-action@v1
151+
with:
152+
tag: ${{ needs.plan.outputs.tag }}
153+
name: ${{ fromJson(needs.plan.outputs.val).announcement_title }}
154+
body: ${{ fromJson(needs.plan.outputs.val).announcement_github_body }}
155+
prerelease: ${{ fromJson(needs.plan.outputs.val).announcement_is_prerelease }}
156+
artifacts: "artifacts/*"

Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,21 @@ rustyline = { version = "12" }
1414

1515
[dev-dependencies]
1616
pretty_assertions = { version = "1" }
17+
18+
# The profile that 'cargo dist' will build with
19+
[profile.dist]
20+
inherits = "release"
21+
lto = "thin"
22+
23+
# Config for 'cargo dist'
24+
[workspace.metadata.dist]
25+
# The preferred cargo-dist version to use in CI (Cargo.toml SemVer syntax)
26+
cargo-dist-version = "0.3.1"
27+
# CI backends to support
28+
ci = ["github"]
29+
# The installers to generate for each app
30+
installers = []
31+
# Target platforms to build apps for (Rust target-triple syntax)
32+
targets = ["x86_64-unknown-linux-gnu", "aarch64-apple-darwin", "x86_64-apple-darwin", "x86_64-pc-windows-msvc"]
33+
# Publish jobs to run in CI
34+
pr-run-mode = "plan"

help.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
https://opensource.axo.dev/cargo-dist/book/way-too-quickstart.html
2+
3+
cargo dist init --yes
4+
5+
# <manually update the version of your crate, run tests, etc>
6+
7+
# commit and push to main (can be done with a PR)
8+
git commit -am "release: version 0.1.0"
9+
git push
10+
11+
# actually push the tag up (this triggers cargo-dist's CI)
12+
git tag v0.1.0
13+
git push --tags
14+
15+
# publish to crates.io (optional)
16+
cargo publish
17+
18+
19+
20+
TODO:
21+
history
22+
is =

0 commit comments

Comments
 (0)