nestjs-starter-rest-api icon indicating copy to clipboard operation
nestjs-starter-rest-api copied to clipboard

Contribution: personal workflow to autogenerate version, CHANGELOG.md and tags based on Angular's convetion commits

Open Markkos89 opened this issue 2 years ago • 0 comments

Hello guys, I'm using this repo and wanted to contribute with a workflow I made using github actions. This workflow will autogenerate the last version and tag, the CHANGELOG.md file and updated them in the destination branch, and in the package.json file (the version) and generate a new release on github releases.

Here is the code, it was made for an UI release but I think it will work anyways in any repo. In any case we can arrange it for this repo if you want it (I would be glad to help with it).

name: Release new version
on:
  pull_request: # in my repo, for each pull request closed on main. we can change it accordingly.
    branches: [main]
    types: [closed]
    paths-ignore:
      - '*.md'

jobs:
  build:
    name: Publish new release
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [16.x] 
    # if: github.event.pull_request.merged == true && github.event.pull_request.head.ref == 'staging' # For only merged pull requests from staging to trigger this job
    steps:
      - uses: actions/checkout@v2
        with:
          ref: ${{ github.head_ref }}
          token: ${{ secrets.GITHUB_TOKEN }} 

      # Generate tag and the version of the current release
      - name: Bump version and push tag
        id: tag_version
        uses: mathieudutour/[email protected]
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          release_branches: main
          pre_release_branches: staging
          append_to_pre_release_tag: 'pre-'

      # set the version of the current release without the prefix
      - name: Set version
        run: |
          VERSION=${{ steps.tag_version.outputs.new_tag }}
          echo "VERSION=${VERSION:1}" >> $GITHUB_ENV

      # Updated the package.json file with the new version
      - name: 'Package.json Version update'
        uses: 'phips28/gh-action-bump-version@master'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          skip-tag: 'true'
          target-branch: ${{ github.event.pull_request.base.ref }}
          commit-message: 'CI: bumps version to {{version}} [skip ci]'
          default: '${{ env.VERSION }}'

      # update changelog file based on data from the previous step
      - name: Update Changelog
        uses: stefanzweifel/changelog-updater-action@v1
        with:
          # Pass the output from the tag_version step: new tag, release date, changelog generated and version to the Action.
          latest-version: ${{ steps.tag_version.outputs.new_tag }}
          release-notes: ${{ steps.tag_version.outputs.changelog }}
          release-date: ${{ steps.tag_version.outputs.release_date }}
          compare-url-target-revision: ${{ github.event.pull_request.head.ref }}

      # Commit the changelog file updated on PR base branch
      - name: Commit updated CHANGELOG on PR base branch
        uses: stefanzweifel/git-auto-commit-action@v4
        with:
          skip_fetch: false
          skip_checkout: false
          branch: ${{ github.event.pull_request.base.ref }}
          commit_message: Update CHANGELOG for ${{ steps.tag_version.outputs.new_tag }}
          file_pattern: CHANGELOG.md
          push_options: '--force'
          # User configuration that will made the commit to the branch, maybe we need to configure it accordingly
          commit_user_name: John Doe # defaults to "github-actions[bot]"
          commit_user_email: [email protected] # defaults to "github-actions[bot]@users.noreply.github.com"
          commit_author: John Doe <[email protected]> # defaults to author of the commit that triggered the run

      # Generate the release
      - name: Create a GitHub release
        uses: ncipollo/release-action@v1
        with:
          tag: ${{ steps.tag_version.outputs.new_tag }}
          name: Release ${{ steps.tag_version.outputs.new_tag }}
          body: ${{ steps.tag_version.outputs.changelog }}

I hope it helps! And also thanks for the hard work of this starter template. It rocks!! Happy to be helping you guys.

Best regards!

Markkos89 avatar Jun 25 '22 01:06 Markkos89