|
| 1 | +name: Release |
| 2 | +on: |
| 3 | + push: |
| 4 | + tags: |
| 5 | + - 'v*' |
| 6 | + workflow_dispatch: |
| 7 | + inputs: |
| 8 | + tag: |
| 9 | + description: 'Release tag (e.g., v1.2.3)' |
| 10 | + required: true |
| 11 | + type: string |
| 12 | + skip_ci_check: |
| 13 | + description: 'Skip CI status check' |
| 14 | + required: false |
| 15 | + type: boolean |
| 16 | + default: false |
| 17 | + |
| 18 | +permissions: |
| 19 | + contents: write |
| 20 | + id-token: write |
| 21 | + |
| 22 | +jobs: |
| 23 | + release: |
| 24 | + name: Release |
| 25 | + runs-on: ubuntu-latest |
| 26 | + environment: npm |
| 27 | + steps: |
| 28 | + - name: Checkout |
| 29 | + uses: actions/checkout@v4 |
| 30 | + with: |
| 31 | + ref: ${{ github.event_name == 'workflow_dispatch' && inputs.tag || github.ref }} |
| 32 | + fetch-depth: 0 |
| 33 | + |
| 34 | + - name: Verify tag matches package.json version |
| 35 | + run: | |
| 36 | + jq --raw-output --exit-status --arg tag "$RELEASE_TAG" ' |
| 37 | + if (.version == ($tag | ltrimstr("v"))) then |
| 38 | + "Package version (\(.version)) matches tag version (\($tag | ltrimstr("v")))" |
| 39 | + else |
| 40 | + "Package version (\(.version)) does not match tag version (\($tag | ltrimstr("v")))" | halt_error(1) |
| 41 | + end' package.json |
| 42 | + env: |
| 43 | + RELEASE_TAG: ${{ github.event_name == 'workflow_dispatch' && inputs.tag || github.ref_name }} |
| 44 | + |
| 45 | + - name: Verify commit is in main branch |
| 46 | + run: | |
| 47 | + # Check if the tagged commit is included in the main branch |
| 48 | + if git merge-base --is-ancestor ${{ github.sha }} origin/main; then |
| 49 | + echo "Tagged commit is properly included in main branch" |
| 50 | + else |
| 51 | + echo "Tagged commit is not included in the main branch" |
| 52 | + echo "Please push the commit to main before releasing" |
| 53 | + exit 1 |
| 54 | + fi |
| 55 | +
|
| 56 | + - name: Check CI status |
| 57 | + if: ${{ !inputs.skip_ci_check }} |
| 58 | + run: | |
| 59 | + # Check if CI has completed successfully for this commit |
| 60 | + gh run list --commit ${{ github.sha }} --status success --json workflowName | jq --raw-output --exit-status ' |
| 61 | + if any(.[]; .workflowName == "Install and test @ava/typescript") then |
| 62 | + "All CI checks have passed!" |
| 63 | + else |
| 64 | + "CI has not completed successfully for this commit" | halt_error(1) |
| 65 | + end' |
| 66 | + env: |
| 67 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 68 | + |
| 69 | + - name: Setup Node.js |
| 70 | + uses: actions/setup-node@v4 |
| 71 | + with: |
| 72 | + node-version-file: package.json |
| 73 | + cache: npm |
| 74 | + registry-url: https://registry.npmjs.org |
| 75 | + |
| 76 | + - name: Publish to npm with provenance |
| 77 | + run: npm publish --provenance |
| 78 | + env: |
| 79 | + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |
| 80 | + |
| 81 | + - name: Create GitHub Release |
| 82 | + run: | |
| 83 | + gh release create "$RELEASE_TAG" \ |
| 84 | + --title "$RELEASE_TAG" \ |
| 85 | + --draft \ |
| 86 | + --generate-notes |
| 87 | + env: |
| 88 | + RELEASE_TAG: ${{ github.event_name == 'workflow_dispatch' && inputs.tag || github.ref_name }} |
| 89 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments