有什么方法可以将代码检查构建步骤集成到 Google Cloud Build 中,特别是 Pylint,并且任何得分低于 8 的代码都将无法构建?
我的 CICD 设置将代码从 Github 移动到 Google Cloud Composer (Airflow) GCS Bucket。
最佳答案
快速回答
您可以通过在其他构建步骤之前添加一个步骤来实现这一点,就像运行 unit tests 一样。 ,然后使用 fail-under
执行 Pylint 命令。正如 @Pierre.Sassoulas 提到的选项,因此如果低于某个分数失败(即 <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 制作了一个小型运行示例,灵感来自 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
关于python-3.x - PEP8 与 Google Cloud Build 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69896311/