使用模板构建 Python pipeline

此 pipeline 旨在为 Python 应用自动化完整的 CI/CD 工作流。 它具有可复用性和灵活性,支持自定义构建参数、多个工作区以及按条件执行步骤。

主要特性:

  • ✅ 灵活的代码来源
  • 🔌 通过 params 插件化任务
  • 🛠️ 动态镜像构建与推送
  • ☁️ 通过 kubectl 更新工作负载镜像

使用 hub resolvers 指定

以下示例 pipeline run 引用 catalog 中的 pipeline:

apiVersion: tekton.dev/v1
kind: PipelineRun
metadata:
  name: python-pipeline-run
spec:
  pipelineRef:
    resolver: hub
    params:
    - name: catalog
      value: catalog
    - name: kind
      value: pipeline
    - name: name
      value: python-image-build-scan-deploy
    - name: version
      value: "0.1"

参数

Git Clone

  • git-url

    • type: string
    • default: ""
    • description: 要克隆的仓库 URL。
  • git-revision

    • type: string
    • default: ""
    • description: 要检出的 revision(branch、tag、sha、ref 等)。
  • skip-git-clone

    • type: string
    • default: "false"
    • description: 跳过执行 git clone 任务。仅当对应的代码仓库已存在于 workspace 中时,才能启用。
  • git-crt-file-name

    • type: string
    • default: "ca-bundle.crt"
    • description: 使用 ssl-ca-directory workspace 挂载的 crt 文件名。默认值为 ca-bundle.crt。

Pre build

  • pre-build-script

    • type: string
    • default: ""
    • description: 在构建前执行的 Python 脚本。可用于单元测试、lint、在构建前运行脚本等。如果未设置此参数,将跳过 pre-build 任务。
  • pre-build-args

    • type: array
    • default: []
    • description: 传递给 pre-build 脚本的参数。
  • pre-build-requirements-file

    • type: string
    • default: "requirements.txt"
    • description: 源码位置内 requirements 文件的名称;如果不存在,则回退到根目录下的 requirements 文件。
  • pre-build-pip-conf-file

    • type: string
    • default: "pip.conf"
    • description: 自定义 pip 配置文件的名称。

SonarQube Scanner

  • sonar-url

    • type: string
    • default: ""
    • description: 你的 SonarQube Server 实例的 URL。如果未指定,将跳过 sonarqube scanner 任务。
  • sonar-project-key

    • type: string
    • default: ""
    • description: SonarQube 项目的唯一 key。
  • sonar-properties

    • type: array
    • default: ["sonar.sources=."]
    • description: 传递给 SonarQube 的附加属性。更多详情可参考 SonarQube 属性配置

构建镜像

  • images

    • type: array
    • description: buildah 将生成的镜像引用。可以包含多个以逗号分隔的镜像地址。例如:
      • busybox:latest
      • busybox:v1 .30.1
  • containerfile-path

    • type: string
    • default: ./Containerfile
    • description: 用于构建的 Containerfile 路径。
  • build-extra-args

    • type: string
    • default: ""
    • description: 构建镜像时传递给 build 命令的额外参数。警告 - 必须经过清理以避免命令注入(例如 --build-arg key=value --label key=value)。
  • build-args

    • type: array
    • default: [""]
    • description: 指定一个 build 参数及其值,它会像环境变量一样在读取自 Containerfile 的指令中进行插值,但不会被添加到生成镜像配置中的环境变量列表。比如 HTTP_PROXY=http://10.10.10.10:8080
  • build-context

    • type: string
    • default: .
    • description: 作为 context 使用的目录路径。
  • push-extra-args

    • type: string
    • default: ""
    • description: 推送镜像时传递给 push 命令的额外参数。警告 - 必须经过清理以避免命令注入(例如 --creds=username:password )。

Trivy Scanner

  • skip-trivy-scan

    • type: string
    • default: "false"
    • description: 设置为在构建后跳过 trivy 镜像扫描。
  • trivy-extra-args

    • type: string
    • default: ""
    • description: 扫描镜像时传递给 trivy 命令的额外参数。警告 - 必须经过清理以避免命令注入(例如 --insecure)。你可以使用 --skip-db-update --skip-java-db-update 来跳过漏洞数据库更新。

