|
| 1 | +name: Release package |
| 2 | +on: |
| 3 | + workflow_dispatch: |
| 4 | + inputs: |
| 5 | + release-type: |
| 6 | + description: 'Release type (one of): patch, minor, major, prepatch, preminor, premajor, prerelease' |
| 7 | + required: true |
| 8 | +permissions: |
| 9 | + id-token: write |
| 10 | + contents: write |
| 11 | +jobs: |
| 12 | + check_ci: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + steps: |
| 15 | + - name: Ensure latest CI on default branch succeeded |
| 16 | + uses: actions/github-script@v7 |
| 17 | + with: |
| 18 | + script: | |
| 19 | + const { data: repo } = await github.rest.repos.get({ |
| 20 | + owner: context.repo.owner, |
| 21 | + repo: context.repo.repo, |
| 22 | + }); |
| 23 | + const { data: branch } = await github.rest.repos.getBranch({ |
| 24 | + owner: context.repo.owner, |
| 25 | + repo: context.repo.repo, |
| 26 | + branch: repo.default_branch, |
| 27 | + }); |
| 28 | + const headSha = branch.commit.sha; |
| 29 | +
|
| 30 | + const { data: runs } = await github.rest.actions.listWorkflowRuns({ |
| 31 | + owner: context.repo.owner, |
| 32 | + repo: context.repo.repo, |
| 33 | + workflow_id: "ci.yml", |
| 34 | + branch: repo.default_branch, |
| 35 | + status: "completed", |
| 36 | + per_page: 1, |
| 37 | + }); |
| 38 | + if (!runs.workflow_runs.length) { |
| 39 | + core.setFailed("No CI runs found on default branch."); |
| 40 | + return; |
| 41 | + } |
| 42 | + const run = runs.workflow_runs[0]; |
| 43 | + if (run.head_sha !== headSha) { |
| 44 | + core.setFailed(`Latest CI run is not for the current ${repo.default_branch} head (${headSha}). Latest run: ${run.html_url}`); |
| 45 | + return; |
| 46 | + } |
| 47 | + if (run.conclusion !== "success") { |
| 48 | + core.setFailed(`Latest CI run on ${repo.default_branch} did not succeed: ${run.html_url}`); |
| 49 | + } |
| 50 | + release: |
| 51 | + runs-on: ubuntu-latest |
| 52 | + needs: [check_ci] |
| 53 | + steps: |
| 54 | + # Checkout project repository |
| 55 | + - name: Checkout |
| 56 | + uses: actions/checkout@v4 |
| 57 | + |
| 58 | + # Setup Node.js environment |
| 59 | + - name: Setup Node.js |
| 60 | + uses: actions/setup-node@v4 |
| 61 | + with: |
| 62 | + registry-url: https://registry.npmjs.org/ |
| 63 | + node-version: "22" |
| 64 | + cache: pnpm |
| 65 | + |
| 66 | + - name: Setup pnpm |
| 67 | + uses: pnpm/action-setup@v4 |
| 68 | + with: |
| 69 | + version: "10" |
| 70 | + |
| 71 | + - name: Ensure npm supports trusted publishing |
| 72 | + run: npm install -g npm@^11.5.1 |
| 73 | + |
| 74 | + - name: Install dependencies |
| 75 | + run: pnpm install --frozen-lockfile |
| 76 | + |
| 77 | + # Configure Git |
| 78 | + - name: Git configuration |
| 79 | + run: | |
| 80 | + git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 81 | + git config --global user.name "GitHub Actions" |
| 82 | +
|
| 83 | + # Bump package version |
| 84 | + # Use tag latest |
| 85 | + - name: Bump release version |
| 86 | + if: startsWith(github.event.inputs.release-type, 'pre') != true |
| 87 | + run: | |
| 88 | + echo "NEW_VERSION=$(npm --no-git-tag-version version $RELEASE_TYPE)" >> $GITHUB_ENV |
| 89 | + echo "RELEASE_TAG=latest" >> $GITHUB_ENV |
| 90 | + env: |
| 91 | + RELEASE_TYPE: ${{ github.event.inputs.release-type }} |
| 92 | + |
| 93 | + # Bump package pre-release version |
| 94 | + # Use tag beta for pre-release versions |
| 95 | + - name: Bump pre-release version |
| 96 | + if: startsWith(github.event.inputs.release-type, 'pre') |
| 97 | + run: | |
| 98 | + echo "NEW_VERSION=$(npm --no-git-tag-version --preid=beta version $RELEASE_TYPE)" >> $GITHUB_ENV |
| 99 | + echo "RELEASE_TAG=beta" >> $GITHUB_ENV |
| 100 | + env: |
| 101 | + RELEASE_TYPE: ${{ github.event.inputs.release-type }} |
| 102 | + |
| 103 | + # Update changelog unreleased section with new version |
| 104 | + - name: Update changelog |
| 105 | + uses: superfaceai/release-changelog-action@v1 |
| 106 | + with: |
| 107 | + path-to-changelog: CHANGELOG.md |
| 108 | + version: ${{ env.NEW_VERSION }} |
| 109 | + operation: release |
| 110 | + |
| 111 | + # Commit changes |
| 112 | + - name: Sync lockfile after version bump |
| 113 | + run: pnpm install --lockfile-only |
| 114 | + |
| 115 | + - name: Commit CHANGELOG.md and package.json changes and create tag |
| 116 | + run: | |
| 117 | + git add "package.json" |
| 118 | + git add "pnpm-lock.yaml" |
| 119 | + git add "CHANGELOG.md" |
| 120 | + git commit -m "chore: release ${{ env.NEW_VERSION }}" |
| 121 | + git tag ${{ env.NEW_VERSION }} |
| 122 | +
|
| 123 | + # Publish version to public repository |
| 124 | + - name: Publish |
| 125 | + run: npm publish --provenance --access public --tag ${{ env.RELEASE_TAG }} |
| 126 | + |
| 127 | + # Push repository changes |
| 128 | + - name: Push changes to repository |
| 129 | + env: |
| 130 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 131 | + run: | |
| 132 | + git push origin && git push --tags |
| 133 | +
|
| 134 | + # Read version changelog |
| 135 | + - id: get-changelog |
| 136 | + name: Get version changelog |
| 137 | + uses: superfaceai/release-changelog-action@v1 |
| 138 | + with: |
| 139 | + path-to-changelog: CHANGELOG.md |
| 140 | + version: ${{ env.NEW_VERSION }} |
| 141 | + operation: read |
| 142 | + |
| 143 | + # Update GitHub release with changelog |
| 144 | + - name: Update GitHub release documentation |
| 145 | + uses: softprops/action-gh-release@v1 |
| 146 | + with: |
| 147 | + tag_name: ${{ env.NEW_VERSION }} |
| 148 | + body: ${{ steps.get-changelog.outputs.changelog }} |
| 149 | + prerelease: ${{ startsWith(github.event.inputs.release-type, 'pre') }} |
| 150 | + env: |
| 151 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments