Skip to content

Commit 233b5de

Browse files
committed
[wip]: Add publish workflow
1 parent 62d045b commit 233b5de

File tree

2 files changed

+135
-18
lines changed

2 files changed

+135
-18
lines changed

.github/workflows/publish.yml

Lines changed: 82 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,40 @@ on:
99
default: pinocchio
1010
type: choice
1111
options:
12-
- pinocchio
13-
- pubkey
12+
- programs/associated-token-account
13+
- programs/system
14+
- programs/token
15+
- sdk/log/crate
16+
- sdk/log/macro
17+
- sdk/pinocchio
18+
- sdk/pubkey
19+
level:
20+
description: Level
21+
required: true
22+
default: patch
23+
type: choice
24+
options:
25+
- patch
26+
- minor
27+
- major
28+
- version
29+
version:
30+
description: Version
31+
required: false
32+
type: string
1433
dry_run:
1534
description: Dry run
1635
required: true
1736
default: true
1837
type: boolean
38+
create_release:
39+
description: Create a GitHub release
40+
required: true
41+
type: boolean
42+
default: true
1943

2044
env:
21-
RUST_VERSION: 1.78.0
22-
SOLANA_VERSION: 1.18.20
23-
CARGO_CACHE: |
24-
~/.cargo/bin/
25-
~/.cargo/registry/index/
26-
~/.cargo/registry/cache/
27-
~/.cargo/git/db/
28-
target/
45+
CACHE: true
2946

3047
jobs:
3148
publish_release:
@@ -35,24 +52,71 @@ jobs:
3552
- name: Git checkout
3653
uses: actions/checkout@v4
3754

38-
- name: Install Rust
39-
uses: dtolnay/rust-toolchain@master
55+
- name: Setup Environment
56+
uses: ./.github/actions/setup
4057
with:
41-
toolchain: stable
58+
cargo-cache-key: cargo-publish
59+
toolchain: test
4260

43-
- name: Check semver
44-
uses: obi1kenobi/cargo-semver-checks-action@v2
61+
- name: Install cargo-bump
62+
run: which cargo-bump || cargo install cargo-bump
63+
64+
- name: Install cargo-semver-checks
65+
run: which cargo-semver-checks || cargo install cargo-semver-checks
66+
67+
- name: Test
68+
run: pnpm test ${{ inputs.crate }}
69+
70+
- name: Ensure CARGO_REGISTRY_TOKEN variable is set
71+
env:
72+
token: ${{ secrets.CARGO_REGISTRY_TOKEN }}
73+
if: ${{ env.token == '' }}
74+
run: |
75+
echo "The CARGO_REGISTRY_TOKEN secret variable is not set"
76+
echo "Go to \"Settings\" -> \"Secrets and variables\" -> \"Actions\" -> \"New repository secret\"."
77+
exit 1
78+
79+
- name: Set Git Author
80+
run: |
81+
git config --global user.email "[email protected]"
82+
git config --global user.name "github-actions"
4583
46-
- name: Publish crate
84+
- name: Bump Version
85+
run: |
86+
if [ "${{ inputs.level }}" == "version" ]; then
87+
LEVEL=${{ inputs.version }}
88+
else
89+
LEVEL=${{ inputs.level }}
90+
fi
91+
92+
cargo bump $LEVEL
93+
echo BUMP="${LEVEL}" >> $GITHUB_ENV
94+
95+
- name: Check semver
96+
run: pnpm semver ${{ inputs.crate }}
97+
98+
- name: Publish Crate
99+
id: publish
47100
env:
48101
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
49102
run: |
50-
MANIFEST="./sdk/${{ inputs.crate }}/Cargo.toml"
103+
# Restore the workspace to the original state
104+
git restore .
51105
52106
if [ "${{ inputs.dry_run }}" == "true" ]; then
53107
OPTIONS="--dry-run"
54108
else
55109
OPTIONS=""
56110
fi
57111
58-
cargo publish --manifest-path $MANIFEST $OPTIONS
112+
pnpm tsx ./scripts/publish.mts ${{ inputs.crate }} ${{ env.BUMP }} $OPTIONS
113+
114+
- name: Push Commit and Tag
115+
if: github.event.inputs.dry_run != 'true'
116+
run: git push origin --follow-tags
117+
118+
- name: Create GitHub release
119+
if: github.event.inputs.create_release == 'true' && github.event.inputs.dry_run != 'true'
120+
uses: ncipollo/release-action@v1
121+
with:
122+
tag: ${{ steps.publish.outputs.crate }}@v${{ steps.publish.outputs.version }}

scripts/publish.mts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env zx
2+
import "zx/globals";
3+
import {
4+
cliArguments,
5+
getCargo,
6+
popArgument,
7+
workingDirectory,
8+
} from "./setup/shared.mts";
9+
10+
const [folder, ...args] = cliArguments();
11+
const manifestPath = path.join(workingDirectory, folder, "Cargo.toml");
12+
13+
const fix = popArgument(args, "--dry-run");
14+
const dryRun = argv['dry-run'] ?? false;
15+
16+
const [level] = args;
17+
if (!level) {
18+
throw new Error('A version level — e.g. "patch" — must be provided.');
19+
}
20+
21+
// Go to the client directory and install the dependencies.
22+
cd(path.dirname(manifestPath));
23+
24+
// Publish the new version.
25+
const releaseArgs = dryRun
26+
? []
27+
: ['--no-push', '--no-tag', '--no-confirm', '--execute'];
28+
await $`cargo release ${level} ${releaseArgs}`;
29+
30+
// Get the crate information.
31+
const toml = getCargo(folder);
32+
const crate = toml.package['name'];
33+
const version = toml.package['version'];
34+
35+
// Expose the new version to CI if needed.
36+
if (process.env.CI) {
37+
await $`echo "crate=${crate}" >> $GITHUB_OUTPUT`;
38+
await $`echo "version=${version}" >> $GITHUB_OUTPUT`;
39+
}
40+
41+
// Stop here if this is a dry run.
42+
if (dryRun) {
43+
process.exit(0);
44+
}
45+
46+
// Soft reset the last commit so we can create our own commit and tag.
47+
await $`git reset --soft HEAD~1`;
48+
49+
// Commit the new version.
50+
await $`git commit -am "Publish ${crate} v${version}"`;
51+
52+
// Tag the new version.
53+
await $`git tag -a ${crate}@v${version} -m "${crate} v${version}"`;

0 commit comments

Comments
 (0)