How to run a job based on a tag-name

Viewed 326

I have the following condition:

name: Build Image

on:
  push:
  pull_request:
  workflow_dispatch:
    

jobs:

  build-image: (This should be only if the tag contains "azure")
    name: Build Image Job
    runs-on: [ self-hosted ]

    steps:
    - uses: actions/checkout@v2

    - name: Run Build Step
      run: |
        ANSIBLE_VERSION=$(cat VERSION)
        docker images

jobs:

  build-image: (This will be for everything else)
    name: Build Image Job
    runs-on: [ self-hosted ]

    steps:
    - uses: actions/checkout@v2

    - name: Run Build Step
      run: |
        ANSIBLE_VERSION=$(cat VERSION)
        docker images

How can that be achieved? How do you reference the tags inside a job? How do you the variable and make an if rule out of it?

1 Answers

You can use regex in your on config to trigger a workflow based on a tag name. For example:

on:
  push:
    tags:
      - '*azure*'

At the level of a job or a step, you can use a contains() expression:

jobs:
  build-for-azure-tag:
    name: build-for-azure-tag
    runs-on: ubuntu-latest
    if: github.ref_type == 'tag' && contains(github.ref_name, 'azure')
Related