GitLab CI/CD - Hands-On Lab: Code Quality Scanning

This Hands-On Guide walks you through using the Code Quality scanner to find and fix a code issue.

Estimated time to complete: 15 - 20 minutes

Objectives

Code quality scans allow you to analyze your source code for quality and complexity. The goal of a code quality scan is to keep your project’s code simple, readable, and easier to maintain. In this lab, you will learn how to enable and view a code quality scan in your project. To learn more about code quality scans, click here.

Task A. Add a Python file with code quality problems

  1. Using the left navigation pane, click Code > Repository.

  2. Create a new file by going to the top of the window and clicking + > This directory > New file.

  3. For the File name, type HelloWorld.py.

  4. Paste the following Python code into the file’s contents:

    1
    2
    3
    
    def hello_world(a, b, c, d, e, f, g):
        print("Hello world")
        # TODO: improve this function
    
  5. In the Commit message field, type Add Python code.

  6. Set the Target Branch to main.

  7. Click Commit changes.

Task B. Configure the .gitlab-ci.yml with Code Quality Scanning

  1. In the left navigation pane, click Code > Repository.

  2. Click on your existing .gitlab-ci.yml file.

  3. Edit the file by clicking the blue Edit dropdown and selecting Edit single file.

  4. Delete all of the existing code in the file. Replace the code with the following file content:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    
    stages:
      - test
    
    test-job:
      stage: test
      script:
        - echo "Pipeline needs at least one job"
    
    include:
      - template: Code-Quality.gitlab-ci.yml
    
    # TODO: should we refactor this file?
    

This code starts by defining a single stage named test. Next, a single job is defined named test-job, which runs as a part of the test stage. The include section enables code quality scanning by including the code quality template. Finally, the TODO command is added as an example of code that is flagged as a code quality problem.

  1. In the Commit message field, type Add CI/CD configuration file that includes code quality scanning.

  2. Set the Target Branch to main.

  3. Click Commit changes.

Task C. View code quality scan results

  1. In the left navigation pane, click Build > Pipelines.

  2. The top row represents the pipeline that started running when you committed the .gitlab-ci.yml file in the previous section. Wait until the status icon at the left of that pipeline says passed.

It can take as long as 5 minutes for the code quality scanner to complete in the training environment, so this is a great time to grab a snack.

  1. Once the pipeline’s status is passed, click the status icon to see the pipeline details.

  2. On the pipeline details screen, click the Code Quality tab above the pipeline graph.

  3. Notice that the scanner found 3 code quality issues in 2 different files: 2 in HelloWorld.py and 1 in .gitlab-ci.yml.

Task D. Make a branch

  1. In the left navigation pane, click Code > Branches.

  2. Click New branch. In the Branch name field, type branch-A.

  3. Click Create branch.

  4. Click Create merge request in the top right of the window. Leave all settings at their default values.

  5. Click Create merge request.

Task E. Fix issues on the branch

  1. In the left navigation pane, click Code > Repository.

  2. In the branch dropdown in the top left of the window, pick branch-A.

  3. Open HelloWorld.py and click Edit.

  4. Fix a code quality problem by replacing line 1 with this code:

    1
    
    def hello_world(a):
    
  5. Fix another code quality problem by deleting line 3.

  6. Commit these changes with the commit message Fix code quality problems

Task F. Compare the code quality of branch-A to the code quality of main

  1. In the left navigation pane, click Build > Pipelines.

  2. Wait for the most recent pipeline to show passed status. This might take as long as 5 minutes.

  3. In the left navigation pane, click Merge requests. Click the Draft: Branch A MR to see the MR details page.

  4. Half-way down the MR details page, if it says No changes to code quality, you may have to refresh the page. The pane should say Code Quality scans found 2 fixed findings.. This means you have fixed 2 code quality problems on branch-A which remain unfixed on main.

  5. Expand the code quality pane to see the code quality problems you fixed on branch-A.

  6. To transport or save the results of code quality scanning, the json artifact is available on the Pipelines page by clicking on the vertical ellipses right of the page.

Lab Guide Complete

You have completed this lab exercise. You can view the other lab guides for this course.

Suggestions?

If you wish to make a change to the Hands-On Guide for GitLab CI/CD, please submit your changes via Merge Request!

Last modified April 26, 2024: Update Lab Descriptions (05f28a88)