部署或升级工作负载

  • workload-name

    • type: string
    • default: ""
    • description: 要部署或升级的工作负载名称。如果未指定,将跳过 deploy-or-upgrade 任务。
  • workload-kind

    • type: string
    • default: Deployment
    • description: 要部署或升级的工作负载类型,例如 Deployment、StatefulSet 等。
  • workload-namespace

    • type: string
    • default: ""
    • description: 要部署或升级的工作负载命名空间。如果未指定,将使用当前 TaskRun 的命名空间。
  • workload-container

    • type: string
    • default: []
    • description: 此参数用于指定工作负载中需要更新镜像的容器。默认情况下,将更新工作负载中所有容器的镜像。
  • workload-rollout-timeout

    • type: string
    • default: "0"
    • description: 等待工作负载就绪后再结束等待的时长,0 表示永不超时。其他值应包含相应的时间单位(例如 1s、2m、3h)。
  • workload-manifests-dir

    • type: string
    • default: ""
    • description: 要部署的工作负载 manifest。如果未指定,则只会更新集群中已存在工作负载的镜像。你可以使用 source workspace 中 manifest 目录的相对路径。例如:"manifests"。

Workspaces

  • source: 用于在各个任务之间共享信息的 Workspace。
  • git-basic-auth: 一个 可选 workspace,包含 .gitconfig.git-credentials 文件。这些文件会在执行任何 git 命令之前复制到用户的 home 目录。此 Workspace 中的其他文件将被忽略。强烈建议在可行时优先使用 ssh-directory 而不是 basic-auth,并且优先将 Secret 绑定到此 Workspace,而不是其他卷类型。
  • git-ssh-directory: 一个 可选 workspace,包含私钥、known_hosts、config 等内容。在执行 git 命令之前会复制到用户的 home 目录。用于在执行 clone 时对 git remote 进行身份验证。强烈建议优先将 Secret 绑定到此 Workspace,而不是其他卷类型。
  • git-ssl-ca-directory: 一个 可选 workspace,包含 CA 证书,Git 会在通过 HTTPS 获取或推送时使用它来验证对端。
  • pip-conf: 一个 可选 workspace,包含自定义 pip 设置。
  • sonar-settings: 一个 可选 workspace,可在其中挂载 SonarQube 属性。
  • sonar-credentials: 一个 可选 workspace,包含供 SonarQube 使用的凭据。
  • registryconfig: 一个 可选 workspace,用于 distribution registry 配置文件,例如 config.json.dockerconfigjson。这是可选项,用于在向 registry 推送镜像时进行身份验证。
  • kubeconfig: 一个 可选 workspace,包含 kubeconfig 文件。kubeconfig 的名称应为 kubeconfig。如果此 workspace 中没有 kubeconfig,则会忽略该 workspace。

Platforms

该 Task 可在 linux/amd64linux/arm64 平台上运行。

用法

最小化配置:使用最少参数端到端运行

未设置或省略时,可选任务(sonarqube-scannerdeploy-or-upgrade 等)将被优雅地跳过。

apiVersion: tekton.dev/v1
kind: PipelineRun
metadata:
  generateName: python-build-run-
spec:
  pipelineRef:
    name: python-image-build-scan-deploy
  params:
    - name: git-url
      value: https://github.com/example/python-app
    - name: git-revision
      value: refs/heads/main
    - name: images
      value: ["registry.example.com/python-app:latest"]
  workspaces:
    - name: source
      persistentVolumeClaim:
        claimName: source
    - name: git-basic-auth
      secret:
        secretName: gitconfig
    - name: registryconfig
      secret:
        secretName: registryconfig

SonarQube 属性配置

你可以挂载 sonar-settings workspace,或者使用 sonar-properties 参数来为 sonar-project.properties 文件应用属性。

默认属性为:sonar.sources=.

你可以从 analysis parameters 以及 the properties for Python 中了解 SonarQube 属性的用法。

以下是一些可设置的属性:

属性说明
sonar.python.versionPython 代码默认会按兼容 Python 2 和 Python 3 进行分析。为了获得更精确的分析,你可以通过 sonar.python.version 参数指定代码所支持的 Python 版本。
sonar.qualitygate.wait强制分析步骤轮询服务器实例并等待 Quality Gate 状态。
sonar.qualitygate.timeoutscanner 等待报告处理完成的秒数。
sonar.projectVersion项目版本。如果你使用基于前一个版本的新代码定义,则应在 branch 分析中设置该值。
sonar.sources以逗号分隔的目录路径,包含主源文件
sonar.organizationSonarQube 中项目所属的 organization

