Abhinav Dhiman
Abhinav Dhiman

Reputation: 755

PEP8 integration with Google Cloud Build

Is there any way I can integrate a Code linting build step in Google Cloud Build, specifically Pylint, and any code that gets a score of less than 8 would fail to build?

My CICD setup moves code from Github to Google Cloud Composer (Airflow) GCS Bucket.

Upvotes: 2

Views: 551

Answers (2)

Javier M
Javier M

Reputation: 192

Quick answer

You could do so by adding a step prior to other build steps just as you would do to run unit tests, and execute the Pylint command in there with the fail-under option as @Pierre.Sassoulas mentioned, so the building process will stop if it fails under a certain score (ie, fail if <8.0).

For instance:

  # Build step to run pylint on my prod.py file. 
  # It will stop the process if the score is less than 8.0.
  - name: python
    entrypoint: python
    args: ["-m", "pylint", "prod.py", "--fail-under=8.0"]

Example

As a reference, I've made a small running example using FastAPI, inspired by this article:

cloudbuild.yaml

steps:
  # Install dependencies
  - name: python
    entrypoint: pip3
    args: ["install", "-r", "./requirements.txt", "--user"]
  # Build step to run pylint on my prod.py file. 
  # It will stop the process if the score is less than 8.0.
  - name: python
    entrypoint: python
    args: ["-m", "pylint", "prod.py", "--fail-under=8.0"]
  # Yay!
  - name: 'bash'
    args: ['echo', 'Success!']

FastAPI sample code:

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}

Result (Fail):

Starting Step #1
Step #1: Already have image (with digest): python
Step #1: ************* Module prod
Step #1: prod.py:1:0: C0114: Missing module docstring (missing-module-docstring)
Step #1: prod.py:4:0: C0116: Missing function or method docstring (missing-function-docstring)
Step #1:
Step #1: -----------------------------------
Step #1: Your code has been rated at 5.00/10
Step #1:
Finished Step #1
2021/11/11 12:29:23 Step Step #1 finished
2021/11/11 12:29:23 status changed to "ERROR"
ERROR
ERROR: build step 1 "python" failed: exit status 16
2021/11/11 12:29:23 Build finished with ERROR status

Now, modifying sample code:

"""
Basic API taken from https://fastapi.tiangolo.com/tutorial/first-steps/
"""
from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root():
    """
    Top level endpoint to test server
    """
    return {"message": "Hello World"}

Result (Success):

Starting Step #1
Step #1: Already have image (with digest): python
Step #1:
Step #1: ------------------------------------
Step #1: Your code has been rated at 10.00/10
Step #1:
Finished Step #1
2021/11/11 12:43:13 Step Step #1 finished
Starting Step #2
Step #2: Pulling image: bash
Step #2: Using default tag: latest
.
.
.
Step #2: Success!
Finished Step #2
2021/11/11 12:43:15 Step Step #2 finished
2021/11/11 12:43:15 status changed to "DONE"
DONE

Upvotes: 2

Pierre.Sassoulas
Pierre.Sassoulas

Reputation: 4282

any code that gets a score of less than 8 would fail to build?

I don't know about the Google Cloud Build part, but you may use the fail-under option when launching pylint (Default is 10.0)

Upvotes: 1

Related Questions