How do I know If workflow is triggered by git branch or git tag?

My workflow run is defined as follows:

on:
  pull_request:
  push:
    branches: ['**']
    tags: ['**']

  # Runs daily
  schedule:
    - cron: '0 0 * * *'

How do I know inside my steps that I am currently on a git tag or git branch? And how do I get the name of either of it?

Goal:

If the workflow is triggered by a git tag, I want to tag my Docker images by the git tag name.
If the workflow is triggered by a git branch, I want to tag my Docker images by the git branch name plus a suffix.

1 Like

You can use Context to access the information about the workflow run which will include the information you want. For information about trigger, you can use github context: github.ref. Usually, if the workflow is triggered by a tag, the value of github.ref would be something like “refs/tags/xxx”, and if the workflow is triggered by a branch, the value would be “refs/heads/xxx”. And then you can choose your next steps base on this information.

1 Like