Denomas CI/CD Hands-On Guide: Lab 9 (alternative)
Denomas CI/CD Hands On Guide: Lab 9 (alternative)
LAB 9: CODE QUALITY SCANNING
Create a new project and enable code quality scanning
The training environment disables code quality scanning by default. This section re-enables it for just this project.
- Make a new blank project called
Code Quality Demoin your training group namespace (not your user namespace), with the Initialize repository with a README checkbox enabled. - In the project, go to the left navigation pane and click Settings > CI/CD.
- Next to the Variables section, click Expand.
- Click Add variable.
- Enter
CODE_QUALITY_DISABLEDin the Key field and a single space in the Value field. Note: you can’t simply leave the Value field blank. - Uncheck the Protect variable flag, to make this setting apply to all branches and not just the protected main branch.
- Click Add variable.
Add a Python file with code quality problems
-
In the left navigation pane, click Repository > Files.
-
Create a new file by going to the top of the window and clicking + > This directory > New file
-
For the File name, type
HelloWorld.py -
Paste the following Python code into the file’s contents:
1 2 3def hello_world(a, b, c, d, e, f, g): print("Hello world") # TODO: improve this function -
In the Commit message field, type
Add Python code -
Click
Commit changes.
Configure the .gitlab-ci.yml with code quality scanning
-
In the left navigation pane, click Repsitory > Files.
-
Near the top of the window, click + > This directory > New file.
-
In the File name field, type
.gitlab-ci.ymland make sure NOT to select .gitlab-ci.yml value in the Apply a template dropdown. -
Paste this YAML code into the file’s contents. It does four things:
- Defines a single stage
- Defines a single job within that stage
- Enables code quality scanning
- Adds a code quality problem to
.gitlab-ci.yml
1 2 3 4 5 6 7 8 9 10 11 12stages: - 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? -
In the Commit message field, type
Add CI/CD configuration file that includes code quality scanning -
Click
Commit changes.
View code quality scan results
- In the left navigation pane, click CI/CD > Pipelines.
- The top row represents the pipeline that started running when you committed the
.gitlab-ci.ymlfile 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.
- Once the pipeline’s status is passed, click the status icon to see the pipeline details.
- On the pipeline details screen, click the Code Quality tab above the pipeline graph.
- Notice that the scanner found 3 code quality issues in 2 different files: 2 in
HelloWorld.pyand 1 in.gitlab-ci.yml.
Make a branch
- In the left navigation pane, click Repository > Branches.
- Click New branch. In the Branch name field, type
branch-A. - Click Create branch.
- Click Create merge request in the top right of the window. Leave all settings at their default values.
- Click Create merge request.
Fix issues on the branch
-
In the left navigation pane, click Repository > Files.
-
In the branch dropdown in the top left of the window, pick branch-A.
-
Open
HelloWorld.pyand click Edit. -
Fix a code quality problem by replacing line 1 with this code:
1def hello_world(a): -
Fix another code quality problem by deleting line 3.
-
Commit these changes with the commit message
Fix code quality problems
Compare the code quality of branch-A to the code quality of main
- In the left navigation pane, click CI/CD > Pipelines.
- Wait for the most recent pipeline to show passed status. This might take as long as 5 minutes.
- In the left navigation pane, click Merge requests. Click the Draft: Branch A MR to see the MR details page.
- 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.
- Expand the code quality pane to see the code quality problems you fixed on branch-A.
- To transport or save the results of code quality scanning, the json artifact is available on the Pipelines page by clicking on the verticle ellipses right of the page.
Suggestions?
If you wish to make a change to the Hands-On Guide for Denomas CI/CD, please submit your changes via Merge Request!
17188382)
