GitLab CI/CD Tutorial: Concepts, Configuration, and Example Pipelines
This tutorial introduces GitLab CI/CD, explains its core concepts, reasons to choose it, outlines typical pipeline stages and jobs, and provides a complete example .gitlab-ci.yml configuration with code snippets for building, testing, and deploying a Node.js application.
This tutorial aims to introduce GitLab CI/CD for beginners who want to try a CI/CD tool. It briefly explains what CI/CD is, why GitLab's tool is chosen, and demonstrates a hands‑on example using a .gitlab-ci.yml file.
CI/CD Basics – CI/CD stands for Continuous Integration, Continuous Delivery, and Continuous Deployment. It enables teams to build, test, and release software faster by automating the steps that would otherwise require manual interaction.
Why Choose GitLab CI/CD?
All‑in‑one open‑source tool that provides source control, merge requests, credential management, and CI/CD pipelines in a single application.
Runs on GitLab Runners – dedicated machines that execute pipeline steps, offering faster builds compared with a single‑instance setup.
Open‑source nature allows contributions and issue reporting.
Typical Demand Scenario – Imagine a Node.js API that retrieves a list of books from a database. A pipeline with three stages – build, test, and deploy – can be defined. The three pipeline types are:
Project pipeline
Continuous‑integration pipeline
Deployment pipeline
Each stage consists of jobs; jobs are the basic building blocks of a pipeline. The hierarchy looks like:
A.) Build
i. Install NPM dependencies
ii. Run ES‑Linter
iii. Run Code‑Minifier
B.) Test
i. Run tests
ii. Compile build
C.) Deploy
i. Release
1.) Test environmentTo implement this, create a .gitlab-ci.yml file at the root of your GitLab repository and add the following configuration:
image: node:10.5.0
stages:
- build
- test
- deploy
before_script:
- npm installBuild Stage – Define a job build-min-code that installs dependencies and runs a minifier:
build-min-code:
stage: build
script:
- npm install
- npm run minifierTest Stage – Define a job run-unit-test that executes the project's test script:
run-unit-test:
stage: test
script:
- npm run testDeploy Stage – Define two deployment jobs, one for staging and one for production, each limited to specific branches using the only keyword:
deploy-staging:
stage: deploy
script:
- npm run deploy-stage
only:
- develop
deploy-production:
stage: deploy
script:
- npm run deploy-prod
only:
- masterThe final .gitlab-ci.yml combines all jobs and stages, providing a complete CI/CD pipeline for the Node.js project. When code is pushed to the appropriate branch, the corresponding jobs are triggered automatically, and the GitLab UI displays the status of each stage and job.
Overall, this tutorial offers a high‑level overview of what GitLab CI/CD can achieve, including building Docker images, integrating with third‑party tools, and automating code‑base workflows.
DevOps Cloud Academy
Exploring industry DevOps practices and technical expertise.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.