title: GitHub workflows: How do I reference the image I have just built and pushed in the previous job?
body: <p>I'm building a new GitHub workflow (my first), which currently consists of 2 simple jobs:</p>
  <ol>
  <li>Build a Docker image for the Django application.</li>
  <li>Run our Django tests inside the Docker image that was just build.</li>
  </ol>
  <p>But I've run into a problem referencing the image from the first job correctly in the second job.</p>
  <p>My <code>workflow.yml</code> file looks like this:</p>
  <pre><code># Inspired by:
  # &lt;https://docs.github.com/en/actions/publishing-packages/publishing-docker-images&gt;
  # and the &quot;Django test&quot; workflow template from GitHub.
  
  name: Build and test
  
  on:
    push:
      branches:
        - master
        - development
        - &quot;feature/**&quot;
  
  env:
    REGISTRY: ghcr.io
    IMAGE_NAME: ${{ github.repository }}
  
  jobs:
    build-and-push-docker-image:
      runs-on: ubuntu-latest
      permissions:
        contents: read
        packages: write
  
      steps:
        - name: Checkout repository
          uses: actions/checkout@v2
  
        - name: Log in to the Container registry
          uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
          with:
            registry: ${{ env.REGISTRY }}
            username: ${{ github.actor }}
            password: ${{ secrets.GITHUB_TOKEN }}
  
        - name: Extract metadata for Docker image
          id: meta
          uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
          with:
            images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
  
        - name: Build and push Docker image
          uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
          with:
            context: .
            push: true
            tags: ${{ steps.meta.outputs.tags }}
            labels: ${{ steps.meta.outputs.labels }}
          env:
            IMAGE_TAG: ${{ steps.meta.outputs.tags }}
  
    run-django-tests:
      needs: build-and-push-docker-image
      runs-on: ubuntu-latest
      container:
        image: ${{ FULL_IMAGE_NAME_HERE }}
      steps:
        - uses: actions/checkout@v2
        - name: Run tests
          run: python ./manage.py test
  </code></pre>
  <p>And it's the <code>${{ FULL_IMAGE_NAME_HERE }}</code> part that I'm currently stuck at.</p>
  <p>I've tried using the same <code>steps.meta.outputs.*</code> variables as used in the first job, but (obviously) they are not available in the second job.</p>
  <p>I also tried defining a new <code>env</code> variable in the first job (I don't know if this is even possible), containing the image tag, to be used in later jobs (over time more jobs will be added that will use the same image). But I can't access the <code>env</code> context in the <code>container</code> section of my second job definition.</p>
  <p>It seems <a href="https://github.com/github/docs/commit/6143d55e0516c31abdc79bf8a911a3d739c5a27f" rel="nofollow noreferrer">the <code>env</code> variable has recently been added</a> to <a href="https://docs.github.com/en/actions/learn-github-actions/contexts#context-availability" rel="nofollow noreferrer">the <code>container</code> section context</a>. But I still get an error when my <code>workflow.yml</code> is parsed:</p>
  <blockquote>
  <p>The workflow is not valid. .github/workflows/workflow.yml (Line: 58, Col: 14): Unrecognized named-value: 'env'.</p>
  </blockquote>
  <p>This above error references <code>jobs.run-django-tests.container.image</code> and is thrown when it has a value of, say, <code>${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}</code>.</p>
  <p>The overall idea is to have a workflow (in the general meaning of that word, not the GitHub workflow sense) where we push feature branches, have the Docker image built for the branch, run tests and such on that branch specific image and if everything passes, the branch can be merged.</p>
  <p>Feature branches follow a naming convention of <code>feature/&lt;my-feature&gt;</code>. But as we have a slash in there, the resulting tags and labels used for images are converted to hyphenated variants, <code>feature-&lt;my-feature&gt;</code>, resulting in images being pushed to the registry as <code>&lt;image-name&gt;/feature-&lt;my-feature&gt;</code>. I'm cool with that, but I need a way to be able to reference that exact image, including the tag. And I can't just use the git branch name, as the slashes are converted to hyphens in the image naming.</p>
  <p>Yes, the easy solution would be to start using hyphens instead of slashes in our branch names, but I'm sure there's a better way around this :)</p>
  <p>Another option would be to use the git commit hash instead of branch name. I could live with that, but I still need to reference the <code>REGISTRY</code> and <code>IMAGE_NAME</code> variables from the <code>env</code> context, which raises the exception if I try.</p>
  <p>I also realise that I can use <a href="https://docs.github.com/en/actions/using-workflows/storing-workflow-data-as-artifacts" rel="nofollow noreferrer">artifacts</a> to store and pass on the Docker image. This would definitely work for me (and might even be the solution). But I kinda like the idea of using the GitHub Container Registry for this, making it easier to also run branch specific images externally (e.g. on a test or staging server).</p>