Tektonを使用してクローン、ビルド、プッシュ

はじめに

CI/CDのTektonを使用して、GitHubからDockerfileを含むレポジトリをクローンし、ビルドしてDockerhubにプッシュする。

環境

  • Windows11(ホストOS)
  • Ubuntu22.04.3(ゲストOS、Virtualbox上に構築)
  • k3s v1.28.6+k3s2
  • CRI-O v1.26.0-rc.0-k3s1

必要なKubernetesリソースの作成

Secret, ServiceAccount作成

Dockerhubに試しにプッシュする。 DockerhubへアクセスができるようにSecret, ServiceAccountを作成する。

アクセストークンを取得

こちらを参考にアクセストークンを取得する。

matsuand.github.io

Secret, ServiceAccount作成

次にこのアクセストークンを使ってSecretを作成する。 username、取得したアクセストークンは適切に設定する。

#ログイン
YOUR_DOCKERHUB_USERNAME=<username>
YOUR_DOCKERHUB_ACCESS_TOKEN=<取得したアクセストークン>
REGISTRY=registry-1.docker.io
$ buildah login -u $YOUR_DOCKERHUB_USERNAME -p $YOUR_DOCKERHUB_ACCESS_TOKEN $REGISTRY
Login Succeeded!

https://github.com/containers/buildah/blob/540a73296f945aacbfaed40fa98e2fe86c7e52ac/docs/buildah-login.1.md?plain=1#L15 を確認すると、defaultでは、/run/user/$UID/containers/auth.jsonに認証情報が保持される。 これを使って、KubernetseのSecretを作成する。

$ AUTH_PATH=/run/user/$UID/containers/auth.json
$ kubectl create secret generic dockerhub-cred \
    --from-file=.dockerconfigjson=$AUTH_PATH \
    --type=kubernetes.io/dockerconfigjson
secret/dockerhub-cred created

#確認
$ kubectl get secret
NAME             TYPE                             DATA   AGE
dockerhub-cred   kubernetes.io/dockerconfigjson   1      7m34s

$ kubectl describe secret dockerhub-cred
Name:         dockerhub-cred
Namespace:    default
Labels:       <none>
Annotations:  <none>

Type:  kubernetes.io/dockerconfigjson

Data
====
.dockerconfigjson:  124 bytes

次にこれに紐づくServiceAccountを設定する。

$ cat > sa.yaml << EOF
apiVersion: v1
kind: ServiceAccount
metadata:
  name: my-tekton-pipeline
secrets:
  - name: dockerhub-cred
EOF

#deploy
$ kubectl apply -f sa.yaml
serviceaccount/my-tekton-pipeline created

#確認
$ kubectl get sa
NAME                 SECRETS   AGE
my-tekton-pipeline   1         9s

task, pipeline, pipelinerunの作成

github.com

こちらのTektonのcatalogのgit-clone、buildahを使用する。

#deploy
$ git clone https://github.com/tektoncd/catalog.git
$ kubectl apply -f catalog/task/git-clone/0.9/git-clone.yaml
$ kubectl apply -f catalog/task/buildah/0.7/buildah.yaml 

#確認
$ kubectl get task
NAME          AGE
git-clone     5d11h
buildah       105m

次にpipelineとpipelinerunを作成する。

$ cat build-image-pipeline.yaml << EOF
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: build-image-pipeline
spec:
  workspaces: 
    - name: shared-workspace
  params:
    - name: git-url
      type: string
    - name: git-revision
      type: string
      default: "master"
    - name: image
      type: string
  tasks:
    - name: fetch-repository
      taskRef:
        name: git-clone
      workspaces:
        - name: output
          workspace: shared-workspace
      params:
        - name: url
          value: $(params.git-url)
        - name: deleteExisting
          value: "true"
        - name: revision
          value: $(params.git-revision)
    - name: build-push-image
      taskRef:
        name: buildah
      params:
        - name: IMAGE
          value: $(params.image)
        - name: DOCKERFILE
          value: "Dockerfile"
        - name: CONTEXT
          value: "$(workspaces.source.path)"
      workspaces:
        - name: source
          workspace: shared-workspace
      runAfter:
        - fetch-repository
EOF

$ cat > build-image-pipeline-run.yaml << EOF
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
  name: build-image-pipeline-run
spec:
  pipelineRef:
    name: build-image-pipeline
  params: 
    - name: git-url
      value: https://github.com/ncskier/myapp
    - name: image
      value: mosuke5/tekton-practice:from-pipeline
  serviceAccountName: my-tekton-pipeline
  workspaces: 
    - name: shared-workspace
      volumeClaimTemplate:
        spec:
          accessModes:
            - ReadWriteOnce
          resources:
            requests:
              storage: 1Gi
EOF

pipelinerunの実行

pipelineruを実行し、動作を確認する。

#pipelineruの実行
$ kubectl apply -f build-image-pipeline-run.yaml

#確認
$ kubectl get pipelinerun
NAME                       SUCCEEDED   REASON      STARTTIME   COMPLETIONTIME
clone-read                 True        Succeeded   5d11h       5d11h
build-image-pipeline-run   True        Succeeded   50s         19s

pipelinerunの実行が成功している。

次に、プッシュ先のDockerhubのレジストリを確認する。

hub.docker.com

こちらにアクセスし、作成したイメージがプッシュされていることを確認した。

参考

blog.mosuke.tech