Environment variables in github actions
Asked Answered
G

3

20

I want to pass maven image version as as env variable but when i am trying to access that env.MAVEN_VERSION variable getting error

Error- The workflow is not valid. .github/workflows/Merge.yaml (Line: 13 image:) Unrecognized named-value: 'env'. Located at position 1 within expression: env.MAVEN_VERSION

Yaml File ---

on:
  push:
    branches: [ master ]

env:
  MAVEN_VERSION: maven:3.8.6-jdk-11
  
jobs:
  build:
    runs-on: ubuntu-latest
    container:
      image: ${{ env.MAVEN_VERSION }}
    steps:
    - name: Env Variable
      run: echo ${{ env.MAVEN_VERSION }}
Granadilla answered 21/9, 2022 at 7:59 Comment(0)
A
23

While env is not available, outputs from previous jobs are. Consider the following example

on:
  push:
    branches: [ master ]

env:
  MAVEN_VERSION: maven:3.8.6-jdk-11
  
jobs:
  prepare-image:
    runs-on: ubuntu-latest
    steps:
       - run: echo "null"
    outputs:
      image: ${{ env.MAVEN_VERSION }}

  build:
    runs-on: ubuntu-latest
    needs: [prepare-image]
    container:
      image: ${{ needs.prepare-image.outputs.image }}
    steps:
    - name: Echo output
      run: echo ${{ needs.prepare-image.outputs.image }}
Aloes answered 27/10, 2022 at 5:5 Comment(3)
I had to add a placeholder step in the prepare-image job. No steps defined in `steps` and no workflow called in `uses` for the following jobs: prepare-imageBestial
for this example to run you need to add a step for the job prepare-image steps: - run: echo "null"Ehman
Really weird, but worked for me. I cannot get why env is not available there, but this handy hack solved my similar issue.Tantivy
G
2

You have to set your environment variables before jobs:

name: Deploy to Github pages
'on':
  push:
    branches:
      - main

env:
  MAVEN_VERSION: maven:3.8.6-jdk-11
  API_KEY: ${{secrets.API_KEY}}
  ...

jobs:
  deploy:
...

If you want to add secret from Github actions then go to the following route on github and set your secrets,

Github > Your project > Settings > Secrets and variables > actions

Go to the actions

Go to the Actions and click on New repository secret to set an environment variable.

Click on New repository secret

Gyn answered 11/5, 2023 at 16:19 Comment(0)
D
-6

when i am trying to access that...

That's not what the error is telling you about. The error Unrecognized named-value: 'env' is telling you that GitHub is not recognizing the YAML you wrote at line 13. It's a syntax error.

In a GitHub workflow you can use env either in jobs.<job_id>.env or in jobs.<job_id>.steps[*].env. See here for details.

This YAML should work:

on:
  push:
    branches: [ master ]

jobs:
  build:
    runs-on: ubuntu-latest
    container:
      image: ${{ env.MAVEN_VERSION }}
    steps:
      - name: Env Variable
        env:
          MAVEN_VERSION: maven:3.8.6-jdk-11
        run: echo ${{ env.MAVEN_VERSION }}

Also, note that when you only specify a container image, you can omit the image keyword.

Decoration answered 21/9, 2022 at 8:11 Comment(4)
The yaml file u shared is not working still getting same errorGranadilla
when trying to access the env object of worflow level at container: image: ${{ env.MAVEN_VERSION }}Granadilla
ah ok, then move env: and MAVEN_VERSION: below runs-on:. Keep env: at the same level of runs-on:Decoration
still getting same errorGranadilla

© 2022 - 2024 — McMap. All rights reserved.