这一章中,我们会添加一个 task,并且使用它来根据commitid更新 github 中的 yaml 文件
这里使用的是 https://github.com/zl86790/tektongolang_dep.git
要部署的镜像是 libaibai/tektongolang:mockup_tag_name
要用来替换 yaml 的镜像是 libaibai/kanikosed
来看部署用的yaml文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: tektongolang
namespace: tektongolang
spec:
selector:
matchLabels:
app: tektongolang
replicas: 2
template:
metadata:
labels:
app: tektongolang
spec:
containers:
- name: tektongolang
image: mockup_tag_name
ports:
- containerPort: 9090
使用下面内容创建一个新镜像
libaibai/kanikosed
Dockerfile
FROM ubuntu
RUN apt update -y && apt install -y git
COPY main.sh /main.sh
CMD [ "/bin/bsh" ]
main.sh
#!/bin/bash
git config --global user.email $USER_EMAIL
git config --global user.name $USER_NAME
dep_repo=$1
dep_yaml=$2
build_repo=$3
build_commitid=$4
echo $USER_NAME
echo $USER_EMAIL
echo $dep_repo
echo $dep_yaml
echo $build_repo
echo $build_commitid
ls $dep_repo
echo "================="
ls $build_repo
cat $build_repo/$build_commitid
cat $dep_repo/$dep_yaml
cat $build_repo/$build_commitid | xargs -i sed -i 's/:[0-9A-Za-z]\{40,40\}$/:{}/' $dep_repo/$dep_yaml
cat $build_repo/$build_commitid | xargs -i sed -i 's/:mockup_tag_name/:{}/' $dep_repo/$dep_yaml
cat $dep_repo/$dep_yaml
cd $dep_repo && git add ./*
cd $dep_repo && cat $build_repo/$build_commitid | xargs -i git commit -m {} ./*
cd $dep_repo && git push origin HEAD:master --force
创建 input resource,关联到部署git仓库
apiVersion: tekton.dev/v1alpha1
kind: PipelineResource
metadata:
name: tektongolang-dep-git
namespace: lizhe
spec:
type: git
params:
- name: revision
value: master
- name: url
value: https://github.com/zl86790/tektongolang_dep.git
我们来创建 sed task
注意这里使用了刚刚创建的 自定义 docker image
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: sed-commitid
namespace: lizhe
spec:
resources:
inputs:
- name: dep-source
type: git
steps:
- name: sed-commitid
image: libaibai/kanikosed
env:
- name: "DOCKER_CONFIG"
value: "/tekton/home/.docker/"
- name: "USER_NAME"
value: "lizhe"
- name: "USER_EMAIL"
value: "lizhe@lizhe.name"
command: ["/bin/bash", "-c", "/main.sh /workspace/dep-source tektongolang_dep.yaml /workspace/git-source commitid.txt"]
workspaces:
- name: build-workspace
description: |
The folder where will be shared among the tasks
optional: false
mountPath: /workspace
修改 pipeline 文件
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
name: pipeline-demo
namespace: lizhe
spec:
resources:
- name: source-repo
type: git
- name: tektongolang-image
type: image
- name: dep-repo
type: git
workspaces:
- name: build-workspace
tasks:
- name: git-commitid
taskRef:
name: get-commitid
resources:
inputs:
- name: git-source
resource: source-repo
workspaces:
- name: build-workspace
workspace: build-workspace
- name: build-img
runAfter: [git-commitid]
taskRef:
name: build-img
resources:
outputs:
- name: builtImage
resource: tektongolang-image
workspaces:
- name: build-workspace
workspace: build-workspace
- name: sed-commitid
runAfter: [build-img]
taskRef:
name: sed-commitid
resources:
inputs:
- name: dep-source
resource: dep-repo
workspaces:
- name: build-workspace
workspace: build-workspace
修改pipeline run
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
name: pipeline-demo-run
namespace: lizhe
spec:
serviceAccountName: build-bot-sa
pipelineRef:
name: pipeline-demo
resources:
- name: source-repo
resourceRef:
name: tektongolang-git
- name: tektongolang-image
resourceRef:
name: tektongolang-image
- name: dep-repo
resourceRef:
name: tektongolang-dep-git
workspaces:
- name: build-workspace # this workspace name must be declared in the Pipeline
volumeClaimTemplate:
spec:
storageClassName: "local-path"
accessModes:
- ReadWriteOnce # access mode may affect how you can use this volume in parallel tasks
resources:
requests:
storage: 1Gi
然后就可以看到 触发了构建
第一步是下载项目代码
然后通过 获得的 commitid 构建docker image
最后根据 commitid 修改 deploy 的 yaml 文件
这里先使用了一个新的 resource git inputs ,然后使用自定义镜像进行sed
项目代码仓库里的 commitid
docker仓库中push的image
最后是 deploy git仓库中的 yaml 文件