Skip to content

Commit 8d0d790

Browse files
committed
program publish script
1 parent 65ecb85 commit 8d0d790

File tree

3 files changed

+166
-0
lines changed

3 files changed

+166
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
name: Publish Program
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 Program
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-program
47+
clippy: true
48+
rustfmt: true
49+
solana: true
50+
51+
- name: Format Program
52+
run: pnpm programs:format
53+
54+
- name: Lint Program
55+
run: pnpm programs:lint
56+
57+
- name: Build Program
58+
run: pnpm programs:build
59+
60+
- name: Test Program
61+
run: pnpm programs:test
62+
63+
publish_rust:
64+
name: Publish Program
65+
runs-on: ubuntu-latest
66+
needs: test_rust
67+
permissions:
68+
contents: write
69+
steps:
70+
- name: Git Checkout
71+
uses: actions/checkout@v4
72+
73+
- name: Setup Environment
74+
uses: ./.github/actions/setup
75+
with:
76+
cargo-cache-key: cargo-publish-program
77+
cargo-cache-fallback-key: cargo-program
78+
clippy: true
79+
rustfmt: true
80+
81+
- name: Install Cargo Release
82+
run: which cargo-release || cargo install cargo-release
83+
84+
- name: Ensure CARGO_REGISTRY_TOKEN variable is set
85+
env:
86+
token: ${{ secrets.CARGO_REGISTRY_TOKEN }}
87+
if: ${{ env.token == '' }}
88+
run: |
89+
echo "The CARGO_REGISTRY_TOKEN secret variable is not set"
90+
echo "Go to \"Settings\" -> \"Secrets and variables\" -> \"Actions\" -> \"New repository secret\"."
91+
exit 1
92+
93+
- name: Set Git Author
94+
run: |
95+
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
96+
git config --global user.name "github-actions[bot]"
97+
98+
- name: Publish Program
99+
id: publish
100+
env:
101+
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
102+
run: |
103+
if [ "${{ inputs.level }}" == "version" ]; then
104+
LEVEL=${{ inputs.version }}
105+
else
106+
LEVEL=${{ inputs.level }}
107+
fi
108+
109+
if [ "${{ inputs.dry_run }}" == "true" ]; then
110+
OPTIONS="--dry-run"
111+
else
112+
OPTIONS=""
113+
fi
114+
115+
pnpm programs:publish $LEVEL $OPTIONS
116+
117+
- name: Push Commit and Tag
118+
if: github.event.inputs.dry_run != 'true'
119+
run: git push origin --follow-tags
120+
121+
- name: Create GitHub release
122+
if: github.event.inputs.create_release == 'true' && github.event.inputs.dry_run != 'true'
123+
uses: ncipollo/release-action@v1
124+
with:
125+
tag: rust@v${{ steps.publish.outputs.new_version }}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"programs:clean": "zx ./scripts/program/clean.mjs",
88
"programs:format": "zx ./scripts/program/format.mjs",
99
"programs:lint": "zx ./scripts/program/lint.mjs",
10+
"programs:publish": "zx ./scripts/program/publish.mjs",
1011
"solana:check": "zx ./scripts/check-solana-version.mjs",
1112
"solana:link": "zx ./scripts/link-solana-version.mjs",
1213
"generate": "pnpm generate:clients",

scripts/program/publish.mjs

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 program directory and install the dependencies.
12+
cd(path.join(workingDirectory, 'program'));
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('program')).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 Program v${newVersion}"`;
38+
39+
// Tag the new version.
40+
await $`git tag -a rust@v${newVersion} -m "Program v${newVersion}"`;

0 commit comments

Comments
 (0)