1

I want to use some variables to set values in GitHub actions steps.

Environment variables tells us that we can set custom environment variables in your workflow file.

By default, Linux runners use the bash shell, so you must use the syntax $NAME.

So I try

name: Workflow

on:
  push:
    branches:
    - main
  pull_request:

env:
  General_Timeout_Minutes: 15

jobs:
  test:
    name: Test
    runs-on: ubuntu-latest
    env:
      Setup_Timeout_Minutes: 10
      Lint_Timeout_Minutes: $General_Timeout_Minutes
      Unit_Test_Timeout_Minutes: $General_Timeout_Minutes
      E2E_Timeout_Minutes: $General_Timeout_Minutes
    steps:
      - uses: actions/checkout@v2

      - name: Setup
        timeout-minutes: $Setup_Timeout_Minutes
        run: npm install

      - name: Lint JavaScript
        timeout-minutes: $Lint_Timeout_Minutes
        run: npm run lint

      - name: Run JavaScript tests
        timeout-minutes: $Unit_Test_Timeout_Minutes
        run: npm run unit-test

      - name: Run end-to-end browser tests
        timeout-minutes: $E2E_Timeout_Minutes
        run: npm run e2e-test

  deploy:
    ...

However, GitHub actions starts fail with

The workflow is not valid. .github/workflows/test.yml: Unexpected value '$Setup_Timeout_Minutes' .github/workflows/test.yml: Unexpected value '$Lint_Timeout_Minutes'

How to use variables in timeout-minutes syntax?

1 Answer 1

2

After asking in GitHub community and doing some tests, I get the final answer.

Here is the discussion link.

name: Workflow

on:
  push:
    branches:
    - main
  pull_request:

env:
  General_Timeout_Minutes: 15

jobs:
  test:
    name: Test
    runs-on: ubuntu-latest
    env:
      Setup_Timeout_Minutes: 10
    steps:
      - uses: actions/checkout@v2

      # Set job level environment variables from global ones
      - name: "Set job env from global env"
        run: |
          echo "Lint_Timeout_Minutes=$General_Timeout_Minutes" >> $GITHUB_ENV
          echo "Unit_Test_Timeout_Minutes=$General_Timeout_Minutes" >> $GITHUB_ENV
          echo "E2E_Timeout_Minutes=$General_Timeout_Minutes" >> $GITHUB_ENV

      - name: Setup
        timeout-minutes: ${{ fromJSON(env.Setup_Timeout_Minutes) }}
        run: npm install

      - name: Lint JavaScript
        timeout-minutes: ${{ fromJSON(env.Lint_Timeout_Minutes) }}
        run: npm run lint

      - name: Run JavaScript tests
        timeout-minutes: ${{ fromJSON(env.Unit_Test_Timeout_Minutes) }}
        run: npm run unit-test

      - name: Run end-to-end browser tests
        timeout-minutes: ${{ fromJSON(env.E2E_Timeout_Minutes) }}
        run: npm run e2e-test

  deploy:
    ...
     
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome sharing your findings with us, thanks! The key here is the "fromJSON" that is responsible to convert the number as string in a real number expected by the timeout-minutes attribute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.