17

You can set env vars available across the entire workflow e.g. like in this post.

(From solution on linked post)

name: Git Pull Request Workflow

on:
  workflow_dispatch:
  pull_request:
    branches:
      - master

env:
  one: 1
  two: zwei
  three: tres

jobs:
  first-job:
    runs-on: ubuntu-latest
    steps:
    - run: |
        echo "${{ env.one }}"
        echo "${{ env.two }}"
        echo "${{ env.three }}"

I have a workflow that uses a matrix strategy and I have to update it in each job if I ever change it. I tried to make it a global variable like above:

name: Model Multipliers
on:
  push:
    branches:
      - main
    
env:
  FRUIT: ["Apple", "Pear", "Banana", "Orange"]

jobs:
  ssql-get:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        FRUIT: ${{ env.FRUIT }}
    name: Get data
    steps:
      - name: Checkout cum-rev repo

But this gives error:

The workflow is not valid. .github/workflows/main.yml (Line: 12, Col: 9): A sequence was not expected .github/workflows/main.yml (Line: 19, Col: 15): Unrecognized named-value: 'env'. Located at position 1 within expression: env.FRUIT

Is what I'm trying to do possible by any other means?

3 Answers 3

8

If you're using bash, you can create a regular array (like in bash) to use in your steps or inside github expressions -

Example:

env:
  MAIN_BRANCHES: ("develop" "main")

Now you can use the array in any step. In the run property, as an environment variable with "${MAIN_BRANCHES}", or inside if conditions with Github expression syntax.

...
    - name: Tag build
      if: ${{ github.event_name == 'push' && contains(env.MAIN_BRANCHES, steps.calculate_changed_services.outputs.diff_dest) }}
      run: echo "my main branches ${MAIN_BRANCHES}"
...

You can find the full Workflow File here - gitversion.yml

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

Comments

0

if you remove the double quotes in the values it should work

2 Comments

I tried this and it failed. It failed when referencing the values, e.g. in env I have env: FRUIT: [Apple, Pear, Banana, Orange] then further down when I reference I have matrix: FRUIT: ${{ env.FRUITS }} then when I run the workflow I get error "The workflow is not valid. .github/workflows/main.yml (Line: 9, Col: 10): A sequence was not expected .github/workflows/main.yml (Line: 16, Col: 15): Unrecognized named-value: 'env'. Located at position 1 within expression: env.FRUITS"
[] is a JSON array, not bash so you'll need fromJson( ${{ env.FRUITS }} )
-1

You can also make a json string and then base64 encode it, github actions should have no problems parsing it

console.log(
    Buffer.from(JSON.stringify([
    {
        "array1": ['test1', 'test2']
    }
])).toString('base64')
)
// W3siYXJyYXkxIjpbInRlc3QxIiwidGVzdDIiXX1d

Comments

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.