Categories
Automation CI/CD DevOps

Go Docker CI in GitLab

Today, I’ll demo a sample Go Docker CI pipeline in GitLab. The pipeline will run on a sample Go containerized app. Hence the name Go Docker. If you later find this article useful take a look at the disclaimer for information on how to thank me.

Go sample GitLab project

I’ll use the sample docker-gs-ping repository below for showcasing Go CI in GitLab. It’s a simple Go web app used in Docker’s “Getting Started with Docker and Go” tutorial.

I forked it, added Makefile and gitlab-ci.yaml.

To see the Go web app in action, clone it from my GitHub and follow below sequence of commands inside the app folder:

make mod # downloads go modules from go.mod
make build # builds go binary
make vet # optional 
make test # optional 

Now, use below docker related commands:

make docker_build
make docker_run

To access the web api run curl localhost:8081. You should be greeted by Hello, Docker!

To stop the container run make docker_clean.

Go Docker GitLab CI

While you can include and use Go GitLab templates, in your GitLab pipelines, I’ll develop my own GitLab CI. It will be similar to GitLab templates with addition of CI for Go containerized apps.

Inspect .gitlab-ci.yml in the project.

stages:
  - vet
  - test
  - build
  - docker-build

default:
  image: warrior7089/go-docker:latest

services:  
  - name: docker:20.10.21-dind 
    command: ["--mtu=1300"]    


vet:
  stage: vet
  script:
    - make vet
  
test:
  stage: test
  script:
    - make test

build:
  stage: build
  script:
    - make mod
    - make build

docker-build:
  stage: docker-build  
  image: docker:20.10.21
  variables:
    DOCKER_HOST: "tcp://docker:2376"
    DOCKER_TLS_CERTDIR: "/certs"
    DOCKER_TLS_VERIFY: "1"
    DOCKER_CERT_PATH: "$DOCKER_TLS_CERTDIR/client"
  script:
    make docker_build

It has jobs for vetting, testing, building go binary and building docker image with the binary bundled inside. Each job uses the relevant make command we’ve seen above.

Let’s see the CI release pipelines in action.

Go Docker CI in GitLab Demo

You can follow along by forking the demo repository to your GitLab.com account. Let’s do a sample change, commit and push it in the feature branch. Afterwards, let’s open the merge request and see the CI pipeline jobs in action.

As we can see, all the jobs mentioned above ran successfully. Note that each job except docker-build ran inside go-docker:latest image I pushed to Docker Hub.

Summary

That’s it about Go Docker CI in GitLab. As always, feel free to share.

If you found this article useful, take a look at the disclaimer for information on how to thank me.

You may also find below articles interesting:

Recommended Gitlab books on Amazon.