|
| 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