|
| 1 | +name: Release |
| 2 | + |
| 3 | +# Run by hand from the Actions tab, on main, once CI is green there. See |
| 4 | +# apps/docs/src/content/contributing/publishing.md. |
| 5 | +on: |
| 6 | + workflow_dispatch: |
| 7 | + inputs: |
| 8 | + version: |
| 9 | + description: 'An exact version (0.1.0), or a bump: patch, or minor for a breaking change' |
| 10 | + required: true |
| 11 | + default: patch |
| 12 | + |
| 13 | +# One release at a time: two would race for the same version. |
| 14 | +concurrency: |
| 15 | + group: release |
| 16 | + cancel-in-progress: false |
| 17 | + |
| 18 | +permissions: |
| 19 | + # The version commit, the tag and the GitHub release. |
| 20 | + contents: write |
| 21 | + # npm provenance and trusted publishing. |
| 22 | + id-token: write |
| 23 | + |
| 24 | +jobs: |
| 25 | + # The same gate as every push to main, on exactly the commit being released. |
| 26 | + ci: |
| 27 | + if: github.ref == 'refs/heads/main' |
| 28 | + uses: ./.github/workflows/ci.yml |
| 29 | + permissions: |
| 30 | + contents: read |
| 31 | + actions: read |
| 32 | + |
| 33 | + release: |
| 34 | + needs: ci |
| 35 | + if: github.ref == 'refs/heads/main' |
| 36 | + runs-on: ubuntu-latest |
| 37 | + timeout-minutes: 30 |
| 38 | + steps: |
| 39 | + - uses: actions/checkout@v7 |
| 40 | + with: |
| 41 | + fetch-depth: 0 |
| 42 | + |
| 43 | + - uses: ./.github/actions/setup |
| 44 | + with: |
| 45 | + registry-url: https://registry.npmjs.org |
| 46 | + |
| 47 | + - name: Version |
| 48 | + id: version |
| 49 | + env: |
| 50 | + INPUT: ${{ inputs.version }} |
| 51 | + working-directory: packages/devtools |
| 52 | + run: | |
| 53 | + if [[ ! "$INPUT" =~ ^(patch|minor|[0-9]+\.[0-9]+\.[0-9]+)$ ]]; then |
| 54 | + echo "::error::version must be patch, minor or an exact x.y.z, not '$INPUT'" |
| 55 | + exit 1 |
| 56 | + fi |
| 57 | + IFS=. read -r major minor patch <<< "$(node -p "require('./package.json').version")" |
| 58 | + case "$INPUT" in |
| 59 | + patch) version="$major.$minor.$((patch + 1))" ;; |
| 60 | + minor) version="$major.$((minor + 1)).0" ;; |
| 61 | + # The same version as now is allowed: running again with the exact version of a |
| 62 | + # release that failed part way through finishes it. |
| 63 | + *) version="$INPUT" ;; |
| 64 | + esac |
| 65 | + # Only the version line changes. `npm version` would rewrite the whole file, and turn |
| 66 | + # the escaped characters in the description into literal ones. |
| 67 | + sed -i -E "s/^( \"version\": )\"[^\"]+\"/\1\"$version\"/" package.json |
| 68 | + test "$(node -p "require('./package.json').version")" = "$version" |
| 69 | + echo "version=$version" >> "$GITHUB_OUTPUT" |
| 70 | +
|
| 71 | + - name: Check the changelog |
| 72 | + env: |
| 73 | + VERSION: ${{ steps.version.outputs.version }} |
| 74 | + run: | |
| 75 | + mkdir -p .release |
| 76 | + file=packages/devtools/CHANGELOG.md |
| 77 | + # This version's section, from its `## x.y.z - date` heading (brackets allowed) to the |
| 78 | + # next `## ` heading. |
| 79 | + if [ -f "$file" ]; then |
| 80 | + awk -v v="$VERSION" ' |
| 81 | + /^## / { |
| 82 | + h = substr($0, 4); sub(/^\[/, "", h); split(h, w, /[] ]/) |
| 83 | + inside = (w[1] == v); next |
| 84 | + } |
| 85 | + inside' "$file" > .release/notes.md |
| 86 | + fi |
| 87 | + if [ ! -s .release/notes.md ]; then |
| 88 | + echo "::error file=$file::Add a '## $VERSION' section to $file before releasing." |
| 89 | + exit 1 |
| 90 | + fi |
| 91 | +
|
| 92 | + # `pnpm pack` applies `publishConfig.exports`, and its `prepack` builds the library and the |
| 93 | + # panel. `npm publish` of the tarball is what speaks npm's OIDC for trusted publishing. |
| 94 | + - name: Build |
| 95 | + working-directory: packages/devtools |
| 96 | + run: pnpm pack --pack-destination "$GITHUB_WORKSPACE/.release" |
| 97 | + |
| 98 | + # The exact tarball, installed from a local registry into a fresh Angular CLI app and built. |
| 99 | + - name: Verify the tarball |
| 100 | + run: pnpm verify:publish --tarball="$(ls .release/*.tgz)" |
| 101 | + |
| 102 | + # No token: the package's trusted publisher on npmjs.com names this workflow, and npm |
| 103 | + # authenticates the publish through OIDC. |
| 104 | + - name: Publish |
| 105 | + env: |
| 106 | + VERSION: ${{ steps.version.outputs.version }} |
| 107 | + run: | |
| 108 | + # Already there means an earlier run got this far: skip it, so running again with the |
| 109 | + # same exact version finishes a release that failed part way through. |
| 110 | + if npm view "@pangular-inspector/devtools@$VERSION" version > /dev/null 2>&1; then |
| 111 | + echo "already published: $VERSION" |
| 112 | + else |
| 113 | + npm publish .release/*.tgz --provenance --access public |
| 114 | + fi |
| 115 | +
|
| 116 | + - name: Push the version commit and tag |
| 117 | + env: |
| 118 | + VERSION: ${{ steps.version.outputs.version }} |
| 119 | + run: | |
| 120 | + git config user.name 'github-actions[bot]' |
| 121 | + git config user.email '41898282+github-actions[bot]@users.noreply.github.com' |
| 122 | + tag="devtools@$VERSION" |
| 123 | + if ! git diff --quiet -- packages/devtools/package.json; then |
| 124 | + git commit -m "chore(release): $VERSION" -- packages/devtools/package.json |
| 125 | + fi |
| 126 | + # Pull requests can merge while a release runs. Replay the version commit on top of them |
| 127 | + # and tag where it lands. A rebase conflict fails the step rather than guessing. |
| 128 | + for attempt in 1 2 3; do |
| 129 | + git fetch origin main |
| 130 | + git rebase origin/main |
| 131 | + git tag -f -a "$tag" -m "$tag" |
| 132 | + if git push origin HEAD:main; then break; fi |
| 133 | + if [ "$attempt" = 3 ]; then exit 1; fi |
| 134 | + done |
| 135 | + git push -f origin "refs/tags/$tag" |
| 136 | +
|
| 137 | + - name: GitHub release |
| 138 | + env: |
| 139 | + GH_TOKEN: ${{ github.token }} |
| 140 | + VERSION: ${{ steps.version.outputs.version }} |
| 141 | + run: | |
| 142 | + tag="devtools@$VERSION" |
| 143 | + if gh release view "$tag" > /dev/null 2>&1; then |
| 144 | + echo "release $tag already exists" |
| 145 | + else |
| 146 | + gh release create "$tag" --title "$VERSION" --notes-file .release/notes.md |
| 147 | + fi |
0 commit comments