Ruan Bekker's Blog

From a Curious mind to Posts on Github

Setup a Basic CI Pipeline on Gitlab

In this tutorial we will setup a Basic CI (Continuous Integration) Pipeline on Gitlab.

The code for this example is available on gitlab.com/rbekker87/demo-ci-basic-pipeline.

If you would like to read more on Continuous Integration / Continuous Deliver (CI/CD).

What will we be doing?

The aim for this is every time there is a commit made to the master branch, that the jobs defined by the .gitlab-ci.yml will be executed and will only pass if exit code 0 has been returned on the scripts.

The jobs gets executed on gitlab runners which is hosted with Giltab. Important to note is that every job runs independently from each other.

Our Basic Pipeline

In this pipeline we will have 2 basic jobs, each job execute a set of scripts:

Build:

1
2
3
4
5
$ echo "this is building" 
$ hostname
$ mkdir builds
$ touch builds/data.txt
$ echo "true" > builds/data.txt

Test:

1
2
3
4
$ echo "this is testing"
$ hostname
$ test -f builds/data.txt
$ grep "true" builds/data.txt

Setup the Pipeline:

From a newly created repository which i’ve cloned to my workspace, create the config:

1
$ touch .gitlab-ci.yml

The config for above yaml file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
stages:
  - build
  - test

build:
  stage: build
  script:
    - echo "this is building"
    - hostname
    - mkdir builds
    - touch builds/data.txt
    - echo "false" > builds/data.txt
  artifacts:
    paths:
      - builds/

test:
  stage: test
  script:
    - echo "this is testing"
    - hostname
    - test -f builds/data.txt
    - grep "true" builds/data.txt

Config Explained

  • We define 2 stages for this pipeline: build and test
  • We provide context of each job, the stage, the script (commands that will be executed in the lifecycle of the runner) and artifacts (artifacts will be the content that will be transferred, as each job runs in a different runner/container)

Note that I deliberately made a mistake so that my pipeline can fail. I populated the content “false” into the builds/data.txt file from the build job and grep for the word “true” on the test job, so this job will fail.

Push to Github

Save the content to the config file, add, commit and push to master:

1
2
3
$ git add .gitlab-ci.yml
$ git commit -m "add gitlab-ci config"
$ git push origin master

Gitlab Pipelines

From the Gitlab UI, if you head over to CI/CD -> Pipelines, you should see your pipeline running:

When you select the Pipeline ID, you should be presented with the jobs available in your pipeline:

Select Jobs, and you should see an overview of your jobs. At this moment we can see that the build job has completed, and that the test job is busy running:

Shortly thereafter the status of the test job should change to failed, select the Job ID and you should see the output:

From the above output it gives you a link to create a new issue, which is quite handy.

Fix the Pipeline Config

Let’s go ahead and change the content in the .gitlab-ci.yml config and push to master:

1
$ vim .gitlab-ci.yml

Change line 12 from - echo "false" > builds/data.txt to - echo "true" > builds/data.txt, the full content of the file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
stages:
  - build
  - test

build:
  stage: build
  script:
    - echo "this is building"
    - hostname
    - mkdir builds
    - touch builds/data.txt
    - echo "true" > builds/data.txt
  artifacts:
    paths:
      - builds/

test:
  stage: test
  script:
    - echo "this is testing"
    - hostname
    - test -f builds/data.txt
    - grep "true" builds/data.txt

Commit and push to master:

1
2
3
$ git add .gitlab-ci.yml
$ git commit -m "change content in script"
$ git push origin master

When you head over to Pipelines, you will see that the pipeline is busy running, and on the right the commit that we just made:

Great Success

Select the Pipeline ID, then select Jobs, you should see both jobs succeeded:

Select the Job ID of the test job, and from the output you will see that the job succeeded:

From this output you can also confirm from both jobs, that each job ran in a different runner as the hostnames that was returned to stdout was different.

Resources

This was a really basic example to demonstrate Gitlab CI. Some relevant resources to this post:

Thank You

Please feel free to show support by, sharing this post, making a donation, subscribing or reach out to me if you want me to demo and write up on any specific tech topic.


Ad space:

Thanks for reading!

Comments