All Articles

How to Add Code Quality to your GitLab pipelines

Disclaimer: This tutorial uses a fresh Ubuntu 18.x Droplet on DigitalOcean. However, this guide is fairly general and will likely aid you regardless of having your server hosted on DigitalOcean.

Code Climate has developed a series of tools that can help your development team produce reliable, extensible and secure code in less time through their suite of utilities. Today I will show you how to incorporate the code quality utility from Code Climate into your GitLab CI/CD process. The code quality standards and template are free of charge and you can configure them to fit your teams needs. I’ll get started with a standard template provided by GitLab and show you where the results will appear and how you can access them.

My guide is a variation of the official guide from the GitLab documentation: https://docs.gitlab.com/ee/user/project/merge_requests/code_quality.html with a few modifications that will help solve issues you may encounter (I know I certainly had my fair share). With that being said, the first step on your journey is to set up a GitLab runner. If you don’t currently have a GitLab runner set up on your server then I suggest you read https://docs.gitlab.com/runner/install/ which will get you started. Be sure that your runner is configured with docker as the executor.

Once you’ve completed this step, you’ll need to modify your gitlab-runner configuration via the config.toml file found at /etc/gitlab-runner/config.toml.

Below is an example file which includes the necessary settings to be able to run Docker which we will be utilizing to spin up a container from the code quality image. Be sure to replace the token with your runners token if you do copy and paste the configuration below as well as modify the url if you are using a self-hosted instance of GitLab. Also feel free to rename the runner.

/etc/gitlab-runner/config.toml

[[runners]]
  name = "Code Quality Docker Runner"
  url = "https://gitlab.com"
  token = "xxxxxxx"
  executor = "docker"
  [runners.docker]
    tls_verify = false
    image = "docker:19.03.11"
    privileged = true
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/cache"]
    shm_size = 0
    wait_for_services_timeout = 30
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]

Two key elements to this config are prileveged = true and volumes=["/cache"]. Without these configuration settings your Docker based tasks will likely fail to complete so be sure to have these configurations set.

The next step will be to create (or modify) your gitlab-ci.yml file and utilize the default code quality template. You can provide your own configuration at any point in time by including your own .yml file but for the sake of example we will continue to use the template.

.gitlab-ci.yml

include:
  - template: Code-Quality.gitlab-ci.yml

stages:
  - test

code_quality:
  image: docker:19.03.11
  artifacts:
    reports:
      codequality: gl-code-quality-report.json

Note that by default Code Qquality will be associated with the test stage. You can however override the default by providing a different stage name in the job configuration. For the sake of this example, we are going to have code quality run when a new commit/merge occurs for any branch.

On your next commit/merge to a branch in your repo you will see a new task in your pipeline for code quality which will produce a report at the end of it’s run. To access this report, go to your most recent pipeline for your master branch and click on the new code quality tab to view it’s output:

Code quality on GitLab