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

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...

Enhanced security for C++, Java, and Scala with Clang-Tidy and SpotBugs

As part of our effort to continue expanding our language support, we are excited to announce the support of two new tools...

Improve the efficiency of your remote engineering team

COVID-19 hit the ground running and the world felt the impact. Although tech companies seemed to be ahead of the curve by...

Further Enterprise security analysis for Scala

We’re excited to announce the latest addition to our suite of security analysis: Spotbugs. SpotBugs is a program which...