Environment variables in github actions

Viewed 42

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 }}
1 Answers

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.

Related