SDPT Lab 6
Week 6 Lab Activity: Continuous Integration (CI) with GitLab
Objective
Today, we automate everything. You will transition from compiling code on your laptop to architecting a professional CI pipeline. You will use your Docker image from last week to cross-compile your C++ code, run your Google Tests in QEMU, and configure GitLab to mechanically prevent broken code from ever reaching the main branch.
We will accomplish this in four phases:
- Repository Setup: Migrating your code to GitLab.
- The Build Stage: Writing your first CI job and generating Artifacts.
- The Test Stage: Consuming Artifacts and failing the pipeline on bad tests.
- Merge Checks: Enforcing quality control via Merge Requests.
Phase 1: Repository Setup
To use GitLab CI, your code must live on a GitLab server.
1. Create a Project:
Log into GitLab and create a new blank project named sdpt-smart-oven. Uncheck "Initialize repository with a README".
2. Push Your Code: Open your terminal on your laptop, navigate to your Week 5 project folder, and push your code to this new repository.
git init
git branch -m main
git remote add origin https://gitlab.com/<your-username>/sdpt-smart-oven.git
git add .
git commit -m "Initial commit: Oven Controller with CMake and Docker"
git push -u origin main
Phase 2: The Build Stage & Artifacts
We will now tell the GitLab Runner how to build our code using the Docker image you designed last week.
Your Challenge:
Create a file named exactly .gitlab-ci.yml in the root of your project. Write the YAML to meet these specifications:
- Image: Set the global
image:to the Docker registry URL of the ARM image you built in Week 5 (or use a public equivalent provided by the instructor, e.g.,registry.gitlab.com/sdpt-course/arm-build-env:latest). - Stages: Define two stages:
buildandtest. - The Build Job: Create a job named
compile_firmware.- Assign it to the
buildstage. - In the
scriptsection, create a build folder, run CMake for theaarch64compiler, and runmake.
- Assign it to the
- Artifacts: Tell GitLab to save the
build/directory as an artifact that expires in 1 hour. (If you forget this, your compiled binary will be deleted before the test stage starts!)
Commit and push this file. Go to your GitLab project -> Build -> Pipelines. You should see your pipeline running! Click on it to watch the live terminal output of the GitLab Runner.
Phase 3: The Test Stage
Once your build job turns green, it's time to test the artifact.
Update your YAML:
Add a second job named verify_logic to your .gitlab-ci.yml:
- Assign it to the
teststage. - Use the
dependencies:keyword to explicitly require the artifacts fromcompile_firmware. - In the
scriptsection, navigate to thebuild/directory and execute your tests using QEMU (qemu-aarch64 -L /usr/aarch64-linux-gnu/ ./unit_tests).
Push the updated YAML. Watch the pipeline. The Runner will download the compiled binary from the previous stage and execute your tests!
Phase 4: Merge Checks and Failure (The DevOps Climax)
A pipeline is useless if developers can just bypass it. We will now configure GitLab to block bad code.
1. Protect the Main Branch: In GitLab, go to Settings -> Merge Requests. Scroll down to Merge checks and check the box that says: "Pipelines must succeed". Save changes.
2. Create a Feature Branch: Back on your laptop terminal, create a new branch:
git checkout -b feature/broken-sensor
3. Sabotage the Code!
Open your test_oven.cpp file. Intentionally change a mathematical assertion so that it will fail (e.g., change EXPECT_EQ(temp, 100); to EXPECT_EQ(temp, 999);).
4. Push and Open a Merge Request: Commit your broken test and push the branch to GitLab.
git commit -am "feat: updated sensor logic (with bugs)"
git push -u origin feature/broken-sensor
In GitLab, open a Merge Request from your feature branch into main.
5. Observe the Block: Wait for the pipeline to run. The Build stage will pass, but the Test stage will turn RED because Google Test caught your broken math. Look at the Merge Request page. The Merge button is physically disabled! GitLab has successfully protected your production code from human error.
6. Fix and Merge: Fix the test on your laptop, commit, and push again. The pipeline will automatically re-run, turn green, and unlock the Merge button. Click Merge!
Assignment Submission
Upload ONLY your finalized .gitlab-ci.yml to the Moodle VPL assignment. The automated grading server will analyze your YAML syntax to ensure you correctly configured the stages, artifacts, and QEMU execution.