githubgithub-actionsworkflowgithub-actions-workflows

GitHub actions: env value in branches names (i.e. on push)


I need to run only on branches what ends with specified env variable.
Let's pretend that we are on feat/project-name branch and this is may workflow.yml:

env:
  PROJECT: project-name

on:
  push:
    # only for branches what ends with given project name
    branches:
      - "**$PROJECT"

Above not work. Below, hard coded is OK:

env:
  PROJECT: project-name

on:
  push:
    # only for branches what ends with given project name
    branches:
      - "**project-name"

Tried with: "**${{ env.PROJECT }}" and other configuration and nothing works.


Solution

  • You can configure env variables at the workflow level, but you can't use them at that level.

    According to the documentation (reference 1 and reference 2):

    Environment variables (at the workflow level) are available to the steps of all jobs in the workflow.

    In your example, the environment variable is used at the workflow level (in the trigger on configuration), not inside a job steps, and the GitHub interpreter doesn't interpolate the value at that level.

    You would need to hardcode the value at that level, or receive it as input (${{ inputs.value }}) from another workflow (or from the GitHub API).