githubEdit

CI/CD example for GitLab

Before discussing the GitLab CI/CD setup, let's assume you already have a working DNSControl setup. Aren't you there yet? Then first check out the 'Getting Started' section.

DNSControl - Demo setup

For this tutorial, there is a GitLab repositoryarrow-up-right ready with an example DNSControl setup/domain.

This is based on:

For convenience, both configuration files are shown below.

dnsconfig.js
var PROVIDER_NONE = NewRegistrar("none");
var PROVIDER_TRANSIP = NewDnsProvider("transip");

D("cafferata.dev",
    PROVIDER_NONE,
    DnsProvider(PROVIDER_TRANSIP),
    DefaultTTL("1d"),
    TXT("spf", [
        "v=spf1",
        "-all"
    ].join(" ")),
);

GitLab CI - Preparation

You may have noticed that the creds.json file contains a variable $TRANSIP_PRIVATE_KEY. This variable is populated from the GitLab CI variables and contain the TransIP API key.

Example of variable $TRANSIP_PRIVATE_KEY contents.

GitLab CI/CD variables
Insert GitLab CI/CD variable TRANSIP_PRIVATE_KEY

GitLab CI - DNSControl preview

Now it's time to apply the power of DNSControl within GitLab CI merge requests. We'll start by adding the basic GitLab CI setup. You can view the git diff online in the GitLab merge request #1arrow-up-right. The GitLab CI setup has also been added for convenience.

.gitlab-ci.yml

What does this YAML configuration mean?

  • The dnscontrol preview is run within the GitLab CI predefined stagearrow-up-right test using the Docker image stackexchange/dnscontrolarrow-up-right.

    • A conscious decision has been made to always use the latest version so that no maintenance is required. Of course you can choose to include a Docker image version. You do this by choosing from the available versionsarrow-up-right, and including it in image: for example: name: 'stackexchange/dnscontrol:v3.20.0'

  • Because the choice was made not to adopt a version, it's nice to know from the GitLab CI jobs which version DNSControl is running. We check and validate the DNSControl set-up dnsconfig.js.

  • Then we ask TransIP which DNS diff there is.

  • (!) This only happens in the context of a GitLab merge request and (very important) only when there is a change in the DNSControl configuration (dnsconfig.js).

Because the above GitLab CI configuration expects a diff, we apply this by (for example) adding the Google Workspace SPF include.

dnsconfig.js

From that moment everything comes together! Within the GitLab merge request #1arrow-up-right, a GitLab pipelinearrow-up-right with a GitLab jobarrow-up-right starts running containing the command dnscontrol preview. The outcome of this job? The desired change that will be made within TransIP. Wow this is cool!

CI/CD job output for DNSControl preview

GitLab CI - DNSControl push

We just saw that we can view the DNSControl diff from the GitLab jobarrow-up-right. Now it's time to make GitLab CI responsible for the command dnscontrol push.

From here several choices can be made. You can choose to have the dnscontrol push run as soon as a merge request is pushed to default branch (e.g. main), or from a GitLab pipeline trigger within the GitLab web interfacearrow-up-right. We have opted for the GitLab pipeline web interfacearrow-up-right so that it cannot happen that DNS changes are made from previous merge requests in default branch (e.g. main).

It will probably not surprise you that the basis of this GitLab YAML configuration corresponds for 90% with the DNSControl preview. See the GitLab merge request #2arrow-up-right here.

.gitlab-ci.yml

What does this (new) YAML configuration mean?

Start new CI/CD pipeline from the GitLab web interface

When we start the new GitLab pipelinearrow-up-right from the GitLab web interfacearrow-up-right, we see the GitLab job dnscontrol-pusharrow-up-right which makes the changes within the DNS provider TransIP.

CI/CD job output for DNSControl push

GitLab CI - Duplicate YAML configuration

We have a working setup at this point that includes a dnscontrol preview and a dnscontrol push command. Well done! You might consider cleaning up the duplicate GitLab YAML configuration. We can move the DNSControl image name and entrypoint to a GitLab YAML extends. Then we can also move the duplicate dnscontrol version command to a GitLab before_script. See the third (and also last) GitLab merge request #3arrow-up-right.

This eventually brings us to the following GitLab CI setup.

.gitlab-ci.yml

If you are unexpectedly unable to set up this setup, feel free to ask questionsarrow-up-right about it.

Last updated