Skip to content

Commit e637e88

Browse files
committed
Add workflows to prepare, publish release
1 parent ab89d0b commit e637e88

6 files changed

Lines changed: 717 additions & 305 deletions

File tree

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
name: prepare-release
2+
3+
# Builds the release files and opens a release pull request. Merging that pull request
4+
# starts the publish-release workflow. See RELEASE.md.
5+
6+
on:
7+
workflow_dispatch:
8+
inputs:
9+
version:
10+
description: 'New version: patch, minor, major, or an exact version such as 4.2.0 or 4.2.0-rc.0'
11+
required: true
12+
skip-audit:
13+
description: 'Continue when npm audit reports a vulnerability that has no fix yet'
14+
type: boolean
15+
default: false
16+
skip-audit-reason:
17+
description: 'Why the release cannot wait for a fix. Necessary when you skip the audit.'
18+
required: false
19+
20+
permissions:
21+
contents: read
22+
23+
concurrency:
24+
group: prepare-release
25+
cancel-in-progress: false
26+
27+
jobs:
28+
prepare:
29+
runs-on: ubuntu-latest
30+
steps:
31+
- name: Validate the inputs
32+
env:
33+
VERSION: ${{ inputs.version }}
34+
SKIP_AUDIT: ${{ inputs.skip-audit }}
35+
SKIP_AUDIT_REASON: ${{ inputs.skip-audit-reason }}
36+
run: |
37+
set -euo pipefail
38+
if [ "$SKIP_AUDIT" = "true" ] && [ -z "${SKIP_AUDIT_REASON// /}" ]; then
39+
echo "::error::Enter a reason in skip-audit-reason when you skip the audit."
40+
exit 1
41+
fi
42+
if ! [[ "$VERSION" =~ ^(patch|minor|major|[0-9]+\.[0-9]+\.[0-9]+(-rc\.[0-9]+)?)$ ]]; then
43+
echo "::error::Invalid version '$VERSION'. Use patch, minor, major, X.Y.Z, or X.Y.Z-rc.N."
44+
exit 1
45+
fi
46+
47+
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
48+
with:
49+
ref: main
50+
fetch-depth: 0
51+
fetch-tags: true
52+
persist-credentials: false
53+
54+
- uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6
55+
with:
56+
node-version-file: '.nvmrc'
57+
cache: 'npm'
58+
59+
- name: Install dependencies
60+
run: npm ci
61+
env:
62+
NODE_OPTIONS: '--max-old-space-size=4096'
63+
64+
- name: Check the tooling and the dependency tree
65+
run: npm run preversion
66+
67+
# A finding of any severity stops the release. The build tools produce dist/, so the audit includes dev dependencies.
68+
# With skip-audit, the release continues and the release pull request shows the findings and the reason.
69+
- name: Audit the dependencies
70+
id: audit
71+
env:
72+
SKIP_AUDIT: ${{ inputs.skip-audit }}
73+
run: |
74+
set -uo pipefail
75+
if npm audit > "$RUNNER_TEMP/audit.txt"; then
76+
cat "$RUNNER_TEMP/audit.txt"
77+
exit 0
78+
fi
79+
cat "$RUNNER_TEMP/audit.txt"
80+
if [ "$SKIP_AUDIT" != "true" ]; then
81+
echo "::error::npm audit reported vulnerabilities. Fix them on main, or run this workflow again with skip-audit."
82+
exit 1
83+
fi
84+
echo "::warning::npm audit reported vulnerabilities. The release continues because skip-audit is set."
85+
echo "skipped=true" >> "$GITHUB_OUTPUT"
86+
87+
- name: Bump the version
88+
id: version
89+
env:
90+
VERSION: ${{ inputs.version }}
91+
run: |
92+
set -euo pipefail
93+
OLD_VERSION=$(node -p "require('./package.json').version")
94+
npm version "$VERSION" --no-git-tag-version --ignore-scripts
95+
NEW_VERSION=$(node -p "require('./package.json').version")
96+
97+
# A lower version is a typo. A maintenance release of an older version is a manual process.
98+
if ! node -e '
99+
const parse = (v) => {
100+
const m = v.match(/^(\d+)\.(\d+)\.(\d+)(?:-rc\.(\d+))?$/);
101+
if (!m) throw new Error(`Unsupported version ${v}`);
102+
// A release sorts after each of its release candidates
103+
return [+m[1], +m[2], +m[3], m[4] === undefined ? Infinity : +m[4]];
104+
};
105+
const [next, prev] = process.argv.slice(1).map(parse);
106+
const i = next.findIndex((n, j) => n !== prev[j]);
107+
process.exit(i !== -1 && next[i] > prev[i] ? 0 : 1);
108+
' "$NEW_VERSION" "$OLD_VERSION"; then
109+
echo "::error::Version $NEW_VERSION is not higher than the current version $OLD_VERSION on main"
110+
exit 1
111+
fi
112+
113+
if git rev-parse -q --verify "refs/tags/v$NEW_VERSION" > /dev/null; then
114+
echo "::error::Tag v$NEW_VERSION already exists"
115+
exit 1
116+
fi
117+
if git ls-remote --exit-code --heads origin "release-v$NEW_VERSION" > /dev/null; then
118+
echo "::error::Branch release-v$NEW_VERSION already exists. Close its pull request and delete the branch, then run this workflow again."
119+
exit 1
120+
fi
121+
122+
echo "version=$NEW_VERSION" >> "$GITHUB_OUTPUT"
123+
124+
- name: Prepare the release files
125+
run: node tasks/prepare_release.mjs
126+
127+
- name: Build dist/
128+
run: npm run build
129+
env:
130+
NODE_OPTIONS: '--max-old-space-size=4096'
131+
132+
- name: Write the pull request body
133+
env:
134+
VERSION: ${{ steps.version.outputs.version }}
135+
REPO: ${{ github.repository }}
136+
AUDIT_SKIPPED: ${{ steps.audit.outputs.skipped }}
137+
SKIP_AUDIT_REASON: ${{ inputs.skip-audit-reason }}
138+
run: |
139+
set -euo pipefail
140+
{
141+
echo "### Description"
142+
echo
143+
echo "Updates for release v$VERSION."
144+
echo
145+
echo "### Notes"
146+
echo
147+
echo "- Merging this pull request starts the [publish-release](https://github.com/$REPO/actions/workflows/publish-release.yml) workflow, which waits for approval before it publishes"
148+
echo "- Review the new changelog entry and make sure that attributions are correct"
149+
echo "- If \`main\` changes before you merge, this changelog entry does not list those changes. To list them, close this pull request, delete the branch, and run [prepare-release](https://github.com/$REPO/actions/workflows/prepare-release.yml) again"
150+
151+
if [ "$AUDIT_SKIPPED" = "true" ]; then
152+
echo
153+
echo "### Security audit"
154+
echo
155+
echo "> [!WARNING]"
156+
echo "> \`npm audit\` reported vulnerabilities, and the release skipped the audit check."
157+
echo ">"
158+
echo "> Reason: $SKIP_AUDIT_REASON"
159+
echo
160+
echo "<details><summary>npm audit output</summary>"
161+
echo
162+
echo '```'
163+
head -n 200 "$RUNNER_TEMP/audit.txt"
164+
echo '```'
165+
echo
166+
echo "</details>"
167+
fi
168+
} > "$RUNNER_TEMP/pr-body.md"
169+
170+
# A pull request that GITHUB_TOKEN opens does not start CI, so the release app opens it
171+
- name: Generate a release app token
172+
id: app-token
173+
uses: actions/create-github-app-token@1b10c78c7865c340bc4f6099eb2f838309f1e8c3 # v3.1.1
174+
with:
175+
client-id: ${{ vars.RELEASE_APP_ID }}
176+
private-key: ${{ secrets.RELEASE_APP_PRIVATE_KEY }}
177+
owner: plotly
178+
repositories: plotly.js
179+
180+
- name: Commit and push the release branch
181+
env:
182+
VERSION: ${{ steps.version.outputs.version }}
183+
APP_SLUG: ${{ steps.app-token.outputs.app-slug }}
184+
APP_TOKEN: ${{ steps.app-token.outputs.token }}
185+
GH_TOKEN: ${{ steps.app-token.outputs.token }}
186+
REPO: ${{ github.repository }}
187+
run: |
188+
set -euo pipefail
189+
BOT_ID=$(gh api "/users/${APP_SLUG}[bot]" --jq .id)
190+
git config user.name "${APP_SLUG}[bot]"
191+
git config user.email "${BOT_ID}+${APP_SLUG}[bot]@users.noreply.github.com"
192+
193+
git switch -c "release-v$VERSION"
194+
git add CHANGELOG.md README.md CITATION.cff draftlogs package.json package-lock.json
195+
git add -A lib dist build src/version.js
196+
git commit -m "chore: Release v$VERSION"
197+
git push "https://x-access-token:${APP_TOKEN}@github.com/${REPO}.git" "release-v$VERSION"
198+
199+
- name: Open the release pull request
200+
env:
201+
VERSION: ${{ steps.version.outputs.version }}
202+
GH_TOKEN: ${{ steps.app-token.outputs.token }}
203+
REPO: ${{ github.repository }}
204+
run: |
205+
set -euo pipefail
206+
URL=$(gh pr create --repo "$REPO" --base main --head "release-v$VERSION" \
207+
--title "chore: Release v$VERSION" --label no-draftlog --body-file "$RUNNER_TEMP/pr-body.md")
208+
echo "### Release pull request: $URL" >> "$GITHUB_STEP_SUMMARY"
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
name: publish-release
2+
3+
# 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.
6+
7+
on:
8+
push:
9+
branches: [main]
10+
paths: [package.json]
11+
workflow_dispatch:
12+
13+
permissions:
14+
contents: read
15+
16+
concurrency:
17+
group: publish-release
18+
cancel-in-progress: false
19+
20+
jobs:
21+
check:
22+
runs-on: ubuntu-latest
23+
outputs:
24+
release: ${{ steps.check.outputs.release }}
25+
version: ${{ steps.check.outputs.version }}
26+
dist-tag: ${{ steps.check.outputs.dist-tag }}
27+
steps:
28+
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
29+
with:
30+
fetch-depth: 0
31+
fetch-tags: true
32+
persist-credentials: false
33+
34+
- name: Decide whether this commit is a release
35+
id: check
36+
run: |
37+
set -euo pipefail
38+
VERSION=$(node -p "require('./package.json').version")
39+
TAG="v$VERSION"
40+
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
41+
42+
# A package.json change that keeps the version, such as a dependency update, is not a release
43+
if git rev-parse -q --verify "refs/tags/$TAG" > /dev/null; then
44+
if [ "$(git rev-list -n 1 "$TAG")" != "$(git rev-parse HEAD)" ]; then
45+
echo "Tag $TAG is on another commit, so this commit is not a release"
46+
echo "release=false" >> "$GITHUB_OUTPUT"
47+
exit 0
48+
fi
49+
echo "Tag $TAG is on this commit. Resume the publish steps that did not finish."
50+
fi
51+
52+
if [[ "$VERSION" == *-* ]]; then
53+
PRERELEASE="${VERSION#*-}"
54+
DIST_TAG="${PRERELEASE%%.*}"
55+
else
56+
DIST_TAG=latest
57+
fi
58+
59+
echo "Release $VERSION with the npm dist-tag '$DIST_TAG'"
60+
echo "release=true" >> "$GITHUB_OUTPUT"
61+
echo "dist-tag=$DIST_TAG" >> "$GITHUB_OUTPUT"
62+
63+
publish:
64+
needs: check
65+
if: needs.check.outputs.release == 'true'
66+
runs-on: ubuntu-latest
67+
environment: release
68+
permissions:
69+
contents: write
70+
id-token: write
71+
env:
72+
VERSION: ${{ needs.check.outputs.version }}
73+
DIST_TAG: ${{ needs.check.outputs.dist-tag }}
74+
steps:
75+
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
76+
with:
77+
fetch-depth: 0
78+
fetch-tags: true
79+
80+
- uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6
81+
with:
82+
node-version-file: '.nvmrc'
83+
cache: 'npm'
84+
registry-url: 'https://registry.npmjs.org'
85+
86+
- name: Install dependencies
87+
run: npm ci
88+
env:
89+
NODE_OPTIONS: '--max-old-space-size=4096'
90+
91+
- name: Tag the release
92+
run: |
93+
set -euo pipefail
94+
if git rev-parse -q --verify "refs/tags/v$VERSION" > /dev/null; then
95+
echo "Tag v$VERSION already exists"
96+
exit 0
97+
fi
98+
git config user.name "github-actions[bot]"
99+
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
100+
git tag -a "v$VERSION" -m "v$VERSION"
101+
git push origin "v$VERSION"
102+
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
114+
run: |
115+
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"
124+
125+
- name: Configure AWS credentials for the CDN
126+
uses: aws-actions/configure-aws-credentials@e1253824e5c10ff9df46874f81ed3ec929e19cfd # v6.3.0
127+
with:
128+
role-to-assume: ${{ vars.CDN_AWS_ROLE_ARN }}
129+
aws-region: ${{ vars.CDN_AWS_REGION }}
130+
131+
- name: Upload the bundles to the CDN
132+
run: |
133+
set -euo pipefail
134+
./tasks/cdn_publish.sh
135+
for FILE in "plotly-$VERSION.js" "plotly-$VERSION.min.js"; do
136+
curl --fail --silent --show-error --head "https://cdn.plot.ly/$FILE" > /dev/null
137+
done
138+
139+
- name: Create the GitHub release
140+
env:
141+
GH_TOKEN: ${{ github.token }}
142+
run: |
143+
set -euo pipefail
144+
if gh release view "v$VERSION" > /dev/null 2>&1; then
145+
echo "GitHub release v$VERSION already exists"
146+
exit 0
147+
fi
148+
149+
node tasks/release_notes.mjs > "$RUNNER_TEMP/notes.md"
150+
FLAGS=()
151+
if [[ "$VERSION" == *-* ]]; then FLAGS+=(--prerelease); fi
152+
gh release create "v$VERSION" --verify-tag --title "v$VERSION" --notes-file "$RUNNER_TEMP/notes.md" "${FLAGS[@]}"
153+
154+
- name: Write the run summary
155+
run: |
156+
{
157+
echo "### Published plotly.js v$VERSION"
158+
echo "- npm dist-tag: \`$DIST_TAG\`"
159+
echo "- GitHub release: https://github.com/${{ github.repository }}/releases/tag/v$VERSION"
160+
} >> "$GITHUB_STEP_SUMMARY"

‎package.json‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@
7878
"start": "node devtools/test_dashboard/server.mjs",
7979
"baseline": "node test/image/make_baseline.mjs",
8080
"preversion": "check-node-version --node 22 --npm 10 && npm-link-check && npm ls --omit=dev --all",
81-
"version": "npm run build && git add -A lib dist build src/version.js",
82-
"postversion": "node -e \"console.log('Version bumped and committed. If ok, run: git push && git push --tags')\"",
83-
"postpublish": "node tasks/sync_packages.js",
81+
"sync-packages": "node tasks/sync_packages.js",
8482
"prepack": "tsc -b tsconfig.build.json --force",
8583
"postpack": "tsc -b tsconfig.build.json --clean"
8684
},

0 commit comments

Comments
 (0)