Logo

dev-resources.site

for different kinds of informations.

Optimizing GitLab CI for Readability and Maintainability: From 1K to 600 Lines!

Published at
1/26/2024
Categories
devops
gitlab
cicd
pipelines
Author
ridaehamdani
Categories
4 categories in total
devops
open
gitlab
open
cicd
open
pipelines
open
Author
12 person written this
ridaehamdani
open
Optimizing GitLab CI for Readability and Maintainability: From 1K to 600 Lines!

This short article aims to share some tips I learnt from optimizing our GitLab CI file (.gitlab-ci.yaml) for building multiple Docker images.

By implementing these techniques, you’ll not only make your GitLab CI files more readable but also set the stage for a more agile and adaptable CI/CD environment.
Let’s delve into the practical insights that can elevate your DevOps experience and contribute to a more efficient and maintainable codebase.

1- Use default runner tag and override it when needed:

In case you are using a standard or a generic runner for the majority of your jobs and a a different runner for a few jobs, consider defining a global runner tag instead of defining one for each job.
You can then override the global tag when necessary.

❌ Don’t do:

compile:
  script:
    - ...
  tags:
    - myruuner
test:
  script:
    - ...
  tags:
    - testrunner  
deploy:
  script:
    - ...
  tags:
    - myrunner
Enter fullscreen mode Exit fullscreen mode

βœ… Do:

default:
  tags:
    - myrunner

compile:
  script:
    - ...

test:
  script:
    - ...
  tags:
    - testrunner  
deploy:
  script:
    - ...
Enter fullscreen mode Exit fullscreen mode

2- Use parallel:matrix

GitLab has a powerful CI feature to run a matrix of jobs in parallel.
In our case we had multiple jobs to test every customized Docker JDK image and ensure that it does not break a standard maven build.
Taking advantage of the keyword parallel:matrix, can save you multiple lines of code and make your CI files more readable and easier to maintain.

Here is an example of refactoring our test jobs:

❌ Instead of configuring multiple job with the same script part, for example:

test-mvn-jdk-11:
  image: openjdk-11
  stage: test-image
  script:
    - mvn -V compile ...

test-mvn-jdk-17:
  image: openjdk-17
  stage: test-image
  script:
    - mvn -V compile ...

test-mvn-jdk-21:
  image: openjdk-21
  stage: test-image
  script:
    - mvn -V compile ...
....
Enter fullscreen mode Exit fullscreen mode

βœ… You can easily replace them with:

test-mvn:
  image: $JAVA_IMAGE
  stage: test-image
  script:
    - mvn -V compile ...
  parallel:
    matrix:
      - JAVA_IMAGE: [openjdk-11,openjdk-17,openjdk-21]
Enter fullscreen mode Exit fullscreen mode

This will also make it easier to incorporate additional JDK versions in the future, requiring minimal adjustments and eliminating the need for extensive line changes or introducing new jobs.

3- Use rules to define variables:

GitLab rules provide a mechanism for specifying conditions to determine when CI/CD jobs run.
Additionally, the use of if statements within these rules allows for the dynamic definition of variables based on conditions such as branches or releases, offering flexibility in variable assignment.

For our repository's CI/CD workflow, we start by building docker images and test them if there's a Merge Request.
Only when everything checks out, we create a final version TAG for release.
Using rules reduced our build jobs by a half.

❌ Instead of defining two jobs, for example:

build-mr-jdk-11:
  stage: build
  script:
    - docker build -t jdk-11:mr-validation
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"

build-jdk-11:
  image: openjdk-11
  stage: build
  script:
    - docker build -t jdk-11:$CI_COMMIT_TAG
  rules:
    - if: $CI_COMMIT_TAG
Enter fullscreen mode Exit fullscreen mode

βœ… Use the same job and override the variables depending on the rules conditions:

build-jdk-11:
  stage: build
  script:
    - docker build -t jdk-11:$TAG
rules:
    - if: $CI_COMMIT_TAG
      variables:
        TAG: "$CI_COMMIT_TAG"
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
      variables:
        TAG: "mr-validation"
Enter fullscreen mode Exit fullscreen mode

Conclusion: Lean back and let the magic happen πŸ§™β€β™€

In summary, applying these tips reduced our GitLab CI file from 1176 to 667 lines, significantly improving efficiency, manageability, and overall workflow.

⭐⭐⭐ Enjoy your learning!!! ⭐⭐⭐

pipelines Article's
30 articles in total
Favicon
The Art of Iteration: Starting the Cycle
Favicon
The Art of Iteration: Loop in Pipeline Stage
Favicon
Automating Docker Workflows with Jenkins: A Complete Guide
Favicon
TIL how to see the entire commit column on GitLab using JS
Favicon
Getting Started with Apache Kafka: A Backend Engineer's Perspective
Favicon
Building pipelines with IAsyncEnumerable in .NET
Favicon
DevOps Security Integrating Best Practices into Your Pipeline
Favicon
Creating a data pipeline using Dataproc workflow templates and cloud Schedule
Favicon
☸️ Kubernetes: A Convenient Variable Substitution Mechanism for Kustomize
Favicon
Setting Up a CI/CD Pipeline with AWS and Git: A Comprehensive Guide
Favicon
Enabling Pipelines: Easier than ever
Favicon
Optimizing GitLab CI for Readability and Maintainability: From 1K to 600 Lines!
Favicon
Building Robust Data Pipelines: A Comprehensive Guide
Favicon
Azure DevOps Pipelines breaks my "additional arguments" when using Deploy to Azure
Favicon
What is CI/CD Pipeline?-Comparing pipelines!
Favicon
🌟 The Power of Automation: Deploying an ARM Template in Microsoft Azure πŸš€
Favicon
Meet cici-tools, a multi-tool for building GitLab CI/CD pipelines
Favicon
Unlocking the Power of Data: 7 Key Factors to Consider When Building Data Pipelines
Favicon
Optimize Development with Jenkins Pipelines and Continuous Integration
Favicon
Amplify Your Tech Stack with Jenkins Shared Libraries
Favicon
Important Questions related to Data Engineering
Favicon
How To Secure Your CI/CD Pipeline
Favicon
Flexible and dynamic flow control of Azure DevOps YAML Pipelines using variables
Favicon
Go API Project Set-Up
Favicon
Sftp with Az Devops
Favicon
Error: Full scoped PAT is restricted by your organisation
Favicon
Error: No hosted parallelism has been purchased or granted
Favicon
SparrowCI - DSL is dead, long live DSL!
Favicon
Introducing the CircleCI Config SDK
Favicon
Run DB Scripts to Azure PostgreSQL Single Server using Azure CLI Task in pipeline

Featured ones: