构建镜像之后,更新yaml repository触发 argo cd
核心脚本只有一个
cat /work/commitid.txt | xargs -i sed 's/:[0-9A-Za-z]\{40,40\}$/:{}/' /argogithubsource_cd/argoapp.yaml
我们创建一个 main.sh
#!/bin/bash
git config --global user.email $USER_EMAIL
git config --global user.name $USER_NAME
echo $GIT_URL
echo $REPO
echo $BRANCH
rm -rf /work/$REPO
git clone -b $BRANCH $GIT_URL
pwd
echo "ls /"
ls /
echo "ls REPO"
ls $REPO
echo "============"/$REPO/$1"================"
cat /$REPO/$1
echo "============/work/commitid.txt================"
cat /work/commitid.txt
cat /work/commitid.txt | xargs -i sed -i 's/:[0-9A-Za-z]\{40,40\}$/:{}/' /$REPO/$1
cat /work/commitid.txt | xargs -i sed -i 's/:mockup_tag_name/:{}/' /$REPO/$1
echo "============"/$REPO/$1"================"
cat /$REPO/$1
cd /$REPO && git add ./*
cd /$REPO && cat /work/commitid.txt | xargs -i git commit -m {} ./*
cd /$REPO && git push
其中主要的配置
5个环境变量
$GIT_URL = https://USERNAME:PASSWORD@github.com/zl86790/argogithubsource_cd.git
$REPO = argogithubsource_cd
$BRANCH = main
$USER_EMAIL = YOUREMAIL@google.com
$USER_NAME = YOURNAME
一个参数
$1 = argoapp.yaml
在提交了新代码之后,这个容器会使用上面的脚本根据前面步骤生成的 commitid,自动更新 deply的git仓库,修改 应用的 tag name,并且提交到部署代码仓库
最后通过 deploy 步骤进行部署
apiVersion: argoproj.io/v1alpha1
kind: Sensor
metadata:
name: github
namespace: argo
spec:
template:
serviceAccountName: operate-workflow-sa
dependencies:
- name: test-dep
eventSourceName: github
eventName: gitwebhook
triggers:
- template:
name: argo-workflow-trigger
argoWorkflow:
group: argoproj.io
version: v1alpha1
resource: workflows
operation: submit
source:
resource:
apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
generateName: buildkit-
namespace: argo
spec:
templates:
- name: main
dag:
tasks:
- name: clone
template: clone
arguments:
parameters:
- name: repo
value: '{{workflow.parameters.repo}}'
- name: branch
value: '{{workflow.parameters.branch}}'
- name: getcommitid
template: getcommitid
arguments: {}
depends: clone
- name: mountcommitid
template: mountcommitid
arguments: {}
depends: getcommitid
- name: build
template: build
arguments: {}
depends: mountcommitid
- name: image
template: image
arguments:
parameters:
- name: image
value: '{{workflow.parameters.image}}'
- name: commitid
value: '{{tasks.mountcommitid.outputs.parameters.commitid}}'
depends: build
- name: deploy
template: deploy
arguments: {}
depends: image
- name: clone
inputs:
parameters:
- name: repo
- name: branch
container:
name: ''
image: 'alpine/git:v2.26.2'
args:
- clone
- '--depth'
- '1'
- '--branch'
- '{{inputs.parameters.branch}}'
- '--single-branch'
- '{{inputs.parameters.repo}}'
- .
workingDir: /work
volumeMounts:
- name: work
mountPath: /work
- name: getcommitid
inputs: {}
outputs: {}
metadata: {}
container:
name: ''
image: libaibai/getcommitid
command:
- /bin/bash
args:
- '-c'
- /getcommitid.sh
workingDir: /work
volumeMounts:
- name: work
mountPath: /work
- name: mountcommitid
outputs:
parameters:
- name: commitid
valueFrom:
path: /work/commitid.txt
default: mockup_commitid
metadata: {}
container:
name: ''
image: busybox
command:
- /bin/sh
args:
- '-c'
- ls /work
workingDir: /work
resources: {}
volumeMounts:
- name: work
mountPath: /work
- name: build
container:
name: ''
image: 'golang:alpine3.13'
command:
- go
args:
- build
- main.go
workingDir: /work
env:
- name: ENVTEST
value: helloworld
volumeMounts:
- name: work
mountPath: /work
- name: image
inputs:
parameters:
- name: image
- name: commitid
container:
name: ''
image: >-
gcr.io/kaniko-project/executor@sha256:f652f28537fa76e8f4f9393de13a064f0206003c451ce2ad6e4359fd5a21acbc
command:
- /kaniko/executor
args:
- '-f'
- /work/Dockerfile
- '-c'
- /work/
- >-
--destination={{inputs.parameters.image}}:{{inputs.parameters.commitid}}
workingDir: /work
env:
- name: envkey
value: envvalue
resources: {}
volumeMounts:
- name: work
mountPath: /work
- name: jenkins-docker-cfg
mountPath: /root
volumes:
- name: jenkins-docker-cfg
projected:
sources:
- secret:
name: regcred
items:
- key: .dockerconfigjson
path: .docker/config.json
- name: deploy
container:
name: ''
image: 'libaibai/git_sed'
command:
- /main.sh
args:
- argoapp.yaml
workingDir: /
env:
- name: GIT_URL
value: https://zl86790:75755618a@github.com/zl86790/argogithubsource_cd.git
- name: REPO
value: argogithubsource_cd
volumeMounts:
- name: work
mountPath: /work
entrypoint: main
arguments:
parameters:
- name: repo
value: 'https://username:password@github.com/zl86790/argogithubsource.git'
- name: branch
value: master
- name: image
value: libaibai/argogolanghelloworld
volumeClaimTemplates:
- metadata:
name: work
creationTimestamp: null
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: local-path
status: {}
ttlStrategy:
secondsAfterCompletion: 1800
secondsAfterSuccess: 1800
secondsAfterFailure: 1800