4

I would like to run my CI on a Docker image. How should I write my .github/workflow/main.yml?

name: CI
on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  build:
    name: build
    runs:
      using: 'docker'
      image: '.devcontainer/Dockerfile'
    steps:
      - uses: actions/checkout@v2
      - name: Build
        run: make

I get the error:

The workflow is not valid. .github/workflows/main.yml 
     (Line: 11, Col: 5): Unexpected value 'runs' 

I managed to make it work but with an ugly workaround:

  build:
    name: Build Project
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v1
      - name: Build docker images
        run: > 
           docker build . -t foobar 
           -f .devcontainer/Dockerfile
      - name: Build exam
        run: >
           docker run -v 
           $GITHUB_WORKSPACE:/srv 
           -w/srv foobar make

Side question: where can I find the documentation about this? All I found is how to write actions.

3

2 Answers 2

5

If you want to use a container to run your actions, you can use something like this:

jobs:
  build:
      runs-on: ubuntu-latest
      container:
        image: docker://{host}/{image}:{tag}
      steps:
        ...

Here is an example.

If you want more details about the jobs.<job_id>.container and its sub-fields, you can check the official documentation.

Note that you can also use docker images at the step level: Example.

2

I am reposting my answer to another question, in order to be sure to find it while Googling it.


The best solution is to build, publish and re-use a Docker image based on your Dockerfile.

I would advise to create a custom build-and-publish-docker.yml action following the Github documentation: Publishing Docker images.

Assuming your repository is public, you should be able to automatically upload your image to ghcr.io without any required configuration. As an alternative, it's also possible to publish the image to Docker Hub.

Once your image is built and published (based on the on event of the action previously created, which can be triggered manually also), you just need to update your main.yml action so it uses the custom Docker image. Again, here is a pretty good documentation page about the container option: Running jobs in a container.

As an example, I'm sharing what I used in a personal repository:

  • Dockerfile: the Docker image to be built on CI
  • docker.yml: the action to build the Docker image
  • lint.yml: the action using the built Docker image

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.