Tekton CI/CD Part 4: Running test with docker compose on Pipelines

Tekton Pipelines advanced use case

·

3 min read

The example is based on the previous blog - Running tests with Docker Compose on Cloud Build

Based on the example above, we wanted to run Docker Compose on CI, to execute tests with a fresh database, and we are able to run it successfully on Cloud Build.

Let's build the same setup on Tekton

Writing Tetkon Tasks and Pipelines

Tekton runs a Task in the form of a Kubernetes pod, where each Step becomes a running container in the pod. In order to have Docker in Docker, we could use Sidecars, which specifies a list of Containers to run alongside the Steps in your Task

We need two Sidecars in this case

  • docker
  • a Sidecars to start Docker Compose

We could not start Docker Compose in one of our Steps because the underlying container that the Step runs on will get terminated after execution. So the next Steps would not be able to connect services from Docker Compose

And steps to run the test

  • the first step would wait for the connection
  • the second step will run the test go test -v

Full source code available at cncf-demo/hello-testing

apiVersion: tekton.dev/v1beta1
kind: Task
...
spec:
  ...
  steps:
    - name: wait-for-connection
      image: ubuntu
      ...
      script: |
        ./wait-for-it.sh -t 0 localhost:27017 -- echo 'up'
    - name: go-test
      image: golang
      ...
      script: |
        go test -v .
  sidecars:
    # server based on https://hub.tekton.dev/tekton/task/docker-build
    - name: server
      image: docker:dind
     ...
    - name: docker-compose
      image: docker/compose
      ...
      script: |
        cd $(workspaces.source.path)/$(params.dir)
        docker-compose -f docker-compose.yml up
  ...

Install git-clone task if needed

tkn hub install task git-clone

Pipeline

apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: go-test-pipeline
spec:
  workspaces:
    - name: git-source
  tasks:
    - name: fetch-from-git
      taskRef:
        name: git-clone
      params:
        - name: url
          value: https://github.com/WLun001/cncf-demo
      workspaces:
        - name: output
          workspace: git-source
    - name: run-test
      runAfter: [ fetch-from-git ]
      taskRef:
        name: go-test
      workspaces:
        - name: source
          workspace: git-source

Run it

Create PipelineRun

apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
  generateName: go-test-pipeline-run-
spec:
  pipelineRef:
    name: go-test-pipeline
  workspaces:
    - name: git-source
      volumeClaimTemplate:
        spec:
          accessModes:
            - ReadWriteOnce # access mode may affect how you can use this volume in parallel tasks
          resources:
            requests:
              storage: 100m

Run it

kubectl create -f pipeline-run.yaml

Should be able to view it from the dashboard

图片.png

Conclusion

From Part 1 to Part 4 Tekton CI/CD series, we have pretty much covered the most common use case of CI service

From landscape of CI/CD, we can see not a lot of open-source complete CI/CD tools we can use. Apart from Jenkins, Tekton is a great tool to consider if we want to rely on open-source instead of hosted CI service

Tekton still improving from time to time, like dashboard enhancement, experiment features and more

I will write more about Tekton when I have learned or discovered useful use cases to be shared

Did you find this article valuable?

Support Wei Lun by becoming a sponsor. Any amount is appreciated!