Release from tagged pushes

Hey everyone, I recently got into CI and saw the way it allows for Artefacts to be used. What I would like to do is:

  1. If I push a commit, that it builds the project (C#). If it is succesful, I can use the tag. This step I’ve figured out.
  2. If I push a tagged commit, it would be nice that it also posts a release version that people could download. I use GitHub desktop, and you have those two boxes. One for summary and one for description. If those would be usable in creating a Title and description for the release, it would be great. (Working on this currently)
  3. Is it possible for the release number to increment. So v1.1, v1.2 etc?

I am currently on 30ish failed updated .yml files.

The project I am working on is: https://github.com/Laemers/BattleNavHelper
My .yml is as follows:

name: .NET Core

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  build:

    runs-on: windows-latest

    steps:
    - uses: actions/checkout@v2
    - name: Setup Nuget.exe
      uses: warrenbuckley/Setup-Nuget@v1
    - name: Restore Package
      run: nuget restore BattleNavHelper.sln 
    - name: Setup MSBuild.exe
      uses: microsoft/Setup-MSBuild@v1
    - name: Build with MSBuild
      run: msbuild BattleNavHelper.sln



  release:
    name: Create Release
    runs-on: windows-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Create Release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token
        with:
          tag_name: ${{ github.ref }}
          release_name: Release ${{ github.ref }}
          body: |
            Changes in this Release
            - First Change
            - Second Change
          draft: false
          prerelease: false

@PloxKevin,

By default, the push event is available to pushes to branches and pushes to tags.

on:
  push:  # no any branches filter or tags filter set on push, all pushes can triggered the workflow

However, if you only define branches filter (branches or branches-ignore), the workflow won’t be triggered when you push to tags. similarly, if your only define tags filter (tags or tags-ignore), the workflow won’t be triggered when you push to branches.

If you want the workflow will be triggered when you push to some specified branches or tags, you need to add both branches filter and tags filter on push event.

on:
  push:
    branches:
      - 'branches_filter_pattern_1'
      - 'branches_filter_pattern_2'
      . . .
      - 'branches_filter_pattern_N'
    tags:
      - 'tags_filter_pattern_1'
      - 'tags_filter_pattern_2'
      . . .
      - 'tags_filter_pattern_N'

About generating auto-incrementing build/release number, I found the following actions from the GitHub Marketplace, you can use one of them in your workflow:

According to your requests, you can reference the demo below to configure your workflow.

on:
  push:
    branches:
      <some branches_filter_patterns>
    tags:
      <some tags_filter_patterns>
  pull_request:
    branches:
      <some branches_filter_patterns>
    tags:
      <some tags_filter_patterns>

jobs:
  build:
    name: Build project
    . . .
    steps:
    ---- some steps for build ----

  get_tag:
    name: Get release tag
    needs: build
    runs-on: ubuntu-latest
    outputs:
      release_tag: ${{ steps.output_tag.outputs.tag }}
    steps:
      - name: Get existing tag from github.ref
        if: ${{ contains(github.ref, 'refs/tags/') }}
        run: echo "::set-env name=tag::${GITHUB_REF#refs/tags/}"

      - name: Generate number
        id: generate_number
        if: ${{ contains(github.ref, 'refs/heads/') }}
        uses: travisj/build-number-action@v1

      - name: Generate new tag
        if: ${{ contains(github.ref, 'refs/heads/') }}
        run: echo "::set-env name=tag::v1.${{ steps.generate_number.outputs.build-number }}"

      - name: Set output
        id: output_tag
        run: echo "::set-output name=tag::${{ env.tag }}"

  release:
    name: Release project
    needs: get_tag
    . . .
    steps:
      . . .
      - name: Create Release
        id: create_release
        uses: actions/create-release@v1
        with:
          tag_name: ${{ needs.get_tag.outputs.release_tag }}
          release_name: Release ${{ needs.get_tag.outputs.release_tag }}
          body: |
           . . .
      . . .
1 Like

Hey @brightran.

Thank you so much for helping me on the last part. I have advanced a bit now and I currently the following. But first, let me explain the situation:

  1. I use GitHub desktop and I use right click on the commits to add a tag to a certain commit.

The build step and the get_tag step work perfectly fine. They generate a pass. Currently, in the code it allows for the artifact to be uploaded. So I am pretty close to my final personal goal.

So when I push using a tag, say for instance v1.3.2, it still gives me the error : This is not a tagged push..
I figured that I need to fix my code in such a way that it needs to work with manual tags first, and when that works, I could look into auto-generating tags. From there on I can use if statements to check when it needs to be released or not. My code is as follows.

Maybe it’s a problem as I use windows-latest on the build part and ubuntu for the other parts?

name: .NET Core

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  build:
    runs-on: windows-latest
    
    steps:
    - uses: actions/checkout@v2
    - name: Setup Nuget.exe
      uses: warrenbuckley/Setup-Nuget@v1
    - name: Restore Package
      run: nuget restore BattleNavHelper.sln
    - name: Setup MSBuild.exe
      uses: microsoft/Setup-MSBuild@v1
    - name: Build with MSBuild
      run: msbuild BattleNavHelper.sln
    - run: mkdir staging && cp BattleNavHelper/bin/Debug/*.exe staging
    - uses: actions/upload-artifact@v1
      with:
        name: Package
        path: staging
        
  get_tag:
    name: Get release tag
    needs: build
    runs-on: ubuntu-latest
    outputs:
      release_tag: ${{ steps.output_tag.outputs.tag }}
    steps:
      - name: Get existing tag from github.ref
        if: ${{ contains(github.ref, 'refs/tags/') }}
        run: echo "::set-env name=tag::${GITHUB_REF#refs/tags/}"

      - name: Generate number
        id: generate_number
        if: ${{ contains(github.ref, 'refs/heads/') }}
        uses: travisj/build-number-action@v1

      - name: Generate new tag
        if: ${{ contains(github.ref, 'refs/heads/') }}
        run: echo "::set-env name=tag::v1.${{ steps.generate_number.outputs.build-number }}"

      - name: Set output
        id: output_tag
        run: echo "::set-output name=tag::${{ env.tag }}"
        
  release:
    runs-on: ubuntu-latest
    needs: [build, get_tag]
    steps:
      - uses: actions/checkout@master
      - name: Create GitHub release
        uses: Roang-zero1/github-create-release-action@master
        with:
          version_regex: '^v[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+'
          update_existing: 'true'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

@PloxKevin,

So when I push using a tag, say for instance v1.3.2, it still gives me the error : This is not a tagged push..

Which step do you get this error in? Manually create tag on GitHub Desktop, or run some step in the workflow?

Maybe it’s a problem as I use windows-latest on the build part and ubuntu for the other parts?

As far as your case is concerned, I don’t think these will have any affect.

In addition, GitHub has provided an official action for users to create release (actions/create-release), you can directly use this action to create a release with the tag that you get from the “get_tag” job.

@brightran

The error is given in the workflow. Adding a tag on Github desktop works, and allows me to push this change. Since I am not filtering for branches or tags, the workflow should trigger on each commit.

So what I did was, create commit, add a tag, and then push it using Github Desktop.

@PloxKevin,

Since I am not filtering for branches or tags, the workflow should trigger on each commit.

But I see you have set branches filter on push event in your workflow.

on:
  push:
    branches: [ master ]
  . . .

This will trigger the workflow when you push commits to master branch. Pushes to tags will not trigger the workflow. As I mentioned abouve,

if you only define branches filter ( branches or branches-ignore ), the workflow won’t be triggered when you push to tags. similarly, if your only define tags filter ( tags or tags-ignore ), the workflow won’t be triggered when you push to branches.

When you push commits to master branch, the git ref (github.ref) is ‘refs/heads/master’, it is a branch ref, not tag ref. You can’t directly release a branch ref.

You can try like as below:

on:
  push:
  . . .

OR

on:
  push:
    branches:
      - master
    tags:
      - '**'
  . . .

In addition, you have get the tag name from the “get_tag” job, you can directly use this tag name to release.