使用模板构建 Java 管道
此管道旨在为 Java 应用自动化完整的 CI/CD 工作流。 它具有可复用性和灵活性,支持自定义构建参数、多个工作区以及条件性执行步骤。
主要功能:
- ✅ 灵活的代码源
- ⚙️ 可自定义的 Maven 设置
- 🚀 支持 Maven 本地仓库缓存
- 🔌 可通过 params 插拔任务
- 🛠️ 动态镜像构建与推送
- ☁️ 通过 kubectl 更新工作负载镜像
目录
使用 hub resolvers 指定它参数Git CloneMavenSonarQube Scanner构建镜像Trivy Scanner部署或升级工作负载工作区平台使用方式最小化配置:使用最少参数运行端到端流程使用 Maven Settings.xml使用构建缓存加速 CISonarQube 属性配置部署或升级工作负载使用 Params 跳过任务包含所有任务的完整示例使用 hub resolvers 指定它
以下示例 PipelineRun 引用了 catalog 中的 pipeline:
参数
Git Clone
-
git-url
- type: string
- default:
"" - description: 要克隆的仓库 URL。
-
git-revision
- type: string
- default:
"" - description: 要检出的修订版本(branch、tag、sha、ref 等)。
-
skip-git-clone
- type: string
- default:
"false" - description: 跳过执行 git clone 任务。仅当对应的代码仓库已存在于工作区中时,才可启用。
-
git-crt-file-name
- type: string
- default:
"ca-bundle.crt" - description: 使用 ssl-ca-directory 工作区挂载的 crt 文件名。默认值为 ca-bundle.crt。
Maven
-
maven-subdirectory
- type: string
- default:
. - description: Maven 项目的上下文目录。
-
maven-goals
- type: array
- default:
["package"] - description: 要运行的 Maven goals。
-
maven-mirror-url
- type: string
- default:
"" - description: Maven 仓库镜像 URL。
SonarQube Scanner
-
sonar-url
- type: string
- default:
"" - description: 你的 SonarQube Server 实例的 URL。如果未指定,将跳过 sonarqube scanner 任务。
-
sonar-project-key
- type: string
- default:
"" - description: SonarQube 项目的唯一键。
-
sonar-properties
- type: array
- default:
["sonar.sources=.", "sonar.java.binaries=target/classes"] - description: 传递给 SonarQube 的附加属性。有关更多详细信息,请参阅 Configuration of SonarQube properties。
构建镜像
-
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: 用作上下文的目录路径。
-
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: 要部署的工作负载清单。如果未指定,则只更新集群中已存在工作负载的镜像。你可以使用
source工作区中的清单目录相对路径。例如:"manifests"。
工作区
- source:用于在任务之间共享信息的工作区。
- git-basic-auth:一个 可选工作区,包含 .gitconfig 和 .git-credentials 文件。在运行任何 git 命令之前,这些文件会被复制到用户的 home 目录。此工作区中的其他文件将被忽略。强烈建议在可能的情况下使用 ssh-directory 而不是 basic-auth,并将 Secret 绑定到此工作区,而不是其他卷类型。
- git-ssh-directory:一个 可选工作区,包含私钥、known_hosts、config 等。在执行 git 命令之前会被复制到用户的 home 目录。在执行 clone 时用于对 git remote 进行身份验证。强烈建议将 Secret 绑定到此工作区,而不是其他卷类型。
- git-ssl-ca-directory:一个 可选工作区,包含 CA 证书,Git 会在通过 HTTPS 进行 fetch 或 push 时使用它来验证对端。
- maven-settings:一个 可选工作区,包含自定义 maven 设置。
- maven-local-repo:一个 可选工作区,用于 Maven 本地仓库。
- maven-server-secret:一个 可选工作区,包含服务器凭据。
- maven-trust-store:一个 可选工作区,包含 Maven 使用的私有 CA 的 JKS truststore。
- sonar-settings:一个 可选工作区,可在其中挂载 SonarQube 属性。
- sonar-credentials:一个 可选工作区,包含可在 SonarQube 中使用的凭据。
- registryconfig:一个 可选工作区,用于 distribution registry 配置文件,例如
config.json或.dockerconfigjson。这是可选的,用于在将镜像推送到 registry 时进行身份验证。 - kubeconfig:一个 Optional workspace,包含 kubeconfig 文件。kubeconfig 的名称应为
kubeconfig。如果此工作区中没有 kubeconfig,则会忽略该工作区。
平台
该 Task 可在 linux/amd64 和 linux/arm64 平台上运行。
使用方式
最小化配置:使用最少参数运行端到端流程
未设置或省略的可选任务(sonarqube-scanner、deploy-or-upgrade 等)将被优雅地跳过。
使用 Maven Settings.xml
你可以挂载 maven-settings 工作区,以便为 Maven 构建应用 settings.xml。
你也可以使用 maven-mirror-url 参数来设置 Maven 仓库镜像。它将作为 settings.xml 中的 <mirror> 使用。
使用构建缓存加速 CI
附加一个持久化的 maven-local-repo 工作区,以便在各次 pipeline run 之间缓存 .m2/repository 内容。
复用依赖可以大幅减少 Maven 依赖解析时间,并显著缩短跨运行的构建时间。
SonarQube 属性配置
你可以挂载 sonar-settings 工作区,或使用 sonar-properties 参数,为 sonar-project.properties 文件应用属性。
默认属性为:sonar.sources=.、sonar.java.binaries=target/classes。
你可以在 analysis parameters 和 the properties for Java 中了解 SonarQube 属性的用法。
你可以设置如下属性:
部署或升级工作负载
你可以使用此 pipeline 中 build-image 任务构建的镜像来部署或升级工作负载。
通过设置 workload-manifests-dir 参数,你可以将指定目录中的 Kubernetes 清单应用到工作负载,以创建或更新工作负载。
同时支持 JSON 和 YAML 格式。指定目录中的所有清单都将使用 kubectl apply 处理。请注意,kubectl apply 不会遍历子目录。
如果目录中包含 Kustomize 配置文件(例如 kustomization.yaml、kustomization.yml 或 Kustomization),则会使用 kubectl kustomize 进行处理。
以下是 workload-manifests-dir 的示例结构:
你可以配置以下参数:
如果你不希望使用清单来部署工作负载,只需将 workload-manifests-dir 参数留空。在这种情况下,该任务只会更新集群中现有工作负载的镜像。
你需要为工作负载管理镜像拉取 Secret。如果新镜像托管在不同的 registry 中,请确保为工作负载创建并配置相应的 pull secret。
使用 Params 跳过任务
该 pipeline 支持通过参数跳过任务:
包含所有任务的完整示例
此示例演示如何在 pipeline 中使用所有任务。它包含以下任务:
- 克隆源代码
- 使用 Maven 构建代码
- 构建镜像并将镜像推送到 registry
- 使用 Trivy 扫描镜像
- 使用 SonarQube 扫描代码
- 部署或升级工作负载