PEP8 与 Google Cloud Build 集成

PEP8 integration with Google Cloud Build

有什么方法可以在 Google Cloud Build 中集成代码检查构建步骤,特别是 Pylint,并且任何得分低于 8 的代码都将无法构建?

我的 CICD 设置将代码从 Github 移动到 Google Cloud Composer (Airflow) GCS Bucket。

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

我不知道 Google Cloud Build 部分,但您可以在启动 pylint 时使用 fail-under option(默认为 10.0)

快速回答

您可以通过在其他构建步骤之前添加一个步骤来做到这一点,就像您对 @Pierre.Sassoulas 提到的 运行 unit tests, and execute the Pylint command in there with the fail-under 选项所做的那样,因此构建过程将停止如果它在某个分数下失败(即,如果 <8.0 则失败)。

例如:

  # 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"]

例子

作为参考,我使用 FastAPI 做了一个 运行ning 小例子,灵感来自 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"}

结果(失败):

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

现修改示例代码:

"""
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"}

结果(成功):

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