Tekton - Create a Pipeline Test+Build+Deploy

Prerequisites

Nodejs express

Java Sprinboot

Java Microprofile

Review the existing Pipelines and Tasks

With kabanero, every collection comes with a default build task, deploy task and build deploy pipeline.

  • Run this command to see the available pipelines.

oc get pipeline -n kabanero

You will see something similar to this.

$ oc get pipeline -n kabanero
NAME                                           AGE
java-microprofile-build-deploy-pipeline        15d
java-spring-boot2-build-deploy-pipeline        15d
nodejs-build-deploy-pipeline                   15d
nodejs-express-build-deploy-pipeline           15d
nodejs-loopback-build-deploy-pipeline          15d
pipeline0                                      15d

You can also access them on the tekton dashboard.

sb lab1 tekton home
  • To see the available tasks, use this command.

oc get task -n kabanero

You will see something similar to this.

$ oc get task -n kabanero
NAME                             AGE
collections-build-task           13d
java-microprofile-build-task     15d
java-microprofile-deploy-task    15d
java-spring-boot2-build-task     15d
java-spring-boot2-deploy-task    15d
monitor-result-task              15d
nodejs-build-task                15d
nodejs-deploy-task               15d
nodejs-express-build-task        15d
nodejs-express-deploy-task       15d
nodejs-loopback-build-task       15d
nodejs-loopback-deploy-task      15d
pipeline0-task                   15d

Let us now see how to define more tasks or create new pipelines for appsody sample applications.

Deploy the example pipelines

If you already reviewed this guide and just want to deploy the tasks and pipelines discussed here you can deploy them with the following command

oc create -n kabanero -f https://raw.githubusercontent.com/ibm-cloud-architecture/Learning-Kabanero-101/master/pipelines/test-pipelines.yaml

Nodejs express

Before defining a new task or pipeline, make sure, you do the following steps.

  • Create a Nodejs express appsody sample application.

  • Push the code to a github repo.

  • Create a github access token.

  • Access tekton dashboard.

If you did not perform any of the above steps, go to the links provided in the prerequisites.

Custom Task

Let us define a new test task.

  • To do this, create test-task.yaml as follows.

Do not name your task too long or you will get errors about Kubernetes resources has invalid names. Keep task and pipeline names less than 40 characters long.
apiVersion: tekton.dev/v1alpha1
kind: Task
metadata:
  name: nodejs-express-test-task
spec:
  inputs:
    resources:
      - name: git-source
        type: git
  steps:
    - name: npm-test
      image: kabanero/nodejs-express:0.2
      workingDir: /project
      command:
        - /bin/bash
      args:
        - -c
        - |
          set -e
          cp -a ${inputs.resources.git-source.path}/* $APPSODY_WATCH_DIR
          # perform npm install
          $APPSODY_INSTALL
          # run npm test on project and user-app
          delimiter=" && "
          s=$APPSODY_TEST$delimiter
          array=();
          while [[ $s ]]; do
              array+=( "${s%%"$delimiter"*}" );
              s=${s#*"$delimiter"};
          done;
          for i in "${array[@]}"
          do
            echo executing $i
            $i
          done
  • Once you create this yaml, create a task in kabanero namespace as follows.

oc create -f test-task.yaml -n kabanero

Once you created it successfully, you will see something like below.

$ oc create -f test-task.yaml -n kabanero
task.tekton.dev/nodejs-express-test-task created
  • Verify it by using the below command.

$ oc get task -n kabanero
NAME                       AGE
nodejs-express-test-task   1h
...

Custom Pipeline

Let us now create a new pipeline that includes the task we created earlier. We are adding the test task we created previously in addition to the default build task and deploy task in the new pipeline.

  • Create nodejs-express-test-build-deploy-pipeline.yaml as follows.

apiVersion: tekton.dev/v1alpha1
kind: Pipeline
metadata:
  name: nodejs-express-test-build-deploy-pipeline
spec:
  resources:
    - name: git-source
      type: git
    - name: docker-image
      type: image
  tasks:
    - name: test-task
      taskRef:
        name: nodejs-express-test-task
      resources:
        inputs:
        - name: git-source
          resource: git-source
    - name: build-task
      taskRef:
        name: nodejs-express-build-task
      runAfter: [test-task]
      resources:
        inputs:
        - name: git-source
          resource: git-source
        outputs:
        - name: docker-image
          resource: docker-image
    - name: deploy-task
      taskRef:
        name: nodejs-express-deploy-task
      runAfter: [build-task]
      resources:
        inputs:
        - name: git-source
          resource: git-source
        - name: docker-image
          resource: docker-image
  • Once you create this yaml, create a new pipeline in kabanero namespace as follows.

oc create -f nodejs-express-test-build-deploy-pipeline.yaml -n kabanero

Once you created it successfully, you will see something like below.

$ oc create -f nodejs-express-test-build-deploy-pipeline.yaml -n kabanero
pipeline.tekton.dev/nodejs-express-test-build-deploy-pipeline created
  • Verify it by using the below command.

$ oc get pipeline  -n kabanero
NAME                                           AGE
nodejs-express-test-build-deploy-pipeline      1h
...

Create a webhook

  • Access the tekton dashboard and now you should be able to see the new pipeline in the list.

sb custom pipeline tekton home
  • Configure the github webhook to your repo. Go to Webhooks > Add Webhook and then create the webhook.

sb custom pipeline tekton webhook
  • Verify if it is created successfully.

sb custom pipeline tekton webhooks

For more detailed instructions on how to create webhook, refer Create Tekton webhook for git repo.

Verify the pipeline

  • Make any changes to your app and push it to github.

  • This will trigger the tekton pipleine.

  • Go to the tekton dashboard and access the new pipeline we created.

sb custom pipeline tekton home
  • Wait till the task is completed and then click on the Pipeline Run.

sb custom pipeline tekton pipeline run
  • Once the tasks are all completed, you will see something like below.

sb custom pipeline tekton pipeline run tasks

Java Spring Boot

Before defining a new task or pipeline, make sure, you do the following steps.

  • Create a java springboot appsody sample application.

  • Push the code to a github repo.

  • Create a github access token.

  • Access tekton dashboard.

If you did not perform any of the above steps, go to the links provided in the prerequisites.

Custom Task

Let us define a new test task.

  • To do this, create test-task.yaml as follows.

apiVersion: tekton.dev/v1alpha1
kind: Task
metadata:
  name: java-spring-boot2-test-task
spec:
  inputs:
    resources:
      - name: git-source
        type: git
  steps:
    - name: test
      securityContext:
        privileged: true
      image: kabanero/java-spring-boot2:0.3
      workingdir: ${inputs.resources.git-source.path}
      command: ["/bin/bash", "-c", "$APPSODY_TEST"]
  • Once you create this yaml, create a task in kabanero namespace as follows.

oc create -f test-task.yaml -n kabanero

Once you created it successfully, you will see something like below.

$ oc apply -f test-task.yaml -n kabanero
task.tekton.dev/java-spring-boot2-test-task created
  • Verify it by using the below command.

$ oc get task -n kabanero
NAME                          AGE
java-spring-boot2-test-task   9m

Custom Pipeline

Let us now create a new pipeline that includes the task we created earlier. We are adding the test task we created previously in addition to the default build task and deploy task in the new pipeline.

  • Create java-spring-boot2-build-deploy-pipeline.yaml as follows.

apiVersion: tekton.dev/v1alpha1
kind: Pipeline
metadata:
  name: java-spring-boot2-test-build-deploy-pipeline
spec:
  resources:
    - name: git-source
      type: git
    - name: docker-image
      type: image
  tasks:
    - name: test-task
      taskRef:
        name: java-spring-boot2-test-task
      resources:
        inputs:
        - name: git-source
          resource: git-source
    - name: build-task
      taskRef:
        name: java-spring-boot2-build-task
      runAfter: [test-task]
      resources:
        inputs:
        - name: git-source
          resource: git-source
        outputs:
        - name: docker-image
          resource: docker-image
    - name: deploy-task
      taskRef:
        name: java-spring-boot2-deploy-task
      runAfter: [build-task]
      resources:
        inputs:
        - name: git-source
          resource: git-source
        - name: docker-image
          resource: docker-image
  • Once you create this yaml, create a new pipeline in kabanero namespace as follows.

oc create -f java-spring-boot2-build-deploy-pipeline.yaml -n kabanero

Once you created it successfully, you will see something like below.

$ oc create -f java-spring-boot2-build-deploy-pipeline.yaml -n kabanero
pipeline.tekton.dev/java-spring-boot2-test-build-deploy-pipeline created
  • Verify it by using the below command.

$ oc get pipeline -n kabanero
NAME                                           AGE
java-spring-boot2-test-build-deploy-pipeline   37m

Create a webhook

  • Access the tekton dashboard and now you should be able to see the new pipeline in the list.

sb custom pipeline tekton java sb2
  • Configure the github webhook to your repo. Go to Webhooks > Add Webhook and then create the webhook.

sb custom pipeline tekton webhook java sb2
  • Verify if it is created successfully.

sb custom pipeline tekton webhooks java sb2

For more detailed instructions on how to create webhook, refer Create Tekton webhook for git repo.

Verify the pipeline

  • Make any changes to your app and push it to github.

  • This will trigger the tekton pipleine.

  • Go to the tekton dashboard and access the new pipeline we created.

sb custom pipeline tekton java sb2
  • Wait till the task is completed and then click on the Pipeline Run.

sb custom pipeline task java sb2
  • Once the tasks are all completed, you will see something like below.

sb custom pipeline tekton pipeline run tasks java

Java Microprofile

Before defining a new task or pipeline, make sure, you do the following steps.

  • Create a java microprofile appsody sample application.

  • Push the code to a github repo.

  • Create a github access token.

  • Access tekton dashboard.

If you did not perform any of the above steps, go to the links provided in the prerequisites.

Custom Task

Let us define a new test task.

  • To do this, create test-task.yaml as follows.

apiVersion: tekton.dev/v1alpha1
kind: Task
metadata:
  name: java-microprofile-test-task
spec:
  inputs:
    resources:
      - name: git-source
        type: git
  steps:
    - name: test
      securityContext:
        privileged: true
      image: kabanero/java-microprofile:0.2
      workingdir: ${inputs.resources.git-source.path}
      command: ["/bin/bash", "-c", "$APPSODY_TEST"]
  • Once you create this yaml, create a task in kabanero namespace as follows.

oc create -f test-task.yaml -n kabanero

Once you created it successfully, you will see something like below.

$ oc create -f test-task.yaml -n kabanero
task.tekton.dev/java-microprofile-test-task created
  • Verify it by using the below command.

oc get task -n kabanero
NAME                          AGE
java-microprofile-test-task   23m

Custom Pipeline

Let us now create a new pipeline that includes the task we created earlier. We are adding the test task we created previously in addition to the default build task and deploy task in the new pipeline.

  • Create java-microprofile-test-build-deploy-pipeline.yaml as follows.

apiVersion: tekton.dev/v1alpha1
kind: Pipeline
metadata:
  name: java-microprofile-test-build-deploy-pipeline
spec:
  resources:
    - name: git-source
      type: git
    - name: docker-image
      type: image
  tasks:
    - name: test-task
      taskRef:
        name: java-microprofile-test-task
      resources:
        inputs:
        - name: git-source
          resource: git-source
    - name: build-task
      taskRef:
        name: java-microprofile-build-task
      runAfter: [test-task]
      resources:
        inputs:
        - name: git-source
          resource: git-source
        outputs:
        - name: docker-image
          resource: docker-image
    - name: deploy-task
      taskRef:
        name: java-microprofile-deploy-task
      runAfter: [build-task]
      resources:
        inputs:
        - name: git-source
          resource: git-source
        - name: docker-image
          resource: docker-image
  • Once you create this yaml, create a new pipeline in kabanero namespace as follows.

oc create -f java-microprofile-test-build-deploy-pipeline.yaml -n kabanero

Once you created it successfully, you will see something like below.

$ oc create -f java-microprofile-test-build-deploy-pipeline.yaml -n kabanero
pipeline.tekton.dev/java-microprofile-test-build-deploy-pipeline created
  • Verify it by using the below command.

$ oc get pipeline -n kabanero
NAME                                           AGE
java-microprofile-test-build-deploy-pipeline   27m

Create a webhook

  • Access the tekton dashboard and now you should be able to see the new pipeline in the list.

mp custom pipeline tekton
  • Configure the github webhook to your repo. Go to Webhooks > Add Webhook and then create the webhook.

mp custom pipeline tekton webhook
  • Verify if it is created successfully.

mp custom pipeline tekton webhooks

For more detailed instructions on how to create webhook, refer Create Tekton webhook for git repo.

Verify the pipeline

  • Make any changes to your app and push it to github.

  • This will trigger the tekton pipleine.

  • Go to the tekton dashboard and access the new pipeline we created.

mp custom pipeline tekton
  • Wait till the task is completed and then click on the Pipeline Run.

mp custom pipeline task
  • Once the tasks are all completed, you will see something like below.

mp custom pipeline tekton pipeline run tasks