Github Actions allows using Docker containers to run jobs, but it doesn't seem to allow providing a dynamic value for this container image (using environment variables).
This works (not the desired solution):
jobs:
pytest-test:
container:
image: ghcr.io/ashrafgt/test:latest ...
This does not work (the desired solution):
jobs:
pytest-test:
container:
# env variables defined at the start of the workflow
image: ${{ env.REGISTRY_NAME }}/test:${{ env.IMAGE_TAG }}
...
Giving this error:
Invalid workflow file : .github/workflows/workflow.yaml
The workflow is not valid. Unrecognized named-value: 'env'. Located at position 1 within expression: env.REGISTRY_NAME
Are there any ways to do this besides doing a run: docker run ...?
In this example, I try to build and push a Docker image (tagged with the current commit SHA) then use the same image to run unit tests:
name: Main CI Pipeline
on: [push]
env:
REGISTRY_NAME: ghcr.io/${{ github.repository_owner }}
REGISTRY_USERNAME: ${{ github.actor }}
REGISTRY_PASSWORD: ${{ secrets.GITHUB_TOKEN }}
IMAGE_TAG: ${{ github.sha }}
jobs:
docker-build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v2
- uses: docker/setup-buildx-action@v1
- uses: docker/login-action@v1
with:
registry: ${{ env.REGISTRY_NAME }}
username: ${{ env.REGISTRY_USERNAME }}
password: ${{ env.REGISTRY_PASSWORD }}
- uses: docker/build-push-action@v2
with:
tags: ${{ env.REGISTRY_NAME }}/test:${{ env.IMAGE_TAG }}
push: true
pytest-test:
needs: docker-build
runs-on: ubuntu-latest
permissions:
contents: read
packages: read
container:
image: ${{ env.REGISTRY_NAME }}/test:${{ env.IMAGE_TAG }}
steps:
- uses: actions/checkout@v2
- run: pytest
Please find the full repository here.
The full error message is:
Invalid workflow file : .github/workflows/workflow.yaml#L38
The workflow is not valid. .github/workflows/workflow.yaml (Line: 38, Col: 14): Unrecognized named-value: 'env'. Located at position 1 within expression: env.REGISTRY_NAME
Please find the Github Actions run here.
Using only container instead of container.image:
jobs:
pytest-test:
container: ${{ env.REGISTRY_NAME }}/test:${{ env.IMAGE_TAG }}
...
Using the docker:// syntax for a single step:
jobs:
pytest-test:
steps:
- uses: docker://${{ env.REGISTRY_NAME }}/test:${{ env.IMAGE_TAG }}
entrypoint: pytest
...
Both fix attempts failed with the same error as the original syntax.
env context is not available for container or container.image. Following is a snapshot from the GitHub Actions plugin on VSCode where the linter throws an error.

Alternatively, you can define configuration variables in GitHub at a repo/org level and then use the vars context to access it (example)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With