Writing a GitHub Actions Workflow that Uses a Docker Image
When working with Github Actions there are a few different ways to write a workflow yaml that uses a docker image or a docker container.
usesoption. This is combined with a docker URI, here's an example Github Actions Job that uses docker:
jobs: docker-example: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Run box version uses: docker://foundeo/minibox:latest with: entrypoint: /opt/box/box args: version
In this example we tell GitHub Actions that this job uses the docker image
foundeo/minibox:latest. You can also specify an
entrypoint (the command to run inside the container) and args (command line arguments to pass to the command specified in entrypoint). You might be able to omit entrypoint if the container already species the entrypoint you want to use.
Can the Docker Container Access My Github Code?
This was a question I had when I first started playing with GitHub Actions. It turns out, you can access your github repository code because when the workflow job executes
uses: docker://container it also mounts a volume, and passes several environment variables. The GitHub Actions workflow workspace is mounted to the path:
Note that the code checked out of our github repository because we ran
uses: actions/checkout@v2 as a prior step.
Another way to use a docker image
Another way to go run commands inside a docker image as part of your Github Actions workflow is to use the
image options. Here's an example of specifying a docker image in the workflow yaml:
jobs: testbox: runs-on: ubuntu-latest container: image: azul/zulu-openjdk-alpine:8-jre steps: - uses: actions/checkout@v2 - name: What OS is running run: uname -a - name: What java version do we have run: java -version
In this example we are running multiple commands inside the container image. In this case we are running Java / OpenJDK inside an Alpine Linux container.
Hopefully you find these examples useful to show the different ways you can run inside a docker container as part of your GitHub Actions workflow.
Like this? Follow me ↯Tweet Follow @pfreitag
Writing a GitHub Actions Workflow that Uses a Docker Image was first published on May 14, 2020.