|
| 1 | +name: Publish to NPM |
| 2 | + |
| 3 | +# Bumps the package version, generates changelog, creates a git tag, publishes to NPM, and creates a GitHub release. |
| 4 | + |
| 5 | +on: |
| 6 | + workflow_dispatch: |
| 7 | + inputs: |
| 8 | + bump: |
| 9 | + description: 'Version bump' |
| 10 | + required: true |
| 11 | + type: choice |
| 12 | + default: 'patch' |
| 13 | + options: |
| 14 | + - 'major' |
| 15 | + - 'minor' |
| 16 | + - 'patch' |
| 17 | + - 'none' # Use 'none' to just publish the current version without bumping, useful if version is set manually. |
| 18 | + |
| 19 | +permissions: |
| 20 | + contents: write |
| 21 | + id-token: write |
| 22 | + |
| 23 | +jobs: |
| 24 | + publish: |
| 25 | + name: Publish |
| 26 | + runs-on: ubuntu-latest |
| 27 | + steps: |
| 28 | + - name: Check if branch is `master` |
| 29 | + if: github.ref != 'refs/heads/master' |
| 30 | + run: | |
| 31 | + echo "This workflow can only be run on the master branch." |
| 32 | + exit 1 |
| 33 | +
|
| 34 | + - name: Checkout repository |
| 35 | + uses: actions/checkout@v6 |
| 36 | + with: |
| 37 | + token: ${{ secrets.APIFY_SERVICE_ACCOUNT_GITHUB_TOKEN }} |
| 38 | + fetch-depth: 0 # Fetch all history for tags |
| 39 | + |
| 40 | + - name: Setup Node.js |
| 41 | + uses: actions/setup-node@v6 |
| 42 | + with: |
| 43 | + node-version: 24 |
| 44 | + |
| 45 | + - name: Install dependencies |
| 46 | + run: npm install |
| 47 | + |
| 48 | + - name: Bump version |
| 49 | + if: ${{ inputs.bump != 'none' }} |
| 50 | + run: npm version ${{ inputs.bump }} --no-git-tag-version |
| 51 | + |
| 52 | + - name: Get current version |
| 53 | + id: get_version |
| 54 | + run: echo "VERSION=$(node -p "require('./package.json').version")" >> $GITHUB_OUTPUT |
| 55 | + |
| 56 | + # We need to create a commit and tag for the new version so that git-cliff can |
| 57 | + # get the current version to generate the changelog. |
| 58 | + # This commit will be ignored by git-cliff because of its title. |
| 59 | + - name: Commit version bump |
| 60 | + run: | |
| 61 | + # Add a newline to CHANGELOG.md so that there is always a change to commit, even if the version was bumped manually before. |
| 62 | + # CHANGELOG.md will be regenerated later with git-cliff, so this is a noop. |
| 63 | + echo "" >> CHANGELOG.md |
| 64 | + git config user.name "Apify Release Bot" |
| 65 | + git config user.email "noreply@apify.com" |
| 66 | + git add . |
| 67 | + git commit -m "chore(release): Release new version [skip ci]" |
| 68 | + git tag "v${{ steps.get_version.outputs.VERSION }}" |
| 69 | + |
| 70 | + - name: Generate full changelog |
| 71 | + id: git-cliff |
| 72 | + uses: orhun/git-cliff-action@v4 |
| 73 | + env: |
| 74 | + OUTPUT: CHANGELOG.md |
| 75 | + |
| 76 | + - name: Amend commit with updated changelog |
| 77 | + run: | |
| 78 | + git add CHANGELOG.md |
| 79 | + git commit --amend --no-edit |
| 80 | + # Move the tag to point to the amended commit |
| 81 | + git tag -f "v${{ steps.get_version.outputs.VERSION }}" |
| 82 | +
|
| 83 | + - name: Publish to NPM |
| 84 | + run: npm publish |
| 85 | + |
| 86 | + - name: Push changes |
| 87 | + run: git push origin $(git rev-parse --abbrev-ref HEAD) --tags |
| 88 | + |
| 89 | + # Generate release notes only from the current version |
| 90 | + - name: Generate changelog for release notes |
| 91 | + id: git-cliff-release-notes |
| 92 | + uses: orhun/git-cliff-action@v4 |
| 93 | + with: |
| 94 | + args: --current --strip all |
| 95 | + |
| 96 | + - name: Format release notes |
| 97 | + id: format-release-notes |
| 98 | + env: |
| 99 | + # Pass input as environment variable to prevent injection issues in the script |
| 100 | + UNFORMATTED_RELEASE_NOTES: ${{ steps.git-cliff-release-notes.outputs.content }} |
| 101 | + run: | |
| 102 | + # Git cliff outputs the version at the top, we don't need that in the release notes. |
| 103 | + # We can simply skip the first two lines and then trim any leading/trailing whitespace. |
| 104 | + FORMATTED_RELEASE_NOTES=$(echo "${UNFORMATTED_RELEASE_NOTES}" | tail -n +3 | sed '/^$/d') |
| 105 | + { |
| 106 | + echo "formatted_release_notes<<EOF" |
| 107 | + echo "${FORMATTED_RELEASE_NOTES}" |
| 108 | + echo "EOF" |
| 109 | + } >> $GITHUB_OUTPUT |
| 110 | +
|
| 111 | + - name: Create release |
| 112 | + uses: softprops/action-gh-release@v2 |
| 113 | + with: |
| 114 | + tag_name: "v${{ steps.get_version.outputs.VERSION }}" |
| 115 | + name: "v${{ steps.get_version.outputs.VERSION }}" |
| 116 | + body: ${{ steps.format-release-notes.outputs.formatted_release_notes }} |
0 commit comments