How to read tag name using workflow_run

Viewed 585

I'm using Github actions with two workflows: CI and CD. The CI workflow is triggered for new tags like v1.1.1 and pull requests to develop and hotfix branches.

name: CI

on:
  push:
    tags: v[1-9]+.[0-9]+.[0-9]+
    
  pull_request:
    branches: [develop, hotfix*]

The CD workflow is triggered when the previous workflow (CI) is completed.

name: CD

on:
  workflow_run:
    workflows: ['CI']
    push:
      tags: v[1-9]+.[0-9]+.[0-9]+
    types:
      - completed

Currently, my goal is to generate packages (Docker images) based on the name of the new tag. I'm trying to read the new tag name in the CD workflow using the action dawidd6/action-get-tag@v1:

- name: Get tag
  id: tag
  uses: dawidd6/action-get-tag@v1
- name: Use tag
  run: echo ${{steps.tag.outputs.tag}}

But I'm getting the following error:

Run dawidd6/action-get-tag@v1
env:
  IMAGE_NAME: open-tuna-api
Error: Not a tag ref (refs/heads/master)

My question is: how to read the tag name in my CD workflow that is triggering after the CI workflow?

1 Answers

First of all, you can get the tag without using an action, with ${GITHUB_REF##*/}.

Sample test workflow:

name: Experiment

on:
  push:

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - name: Debug
      run: echo "Works. Tag is ${GITHUB_REF##*/}"

As for the chained workflows you mention - I am not sure it is possible to get the tag of the ancestor workflow, since the documentation mentions it is triggered on the default branch, and the last commit on that branch.

Related