Builing Docker images with Github Actions

2019-09-02

Building Docker images in a CI pipeline is a common task. And Actions makes it convenient to do.

In this article I show a way to build Docker images on Github Actions.

Using an action

First I tried finding an action that does what I planned to do.

There is a Github maintained action under actions/docker, but it looks to be using the old HCL syntax, not the new yaml one.

If there is no suitable action to find, defaulting to a run step always does the job.

Defaulting to a run step

A run step can run all the commands that you run on your laptop normally. If the necessary tools are installed of course.

Luckilly Actions have docker installed by default.

See it by running:

name: Docker build
on: [push]

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:

    - name: Debug
      run: |
        docker version

Building and pushing images to Docker Hub

name: Docker build
on: [push]

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:

    - name: Checkout
      uses: actions/[email protected]
      with:
        fetch-depth: 1

    - name: Docker build
      run: |
        docker login -u "$DOCKER_USERNAME" -p "$DOCKER_PASSWORD"
        docker build -t laszlocloud/actions-test .
        docker push laszlocloud/actions-test
      env:
        DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
        DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}

The docker commands don’t need much explanation, the only trick is in using the secrets.

Secrets needs to be first created in the repository settings, then included in a workflow step as an environment variable. See the documentation here

Onwards!

I use Drone.io extensively, perhaps you like what I gathered over the years:

I'm enthusiastic about Github Actions. See my first experiences:

I can also notify you if there is a new Drone related article. Won't spam you, I promise.