Home Developer Ensure code quality in the CI/CD pipeline

Ensure code quality in the CI/CD pipeline

Author

Date

Category

Continuous integration and continuous delivery tools (CI/CD) like Jenkins, Circle CI and GitHub Actions help automate steps in the software development process. However, these tools cannot ensure the quality of your application once deployed.

To help solve for this, Codacy can block a pull request (PR) if it’s not up to standards (a top feature of our tool). This prevents a merge in Github or other repository generating a build without technical debt.


Let’s assume that someone with admin rights approved the PR. However, your company doesn’t want to automatically build if quality is below a certain threshold. You can prevent the build if it’s under a designated level using Codacy’s REST API.

Example with CI/CD

Although we use GitHub Actions as the CI/CD tool in this example the same actions apply across CI/CD systems.
First let’s create an action and set it up to run only on the master branch.


name: Release

on:
  push:
    branches:
      - master

Then, generate a Project API token on Codacy and add it to GitHub Secrets.

Now we use the REST API mentioned above to check repo details on Codacy. We use the “Project Details” method (below) which returns the following data:

{
  "commit": {
    "commit": {
      "complexity": {},
      "coverage": {},
      "grade": "string",
      "nrClones": {},
      "nrIssues": 0
    },
    "delta": {
      "complexity": {},
      "coverage": {},
      "files": [
        {
          "complexity": {},
          "coverage": {},
          "fixedIssues": 0,
          "newIssues": 0,
          "nrClones": {},
          "path": "string"
        }
      ],
      "fixedIssues": 0,
      "newIssues": 0,
      "nrClones": {}
    },
    "sha": "string",
    "state": "string",
    "urls": [
      {
        "name": "string",
        "url": "string"
      }
    ]
  },
  "name": "string",
  "urls": [
    {
      "name": "string",
      "url": "string"
    }
  ],
  "user": "string"
}

We check the project grade and receive the following:

grade=$(curl -s -X GET https://api.codacy.com/2.0/project \
          -H 'Accept: application/json' \
          -H 'project_token: ${{ secrets.CODACY_TOKEN }}' | jq -r '.commit.commit.grade')
 echo $grade
 if [[ "$grade" == "A" ]]
    then
      echo -e "\e[1;42m Grade $grade is ok. Build will continue. \e[0m"
    else
      echo -e "\e[1;31m Grade $grade is below the expected and the build will stop. \e[0m"
            exit 1
 fi

Therefore the project will exit the build process with error status 1 if the repo grade is not “A.” It will look like this:

Below is the full GitHub Action code for the example:

name: Release

on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [12.x]
    steps:
    - uses: actions/checkout@v1
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v1
      with:
        node-version: ${{ matrix.node-version }}
    - name: npm coverage
      run: |
        set -e
        npm install
        npm run coverage-to-codacy
        grade=$(curl -s -X GET https://api.codacy.com/2.0/project \
          -H 'Accept: application/json' \
          -H 'project_token: ${{ secrets.CODACY_TOKEN }}' | jq -r '.commit.commit.grade')
        echo $grade
        if [[ "$grade" == "A" ]]
          then
            echo -e "\e[1;42m Grade $grade is ok. Build will continue. \e[0m"
          else
            echo -e "\e[1;31m Grade $grade is below the expected and the build will stop. \e[0m"
            exit 1
        fi
      env:
        CI: true

We have now ensured that we meet the designated level of code quality in the project!


About the author

Helio Rocha is a Solutions Engineer at Codacy. You can read more from Helio on our blog and on his medium page. Reach him on twitter too at @ItsOnlyHelio.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Subscribe to our newsletter

To be updated with all the latest news, offers and special announcements.

Recent posts

What are Static Analysis Tools?

Static analysis tools are carried out on a software product in a non-runtime environment. This means that it is unnecessary to execute a program...

Your Static Code Analysis Guide

Errors - A word that is not so peaceful in the world of developers and software development. So, without any doubt, developers work hard...

Technical Debt In Coding

In this world, everybody has some kind of debt. And if we talk about the technical debt, it sounds like some kind of a...

Why we implemented Offline days at Codacy

Since the Coronavirus outbreak, like most people, we are facing a unique reality that is challenging us in many ways at the...

Pair programming at Codacy and why we do it

Pair programming, also known as pairing or “dynamic duo” model is not a new concept, and it was pioneered by C/C++ guru...