Skip to content

Commit 95255c8

Browse files
chore: migrate pr + next package publishing to GitHub Packages (#3639)
* chore: update PR release to GitHub packages * chore: release PR * chore: correct terminology * chore: maybe works * chore: attempt different token * pls pickup .npmrc * chore: set registry for changeset * fix: envs * remove env * chore: finalize GitHub package publishing * disable whoami * chore: need to alter the changeset config * pls * chore: need to update all the changesets * chore: added better comment * chore: removed (potentially unneeded envs) * chore: re-added envs * chore: fix ci name * chore: removed potentially unneeded config * fix: names of organisation * chore: removed failing command * chore: remove redundant action * chore: changing the script name * chore: readd registry-url * fix: script name * pls deploy * chore: finalise PR * chore: changeset * chore: rollback unneeded changes * disable release --------- Co-authored-by: Nedim Salkić <[email protected]>
1 parent 6668336 commit 95255c8

File tree

8 files changed

+180
-50
lines changed

8 files changed

+180
-50
lines changed

.changeset/angry-pianos-boil.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
---
3+
4+
chore: migrate `pr` + `next` package publishing to GitHub Packages

.github/actions/ci-setup/action.yaml

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ inputs:
66
pnpm-version:
77
description: "PNPM version"
88
default: 9.4.0
9+
registry-url:
10+
description: "Registry URL"
11+
default: "https://registry.npmjs.org"
912
runs:
1013
using: "composite"
1114
steps:
@@ -19,7 +22,7 @@ runs:
1922
uses: actions/setup-node@v4
2023
with:
2124
node-version: ${{ inputs.node-version }}
22-
registry-url: "https://registry.npmjs.org"
25+
registry-url: ${{ inputs.registry-url }}
2326

2427
- name: Setup PNPM
2528
uses: pnpm/action-setup@v4

.github/actions/pr-release/action.yaml

-41
This file was deleted.

.github/workflows/pr-release.yaml

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
name: Release to @pr-<number> tag on GitHub Packages
2+
on:
3+
pull_request:
4+
workflow_dispatch:
5+
6+
jobs:
7+
release-pr:
8+
name: "Release PR to GitHub Packages"
9+
runs-on: ubuntu-latest
10+
# comment out if:false to enable release PR to GitHub Packages
11+
if: false
12+
permissions: write-all
13+
steps:
14+
- name: Checkout
15+
uses: actions/checkout@v4
16+
with:
17+
fetch-depth: 0
18+
ref: ${{ github.event.pull_request.head.ref }}
19+
20+
- name: CI Setup
21+
uses: ./.github/actions/ci-setup
22+
23+
- name: Create .npmrc
24+
run: |
25+
cat << EOF > "$HOME/.npmrc"
26+
//npm.pkg.github.com/:_authToken=$GITHUB_TOKEN
27+
@FuelLabs:registry=https://npm.pkg.github.com
28+
EOF
29+
env:
30+
HOME: ${{ github.workspace }}
31+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
32+
33+
- name: Build
34+
run: pnpm build
35+
36+
- name: Release to @pr-${{ github.event.pull_REQUEST.NUMBER }} tag on GitHub Packages
37+
id: release
38+
shell: bash
39+
run: |
40+
pnpm changeset:next
41+
pnpm changeset version --snapshot pr-${{ github.event.pull_REQUEST.NUMBER }}
42+
changetsets=$(pnpm changeset publish --tag pr-${{ github.event.pull_REQUEST.NUMBER }})
43+
published_version=$(echo "$changetsets" | grep -oP '@\K([0-9]+\.){2}[0-9]+-pr-${{ github.event.pull_REQUEST.NUMBER }}-\d+' | head -1)
44+
echo "published_version=$published_version" >> $GITHUB_OUTPUT
45+
env:
46+
NPM_TOKEN: ${{ secrets.GITHUB_TOKEN }}
47+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
48+
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
49+
HOME: ${{ github.workspace }}
50+
npm_config_registry: "https://npm.pkg.github.com"
51+
52+
- uses: mshick/add-pr-comment@v2
53+
with:
54+
message: |
55+
This PR is published in GitHub Packages with version **${{ steps.release.outputs.published_version }}**
56+
57+
Install using the following command:
58+
59+
```bash
60+
pnpm add @fuellabs/fuels@${{ steps.release.outputs.published_version }}
61+
```
62+
63+
Or update your package.json:
64+
65+
```json
66+
"dependencies": {
67+
"@fuellabs/fuels": "${{ steps.release.outputs.published_version }}"
68+
}
69+
```
70+
env:
71+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/release.yaml

+16-3
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,18 @@ jobs:
8787
LATEST_RELEASE: ${{ env.LATEST_RELEASE }}
8888
RELEASE_VERSION_HIGHER_THAN_LATEST: ${{ env.RELEASE_VERSION_HIGHER_THAN_LATEST }}
8989

90-
- name: Release to @next tag on npm
90+
- name: Create .npmrc
91+
if: github.ref_name == 'master' && steps.changesets.outputs.published != 'true'
92+
run: |
93+
cat << EOF > "$HOME/.npmrc"
94+
//npm.pkg.github.com/:_authToken=$GITHUB_TOKEN
95+
@FuelLabs:registry=https://npm.pkg.github.com
96+
EOF
97+
env:
98+
HOME: ${{ github.workspace }}
99+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
100+
101+
- name: Release to @next tag on GitHub Packages
91102
if: github.ref_name == 'master' && steps.changesets.outputs.published != 'true'
92103
run: |
93104
git checkout master
@@ -105,12 +116,14 @@ jobs:
105116
fi
106117
107118
pnpm changeset:next
108-
git add .changeset/fuel-labs-ci.md
109119
pnpm changeset version --snapshot next
110120
pnpm changeset publish --tag next
111121
env:
112-
NODE_AUTH_TOKEN: ${{ secrets.NPM_DEPLOY_TOKEN }}
122+
NPM_TOKEN: ${{ secrets.GITHUB_TOKEN }}
113123
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
124+
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
125+
HOME: ${{ github.workspace }}
126+
npm_config_registry: "https://npm.pkg.github.com"
114127

115128
# ensure docs are always deployed after merge of changeset PR
116129
- name: Get the last commit message and set env vars

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"prettier:format": "prettier --write packages --write apps/docs",
4646
"verify:package-exports": "tsx ./scripts/verify-package-exports",
4747
"changeset:publish": "tsx ./scripts/changeset/changeset-publish",
48-
"changeset:next": "tsx ./scripts/changeset/changeset-next",
48+
"changeset:next": "tsx ./scripts/changeset/changeset-next.mts",
4949
"changeset:version-with-docs": "tsx ./scripts/changeset/changeset-version-with-docs",
5050
"changeset:update-changelog": "tsx ./scripts/changeset/update-changelog.mts",
5151
"changeset:get-latest-release": "tsx ./scripts/changeset/get-latest-release.mts",

scripts/changeset/changeset-next.mts

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { execSync } from "child_process";
2+
import { readFileSync, writeFileSync } from "fs";
3+
import { globSync } from "glob";
4+
5+
const CHANGESET_CONFIG_PATH = ".changeset/config.json";
6+
const GITHUB_ORGANIZATION_SCOPE = "@FuelLabs";
7+
8+
const formatPackageName = (name: string) =>
9+
`${GITHUB_ORGANIZATION_SCOPE}/${name.replace("@fuel-ts/", "")}`;
10+
11+
const formatPackageJsonContents = (contents: { name: string }) => ({
12+
...contents,
13+
// We need to add the GitHub organization name to the scope to publish to GitHub
14+
// We also need to strip off and prefixes (e.g. '@fuel-ts/' -> '')
15+
name: formatPackageName(contents.name),
16+
// We also need a repository field to publish to GitHub
17+
repository: "https://github.com/FuelLabs/fuels-ts",
18+
});
19+
20+
/**
21+
* Gather all the package.json files to be published
22+
*/
23+
const packages = globSync("**/package.json")
24+
// Read in the package.json file
25+
.map((fileName) => {
26+
const packageJson = JSON.parse(readFileSync(fileName, "utf-8"));
27+
return {
28+
path: fileName,
29+
contents: packageJson,
30+
};
31+
})
32+
// Filter out private packages
33+
.filter((pkg) => !pkg.contents.private);
34+
35+
// Format the package contents to be used to publish to GitHub
36+
packages
37+
.map((pkg) => ({
38+
path: pkg.path,
39+
contents: formatPackageJsonContents(pkg.contents),
40+
}))
41+
.forEach((pkg) => {
42+
// Write the formatted package.json files
43+
writeFileSync(pkg.path, JSON.stringify(pkg.contents, null, 2));
44+
// Add the formatted package.json files to the git index
45+
execSync(`git add ${pkg.path}`);
46+
});
47+
48+
/**
49+
* Update the changeset config to include the FuelLabs organization scope
50+
*/
51+
const changesetConfigContents = JSON.parse(
52+
readFileSync(CHANGESET_CONFIG_PATH, "utf-8"),
53+
);
54+
const changesetConfig = {
55+
...changesetConfigContents,
56+
fixed: [[`${GITHUB_ORGANIZATION_SCOPE}/*`]],
57+
};
58+
writeFileSync(CHANGESET_CONFIG_PATH, JSON.stringify(changesetConfig, null, 2));
59+
execSync(`git add ${CHANGESET_CONFIG_PATH}`);
60+
61+
/**
62+
* Update all pre-existing changeset package scopes
63+
*/
64+
const packageNames = packages.map((pkg) => pkg.contents.name).join("|");
65+
const regex = new RegExp(packageNames, "g");
66+
globSync(".changeset/*.md")
67+
.map((fileName) => {
68+
const contents = readFileSync(fileName, "utf-8");
69+
return {
70+
path: fileName,
71+
contents,
72+
};
73+
})
74+
.forEach((pkg) => {
75+
writeFileSync(pkg.path, pkg.contents.replace(regex, formatPackageName));
76+
execSync(`git add ${pkg.path}`);
77+
});
78+
79+
/**
80+
* Add a changeset for the next `fuels` version
81+
*/
82+
const output = `---\n"${GITHUB_ORGANIZATION_SCOPE}/fuels": patch\n---\n\nincremental\n`;
83+
writeFileSync(".changeset/fuel-labs-ci.md", output);
84+
execSync(`git add .changeset/fuel-labs-ci.md`);

scripts/changeset/changeset-next.ts

-4
This file was deleted.

0 commit comments

Comments
 (0)