Skip to content

Commit cf771fa

Browse files
authored
Merge pull request #8177 from plotly/cam/8136/automate-release-process
chore: Automate release process
2 parents 63feec2 + 8d08925 commit cf771fa

17 files changed

Lines changed: 1595 additions & 1147 deletions

‎.github/workflows/check-draftlog.yml‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,28 @@ jobs:
6464
fi
6565
6666
MISSING_LINK=""
67+
BAD_INDENT_FILES=""
6768
while IFS= read -r f; do
6869
[ -z "$f" ] && continue
6970
content=$(gh api -H 'Accept: application/vnd.github.raw' "/repos/$REPO/contents/$f?ref=$HEAD_SHA")
7071
if ! echo "$content" | grep -qE "\[\[#${PR_NUMBER}\]\(https://github\.com/plotly/plotly\.js/(pull|issues)/${PR_NUMBER}\)\]"; then
7172
MISSING_LINK="$MISSING_LINK$f"$'\n'
7273
fi
74+
# The first line is the top-level entry. Later lines can need indentation.
75+
if echo "$content" | head -n 1 | grep -qE '^[[:space:]]'; then
76+
BAD_INDENT_FILES="$BAD_INDENT_FILES$f"$'\n'
77+
fi
7378
done <<< "$ADDED"
7479
80+
if [ -n "$BAD_INDENT_FILES" ]; then
81+
echo "::error::Draftlog entry(ies) with leading whitespace on the first line:"
82+
printf '%s' "$BAD_INDENT_FILES" | sed 's/^/ - /'
83+
echo ""
84+
echo "Start the first line with '- ' and no leading whitespace."
85+
echo "See draftlogs/README.md for an example."
86+
exit 1
87+
fi
88+
7589
if [ -n "$MISSING_LINK" ]; then
7690
echo "::error::Draftlog entry(ies) missing a link to this PR (#${PR_NUMBER}):"
7791
printf '%s' "$MISSING_LINK" | sed 's/^/ - /'
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"

0 commit comments

Comments
 (0)