How do you build a docker image and use it as the container for the next job in GitHub actions?

Viewed 456

On my CI, I want to build a docker image once and then use the same image twice (or multiple times) for any following jobs, such as "test" and "lint".

  ╭─────────────╮
  │ build-image │
  ╰─────────────╯
        ⬇
 ╭──────╮ ╭──────╮
 │ test │ │ lint │
 ╰──────╯ ╰──────╯

It is possible to use a custom container using the container attribute. However, I'm unsure of how to make the image built in the "build-image" step accessible to that property.

What's the best approach to building an image and using it in the following jobs?

1 Answers

I was able to build the image in the first step and tag that image with the commit sha. Then, in the next step, load the container with the sha tag.

build-image:
  runs-on: ubuntu-latest
  steps:
    - uses: actions/checkout@v2
    - uses: docker/build-push-action@v1
      with:
        registry: registry
        repository: user/repo/image-name
        tags: ${{ github.sha }}
test:
  needs: [build-image]
  runs-on: ubuntu-latest
  container:
    image: registry/user/repo/image-name:${{ github.sha }}
Related