Task 4: First CI/CD pipeline

Table of Contents

Create the pipeline file

Alright! This is the moment we have been waiting for. Let’s build the first pipeline.

Go to Repository > Files , Click on the + symbol and New File

A new window will open with an editor view. The new name of the file is: .gitlab-ci.yml 

Please copy and paste the filename. Yes, it is starting with a . (dot)

In the editor window, copy and paste the following content and submit the changes:

Content of the pipeline file
				
					workflow:
  rules:
    - if: $CI_COMMIT_BRANCH != "main" && $CI_PIPELINE_SOURCE != "merge_request_event"      
      when: never
    - when: always

stages:
  - test_docker
  - test_shell

run_test_docker:
  stage: test_docker
  tags:
    - docker
  script:
    - echo $HOSTNAME
    - echo "I am a test output on a docker environment"

run_test_shell:
  stage: test_shell
  tags:
    - shell
  script:
    - echo $HOSTNAME
    - echo "I am a test output on a shell environment"
    - ansible --version

				
			
Explanation

What exactly are we doing here?! 

  • First of all we are defining rules when the pipeline should be executed
    • In this case when we are in the main branch and a merge request is created 
  • We define stages for your pipeline
  • The first task is “run_test_docker” and this will be part of the “test_docker” stage
    • The tag defines on which gitlab-runner it will be executed 
    • The script section defines which commands will be executed on the shell inside of the docker
  • The next task run in the “test_shell” stage and runs on the shell gitlab-runner

 

Check the pipeline execution

Whenever you are submitting changes in the entire repository, the pipeline will be triggered! Let’s check the status of the pipeline execution. Click on CI/CD and Pipelines then click on the (hopefully😉) passed pipeline execution.

Click on each job to see what exactly has been executed in the each job:

Docker execution output
Shell execution output

Go back to CI/CD and click on Editor. Try to experiment and get familiar with the syntax of the pipeline code. 

Try to do the following steps:

  • Run “ansible –version” in the “test_docker” stage
  • Run wrong commands
  • Try to add more tasks per stage
  • Test  before_script and after script in the tasks 
References

As a command reference you can go to the following GitLab documentation: