Tektonを使ってGitHubのprivateレポジトリからクローン

はじめに

TektonのCatalogを使ってGitHubのprivateレポジトリからクローンする。

環境

  • Ubuntu20.04.1
  • Kubernetes 1.25.5.
  • CRI-O 1.25.2
  • Tekton Pipeline 0.53.3

前提条件

  • ローカルマシンにSSH鍵を保持すること
  • GitHubSSHの公開鍵を登録してあること

構築とクローン

カタログを活用する

カタログの一つであるgit-cloneのインストールする。 カタログを活用することで、Tektonのtaskの利用が容易になる。 今回はversionの0.9を使用する。

$ git clone https://github.com/tektoncd/catalog
$ cd catalog/task/git-clone
$ ls
0.1  0.2  0.3  0.4  0.5  0.6  0.7  0.8  0.9
$ kubectl apply -f 0.9/git-clone.yaml
$ kubectl get task git-clone
NAME                 AGE
git-clone   23h

Secretの作成

github のプライベートリポジトリからCloneするには認証情報を取得する必要があるので、そのSecretと対応するServiceAccountを事前に作成する。

Secret作成に必要な値を取得する。

SecretとServiceAccountを作成する。

#private keyのパスを指定
$ SSH_PRIVATEKEY_PATH=~/.ssh/id_ed25519

#tmpファイル作成
$ ssh-keyscan github.com 2&>/dev/null > tmp.txt

$ cat > secret_sa.yaml << EOF
apiVersion: v1
kind: Secret
metadata:
  name: git-credentials
  annotations:
    tekton.dev/git-0: github.com
type: kubernetes.io/ssh-auth
data:
  ssh-privatekey: $(cat $SSH_PRIVATEKEY_PATH | base64 -w 0) # ... base64-encoded private key ...
  known_hosts: $(cat tmp.txt |base64 -w 0) # ... base64-encoded known_hosts file …
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: tekton-admin
secrets:
  - name: git-credentials
EOF

#tmpファイル削除
$ rm tmp.txt

#作成
$ kubectl apply -f secret_sa.yaml

#確認
$ kubectl get secret,sa
NAME                    TYPE                     DATA   AGE
secret/ssh-key          kubernetes.io/ssh-auth   2      5s

NAME                                SECRETS   AGE
serviceaccount/tekton-admin         1         5s

PVの作成

PVCの動的プロビジョニングをサポートしていないため、手動で作成する。

$ cat > pv.yaml << EOF
apiVersion: v1
kind: PersistentVolume
metadata:
  name: example2
spec:
  capacity:
    storage: 10Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: /home/ubuntu/my-pv
EOF

$ kubectl apply -f pv.yaml

プライベートレポジトリのクローン

task作成

$ cat  > task-clone-read.yaml << "EOF"
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: show-readme
spec:
  description: Read and display README file.
  workspaces:
  - name: source
  steps:
  - name: read
    image: alpine:latest
    script: | 
      #!/usr/bin/env sh
      cat $(workspaces.source.path)/README.md
EOF

pipeline作成

$ cat > pipline-clone-read.yaml << "EOF"
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: clone-read
spec:
  description: | 
    This pipeline clones a git repo, then echoes the README file to the stout.
  params:
  - name: repo-url
    type: string
    description: The git repo URL to clone from.
  workspaces:
  - name: shared-data
    description: | 
      This workspace contains the cloned repo files, so they can be read by the
      next task.
  - name: git-credentials
    description: My ssh credentials
  tasks:
  - name: fetch-source
    taskRef:
      name: git-clone
    workspaces:
    - name: output
      workspace: shared-data
    - name: ssh-directory
      workspace: git-credentials
    params:
    - name: url
      value: $(params.repo-url)
  - name: show-readme
    runAfter: ["fetch-source"]
    taskRef:
      name: show-readme
    workspaces:
    - name: source
      workspace: shared-data
EOF

pipelinerun作成

REPO_URLには、GitHubのURLを記載する。 今回は例として、公開レポジトリのhttps://github.com/tektoncd/catalog を選択した場合を記載する。

$ REPO_URL=git@github.com:tektoncd/catalog.git
$ cat > piplinerun-clone-read.yaml << EOF
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
  name: clone-read-run
spec:
  serviceAccountName: tekton-admin
  pipelineRef:
    name: clone-read
  podTemplate:
    securityContext:
      fsGroup: 65532
  workspaces:
  - name: shared-data
    volumeClaimTemplate:
      spec:
        accessModes:
        - ReadWriteOnce
        resources:
          requests:
            storage: 1Gi
  - name: git-credentials
    secret:
      secretName: git-credentials
  params:
  - name: repo-url
    value: $REPO_URL
EOF

デプロイ

$ kubectl apply -f task-clone-read.yaml
$ kubectl apply -f pipline-clone-read.yaml
$ kubectl apply -f piplinerun-clone-read.yaml

デプロイした内容を確認する

$ kubectl get taskrun,pv,pvc,pod
NAME                                             SUCCEEDED   REASON      STARTTIME   COMPLETIONTIME
taskrun.tekton.dev/clone-read-run-fetch-source   True        Succeeded   13s         7s
taskrun.tekton.dev/clone-read-run-show-readme    Unknown     Running     7s

NAME                        CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                    STORAGECLASS   REASON   AGE
persistentvolume/example2   10Gi       RWO            Retain           Bound    default/pvc-b8db0b2018                           14s

NAME                                   STATUS   VOLUME     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
persistentvolumeclaim/pvc-b8db0b2018   Bound    example2   10Gi       RWO                           13s

NAME                                      READY   STATUS      RESTARTS      AGE
pod/affinity-assistant-615320202e-0       1/1     Running     0             13s
pod/clone-read-run-fetch-source-pod       0/1     Completed   0             13s
pod/clone-read-run-show-readme-pod        1/1     Running     0             7s

$ kubectl logs pod/clone-read-run-show-readme-pod
# Tekton Catalog

**If you want `v1alpha1` resources, you need to go to the
[`v1alpha1`](https://github.com/tektoncd/catalog/tree/v1alpha1)
(省略)

クローンできていることを確認できた

参考

blog.mosuke.tech