部署或升级工作负载

你可以使用此 pipeline 中 build-image 任务构建的镜像来部署或升级工作负载。

通过设置 workload-manifests-dir 参数,你可以应用指定目录中的 Kubernetes manifests 来创建或更新工作负载。

支持 JSON 和 YAML 格式。指定目录中的所有 manifest 都会使用 kubectl apply 进行处理。请注意,kubectl apply 不会遍历子目录。

如果该目录包含 Kustomize 配置文件(例如 kustomization.yamlkustomization.ymlKustomization),则会使用 kubectl kustomize 进行处理。

以下是 workload-manifests-dir 的示例结构:

manifests
├── deployment.yaml
├── kustomization.yaml
└── service.yaml

你可以配置以下参数:

spec:
  params:
    # ...
    - name: images
      value:
        - foo/bar:latest
    - name: workload-name
      value: bar
    - name: workload-namespace
      value: default
    - name: workload-kind
      value: Deployment
    - name: workload-manifests-dir
      value: manifests
    # ...

如果你不想使用 manifests 部署工作负载,只需将 workload-manifests-dir 参数留空即可。在这种情况下,该任务只会更新集群中现有工作负载的镜像。

你需要为工作负载管理 image pull secret。如果新镜像托管在不同的 registry 中,请确保为你的工作负载创建并配置合适的 pull secret。

运行 pytest

你可以在此 pipeline 中使用 pre-build 任务运行 pytest。

以下是运行 pytest 的示例:

apiVersion: tekton.dev/v1
kind: PipelineRun
metadata:
  generateName: python-build-run-
spec:
  pipelineRef:
    name: python-image-build-scan-deploy
  params:
    - name: git-url
      value: https://github.com/example/python-app
    - name: git-revision
      value: refs/heads/main
    - name: images
      value: ["registry.example.com/python-app:latest"]
    - name: pre-build-script
      value: |
        pytest
  workspaces:
    - name: source
      persistentVolumeClaim:
        claimName: source
    - name: git-basic-auth
      secret:
        secretName: gitconfig
    - name: registryconfig
      secret:
        secretName: registryconfig

使用 Params 跳过任务

该 pipeline 支持通过 params 跳过任务:

Task参数默认值说明
git-cloneskip-git-clone"false"如果代码已经存在于 source workspace 中,你可以将其设置为 "true" 以跳过克隆。
pre-buildpre-build-script""如果未设置 pre-build-script,将跳过 pre-build Task。
sonarqube-scannersonar-url""如果未设置 sonar-url,将跳过扫描。
trivy-scannerskip-trivy-scan"false"如果将 skip-trivy-scan 设置为 "true",将跳过扫描。
deploy-or-upgradeworkload-name""如果你不想部署或升级工作负载,可以将 workload-name 留空。

包含所有任务的完整示例

此示例演示如何使用 pipeline 中的所有任务。它包含以下任务:

  • 克隆源代码
  • 使用 pre-build 任务运行单元测试
  • 构建镜像并将镜像推送到 registry
  • 使用 Trivy 扫描镜像
  • 使用 SonarQube 扫描代码
  • 部署或升级工作负载
apiVersion: tekton.dev/v1
kind: PipelineRun
metadata:
  generateName: python-build-run-
spec:
  pipelineRef:
    name: python-image-build-scan-deploy
  params:
    - name: git-url
      value: https://github.com/example/python-app
    - name: git-revision
      value: refs/heads/main
    - name: sonar-url
      value: https://sonar.example.com
    - name: sonar-project-key
      value: python-pipeline-example-key
    - name: images
      value: ["registry.example.com/python-app:latest"]
    - name: workload-name
      value: python-app
    - name: workload-namespace
      value: default
    - name: pre-build-script
      value: |
        pytest
  workspaces:
    - name: source
      persistentVolumeClaim:
        claimName: source
    - name: git-basic-auth
      secret:
        secretName: gitconfig
    - name: sonar-credentials
      secret:
        secretName: sonar-credentials
    - name: registryconfig
      secret:
        secretName: registry-creds
    - name: kubeconfig
      configMap:
        name: kubeconfig