beye.blog logo

Task 4: First CI/CD Pipeline

Task 4: First CI/CD Pipeline

Create the Pipeline File

Navigate to Repository > Files and click the + symbol to create a new file.

GitLab new file interface
GitLab new file interface

Name the file .gitlab-ci.yml (note the leading dot). This filename is essential for GitLab to recognize it as a pipeline configuration.

File naming in GitLab editor
File naming in GitLab editor

Pipeline Configuration

Copy and paste this configuration into your editor:

yaml
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

Configuration Explanation

The pipeline configuration accomplishes several things:

Workflow Rules: Pipeline execution triggers on the main branch and merge request events only.

Pipeline Stages: Two distinct stages organize your jobs sequentially—test_docker runs first, followed by test_shell.

Docker Job: The run_test_docker task executes on Docker runners (identified by the docker tag) and outputs hostname information.

Shell Job: The run_test_shell task runs on shell runners and includes Ansible version verification.

Monitor Pipeline Execution

After committing changes, visit CI/CD > Pipelines to track execution status.

Pipeline execution status view
Pipeline execution status view

Click individual jobs to inspect detailed output logs.

Docker Job Output

Docker execution results
Docker execution results

Shell Job Output

Shell execution results
Shell execution results

Practice Exercises

Experiment with these modifications in the CI/CD Editor:

  • Execute "ansible –version" within the docker stage
  • Introduce intentional command errors to observe failure handling
  • Expand job counts within each stage
  • Incorporate before_script and after_script blocks in your jobs

Reference Documentation

Consult GitLab's official resources for deeper learning: