Yong Wei Lun
Cloud Zone

Cloud Zone

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

Photo by Markus Winkler on Unsplash

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

Tekton Pipelines advanced use case

Yong Wei Lun's photo
Yong Wei Lun
·Apr 2, 2022·

3 min read

Table of contents

  • Writing Tetkon Tasks and Pipelines
  • Run it
  • Conclusion

The example is based on 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 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 run on will get terminated after execution. So the next Steps would not able to connect services from Docker Compose

And steps to run test

  • first step would wait for the connection
  • 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 able to view it from dashboard

图片.png

Conclusion

From Part 1 to Part 4 Tekton CI/CD series, we have pretty much cover 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 tools to consider 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 case to be shared

Did you find this article valuable?

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

Learn more about Hashnode Sponsors
 
Share this