SReject
SReject

Reputation: 3936

How to setup Github action to only run if a specified git tag does not exist

I'm working to create a github action that will create a release draft. In the action I'd like to only run the release code if the app version does not have an respective git tag

The current action yaml looks similar to:

# ...

jobs:
  # test, winbuild and linuxbuild jobs


  draftrelease:
    needs: [test, winbuild, linuxbuild]
    runs-on: ubuntu-latest
    # if ${{jobs.test.steps.appversion.outputs.version}} is not a tag
    steps:
      # ...

I know I can use the following to print if the tag exists, but I need to check if the tag does not exist within the if:

git show-ref --tags --verify -- "refs/tags/${{jobs.test.steps.appversion.outputs.version}}" 

How would I go about setting up the job to only run if jobs.test.steps.appversion.outputs.versions is not a git tag?

Upvotes: 3

Views: 2686

Answers (1)

SReject
SReject

Reputation: 3936

I managed to achieve this by using a module that reads the app version, checking git tags, and then checking a variable at each step of the build:

jobs:
  build:
    name: Compile Bundles
    strategy:
      matrix:
        os: [windows-latest, ubuntu-latest]

    runs-on: ${{ matrix.os }}
    steps:
      - name: Checkout master branch
        uses: 'actions/checkout@v2'

      # Fetches all tags for the repo
      - name: Fetch tags
        run: git fetch --depth=1 origin +refs/tags/*:refs/tags/*

      # Reads the app's version
      #   In my case, I have a nodejs project, so I read the app's version
      #   from package.json. You may need to find a different github action
      #   module specific to your language to extract the app version
      - name: Read package.json
        id: package
        uses: gregoranders/[email protected]

      # Check if the app version has a git tag
      # If there is a git tag for the version set the variable 'tagged' to 0
      # if there is NOT a git tag for the version set the variable 'tagged' to 1
      - name: 'Check: package version has corrosponding git tag'
        id: tagged
        shell: bash
        run: git show-ref --tags --verify --quiet -- "refs/tags/v${{ steps.package.outputs.version }}" && echo "::set-output name=tagged::0" || echo "::set-output name=tagged::1"

      - name: Step to only run if there is no git-tag for the version
        if: steps.tagged.outputs.tagged == 1
        # ...

      - name: Another step to only run if there is no git-tag for the version
        if: steps.tagged.outputs.tagged == 1
        # ...

The two big things to note is the git --show-ref line and then the if: statements that follow in later steps

# Attempt to output the tagged reference for the app's version
# In my case all version tags are prefixed with 'v' so you may need to alter
# this line to better suit your needs
git show-ref --tags --verify --quiet -- "refs/tags/v${{ steps.package.outputs.version }}"

  # If outputting the tag was successful set the variable indicating the tag exists
  && echo "::set-output name=tagged::0"

  # if outputting failed/errored, set the variable indicating the tag does not exist
  || echo "::set-output name=tagged::1

Once the above has been ran, then the github actions variable steps.tagged.outputs.tagged will be 0 if the version has been tagged, and 1 if the version has not been tagged.

From there you just need to check that variable with each step you only want to run if the version has not been tagged:

if: steps.tagged.outputs.tagged == 1

Git Show-ref Documentation

Github Actions Documentation

Upvotes: 7

Related Questions