
Introduction
Elevate your development workflow by automating code testing and deployment with GitLab CI/CD. In this comprehensive guide, we’ll walk you through creating a GitLab CI/CD configuration file (gitlab-ci.yml). This configuration file executes different jobs based on whether the code is pushed to the main branch or a feature branch.
YAML Configuration File
Dive into a complete example of a GitLab CI/CD configuration file:
stages:
- build
variables:
MY_VARIABLE: "Hello, World!"
before_script:
- echo "Setting up the environment..."
job_main_branch:
stage: build
script:
- echo "Running this job on the main branch"
- echo "Accessing the variable MY_VARIABLE: $MY_VARIABLE"
rules:
- if: '$CI_COMMIT_BRANCH == "main"'
when: always
job_feature_branch:
stage: build
script:
- echo "Running this job on a feature branch"
- echo "Accessing the variable MY_VARIABLE: $MY_VARIABLE"
rules:
- if: '$CI_COMMIT_BRANCH != "main"'
when: always
Explanation:
stages: Defines different stages in the pipeline. In this example, we’ve focused on a single stage named “build.”variables: Establishes global variables accessible by all jobs. Here,MY_VARIABLEcarries the value “Hello, World!”before_script: Incorporates commands executed before each job. Here, we emphasize the importance of setting up the environment.job_main_branch: Specifies a job that runs exclusively on the main branch. Therulessection guarantees the job runs only if the commit branch is “main.”job_feature_branch: Outlines a job that operates on feature branches. Therulessection ensures the job runs if the commit branch is not “main.”script: Contains the commands executed by the job. In this instance, it delivers messages indicating the branch being processed and accesses the global variable.
Using Variables
Leverage variables in GitLab CI/CD to define values reused across jobs. Here, the variable MY_VARIABLE serves as an example. To access this variable within a job, utilize the syntax $VARIABLE_NAME.
Secrets
GitLab CI/CD enables the secure storage of sensitive information using secrets. To access secrets within a job, utilize the syntax $CI_JOB_TOKEN for predefined variables or $CI_JOB_TOKEN for custom variables.
Explore an example of using a secret in your job:
job_using_secret:
stage: build
script:
- echo "Using secret API_KEY: $CI_JOB_TOKEN"
rules:
- if: '$CI_COMMIT_BRANCH == "main"'
when: always
In this example, $CI_JOB_TOKEN is used to access a secret named API_KEY. Ensure the definition of secrets in the GitLab project settings.
You Might Also Like
🛠️ Recommended Tools for Developers & Tech Pros
Save time, boost productivity, and work smarter with these AI-powered tools I personally use and recommend:
1️⃣ CopyOwl.ai – Research & Write Smarter
Write fully referenced reports, essays, or blogs in one click.
✅ 97% satisfaction • ✅ 10+ hrs saved/week • ✅ Academic citations
2️⃣ LoopCV.pro – Build a Job-Winning Resume
Create beautiful, ATS-friendly resumes in seconds — perfect for tech roles.
✅ One-click templates • ✅ PDF/DOCX export • ✅ Interview-boosting design
3️⃣ Speechify – Listen to Any Text
Turn articles, docs, or PDFs into natural-sounding audio — even while coding.
✅ 1,000+ voices • ✅ Works on all platforms • ✅ Used by 50M+ people