使用Argo workflows构建java CI流水线
本文将介绍如何使用Argo workflows构建java CI流水线,将过程中躺过的坑都记录下来。
Argo workflows介绍
Argo Workflows 是一个开源的容器原生的工作流引擎,实现为 Kubernetes CRD,可在 Kubernetes 上编排并行作业。
Argo Workflows比较符合现在云原生的概念,与集群无关,可以在任何k8s集群上执行argo workflows,可以在k8s上轻松编排工作流。
Argo Workflows安装
github.com/argoproj/ar…
下载代码直接用argocd纳管就行。
CI流程
参考
在 Kubernetes 上使用 Argo 实现
使用Argo workflows构建java CI流水线
clone代码仓和配置仓
git仓代码clone如何配置用户和密码 在请求串中加入身份信息即可: 格式
https://[username]:[password]@gitee.com/project.git复制代码
直接写密码肯定是不安全的
gitlab可以生成token,如下:
然后使用如下命令可以进行访问
git clone https://[username]:[token]@xxxxxx.com/MyUser/MyRepo.git复制代码
- name: clone-code inputs: parameters: - name: code-repo - name: code-branch container: volumeMounts: - mountPath: /work name: work image: alpine/git:v2.26.2 workingDir: /work # Do a shallow clone, which is the fastest way to clone, by using the # --depth, --branch, and --single-branch options args: - clone # - --depth # - "1" - --branch - "{{inputs.parameters.code-branch}}" - --single-branch - "{{inputs.parameters.code-repo}}" - .复制代码
代码仓或者镜像仓是域名可以通过在spec下添加hostAliases来进行域名解析
hostAliases: - ip: "10.114.61.24" hostnames: - "git.xxxxxx.com" - ip: "10.120.43.49" hostnames: - "xxxxxx.xxxxxx.com"复制代码
maven构建
通过maven编译的时候,统一通过settings.xml来配置本地仓库来加速构建过程。settings.xml可以放到代码仓下,但是这样就不通用了,每一个代码仓都需要添加一份。可以通过把settings.xml放到配置仓,加一个clone配置仓库的步骤和clone代码仓一起,然后将其也挂在到build的容器的某个路径就好了。
首先我们定义一个名字叫workflow-build-pv-claim的pvc,然后在spce中声明这个卷maven-repo。
volumes: - name: maven-repo persistentVolumeClaim: claimName: workflow-build-pv-claim复制代码
然后将maven-repo这个卷挂载到build容器的某个路径下,如“/maven-repo”。
- name: build inputs: parameters: - name: code-path container: image: maven:3-alpine volumeMounts: - mountPath: /work name: work - mountPath: /config name: config - mountPath: /maven-repo name: maven-repo workingDir: /work/ command: - mvn args: - --settings=/config/settings.xml - -B - -DskipTests - clean - deploy复制代码
settings.xml的localRepository设置为“/maven-repo”
<localRepository>/maven-repo</localRepository>复制代码
kaniko打包镜像
Kaniko
是 Google 造的轮子之一,用于在 Kubernetes 上无需特权模式
构建 docker image
。
Kaniko
不依赖Docker daemon
守护程序,而是完全在userspace
中执行Dockerfile
中的每个命令。这使您可以在没有特权模式
或没有运行Docker daemon
的环境(例如:Kubernetes集群)中构建容器镜像。
我们需要将镜像产物推送到镜像仓库,我的镜像仓库是Harbor.
可以创建一个机器人账号,然后使用如下命令创建一个secret
kubectl create secret kaniko-secret docker-registry-creds --docker-server="https://xxxxxxxxxxxx.com" --docker-username='robot$xxxxxxxxxxxx' --docker-password='hghghjhoigtfgiohgtyfuyuhugftybhyuftuy'复制代码
然后在spec下定义kaniko-secret的卷
volumes: - name: kaniko-secret secret: secretName: kaniko-secret items: - key: .dockerconfigjson path: config.json复制代码
然后就可以在容器中mount到容器的路径/kaniko/.docker下
- name: image inputs: parameters: - name: path - name: image - name: dockerfile - name: cache-image container: image: daocloud.io/gcr-mirror/kaniko-project-executor:debug volumeMounts: - name: kaniko-secret mountPath: /kaniko/.docker - name: work mountPath: /work workingDir: /work args: ["--dockerfile={{inputs.parameters.dockerfile}}", "--context=/work", "--insecure=false", "--skip-tls-verify=false", "--destination={{inputs.parameters.image}}"]
作者:Native
链接:https://juejin.cn/post/7056297110034972679