Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use env variable on workflow level github-actions

I have a simple workflow:

name: Test CI

env:
  path_to_watch_for_commits: 'testdir/testfile'

on:
  push:
    branches:
      - master
    paths:
      - ${{ env.path_to_watch_for_commits }}

  workflow_dispatch:

jobs:
  build:
    runs-on:  ubuntu

    steps:
      - uses: actions/checkout@v2

I want to use variable path_to_watch_for_commits in paths. But this syntax is wrong. Also i try ${{ path_to_watch_for_commits }} and $path_to_watch_for_commits with no results. What am i doing wrong?

like image 361
sharedfeel Avatar asked Apr 07 '26 08:04

sharedfeel


1 Answers

There is a Naming conventions for environment variables explaining that:

Any new environment variables you set that point to a location on the filesystem should have a _PATH suffix. The HOME and GITHUB_WORKSPACE default variables are exceptions to this convention because the words "home" and "workspace" already imply a location.

Regarding the use of environment variables at different levels inside the workflow, here is an example with WORKFLOW, JOB and STEP variables:

name: Example

on: [push, workflow_dispatch]

env:
  WORKFLOW_VARIABLE: WORKFLOW

jobs:
  test:
    runs-on: ubuntu-latest
    env:
      JOB_VARIABLE: JOB
    steps:
      - name: Run Commands with various variables
        if: ${{ env.WORKFLOW_VARIABLE == 'WORKFLOW' }}
        env:
          STEP_VARIABLE: STEP
        run: |
          echo "Hello World"
          echo "This is the $WORKFLOW_VARIABLE environment variable"
          echo "This is the $JOB_VARIABLE environment variable"
          echo "This is the $STEP_VARIABLE environment variable"

Regarding the possibility of the behaviour you want to achieve in your workflow, you can't set variables at the workflow and use them as paths on the same level, because:

Variables are substituted in the runner operating system after the job is sent to the runner.

Reference

A workaround could be to trigger the workflow every time on push event, using the paths-filter action with an if conditional to execute specific steps if it match your paths.

It's not the best solution regarding optimisation, but it will work.

like image 125
GuiFalourd Avatar answered Apr 08 '26 22:04

GuiFalourd