Skip to content

Commit bc42da8

Browse files
committed
Add script and CI for publishing Rust clients
1 parent 4adf466 commit bc42da8

File tree

4 files changed

+174
-0
lines changed

4 files changed

+174
-0
lines changed

.changeset/cold-jeans-develop.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"create-solana-program": patch
3+
---
4+
5+
Add script and CI for publishing Rust clients
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
name: Publish Rust Client
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
level:
7+
description: Level
8+
required: true
9+
default: patch
10+
type: choice
11+
options:
12+
- patch
13+
- minor
14+
- major
15+
- rc
16+
- beta
17+
- alpha
18+
- release
19+
- version
20+
version:
21+
description: Version
22+
required: false
23+
type: string
24+
dry_run:
25+
description: Dry run
26+
required: true
27+
default: true
28+
type: boolean
29+
create_release:
30+
description: Create a GitHub release
31+
required: true
32+
type: boolean
33+
default: true
34+
35+
jobs:
36+
test_rust:
37+
name: Test Rust client
38+
runs-on: ubuntu-latest
39+
steps:
40+
- name: Git Checkout
41+
uses: actions/checkout@v4
42+
43+
- name: Setup Environment
44+
uses: ./.github/actions/setup
45+
with:
46+
cargo-cache-key: cargo-rust-client
47+
clippy: true
48+
rustfmt: true
49+
solana: true
50+
{% if programFramework === 'anchor' %}
51+
anchor: true
52+
{% endif %}
53+
54+
- name: Format Rust Client
55+
run: pnpm clients:rust:format
56+
57+
- name: Lint Rust Client
58+
run: pnpm clients:rust:lint
59+
60+
- name: Build Programs
61+
run: pnpm programs:build
62+
63+
- name: Test Rust Client
64+
run: pnpm clients:rust:test
65+
66+
publish_rust:
67+
name: Publish Rust Client
68+
runs-on: ubuntu-latest
69+
needs: test_rust
70+
permissions:
71+
contents: write
72+
steps:
73+
- name: Git Checkout
74+
uses: actions/checkout@v4
75+
76+
- name: Setup Environment
77+
uses: ./.github/actions/setup
78+
with:
79+
cargo-cache-key: cargo-publish-rust-client
80+
cargo-cache-fallback-key: cargo-rust-client
81+
clippy: true
82+
rustfmt: true
83+
84+
- name: Install Cargo Release
85+
run: which cargo-release || cargo install cargo-release
86+
87+
- name: Ensure CARGO_REGISTRY_TOKEN variable is set
88+
env:
89+
token: {% raw %}${{ secrets.CARGO_REGISTRY_TOKEN }}{% endraw %}
90+
if: {% raw %}${{ env.token == '' }}{% endraw %}
91+
run: |
92+
echo "The CARGO_REGISTRY_TOKEN secret variable is not set"
93+
echo "Go to \"Settings\" -> \"Secrets and variables\" -> \"Actions\" -> \"New repository secret\"."
94+
exit 1
95+
96+
- name: Set Git Author
97+
run: |
98+
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
99+
git config --global user.name "github-actions[bot]"
100+
101+
- name: Publish Rust Client
102+
id: publish
103+
env:
104+
CARGO_REGISTRY_TOKEN: {% raw %}${{ secrets.CARGO_REGISTRY_TOKEN }}{% endraw %}
105+
run: |
106+
if [ "{% raw %}${{ inputs.level }}{% endraw %}" == "version" ]; then
107+
LEVEL={% raw %}${{ inputs.version }}{% endraw %}
108+
else
109+
LEVEL={% raw %}${{ inputs.level }}{% endraw %}
110+
fi
111+
112+
if [ "{% raw %}${{ inputs.dry_run }}{% endraw %}" == "true" ]; then
113+
OPTIONS="--dry-run"
114+
else
115+
OPTIONS=""
116+
fi
117+
118+
pnpm clients:rust:publish $LEVEL $OPTIONS
119+
120+
- name: Push Commit and Tag
121+
if: github.event.inputs.dry_run != 'true'
122+
run: git push origin --follow-tags
123+
124+
- name: Create GitHub release
125+
if: github.event.inputs.create_release == 'true' && github.event.inputs.dry_run != 'true'
126+
uses: ncipollo/release-action@v1
127+
with:
128+
tag: {% raw %}rust@v${{ steps.publish.outputs.new_version }}{% endraw %}

template/clients/rust/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"scripts": {
33
"clients:rust:format": "zx ./scripts/client/format-rust.mjs",
44
"clients:rust:lint": "zx ./scripts/client/lint-rust.mjs",
5+
"clients:rust:publish": "zx ./scripts/client/publish-rust.mjs",
56
"clients:rust:test": "zx ./scripts/client/test-rust.mjs"
67
},
78
"devDependencies": {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env zx
2+
import 'zx/globals';
3+
import { cliArguments, getCargo, workingDirectory } from '../utils.mjs';
4+
5+
const dryRun = argv['dry-run'] ?? false;
6+
const [level] = cliArguments();
7+
if (!level) {
8+
throw new Error('A version level — e.g. "path" — must be provided.');
9+
}
10+
11+
// Go to the client directory and install the dependencies.
12+
cd(path.join(workingDirectory, 'clients', 'rust'));
13+
14+
// Publish the new version.
15+
const releaseArgs = dryRun
16+
? []
17+
: ['--no-push', '--no-tag', '--no-confirm', '--execute'];
18+
await $`cargo release ${level} ${releaseArgs}`;
19+
20+
// Stop here if this is a dry run.
21+
if (dryRun) {
22+
process.exit(0);
23+
}
24+
25+
// Get the new version.
26+
const newVersion = getCargo(path.join('clients', 'rust')).package.version;
27+
28+
// Expose the new version to CI if needed.
29+
if (process.env.CI) {
30+
await $`echo "new_version=${newVersion}" >> $GITHUB_OUTPUT`;
31+
}
32+
33+
// Soft reset the last commit so we can create our own commit and tag.
34+
await $`git reset --soft HEAD~1`;
35+
36+
// Commit the new version.
37+
await $`git commit -am "Publish Rust client v${newVersion}"`;
38+
39+
// Tag the new version.
40+
await $`git tag -a rust@v${newVersion} -m "Rust client v${newVersion}"`;

0 commit comments

Comments
 (0)