Use an array in workflow level env variable?

Viewed 1814

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?

2 Answers

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

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

Related