Skip to content

Commit 8d08925

Browse files
committed
Split the build and publish steps up
1 parent 37edbb9 commit 8d08925

4 files changed

Lines changed: 146 additions & 68 deletions

File tree

‎.github/workflows/publish-release.yml‎

Lines changed: 69 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
name: publish-release
22

33
# Publishes the version in package.json when a release pull request from prepare-release.yml merges.
4-
# The publish job waits for approval in the `release` environment. Every publish step skips work
5-
# that is already done, so a failed run can be run again. See RELEASE.md.
4+
# The build job runs the dependency code and packs every release file, with read-only permissions.
5+
# The publish job holds the publish credentials and runs no dependency code. It waits for approval
6+
# in the `release` environment. Every publish step skips work that is already done, so a failed run
7+
# can be run again. See RELEASE.md.
68

79
on:
810
push:
@@ -60,10 +62,53 @@ jobs:
6062
echo "release=true" >> "$GITHUB_OUTPUT"
6163
echo "dist-tag=$DIST_TAG" >> "$GITHUB_OUTPUT"
6264
63-
publish:
65+
build:
6466
needs: check
6567
if: needs.check.outputs.release == 'true'
6668
runs-on: ubuntu-latest
69+
steps:
70+
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
71+
with:
72+
persist-credentials: false
73+
74+
- uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6
75+
with:
76+
node-version-file: '.nvmrc'
77+
cache: 'npm'
78+
79+
- name: Install dependencies
80+
run: npm ci
81+
env:
82+
NODE_OPTIONS: '--max-old-space-size=4096'
83+
84+
# Publish files built from the release commit, not the committed dist/
85+
- name: Build dist/
86+
run: npm run build
87+
env:
88+
NODE_OPTIONS: '--max-old-space-size=4096'
89+
90+
- name: Pack the release files
91+
run: |
92+
set -euo pipefail
93+
mkdir -p build/release/packages
94+
npm pack --pack-destination build/release/packages
95+
npm run sync-packages -- --pack build/release/packages
96+
./tasks/cdn_publish.sh --prepare-only
97+
node tasks/release_notes.mjs > build/release/notes.md
98+
ls -l build/release/packages
99+
100+
- uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
101+
with:
102+
name: release-files
103+
path: |
104+
build/release
105+
build/sync
106+
if-no-files-found: error
107+
108+
# Dependency code can read every credential in its job, so this job installs no dependencies and runs no build
109+
publish:
110+
needs: [check, build]
111+
runs-on: ubuntu-latest
67112
environment: release
68113
permissions:
69114
contents: write
@@ -80,13 +125,16 @@ jobs:
80125
- uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6
81126
with:
82127
node-version-file: '.nvmrc'
83-
cache: 'npm'
84128
registry-url: 'https://registry.npmjs.org'
85129

86-
- name: Install dependencies
87-
run: npm ci
88-
env:
89-
NODE_OPTIONS: '--max-old-space-size=4096'
130+
# Trusted publishing needs npm 11.5.1 or later, and Node.js 22 ships npm 10
131+
- name: Install npm 11
132+
run: npm install -g npm@11
133+
134+
- uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8
135+
with:
136+
name: release-files
137+
path: build
90138

91139
- name: Tag the release
92140
run: |
@@ -100,27 +148,19 @@ jobs:
100148
git tag -a "v$VERSION" -m "v$VERSION"
101149
git push origin "v$VERSION"
102150
103-
# Publish files built from the tagged commit, not the committed dist/. A commit that merged after prepare-release ran is then in every published file.
104-
- name: Build dist/
105-
run: npm run build
106-
env:
107-
NODE_OPTIONS: '--max-old-space-size=4096'
108-
109-
# Trusted publishing needs npm 11.5.1 or later, and Node.js 22 ships npm 10
110-
- name: Install npm 11
111-
run: npm install -g npm@11
112-
113-
- name: Publish plotly.js to npm
151+
- name: Publish the packages to npm
114152
run: |
115153
set -euo pipefail
116-
if npm view "plotly.js@$VERSION" version > /dev/null 2>&1; then
117-
echo "plotly.js@$VERSION is already on npm"
118-
exit 0
119-
fi
120-
npm publish --tag "$DIST_TAG"
121-
122-
- name: Publish the bundle and locale packages to npm
123-
run: npm run sync-packages -- --tag "$DIST_TAG"
154+
cd build/release/packages
155+
# Publish plotly.js first, then the bundle and locale packages
156+
for FILE in "plotly.js-$VERSION.tgz" $(ls -- *.tgz | grep -vxF "plotly.js-$VERSION.tgz"); do
157+
NAME="${FILE%-"$VERSION".tgz}"
158+
if npm view "$NAME@$VERSION" version > /dev/null 2>&1; then
159+
echo "Skip $NAME@$VERSION, which is already on npm"
160+
continue
161+
fi
162+
npm publish "$FILE" --tag "$DIST_TAG"
163+
done
124164
125165
- name: Configure AWS credentials for the CDN
126166
uses: aws-actions/configure-aws-credentials@e1253824e5c10ff9df46874f81ed3ec929e19cfd # v6.3.0
@@ -131,7 +171,7 @@ jobs:
131171
- name: Upload the bundles to the CDN
132172
run: |
133173
set -euo pipefail
134-
./tasks/cdn_publish.sh
174+
./tasks/cdn_publish.sh --upload-only
135175
for FILE in "plotly-$VERSION.js" "plotly-$VERSION.min.js"; do
136176
curl --fail --silent --show-error --head "https://cdn.plot.ly/$FILE" > /dev/null
137177
done
@@ -146,10 +186,9 @@ jobs:
146186
exit 0
147187
fi
148188
149-
node tasks/release_notes.mjs > "$RUNNER_TEMP/notes.md"
150189
FLAGS=()
151190
if [[ "$VERSION" == *-* ]]; then FLAGS+=(--prerelease); fi
152-
gh release create "v$VERSION" --verify-tag --title "v$VERSION" --notes-file "$RUNNER_TEMP/notes.md" "${FLAGS[@]}"
191+
gh release create "v$VERSION" --verify-tag --title "v$VERSION" --notes-file build/release/notes.md "${FLAGS[@]}"
153192
154193
- name: Write the run summary
155194
run: |

