Home > Software design >  How to versioning releases in Github Actions
How to versioning releases in Github Actions

Time:12-30

Is there a way to publish releases on GitHub using Actions with custom version numbers? Currently, I'm using github.run_number provided by GitHub Context and as mentioned in the docs:

github.run_number (string) - A unique number for each run 
of a particular workflow in a repository. This number begins
at 1 for the workflow's first run, and increments with each new run.

Not every run of my workflow creates a release (e.g. when a workflow fails), resulting in inconsistent version numbers. I've created a demo repository and as you can see, the release numbers are ...38,39,40,47,49. I didn't find any solution for this in GitHub Actions Docs.

I want to have consistent incrementally growing version numbers or even a v.x.x structure if it's possible.

My complete workflow can be found here, my release-project job is:

...previous jobs: build, test, deploy...
release-project:
    name: Release project
    needs: deploy-project
    ...
      - name: Create release
        id: create_release_id
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.run_number }}
          release_name: Release ${{ github.run_number }}
      - name: Upload release asset
        id: upload-release-asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release_id.outputs.upload_url }}
          asset_path: ./project.zip
          asset_name: project-v${{ github.run_number }}.zip
          asset_content_type: application/zip

CodePudding user response:

I would suggest not relying on run_number and using the latest tag from the repo to generate the next version based on it. For example, you can use the Get Latest Tag, Next SemVers, and Next Monotonic Release version GH Actions.

Semantic versioning workflow:

...

jobs:
  test-next-release:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0 # required for github-action-get-previous-tag

      - name: Get previous tag
        id: previoustag
        uses: 'WyriHaximus/github-action-get-previous-tag@v1'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Get next minor version
        id: semver
        uses: 'WyriHaximus/github-action-next-semvers@v1'
        with:
          version: ${{ steps.previoustag.outputs.tag }}

      - name: Create release
        id: create_release_id
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ steps.semver.outputs.patch }}
          release_name: Release ${{ steps.semver.outputs.patch }}

Consecutive numbers versioning workflow:

...

jobs:
  test-next-release-custom:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0 # required for github-action-get-previous-tag

      - name: Get Previous tag
        id: previoustag
        uses: 'WyriHaximus/github-action-get-previous-tag@v1'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          
      - name: Get next version
        id: next
        uses: 'WyriHaximus/[email protected]'
        with:
          version: ${{ steps.previoustag.outputs.tag }}

      - name: Create release
        id: create_release_id
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ steps.next.outputs.version }}
          release_name: Release ${{ steps.next.outputs.version }}
  • Related