Gitlab-CI Basics

Introduction

In this article we will continue where we left off the forward project last time, in this article we will use gitlab-ci to test, build and push the image of our operator to dockerhub.

Gitlab offers a pretty complete solution, but we will only sync our repo from github and set a basic pipeline to test, build and push our docker image to the registry, note that I do not have any kind of affiliation with gitlab, but I like their platform. Also this article demonstrates that you can use github and gitlab in a straight forward manner using the free tier in both sides, we rely in the free shared runners to make our custom CI system.

If you want to check the previous article go here, that way you will know what the project is all about.

Prerequisites
Create the project

Once you have your accounts configured, let’s create a project, the page should look something like this

We want to create a repo or sync a repo in this case, so we select Create a project and continue

Project type

In this step we have a few options and since we have our code in Github and we want to work there, we only want to sync it, so we need to choose CI/CD for external repo

Note that if the repo is public you can fetch/clone using the repo URL, but since I want to check also private repos I went for the github token alternative. Once you hit github it will ask you for the token then it will show you the full list of repos in your account

Github Token

I picked to use a personal token to fetch the repos to be able to grab private repos, etc, so you will need to go to your github account, Settings->Developer settings and then create a new token or click here

Now you only need to give it access to repo, and hit save or create new personal token

Make sure you don’t expose or publish that token in any way, otherwise someone could gain access to your account

(Back to gitlab) Select the repository to sync

Here we need to select the repo that we want to sync and hit connect, it will automatically fetch everything periodically from github.

Dockerhub token

Now we will need to create a token for dockerhub so we can push our image from the build runner, go to your dockerhub account and create a token

Basically you have to go to Account settings->Security->New Access Token or click here.

Then we need to save that token as DOCKERHUB_TOKEN in this case as an environment variable in the gitlab project, Settings->CI/CD->Variables

make sure masked is marked but not protected, protected is only used when you want to use that secret in specific branches

Gitlab-CI config

After that we only need to add the code to the repo and that will trigger a build, the file needs to be called .gitlab-ci.yml basically we just install everything we need run the tests if everything goes well, then the build and push process. There is a lot of room for improvement in that initial config, but for now we only care in having some sort of CI system

Then we will see something like this in the CI/CD->Pipelines tab, after each commit it will trigger a test, build and push

Checking the results

And we can validate that the images are in dockerhub

Some useful links:

Closing notes

I hope you enjoyed it and hope to see you on twitter or github!

Errata

If you spot any error or have any suggestion, please send me a message so it gets fixed.

Also, you can check the source code and changes in the generated code and the sources here