DEV Community

Ruan Bekker
Ruan Bekker

Posted on • Updated on

Setup a Basic CI Pipeline on Gitlab

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

The intention of this post is just to touch on the basics, and in my next post, we will setup a ci/cd pipeline to deploy a python flask api with postgres on heroku.

The code for this example is available on my gitlab.com repo.

Relevant Posts

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:

$ echo "this is building"
$ hostname
$ mkdir builds
$ touch builds/data.txt
$ echo "true" > builds/data.txt
Enter fullscreen mode Exit fullscreen mode

Test:

$ echo "this is testing"
$ hostname
$ test -f builds/data.txt
$ grep "true" builds/data.txt
Enter fullscreen mode Exit fullscreen mode

Setup the Pipeline:

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

$ touch .gitlab-ci.yml
Enter fullscreen mode Exit fullscreen mode

The config for above yaml file:

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
Enter fullscreen mode Exit fullscreen mode

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 Gitlab

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

$ git add .gitlab-ci.yml
$ git commit -m "add gitlab-ci config"
$ git push origin master
Enter fullscreen mode Exit fullscreen mode

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:

$ vim .gitlab-ci.yml
Enter fullscreen mode Exit fullscreen mode

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

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
Enter fullscreen mode Exit fullscreen mode

Commit and push to master:

$ git add .gitlab-ci.yml
$ git commit -m "change content in script"
$ git push origin master
Enter fullscreen mode Exit fullscreen mode

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

Let me know what you think. If you liked my content, feel free to visit me at ruan.dev or follow me on twitter at @ruanbekker

ko-fi

Top comments (5)

Collapse
 
javatpoint profile image
JavaTpoint

Thanks for share good article on Git.

Collapse
 
varunra35569603 profile image
Varun Rana

Thanks for share good article on Git.

Collapse
 
dhandspikerwade profile image
Devin Handspiker-Wade

Push to Github

Shouldn't we be pushing to Gitlab?

Collapse
 
ruanbekker profile image
Ruan Bekker

Ah! Thanks Devin, completely missed that. Fixed.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.