GitLab CI/CD: Architecting, Deploying, and Optimizing Pipelines
Auto DevOps
Raise a Merge Request and Checkout AutoDevOps
In this lesson, you’ll learn how to create a feature branch, submit a Merge Request (MR) in GitLab, and observe how Auto DevOps adapts its pipeline stages for MR workflows. By the end, you’ll see Review and Cleanup stages in action and understand how to inject CI/CD variables into review environments.
1. Inspect the Initial Auto DevOps Pipeline on main
Once Auto DevOps is enabled, the pipeline for main
runs standard stages—Build, Test, Production, and Performance—though the Build stage may be canceled if it’s already succeeded elsewhere:
2. Create a Feature Branch and Update Your Code
- Open
app.js
in your editor. - Uncomment the
console.log
callback inmongoose.connect
. - Extend the Mongoose schema by adding a
description
field:
const express = require('express');
const app = express();
const cors = require('cors');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const path = require('path');
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, '/')));
app.use(cors());
mongoose.connect(process.env.MONGO_URI, {
user: process.env.MONGO_USERNAME,
pass: process.env.MONGO_PASSWORD,
useNewUrlParser: true,
useUnifiedTopology: true
}, function(err) {
if (err) {
console.log("error!! " + err);
} else {
console.log("MongoDB Connection Successful");
}
});
const Schema = mongoose.Schema;
const dataSchema = new Schema({
name: String,
id: Number,
description: String
});
module.exports = app;
Now commit and push your changes on a new feature branch:
git checkout -b feature/auto-devops
git add app.js
git commit -m "Update app.js: add description and enable console.log"
git push -u origin feature/auto-devops
3. Open a Merge Request in GitLab
- Go to your project in GitLab.
- Click Merge Requests → New Merge Request.
- Select
feature/auto-devops
as the source andmain
as the target. - Fill in the title, description, assign reviewers, and click Create Merge Request.
After creating the MR, the previous pipeline is canceled. Click Retry on canceled jobs to trigger a fresh MR pipeline:
4. Observe the Auto DevOps Pipeline for the MR
A new pipeline runs on your feature branch. In addition to Build, Test, Production (swapped for Review), and Performance, you’ll see Review and Cleanup stages:
Stage | Purpose | Trigger |
---|---|---|
Build | Package the application into a container | commit/MR |
Test | Run code quality, security scans, and unit tests | commit/MR |
Review | Deploy a temporary review app | MR |
Performance | Execute browser and load tests | commit/MR |
Cleanup | Tear down review app | after MR |
5. Build Stage (Auto Build
)
Auto DevOps uses your Dockerfile if present, otherwise it falls back to Cloud Native Buildpacks:
Trimmed logs from the build job:
Running with gitlab-runner 16.6.0 on docker+machine executor
Starting service docker:20.10.12-dind ...
Logging in to GitLab Container Registry...
Login Succeeded
Building Cloud Native Buildpack-based application:
DETECTING heroku/nodejs-engine 2.5, nodejs-npm-install 2.6
BUILDING [email protected], npm ci --production=false
EXPORTING image 'tmp-cnb-image-6136662405'
Pushing image to registry.gitlab.com/demos-group/solar-system-autodevops:feature-auto-devops
Job succeeded
6. Test Stage and Template Jobs
Auto DevOps runs several parallel test jobs:
- Code Quality
- Container Scanning
- Dependency Scanning
- SAST
- Secret Detection
- Semgrep
- Test (your Mocha suite)
All templates pass except Test, which fails due to missing MongoDB environment variables:
6.1. Understanding the Test Failure
> mocha app-test.js --timeout 10000 --reporter mocha-junit-reporter --exit
MongooseError: The uri parameter to `openUri()` must be a string, got "undefined".
Make sure the first parameter to mongoose.connect() or mongoose.createConnection() is a string.
Note
The error indicates that MONGO_URI
(and related credentials) are not defined in CI.
7. Define CI/CD Variables in GitLab
Add your MongoDB credentials (and enable historic secret scanning) under Settings → CI/CD → Variables:
Variable Name | Value |
---|---|
MONGO_URI | mongodb://<host>:27017 |
MONGO_USERNAME | superuser |
MONGO_PASSWORD | superpassword |
SECRET_DETECTION_HISTORIC_SCAN | true |
Warning
Ensure sensitive values like MONGO_PASSWORD
are protected and masked in GitLab to prevent exposure.
8. Rerun Jobs and Verify Test Success
Rather than rerunning the whole pipeline, retry the Secret Detection and Test jobs:
# Secret Detection
[INFO] GitLab secrets analyzer v5.1.19
35 commits scanned. Leaks found: 2
Job succeeded
# Test job
> mocha app-test.js --timeout 10000 --reporter mocha-junit-reporter --exit
MongoDB Connection Successful
✓ should return all items
✓ should add a new item
...
4 passing (150ms)
With tests green, the pipeline proceeds to Review.
9. Review Apps Stage
The Review stage provisions a temporary environment in your Kubernetes cluster via Helm. Auto DevOps:
- Validates the base domain.
- Downloads or uses the embedded Helm chart.
- Switches to the MR namespace.
- Creates registry secrets.
- Deploys the review app.
In Operations → Environments, you’ll see your running review app:
9.1. Handling Pod Crash Loops
If the review pod crashes, inspect it:
kubectl get all -n default
kubectl describe pod review-feature-auto-devops-xxxxx -n default
kubectl logs review-feature-auto-devops-xxxxx -n default
You may see the same Mongoose error, which means the pod lacks the CI/CD environment variables:
MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined".
Environment variables defined in GitLab CI/CD are not automatically injected into Kubernetes pods. To pass them, configure your Helm chart’s values.yaml
or use GitLab’s Review App variables documentation.
Because the pod fails to start, the Review job times out and a Cleanup stage is triggered:
Up next, we’ll explore how to inject CI/CD variables into your Helm chart so that Review Apps can connect to external services like MongoDB.
Links and References
Watch Video
Watch video content