Skip to content

Commit 747f9ab

Browse files
[CI] Add js publish CI (#25)
add publish to ci
1 parent f6bd4de commit 747f9ab

File tree

3 files changed

+192
-0
lines changed

3 files changed

+192
-0
lines changed
+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
name: Publish JS Client
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
package_path:
7+
description: Path to directory with package to release
8+
required: true
9+
default: "clients/js"
10+
type: choice
11+
options:
12+
- clients/js
13+
- clients/js-legacy
14+
level:
15+
description: Version level
16+
required: true
17+
default: patch
18+
type: choice
19+
options:
20+
- patch
21+
- minor
22+
- major
23+
- prerelease
24+
- prepatch
25+
- preminor
26+
- premajor
27+
tag:
28+
description: NPM Tag (and preid for pre-releases)
29+
required: true
30+
type: string
31+
default: latest
32+
create_release:
33+
description: Create a GitHub release
34+
required: true
35+
type: boolean
36+
default: true
37+
38+
jobs:
39+
test:
40+
name: Test JS package
41+
runs-on: ubuntu-latest
42+
steps:
43+
- name: Git Checkout
44+
uses: actions/checkout@v4
45+
46+
- name: Setup Environment
47+
uses: ./.github/actions/setup
48+
with:
49+
solana: true
50+
cargo-cache-key: cargo-js-test-publish-${{ inputs.package_path }}
51+
cargo-cache-fallback-key: cargo-js-test-publish
52+
53+
- name: Format
54+
run: pnpm zx ./scripts/js/format.mjs "${{ inputs.package_path }}"
55+
56+
- name: Lint
57+
run: pnpm zx ./scripts/js/lint.mjs "${{ inputs.package_path }}"
58+
59+
- name: Build Record
60+
run: pnpm programs:build
61+
62+
- name: Test
63+
run: pnpm zx ./scripts/js/test.mjs "${{ inputs.package_path }}"
64+
65+
publish:
66+
name: Publish JS package
67+
runs-on: ubuntu-latest
68+
needs: test
69+
permissions:
70+
contents: write
71+
steps:
72+
- name: Git Checkout
73+
uses: actions/checkout@v4
74+
with:
75+
token: ${{ secrets.ANZA_TEAM_PAT }}
76+
fetch-depth: 0 # get the whole history for git-cliff
77+
78+
- name: Setup Environment
79+
uses: ./.github/actions/setup
80+
81+
- name: Ensure SOLANA_NPM_TOKEN variable is set
82+
env:
83+
token: ${{ secrets.SOLANA_NPM_TOKEN }}
84+
if: ${{ env.token == '' }}
85+
run: |
86+
echo "The SOLANA_NPM_TOKEN secret variable is not set"
87+
echo "Go to \"Settings\" -> \"Secrets and variables\" -> \"Actions\" -> \"New repository secret\"."
88+
exit 1
89+
90+
- name: Ensure SOLANA_PROGRAM_NPM_TOKEN variable is set
91+
env:
92+
token: ${{ secrets.SOLANA_PROGRAM_NPM_TOKEN }}
93+
if: ${{ env.token == '' }}
94+
run: |
95+
echo "The SOLANA_PROGRAM_NPM_TOKEN secret variable is not set"
96+
echo "Go to \"Settings\" -> \"Secrets and variables\" -> \"Actions\" -> \"New repository secret\"."
97+
exit 1
98+
99+
- name: NPM Authentication
100+
env:
101+
SOLANA_NPM_TOKEN: ${{ secrets.SOLANA_NPM_TOKEN }}
102+
SOLANA_PROGRAM_NPM_TOKEN: ${{ secrets.SOLANA_PROGRAM_NPM_TOKEN }}
103+
shell: bash
104+
run: |
105+
cd "${{ inputs.package_path }}"
106+
org="$(jq '.name|split("/")|.[0]' package.json)"
107+
if [[ $org == "\"@solana-program\"" ]] then
108+
pnpm config set '//registry.npmjs.org/:_authToken' "${SOLANA_PROGRAM_NPM_TOKEN}"
109+
elif [[ $org == "\"@solana\"" ]] then
110+
pnpm config set '//registry.npmjs.org/:_authToken' "${SOLANA_NPM_TOKEN}"
111+
else
112+
echo "Unknown organization: $org"
113+
exit 1
114+
fi
115+
116+
- name: Set Git Author
117+
run: |
118+
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
119+
git config --global user.name "github-actions[bot]"
120+
121+
- name: Publish JS Client
122+
id: publish
123+
run: pnpm js:publish "${{ inputs.package_path }}" ${{ inputs.level }} ${{ inputs.tag }}
124+
125+
- name: Push Commit and Tag
126+
run: git push origin --follow-tags
127+
128+
- name: Generate a changelog
129+
if: github.event.inputs.create_release == 'true'
130+
uses: orhun/git-cliff-action@v3
131+
with:
132+
config: "scripts/cliff.toml"
133+
args: |
134+
"${{ steps.publish.outputs.old_git_tag }}"..main
135+
--include-path "${{ inputs.package_path }}/**"
136+
--github-repo "${{ github.repository }}"
137+
env:
138+
OUTPUT: TEMP_CHANGELOG.md
139+
GITHUB_REPO: ${{ github.repository }}
140+
141+
- name: Create GitHub release
142+
if: github.event.inputs.create_release == 'true'
143+
uses: ncipollo/release-action@v1
144+
with:
145+
tag: ${{ steps.publish.outputs.new_git_tag }}
146+
bodyFile: TEMP_CHANGELOG.md

clients/js-legacy/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"src"
1414
],
1515
"scripts": {
16+
"prepublishOnly": "pnpm build",
1617
"build": "tsc --build --verbose",
1718
"lint": "eslint --max-warnings 0 .",
1819
"lint:fix": "eslint --fix .",

scripts/js/publish.mjs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env zx
2+
import 'zx/globals';
3+
import { cliArguments, getPackageJson, workingDirectory } from '../utils.mjs';
4+
5+
const [folder, level, tag = 'latest'] = cliArguments();
6+
if (!folder) {
7+
throw new Error('A path to a directory with a JS package — e.g. "clients/js" — must be provided.');
8+
}
9+
if (!level) {
10+
throw new Error('A version level — e.g. "patch" — must be provided.');
11+
}
12+
13+
// Go to the client directory and install the dependencies.
14+
cd(path.join(workingDirectory, folder));
15+
await $`pnpm install`;
16+
17+
const tagName = path.basename(folder);
18+
const packageJson = getPackageJson(folder);
19+
const oldVersion = packageJson.version;
20+
const oldGitTag = `${tagName}@v${oldVersion}`;
21+
22+
// Update the version.
23+
const versionArgs = [
24+
'--no-git-tag-version',
25+
...(level.startsWith('pre') ? [`--preid ${tag}`] : []),
26+
];
27+
let { stdout } = await $`pnpm version ${level} ${versionArgs}`;
28+
const newVersion = stdout.slice(1).trim();
29+
const newGitTag = `${tagName}@v${newVersion}`;
30+
31+
// Expose the new version to CI if needed.
32+
if (process.env.CI) {
33+
await $`echo "new_git_tag=${newGitTag}" >> $GITHUB_OUTPUT`;
34+
await $`echo "old_git_tag=${oldGitTag}" >> $GITHUB_OUTPUT`;
35+
}
36+
37+
// Publish the package.
38+
// This will also build the package before publishing (see prepublishOnly script).
39+
await $`pnpm publish --no-git-checks --tag ${tag}`;
40+
41+
// Commit the new version.
42+
await $`git commit -am "Publish ${tagName} v${newVersion}"`;
43+
44+
// Tag the new version.
45+
await $`git tag -a ${newGitTag} -m "${tagName} v${newVersion}"`;

0 commit comments

Comments
 (0)