> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Raise a Merge Request and Checkout AutoDevOps

> Learn to create a feature branch, submit a Merge Request in GitLab, and observe Auto DevOps pipeline stages for MR workflows.

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:

<Frame>
  ![The image shows a GitLab pipeline interface with stages for build, test, production, and performance, where the build stage is canceled.](https://kodekloud.com/kk-media/image/upload/v1752877111/notes-assets/images/GitLab-CICD-Architecting-Deploying-and-Optimizing-Pipelines-Raise-a-Merge-Request-and-Checkout-AutoDevOps/gitlab-pipeline-build-canceled.jpg)
</Frame>

## 2. Create a Feature Branch and Update Your Code

1. Open `app.js` in your editor.
2. Uncomment the `console.log` callback in `mongoose.connect`.
3. Extend the Mongoose schema by adding a `description` field:

```javascript theme={null}
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:

```bash theme={null}
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

1. Go to your project in GitLab.
2. Click **Merge Requests → New Merge Request**.
3. Select `feature/auto-devops` as the source and `main` as the target.
4. Fill in the title, description, assign reviewers, and click **Create Merge Request**.

<Frame>
  ![The image shows a GitLab interface where a new merge request is being created, with fields for the title and description of the request. The sidebar displays project management options like issues and merge requests.](https://kodekloud.com/kk-media/image/upload/v1752877112/notes-assets/images/GitLab-CICD-Architecting-Deploying-and-Optimizing-Pipelines-Raise-a-Merge-Request-and-Checkout-AutoDevOps/gitlab-merge-request-interface.jpg)
</Frame>

After creating the MR, the previous pipeline is canceled. Click **Retry** on canceled jobs to trigger a fresh MR pipeline:

<Frame>
  ![The image shows a GitLab interface with a merge request titled "Update app.js for testing autodevops." The request is open, but the pipeline was canceled, and there is a merge conflict that needs resolution.](https://kodekloud.com/kk-media/image/upload/v1752877114/notes-assets/images/GitLab-CICD-Architecting-Deploying-and-Optimizing-Pipelines-Raise-a-Merge-Request-and-Checkout-AutoDevOps/gitlab-merge-request-autodevops-conflict.jpg)
</Frame>

## 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  |

<Frame>
  ![The image shows a GitLab pipeline interface with stages for building, testing, reviewing, performance, and cleanup. It includes various jobs like code quality, container scanning, and browser performance.](https://kodekloud.com/kk-media/image/upload/v1752877114/notes-assets/images/GitLab-CICD-Architecting-Deploying-and-Optimizing-Pipelines-Raise-a-Merge-Request-and-Checkout-AutoDevOps/gitlab-pipeline-interface-stages.jpg)
</Frame>

## 5. Build Stage (`Auto Build`)

Auto DevOps uses your Dockerfile if present, otherwise it falls back to Cloud Native Buildpacks:

<Frame>
  ![The image shows a GitLab documentation page about the stages of Auto DevOps, specifically focusing on the "Auto Build" section. It includes navigation menus on the left and right sides.](https://kodekloud.com/kk-media/image/upload/v1752877116/notes-assets/images/GitLab-CICD-Architecting-Deploying-and-Optimizing-Pipelines-Raise-a-Merge-Request-and-Checkout-AutoDevOps/gitlab-autodevops-auto-build-docs.jpg)
</Frame>

<Frame>
  ![The image shows a GitLab documentation page about "Auto Build using Cloud Native Buildpacks," detailing how to build applications using Dockerfiles and Cloud Native Buildpacks. The page includes navigation menus on the left and right sides.](https://kodekloud.com/kk-media/image/upload/v1752877117/notes-assets/images/GitLab-CICD-Architecting-Deploying-and-Optimizing-Pipelines-Raise-a-Merge-Request-and-Checkout-AutoDevOps/gitlab-auto-build-cloud-native-buildpacks.jpg)
</Frame>

Trimmed logs from the build job:

```bash theme={null}
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 Node.js@20.11.0, 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:

<Frame>
  ![The image shows a GitLab pipeline interface with a failed job in the "test" stage, specifically the "test" task, due to a script failure. The pipeline includes stages for build, test, review, performance, and cleanup.](https://kodekloud.com/kk-media/image/upload/v1752877118/notes-assets/images/GitLab-CICD-Architecting-Deploying-and-Optimizing-Pipelines-Raise-a-Merge-Request-and-Checkout-AutoDevOps/gitlab-pipeline-failed-test-job.jpg)
</Frame>

### 6.1. Understanding the Test Failure

```bash theme={null}
> 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.
```

<Callout icon="lightbulb" color="#1CB2FE">
  The error indicates that `MONGO_URI` (and related credentials) are not defined in CI.
</Callout>

## 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`                   |

<Frame>
  ![The image shows a GitLab CI/CD settings page with various environment variables listed, such as KUBE\_INGRESS\_BASE\_DOMAIN and MONGO\_PASSWORD. A notification at the bottom indicates a variable has been successfully added.](https://kodekloud.com/kk-media/image/upload/v1752877119/notes-assets/images/GitLab-CICD-Architecting-Deploying-and-Optimizing-Pipelines-Raise-a-Merge-Request-and-Checkout-AutoDevOps/gitlab-ci-cd-settings-environment-variables.jpg)
</Frame>

<Callout icon="triangle-alert" color="#FF6B6B">
  Ensure sensitive values like `MONGO_PASSWORD` are **protected** and **masked** in GitLab to prevent exposure.
</Callout>

## 8. Rerun Jobs and Verify Test Success

Rather than rerunning the whole pipeline, retry the **Secret Detection** and **Test** jobs:

```bash theme={null}
# 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:

1. Validates the base domain.
2. Downloads or uses the embedded Helm chart.
3. Switches to the MR namespace.
4. Creates registry secrets.
5. Deploys the review app.

In **Operations → Environments**, you’ll see your running review app:

<Frame>
  ![The image shows a GitLab interface displaying the "Environments" section, with a focus on a running job in the "feature/auto-devops" branch. The sidebar includes options like Merge requests, Manage, Plan, Code, and more.](https://kodekloud.com/kk-media/image/upload/v1752877120/notes-assets/images/GitLab-CICD-Architecting-Deploying-and-Optimizing-Pipelines-Raise-a-Merge-Request-and-Checkout-AutoDevOps/gitlab-environments-feature-auto-devops.jpg)
</Frame>

### 9.1. Handling Pod Crash Loops

If the review pod crashes, inspect it:

```bash theme={null}
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:

```bash theme={null}
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](https://docs.gitlab.com/ee/ci/review_apps/).

<Frame>
  ![The image shows a GitLab CI/CD settings page with various configuration variables such as KUBE\_CONTEXT, MONGO\_PASSWORD, and PRODUCTION\_REPLICAS. The sidebar includes options like Deploy, Operate, and Monitor.](https://kodekloud.com/kk-media/image/upload/v1752877121/notes-assets/images/GitLab-CICD-Architecting-Deploying-and-Optimizing-Pipelines-Raise-a-Merge-Request-and-Checkout-AutoDevOps/gitlab-ci-cd-settings-variables.jpg)
</Frame>

Because the pod fails to start, the Review job times out and a Cleanup stage is triggered:

<Frame>
  ![The image shows a GitLab pipeline interface with stages for build, test, review, performance, and cleanup. The review stage has failed, while other stages like build and test have passed.](https://kodekloud.com/kk-media/image/upload/v1752877122/notes-assets/images/GitLab-CICD-Architecting-Deploying-and-Optimizing-Pipelines-Raise-a-Merge-Request-and-Checkout-AutoDevOps/gitlab-pipeline-build-test-review-failed.jpg)
</Frame>

***

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

* [GitLab Auto DevOps Documentation](https://docs.gitlab.com/ee/topics/autodevops/)
* [GitLab CI/CD Variables](https://docs.gitlab.com/ee/ci/variables/)
* [Review Apps Configuration](https://docs.gitlab.com/ee/ci/review_apps/)
* [Helm Official Site](https://helm.sh/)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/gitlab-ci-cd-architecting-deploying-and-optimizing-pipelines/module/a6a5540f-e7d1-4820-afc8-2be1d6e3061a/lesson/2cf2a3f3-0b86-487f-ad06-d7877e2102ca" />
</CardGroup>