‎RELEASE.md‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ plotly.js follows [semver](https://semver.org). A release that adds functionalit
77
Two GitHub Actions workflows do the release:
88

99
1. [`prepare-release`](.github/workflows/prepare-release.yml) builds the release files and opens a release pull request. A maintainer starts it by hand.
10-
2. [`publish-release`](.github/workflows/publish-release.yml) starts when the release pull request merges. It tags the merge commit, waits for approval, and then publishes the release to npm, the Plotly CDN, and GitHub.
10+
2. [`publish-release`](.github/workflows/publish-release.yml) starts when the release pull request merges. It builds and packs the release files, waits for approval, and then tags the merge commit and publishes the release to npm, the Plotly CDN, and GitHub.
1111

1212
The workflows tag and publish nothing before the approval. Until then, you can stop a release at no cost. Close the pull request, delete the branch, and start again.
1313

@@ -34,10 +34,9 @@ The workflows tag and publish nothing before the approval. Until then, you can s
3434
> [!CAUTION]
3535
> If `main` changes before you merge, the release includes those changes, but the new `CHANGELOG.md` section does not list them. Their draftlogs stay for the next release. To list them in this release, close the pull request, delete the branch, and run `prepare-release` again.
3636
37-
8. Open the [`publish-release`](https://github.com/plotly/plotly.js/actions/workflows/publish-release.yml) run for the merge commit
38-
9. Click "Review deployments", select `release`, and approve. The workflow then does these steps:
37+
8. Open the [`publish-release`](https://github.com/plotly/plotly.js/actions/workflows/publish-release.yml) run for the merge commit. Its `build` job runs `npm run build` on the merge commit and packs every release file. The published files come from this build, not from the committed `dist/`.
38+
9. Click "Review deployments", select `release`, and approve. The `publish` job then does these steps:
3939
- It tags the merge commit `vX.Y.Z`
40-
- It runs `npm run build` on the tagged commit. The published files come from this build, not from the committed `dist/`.
4140
- It publishes `plotly.js` and the 19 bundle and locale packages to npm, with the dist-tag `latest`, or `rc` for a release candidate
4241
- It uploads the bundles to the Plotly CDN. Then it makes sure that the CDN serves `plotly-X.Y.Z.js` and `plotly-X.Y.Z.min.js`.
4342
- It creates the GitHub release from the CHANGELOG section
@@ -88,7 +87,7 @@ Do not upload a maintenance release to the CDN. This policy encourages every use
8887

8988
## If a publish step fails
9089

91-
Every step in the publish job skips the work that is already done. The npm and GitHub steps make sure that the version is not already published before they publish. The CDN step uploads the files again and overwrites them.
90+
Every step in the `publish` job skips the work that is already done. The job reuses the files from the `build` job of the same run. The npm and GitHub steps make sure that the version is not already published before they publish. The CDN step uploads the files again and overwrites them.
9291

9392
1. Read the failed step in the `publish-release` run, and fix the cause. For example, renew a credential or wait for an npm outage to end.
9493
2. Click "Re-run failed jobs"

‎tasks/cdn_publish.sh‎

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,57 @@
11
#!/bin/bash -e
22

3-
# check if aws is installed
4-
if [[ ! -n `which aws` ]]; then
5-
echo "Error: Please install and configure 'aws'."
3+
# Usage: ./tasks/cdn_publish.sh [--prepare-only | --upload-only]
4+
# With no argument, the script prepares the sync folder and uploads it.
5+
# The release workflow prepares the folder in a job without publish credentials, and uploads it in another job.
6+
mode=$1
7+
if [ -n "$mode" ] && [ "$mode" != "--prepare-only" ] && [ "$mode" != "--upload-only" ]; then
8+
echo "Error: Unknown argument '$mode'. Use --prepare-only or --upload-only."
69
exit 1
710
fi
811

9-
# get plotly.js version from its package json
10-
version=$(node -e "console.log(require('./package.json').version);")
1112
dist=dist
1213
sync=build/sync
1314

14-
# clear and make a sync folder
15-
if [ -d "$sync" ]; then rm -rf $sync; fi
16-
mkdir -p $sync
17-
18-
# copy dist bundles over to the sync folder and rename them
19-
for path in `ls $dist/plotly*`; do
20-
basename=${path##*/}
21-
name=${basename%%.*}
22-
ext="${basename#*.}"
15+
if [ "$mode" != "--upload-only" ]; then
16+
# get plotly.js version from its package json
17+
version=$(node -e "console.log(require('./package.json').version);")
18+
19+
# clear and make a sync folder
20+
if [ -d "$sync" ]; then rm -rf $sync; fi
21+
mkdir -p $sync
22+
23+
# copy dist bundles over to the sync folder and rename them
24+
for path in `ls $dist/plotly*`; do
25+
basename=${path##*/}
26+
name=${basename%%.*}
27+
ext="${basename#*.}"
28+
29+
if [ $name == 'plotly-geo-assets' ] || [ $name == 'plotly-with-meta' ]; then
30+
continue
31+
fi
32+
33+
cp $path "$sync/${name}-${version}.$ext"
34+
done
35+
36+
# copy topojson files over to the sync folder
37+
# NOTE: Temporarily syncing topojson to 'un' folder to avoid overwriting the old topojson
38+
# which some old plots might depend on. These can be accessed by setting 'topojsonURL' to
39+
# 'https://cdn.plot.ly/un/' in the Plotly config.
40+
mkdir $sync/un
41+
cp $dist/topojson/* $sync/un
42+
43+
# list folder and files
44+
echo $sync
45+
ls $sync
46+
fi
2347

24-
if [ $name == 'plotly-geo-assets' ] || [ $name == 'plotly-with-meta' ]; then
25-
continue
48+
if [ "$mode" != "--prepare-only" ]; then
49+
# check if aws is installed
50+
if [[ ! -n `which aws` ]]; then
51+
echo "Error: Please install and configure 'aws'."
52+
exit 1
2653
fi
2754

28-
cp $path "$sync/${name}-${version}.$ext"
29-
done
30-
31-
# copy topojson files over to the sync folder
32-
# NOTE: Temporarily syncing topojson to 'un' folder to avoid overwriting the old topojson
33-
# which some old plots might depend on. These can be accessed by setting 'topojsonURL' to
34-
# 'https://cdn.plot.ly/un/' in the Plotly config.
35-
mkdir $sync/un
36-
cp $dist/topojson/* $sync/un
37-
38-
# list folder and files
39-
echo $sync
40-
ls $sync
41-
42-
# sync to s3
43-
aws s3 sync $sync/ s3://plotly-cdn/ --acl public-read
55+
# sync to s3
56+
aws s3 sync $sync/ s3://plotly-cdn/ --acl public-read
57+
fi

‎tasks/sync_packages.js‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,22 @@ const getDistTag = () => {
4141
return prerelease ? prerelease.split('.')[0] : 'latest';
4242
};
4343

44+
/**
45+
* Get the folder for `--pack`.
46+
*
47+
* @returns The absolute path that follows `--pack`, or undefined when the caller does not pass `--pack`
48+
*/
49+
const getPackDir = () => {
50+
const i = process.argv.indexOf('--pack');
51+
if (i === -1) return undefined;
52+
53+
const dir = process.argv[i + 1];
54+
if (!dir || dir.startsWith('--')) throw new Error('Missing value for --pack');
55+
return path.resolve(dir);
56+
};
57+
58+
const packDir = getPackDir();
59+
4460
const partialBundlePaths = constants.partialBundleNames.map(constants.makePartialBundleOpts);
4561

4662
const distPackages = partialBundlePaths
@@ -236,6 +252,11 @@ const runNpm = (args, cwd) =>
236252
});
237253

238254
const publishToNPM = async (pkgPath, name, tag) => {
255+
if (packDir) {
256+
await runNpm(['pack', '--pack-destination', packDir], pkgPath);
257+
return;
258+
}
259+
239260
if (process.env.DRYRUN) {
240261
console.log(`Dry run, did not publish ${name}`);
241262
await runNpm(['publish', '--dry-run', '--tag', tag], pkgPath);
@@ -285,7 +306,12 @@ const syncLocalesPkg = async (d, tag) => {
285306

286307
const main = async () => {
287308
const tag = getDistTag();
288-
console.log(`Publish the plotly.js ${pkg.version} bundle packages with the "${tag}" dist-tag`);
309+
if (packDir) {
310+
await fs.ensureDir(packDir);
311+
console.log(`Pack the plotly.js ${pkg.version} bundle packages into ${packDir}`);
312+
} else {
313+
console.log(`Publish the plotly.js ${pkg.version} bundle packages with the "${tag}" dist-tag`);
314+
}
289315

290316
// Publish one package at a time, so that a failure stops the run with a clear cause
291317
for (const d of [...distPackages, ...distMinPackages]) await syncBundlePkg(d, tag);

0 commit comments

Comments
 (0)