How to use env variables in the `with` block of Github Action workflows?
Asked Answered
M

1

2

I want to define a key-value pair for the entire workflow and access it in the with block of multiple jobs.

Right now, I get the error:

The workflow is not valid. .github/workflows/main.yaml (Line: 41, Col: 25): Unrecognized named-value: 'env'. Located at position 1 within expression: env.database_version .github/workflows/main.yaml (Line: 64, Col: 25): Unrecognized named-value: 'env'. Located at position 1 within expression: env.database_version

My snippet is as follows:

name: Main

on:
  push:
    branches:
      - main
      - fix-main-deployment-workflow-post-database_version

env:
  database_version: 3.1.0-git-c919c26

jobs:

  <job_name>:
    name: Deploy to <some zone>
    needs:
      - helm
      - docker
    uses: <another yaml>
    with:
      database_version: ${{ env.database_version }}

I also tried removing the env and directly referencing it:

The workflow is not valid. .github/workflows/main.yaml (Line: 41, Col: 25): Unrecognized named-value: 'database_version'. Located at position 1 within expression: database_version .github/workflows/main.yaml (Line: 64, Col: 25): Unrecognized named-value: 'database_version'. Located at position 1 within expression: database_version
Marleen answered 9/5, 2023 at 16:39 Comment(4)
There is not steps field in the <job_name> implementation you shared. Is it informed on the original workflow?Mcclintock
Yes, the workflow referred in the uses section has the steps in many jobs.Marleen
Does this answer your question? Passing env variable inputs to a reusable workflowSubtraction
For reusable workflows, you can configure a separate job to set env vars as output parameters and then use those in the input parameters. See the duplicate threads for an example.Subtraction
H
0

It is limitation of reusable workflows. Environments specified inside YAML are worthless as they aren't passed down to nested YAMLs.

To achieve what you want you can:

  1. use vars context instead or...
  2. wrap your workflow into another reusable flow with default input
on:
  workflow_call:
    inputs:
      database_version:
        default: '3.1.0-git-c919c26'
        type: string

jobs:
  nested-job:
    name: Deploy to <some zone>
    needs:
      - helm
      - docker
    uses: <another yaml>
    with:
      database_version: ${{ inputs.database_version }}

And then you call your workflow like this:

name: Main

on:
  push:
    branches:
      - main
      - fix-main-deployment-workflow-post-database_version

jobs:
  <job_name>:
    uses: ./.github/workflows/<workflow_above_name>.yml
# optionally you can inherit secrets
    secrets: inherit
# and add some fixed inputs
    with:
      some_input: 'some_fixed_value'
Hamburg answered 17/5, 2023 at 8:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.