Use an array in workflow level env variable?
Asked Answered
G

3

15

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?

Gerianne answered 28/10, 2021 at 18:59 Comment(0)
A
6

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

Aliped answered 8/8, 2022 at 8:18 Comment(0)
M
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
Merely answered 30/7, 2024 at 16:11 Comment(0)
D
0

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

Doss answered 14/4, 2022 at 13:5 Comment(2)
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"Gerianne
[] is a JSON array, not bash so you'll need fromJson( ${{ env.FRUITS }} )Adkisson

© 2022 - 2025 — McMap. All rights reserved.