Membuat pipeline CI dengan Tekton (di dalam Kubernetes). Bagian 1/2

Pada artikel ini, kita akan membuat pipeline continuous integration (CI) dengan Tekton , framework open source untuk membuat pipeline CI / CD di Kubernetes.







Kami akan menyediakan cluster Kubernetes lokal melalui kind dan menginstal Tekton di atasnya. Setelah itu, kita akan membuat pipeline dua langkah yang akan menjalankan pengujian unit aplikasi, membuat image Docker, dan mendorongnya ke DockerHub.







Ini adalah bagian 1 dari 2 di mana kita akan menginstal Tekton dan membuat tugas yang menjalankan pengujian aplikasi kita. Bagian kedua tersedia di sini .







Membuat cluster k8s



Kami menggunakan kind untuk membuat cluster Kubernetes untuk instalasi Tekton kami:







$ kind create cluster --name tekton
      
      





Menginstal Tekton



Kita dapat menginstal Tekton menggunakan file release.yaml dari repositori tektoncd / pipeline terbaru di GitHub:







$ kubectl apply -f https://storage.googleapis.com/tekton-releases/pipeline/previous/v0.20.1/release.yaml
      
      





Tekton tekton-pipelines. , , .







$ kubectl get pods --namespace tekton-pipelines
NAME                                           READY   STATUS    RESTARTS   AGE
tekton-pipelines-controller-74848c44df-m42gf   1/1     Running   0          20s
tekton-pipelines-webhook-6f764dc8bf-zq44s      1/1     Running   0          19s
      
      





Tekton CLI



, , , kubectl, Tekton. , , .







Homebrew:







$ brew tap tektoncd/tools
$ brew install tektoncd/tools/tektoncd-cli

$ tkn version
Client version: 0.16.0
Pipeline version: v0.20.1
      
      







Tekton (CRD) Kubernetes, . :







  • : , ( CircleCI Job).







  • : ( CircleCI Workflow)







  • PipelineResource: Pipeline (, git tar)









:







  • TaskRun



    :
  • PipelineRun



    :


, , TaskRun



. : PipelineRun



.









Pipeline Go, . , Dockerfile src/



.









git. 01-task-test.yaml :







apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: test
spec:
  resources:
    inputs:
      - name: repo
        type: git
  steps:
    - name: run-test
      image: golang:1.14-alpine
      workingDir: /workspace/repo/src
      command: ["go"]
      args: ["test"]
      
      





resources: , . ( run-test) git tekton , PipelineResource.







02-pipelineresource.yaml:







apiVersion: tekton.dev/v1alpha1
kind: PipelineResource
metadata:
  name: arthurk-tekton-example
spec:
  type: git
  params:
    - name: url
      value: https://github.com/arthurk/tekton-example
    - name: revision
      value: master
      
      





git git /workspace/$input_name



. repo, /workspace/repo



. foobar, /workspace/foobar



.







(steps:



) Docker, . golang Docker, Go .







go test . /workspace/repo



, tekton Go src



. , : /workspace/repo/src



.







(go test



), , (go



) args (test



) YAML.







Task PipelineResource kubectl:







$ kubectl apply -f 01-task-test.yaml
task.tekton.dev/test created

$ kubectl apply -f 02-pipelineresource.yaml
pipelineresource.tekton.dev/arthurk-tekton-example created
      
      







, TaskRun



, (PipelineResource



).







03-taskrun.yaml :







apiVersion: tekton.dev/v1beta1
kind: TaskRun
metadata:
  name: testrun
spec:
  taskRef:
    name: test
  resources:
    inputs:
      - name: repo
        resourceRef:
          name: arthurk-tekton-example
      
      





(taskRef



โ€” test) git tekton-example (resourceRef



โ€” PipelineResource



arthurk-tekton-example



) .







kubectl, Pods TaskRun. Pod Init:0/2



PodInitializing



, :







$ kubectl apply -f 03-taskrun.yaml
pipelineresource.tekton.dev/arthurk-tekton-example created

$ kubectl get pods
NAME                READY   STATUS      RESTARTS   AGE
testrun-pod-pds5z   0/2     Completed   0          4m27s

$ kubectl get taskrun
NAME      SUCCEEDED   REASON      STARTTIME   COMPLETIONTIME
testrun   True        Succeeded   70s         57s
      
      





, . testrun-pod-pds5z



( ).







$ kubectl logs testrun-pod-pds5z --all-containers
{"level":"info","ts":1588477119.3692405,"caller":"git/git.go:136","msg":"Successfully cloned https://github.com/arthurk/tekton-example @ 301aeaa8f7fa6ec01218ba6c5ddf9095b24d5d98 (grafted, HEAD, origin/master) in path /workspace/repo"}
{"level":"info","ts":1588477119.4230678,"caller":"git/git.go:177","msg":"Successfully initialized and updated submodules in path /workspace/repo"}
PASS
ok      _/workspace/repo/src    0.003s
      
      





, . Tekton CLI, , .







Tekton CLI



Tekton CLI .







, TaskRun, , ( test), TaskRun ( ) :







$ tkn task start test --inputresource repo=arthurk-tekton-example --showlog
Taskrun started: test-run-8t46m
Waiting for logs to be available...
[git-source-arthurk-tekton-example-dqjfb] {"level":"info","ts":1588477372.740875,"caller":"git/git.go:136","msg":"Successfully cloned https://github.com/arthurk/tekton-example @ 301aeaa8f7fa6ec01218ba6c5ddf9095b24d5d98 (grafted, HEAD, origin/master) in path /workspace/repo"}
[git-source-arthurk-tekton-example-dqjfb] {"level":"info","ts":1588477372.7954974,"caller":"git/git.go:177","msg":"Successfully initialized and updated submodules in path /workspace/repo"}

[run-test] PASS
[run-test] ok   _/workspace/repo/src    0.006s
      
      







Tekton Kubernetes, , TaskRun YAML, Tekton CLI tkn.







Semua kode contoh tersedia di sini .







Di bagian selanjutnya, kita akan membuat sebuah tugas yang akan menggunakan Kaniko untuk membangun image Docker untuk aplikasi kita dan kemudian mengirimkannya ke DockerHub. Kemudian kita akan membuat pipeline yang akan menjalankan kedua tugas kita secara berurutan (menjalankan pengujian aplikasi, membuat, dan mengirimkan).







Bagian 2 tersedia di sini .








All Articles