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
5 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:
      - ...
1 Like
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?

1 Like

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).

3 Likes

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.

Same thing here. I can tag an image using the sha pretty easily but impossible to retries it. Looks like no interpolation is done on the steps.uses field.

Any news on this? We’re also looking for a solution to this problem.

This is not and will not be supported for security reasons:

Same problem here. It’s kinda ridiculous that this is not enabled yet. There’s also nothing in the docs

I had the same need and added a request in the new feedback forum here.

Another use case is building a docker image on the fly to be consumed by subsequent jobs as an alternative to the (really broken) pipeline caching.

env:
  IMAGE_PATH: "my-image:${{ github.sha }}"

jobs:
  build-docker-image:
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v2
      - name: Login to Docker Hub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - run: |
          docker build \
            -t ${{ env.IMAGE_PATH }} \
            docker/
          docker push ${{ env.IMAGE_PATH }}


  consume-image:
    needs:
      - build-docker-image

    container: 
      image: ${{ env.IMAGE_PATH }}
    runs-on: ubuntu-20.04

    steps:
      - uses: actions/checkout@v2

In fact, my example above does actually work. For me, the key was using

env:
  NODE: node:latest

jobs:
  test:
    runs-on: ubuntu-latest
    container: 
      # "image" field seems to be able to accept variables
      image: ${{ env.NODE }}
    steps:
      - ...

instead of

env:
  NODE: node:latest

jobs:
  test:
    runs-on: ubuntu-latest
    # this "inline" form of container path won't accept a variable
    container: ${{ env.NODE }}
    steps:
      - ...

an alternative is to manage the docker container run yourself with something like this:

env:
  NODE: node:latest

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - run: |
          docker run -it --rm=true -v $(realpath .):/media -w /media ${{ env.NODE }} npm run <something>