LoginSignup
0
4

More than 3 years have passed since last update.

tektonでPrivate Github Repositoryをcloneする

Last updated at Posted at 2020-01-13

目的

tektonのtask-run内でprivateなgithub repositoryを扱います.

Version

  • tekton v0.9

事前準備

はじめにtutorialにあるhello worldを準備します

apiVersion: tekton.dev/v1alpha1
kind: Task
metadata:
  name: echo-hello-world
spec:
  steps:
    - name: echo
      image: ubuntu
      command:
        - echo
      args:
        - "hello world"
---
apiVersion: tekton.dev/v1alpha1
kind: TaskRun
metadata:
  name: echo-hello-world-task-run
spec:
  taskRef:
    name: echo-hello-world

やってみる

今回はSSH Keyを使用してPrivate Github Repositoryをclonseします(参照: pipeline/auth).
そのためtaskを書く前にkubernetes上でSecretとService Accountを作成する必要があります.

Secretには,ssh-privatekeyknown_hosts をbase64にしたものをdataとして設定します.
ssh-privatekeyに関しては適宜作成,known_hostに関しては下記コマンドで取得できます

$ ssh-keyscan github.com | base64

作成したbase64の文字列をyamlファイルに保存します.

# ref: https://github.com/tektoncd/pipeline/blob/master/docs/auth.md#ssh-authentication-git
apiVersion: v1
kind: Secret
metadata:
  name: ssh-key
  annotations:
    tekton.dev/git-0: github.com # Described below
type: kubernetes.io/ssh-auth
data:
  ssh-privatekey: <base64 encoded>
  known_hosts: <base64 encoded>

実際は,kubesecを使用して暗号化しておくのがbetterです.

apiVersion: v1
data:
  known_hosts: mTDLhG7c...
  ssh-privatekey: QIg2T...
kind: Secret
metadata:
  annotations:
    tekton.dev/git-0: github.com
  name: github-ssh-key
  namespace: tekton-playground
type: kubernetes.io/ssh-auth
# kubesec:v:3
# kubesec:gcp:projects...

これらをapplyします

$ kubectl apply -f <FILE>
$ kubesec decrypt <FILE>  | kubectl apply -f -  # kubesecを使ってる場合

ServiceAccountも作成します

apiVersion: v1
kind: ServiceAccount
metadata:
  name: github-account
secrets:
- name: ssh-key

Secret類が用意できたので,TaskRun内で使用していきます.
TaskRun内で使用するために,PipelineResourceの作成,TaskにInputを追加,TaskRunにてInputするResourceの指定を行います.

はじめにPipelineResourceを作成します
今回はSSH Keyを使って認証を行うためgit@から始まるURLを指定する
なおPublicなGithub Repositoryならhttpsから始まるURLでも大丈夫です.

apiVersion: tekton.dev/v1alpha1
kind: PipelineResource
metadata:
  name: private-repository
spec:
  type: git
  params:
  - name: url
    value: git@github.com:<Private Repository>.git
  - name: revision
    value: master

次にTaskにInputを追加,TaskRunにてInputするResourceの指定します

apiVersion: tekton.dev/v1alpha1
kind: Task
metadata:
  name: echo-hello-world
spec:
  inputs:
    resources:
    - name: repository
      type: git
  steps:
    - name: echo
      image: ubuntu
      command:
        - echo
      args:
        - "hello world"
---
apiVersion: tekton.dev/v1alpha1
kind: TaskRun
metadata:
  name: echo-hello-world-task-run
spec:
  serviceAccountName: github-account
  taskRef:
    name: echo-hello-world
  inputs:
    resources:
    - name: repository
      resourceRef:
        name: private-repository

以上で完了です.

$ kubectl apply -f <task file>

で確認してみてください

まとめ

tektonでPrivate Github Repositoryをcloneする方法をまとめました.

0
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
4