How to use env with container/image?

Hello,

How can I use an environment variable with container/image?

I’m getting the following error:

Set up job 1s

1. Current runner version: '2.163.1'
2. Prepare workflow directory
3. ##[error]Unexpected value 'NODE_VERSION = node:12.13'
4. ##[error]The template is not valid. Unexpected value 'NODE_VERSION = node:12.13'

When set it up as follows:

name: CI

on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master

env:
  NODE_VERSION: node:12.13

jobs:
  test:
    name: Test
    runs-on: ubuntu-latest
    container:
      image: $NODE_VERSION

    steps:
    - name: Check out from Git
      uses: actions/checkout@v1

Best Wishes,

  • Oleg
2 Likes

https://help.github.com/en/actions/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions#jobsjob_idcontainerenv

Hi @dawidd6,

Thank you for your answer. 

The link you posted referes to enviroment variables inside a container. Not exactly what I’m lokking for.

I want define env variable in workflow (*.yml) file and ruse it’s value for a container version. Similar to this, but instead of strategy matrix I want to use env var.

name: CI

on: push

jobs:
  test:
    strategy:
      matrix:
        node:
        - 'node:12.13'

    runs-on: ubuntu-latest
    container: ${{ matrix.node }}
    steps:
      - ...
name: CI

on: push

env:
  NODE: node:latest

jobs:
  test:
    runs-on: ubuntu-latest
    container: ${{ env.NODE }}
    steps:
      - ...

You can try this.

Unfortunately, it doesn’t work for me.

### ERRORED 18:22:49Z

- Your workflow file was invalid: The pipeline is not valid. .github/workflows/main.yml (Line: 36, Col: 14): Unrecognized named-value: 'env'. Located at position 1 within expression: env.NODE_VERSION,.github/workflows/main.yml (Line: 54, Col: 16): Unrecognized named-value: 'env'. Located at position 1 within expression: env.NODE_VERSION

Does it work for you?

Looks like it worked here: https://github.community/t5/GitHub-Actions/Support-for-use-of-environment-variables-in-parameters/m-p/41657#M4699

Yeah, it works inside jobs.<job_id>.steps (https://help.github.com/en/actions/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions#jobsjob_idsteps), but not when you try to pass it inside jobs.<job_id>.container.image (https://help.github.com/en/actions/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions#jobsjob_idcontainerimage).

1 Like

I have exactly the same requirement.

Building an image in the previous step which I wan to use in my test job…

@olegburov did you find a solution?

Nope, but maybe @ethomson knows how?

Same requirement here +1

Did anyone managed to get this working? In previous step I have built and image and now I need to run run test using that image. I don’t want to just use latest tag as this could cause problems with parallel builds so I need to specify the image name “dynamically”. But something like image: gcr.io/***/image-name:${{ github.sha }} will throw this error: Unrecognized named-value: 'github'. Located at position 1 within expression: github.sha . Seems to be the same problem.

I have the same problem. I push a docker image to docker hub using sha and I want to pull it to run test but I can’t reference the variable in the container/image place.