Skip to content

Tekton

Prerequisites

Make sure your environment is properly setup.

Follow the instructions here

SetUp

Tekton CLI Installation

  • Tekton CLI is command line utility used to interact with the Tekton resources.

  • Follow the instructions on the tekton CLI github repository https://github.com/tektoncd/cli#installing-tkn

  • For MacOS for example you can use brew

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

  • Verify the Tekton cli
    tkn version
    
  • The command should show a result like:
    $ tkn version
    Client version: 0.10.0
    
  • If you already have the tkn install you can upgrade running
    brew upgrade tektoncd/tools/tektoncd-cli
    

Tekton Pipelines Installation

  • To deploy the Tekton pipelines: oc apply --filename https://raw.githubusercontent.com/ibm-cloud-architecture/learning-cloudnative-101/master/static/yamls/tekton-lab/tekton-operator.yaml
  • Note: It will take few mins for the Tekton pipeline components to be installed, you an watch the status using the command:
    oc get pods -n openshift-operators -w
    
    You can use Ctrl+c to terminate the watch
  • A successful deployment of Tekton pipelines will show the following pods:
    NAME                                         READY   STATUS    RESTARTS   AGE
    openshift-pipelines-operator-9cdbbb854-x9tvs   1/1     Running   0          25s
    

Create Target Namespace

  • Set the environment variable NAMESPACE to tekton-demo, if you open a new terminal remember to set this environment again
    export NAMESPACE=tekton-demo
    
  • Create a the namespace using the variable NAMESPACE
    oc new-project $NAMESPACE
    

Tasks

Task Creation

  • Create the below yaml files.
  • The following snippet shows what a Tekton Task YAML looks like:
  • Create the file task-test.yaml

    apiVersion: tekton.dev/v1beta1
    kind: Task
    metadata:
    name: java-test
    spec:
    params:
        - name: url
        default: https://github.com/ibm-cloud-architecture/cloudnative_sample_app
        - name: revision
        default: master
    steps:
        - name: git-clone
        image: alpine/git
        script: |
            git clone -b $(params.revision) --depth 1 $(params.url) /source
        volumeMounts:
            - name: source
            mountPath: /source
        - name: test
        image: maven:3.3-jdk-8
        workingdir: /source
        script: |
            mvn test
            echo "tests passed with rc=$?"
        volumeMounts:
            - name: m2-repository
            mountPath: /root/.m2
            - name: source
            mountPath: /source
    volumes:
        - name: m2-repository
        emptyDir: {}
        - name: source
        emptyDir: {}
    

  • Each Task has the following:

  • name - the unique name using which the task can be referred
    • name - the name of the parameter
    • description - the description of the parameter
    • default - the default value of parameter
  • Note: The TaskRun or PipelineRun could override the parameter values, if no parameter value is passed then the default value will be used.

  • steps - One or more sub-tasks that will be executed in the defined order. The step has all the attributes like a Pod spec

  • volumes - the task can also mount external volumes using the volumes attribute.
  • The parameters that were part of the spec inputs params can be used in the steps using the notation $(<variable-name>).

Task Deploy

  • The application test task could be created using the command:

    oc apply -f task-test.yaml -n $NAMESPACE
    

  • We will use the Tekton cli to inspect the created resources

    tkn task ls -n $NAMESPACE
    

  • The above command should list one Task as shown below:

    NAME        AGE
    java-test   22 seconds ago
    

TaskRun

  • The TaskRun is used to run a specific task independently. In the following section we will run the build-app task created in the previous step

TaskRun Creation

  • The following snippet shows what a Tekton TaskRun YAML looks like:
  • Create the file taskrun-test.yaml
    apiVersion: tekton.dev/v1beta1
    kind: TaskRun
    metadata:
    generateName: test-task-run-
    spec:
    taskRef:
        name: java-test
    params:
        - name: url
        value: https://github.com/ibm-cloud-architecture/cloudnative_sample_app
    
  • generateName - since the TaskRun can be run many times, in order to have unqiue name across the TaskRun ( helpful when checking the TaskRun history) we use this generateName instead of name. When Kubernetes sees generateName it will generate unquie set of characters and suffix the same to build-app-, similar to how pod names are generated
  • taskRef - this is used to refer to the Task by its name that will be run as part of this TaskRun. In this example we use build-app Task.
  • As described in the earlier section that the Task inputs and outputs could be overridden via TaskRun.
  • params - this are the parameter values that are passed to the task
  • The application test task(java-maven-test) could be run using the command:
    kubectl create -f taskrun-test.yaml -n $NAMESPACE 
    
  • Note - As tasks will use generated name, never use oc apply -f taskrun-test.yaml
  • We will use the Tekton cli to inspect the created resources:

    tkn tr ls -n $NAMESPACE
    
    The above command should list one TaskRun as shown below:
    NAME                       STARTED        DURATION   STATUS
    test-task-run-q6s8c        1 minute ago   ---        Running(Pending)
    
    Note - It will take few seconds for the TaskRun to show status as Running as it needs to download the container images.

  • To check the logs of the Task Run using the tkn:

    tkn tr logs -f --last -n $NAMESPACE
    
    Note - Each task step will be run within a container of its own. The -f or -a allows to tail the logs from all the containers of the task. For more options run tkn tr logs --help

  • If you see the TaskRun status as Failed or Error use the following command to check the reason for error:
    tkn tr describe --last -n $NAMESPACE
    
  • If it is successful, you will see something like below.
    tkn tr ls -n $NAMESPACE
    
    The above command should list one TaskRun as shown below:
    NAME                  STARTED          DURATION     STATUS
    test-task-run   47 seconds ago   34 seconds   Succeeded
    

Creating additional tasks and deploying them

  • Create a Task to build a container image and push to the registry
  • This task will be later used by the pipeline.
  • Download the task file task-buildah.yaml to build the image, push the image to the registy:
  • Create the buildah Task using the file and the command:
    oc apply -f task-buildah.yaml -n $NAMESPACE
    
  • Use the Tekton cli to inspect the created resources
    tkn task ls -n $NAMESPACE
    
  • The above command should list one Task as shown below:

    NAME              AGE
    buildah            4 seconds ago
    java-test         46 minutes ago
    

  • Create an environment variable for location to push the image to be build. Replace NAMESPACE for the dockerhub username, or IBM CR Namespace

    export REGISTRY_SERVER=image-registry.openshift-image-registry.svc:5000
    export IMAGE_URL=${REGISTRY_SERVER}/${NAMESPACE}/cloudnative_sample_app
    echo IMAGE_URL=${IMAGE_URL}
    

  • Lets create a Task Run for buildah Task using the tkn CLI passing the inputs, outputs and service account.

    tkn task start buildah --showlog \
    -p image=${IMAGE_URL} \
    -p url=https://github.com/ibm-cloud-architecture/cloudnative_sample_app \
    -s pipeline \
    -n $NAMESPACE
    
    The task will start and logs will start printing automatically
    Taskrun started: buildah-run-vvrg2
    Waiting for logs to be available...
    

  • Verify the status of the Task Run

    tkn tr ls -n $NAMESPACE
    
    Output should look like this
    NAME                  STARTED          DURATION     STATUS
    buildah-run-zbsrv      2 minutes ago    1 minute     Succeeded
    

  • To clean up all Pods associated with all Task Runs, delete all the task runs resources
    oc delete taskrun --all -n $NAMESPACE
    
  • (Optional) Instead of starting the Task via tkn task start you could also use yaml TaskRun, create a file taskrun.yaml
    apiVersion: tekton.dev/v1beta1
    kind: TaskRun
    metadata:
    generateName: buildah-task-run-
    spec:
    serviceAccountName: pipeline
    taskRef:
        name: buildah
    params:
        - name: url
        value: https://github.com/ibm-cloud-architecture/cloudnative_sample_app
        - name: image
        value: image-registry.openshift-image-registry.svc:5000/tekton-demo/cloudnative_sample_app
    
    Then create the TaskRun with
    oc create -f taskrun-buildah.yaml -n $NAMESPACE
    
    Follow the logs with:
    tkn tr logs -f -n $NAMESPACE
    

Pipelines

Pipeline Creation

  • Pipelines allows to start multiple Tasks, in parallel or in a certain order

  • Create the file pipeline.yaml, the Pipeline contains two Tasks

    apiVersion: tekton.dev/v1beta1
    kind: Pipeline
    metadata:
    name: test-build
    spec:
    params:
        - name: repo-url
        default: https://github.com/ibm-cloud-architecture/cloudnative_sample_app
        - name: revision
        default: master
        - name: image-server
        default: image-registry.openshift-image-registry.svc:5000
        - name: image-namespace
        default: tekton-demo
        - name: image-repository
        default: cloudnative_sample_app
    tasks:
        - name: test
        taskRef:
            name: java-test
        params:
            - name: url
            value: $(params.repo-url)
            - name: revision
            value: $(params.revision)
        - name: build
        runAfter: [test]
        taskRef:
            name: buildah
        params:
            - name: image
            value: $(params.image-server)/$(params.image-namespace)/$(params.image-repository)
            - name: url
            value: $(params.repo-url)
            - name: revision
            value: $(params.revision)
    

  • Pipeline defines a list of Tasks to execute in order, while also indicating if any outputs should be used as inputs of a following Task by using the from field and also indicating the order of executing (using the runAfter and from fields). The same variable substitution you used in Tasks is also available in a Pipeline.

  • Create the Pipeline using the command:
    oc apply -f pipeline.yaml -n $NAMESPACE
    
  • Use the Tekton cli to inspect the created resources
    tkn pipeline ls -n $NAMESPACE
    
    The above command should list one Pipeline as shown below:
    NAME              AGE              LAST RUN   STARTED   DURATION   STATUS
    test-build-push   31 seconds ago   ---        ---       ---        ---
    

PipelineRun

PipelineRun Creation

  • To execute the Tasks in the Pipeline, you must create a PipelineRun. Creation of a PipelineRun will trigger the creation of TaskRuns for each Task in your pipeline.
  • Create the file pipelinerun.yaml
    apiVersion: tekton.dev/v1alpha1
    kind: PipelineRun
    metadata:
    generateName: test-build-run-
    spec:
    serviceAccountName: pipeline
    pipelineRef:
        name: test-build
    params:
        - name: image-server
        value: image-registry.openshift-image-registry.svc:5000
        - name: image-namespace
        value: tekton-demo
    
    serviceAccount - it is always recommended to have a service account associated with PipelineRun, which can then be used to define fine grained roles.
  • Create the PipelineRun using the command:
    oc create -f pipelinerun.yaml -n $NAMESPACE
    
  • We will use the Tekton cli to inspect the created resources

    tkn pipelinerun ls -n $NAMESPACE
    

  • The above command should list one PipelineRun as shown below:

    NAME                        STARTED         DURATION   STATUS
    test-build-push-run-c7zgv   8 seconds ago   ---        Running
    

  • Wait for few minutes for your pipeline to complete all the tasks. If it is successful, you will see something like below.

    tkn pipeline ls -n $NAMESPACE
    
    NAME              AGE              LAST RUN                    STARTED         DURATION    STATUS
    test-build-push   33 minutes ago   test-build-push-run-c7zgv   2 minutes ago   2 minutes   Succeeded
    

  • Run again the pipeline ls command

    tkn pipelinerun ls -n $NAMESPACE
    
    NAME                        STARTED         DURATION    STATUS
    test-build-push-run-c7zgv   2 minutes ago   2 minutes   Succeeded
    
    If it is successful, go to your container registry account and verify if you have the cloudnative_sample_app image pushed.

  • (Optional) Run the pipeline again using the tkn CLI

    tkn pipeline start test-build --showlog \
    -s pipeline \
    -n $NAMESPACE
    

  • (Optional) Re-run the pipeline using last pipelinerun values
    tkn pipeline start test-build-push --last -n $NAMESPACE
    

Deploy Application

  • Create a deployment
    oc create deployment cloudnative --image=${IMAGE_URL} -n $NAMESPACE
    
  • Verify if the pods are running:
    oc get pods -l app=cloudnative -n $NAMESPACE
    
  • Expose the deployment as a service
    oc expose deployment cloudnative --port=9080 -n $NAMESPACE
    
  • Expose the service as a route
    oc expose service cloudnative -n $NAMESPACE
    
  • Now access the compose the URL of the App using IP and NodePort
    export APP_URL="$(oc get route cloudnative --template 'http://{{.spec.host}}')/greeting?name=Carlos"
    echo APP_URL=$APP_URL
    
    http://cloudnative-tekton-demo.apps-crc.testing/greeting?name=Carlos
    
  • Now access the app from terminal or browser
    curl $APP_URL
    
    Output should be
    {"id":4,"content":"Welcome to Cloudnative bootcamp !!! Hello, Carlos :)"}
    
    open $APP_URL
    

Prerequisites

Make sure your environment is properly setup.

Follow the instructions here

SetUp

Tekton CLI Installation

  • Tekton CLI is command line utility used to interact with the Tekton resources.

  • Follow the instructions on the tekton CLI github repository https://github.com/tektoncd/cli#installing-tkn

  • For MacOS for example you can use brew

    brew install tektoncd-cli
    

  • Verify the Tekton cli
    tkn version
    
  • The command should show a result like:
    $ tkn version
    Client version: 0.10.0
    
  • If you already have the tkn install you can upgrade running
    brew upgrade tektoncd/tools/tektoncd-cli
    

Tekton Pipelines Installation

  • To deploy the Tekton pipelines:
    kubectl apply --filename https://storage.googleapis.com/tekton-releases/pipeline/previous/v0.13.2/release.yaml
    
  • Note: It will take few mins for the Tekton pipeline components to be installed, you an watch the status using the command:
    kubectl get pods -n tekton-pipelines -w
    
    You can use Ctrl+c to terminate the watch
  • A successful deployment of Tekton pipelines will show the following pods:
    NAME                                         READY   STATUS    RESTARTS   AGE
    tekton-pipelines-controller-9b8cccff-j6hvr   1/1     Running   0          2m33s
    tekton-pipelines-webhook-6fc9d4d9b6-kpkp7    1/1     Running   0          2m33s
    

Tekton Dashboard Installation (Optional)

  • To deploy the Tekton dashboard:
    kubectl apply --filename https://github.com/tektoncd/dashboard/releases/download/v0.7.0/tekton-dashboard-release.yaml
    
  • Note: It will take few mins for the Tekton dashboard components to be installed, you an watch the status using the command:
    kubectl get pods -n tekton-pipelines -w
    
    You can use Ctrl+c to terminate the watch
  • A successful deployment of Tekton pipelines will show the following pods:
    NAME                                           READY   STATUS    RESTARTS   AGE
    tekton-dashboard-59c7fbf49f-79f7q              1/1     Running   0          50s
    tekton-pipelines-controller-6b7f7cf7d8-r65ps   1/1     Running   0          15m
    tekton-pipelines-webhook-7bbd8fcc45-sfgxs      1/1     Running   0          15m
    
  • Access the dashboard as follows:
    kubectl --namespace tekton-pipelines port-forward svc/tekton-dashboard 9097:9097
    
    You can access the web UI at http://localhost:9097 .

Create Target Namespace

  • Set the environment variable NAMESPACE to tekton-demo, if you open a new terminal remember to set this environment again
    export NAMESPACE=tekton-demo
    
  • Create a the namespace using the variable NAMESPACE
    kubectl create namespace $NAMESPACE
    

Tasks

Task Creation

  • Create the below yaml files.
  • The following snippet shows what a Tekton Task YAML looks like:
  • Create the file task-test.yaml

    apiVersion: tekton.dev/v1beta1
    kind: Task
    metadata:
    name: java-test
    spec:
    params:
        - name: url
        - name: revision
        default: master
    steps:
        - name: git-clone
        image: alpine/git
        script: |
            git clone -b $(params.revision) --depth 1 $(params.url) /source
        volumeMounts:
            - name: source
            mountPath: /source
        - name: test
        image: maven:3.3-jdk-8
        workingdir: /source
        script: |
            mvn test
            echo "tests passed with rc=$?"
        volumeMounts:
            - name: m2-repository
            mountPath: /root/.m2
            - name: source
            mountPath: /source
    volumes:
        - name: m2-repository
        emptyDir: {}
        - name: source
        emptyDir: {}
    

  • Each Task has the following:

  • name - the unique name using which the task can be referred
    • name - the name of the parameter
    • description - the description of the parameter
    • default - the default value of parameter
  • Note: The TaskRun or PipelineRun could override the parameter values, if no parameter value is passed then the default value will be used.

  • steps - One or more sub-tasks that will be executed in the defined order. The step has all the attributes like a Pod spec

  • volumes - the task can also mount external volumes using the volumes attribute.
  • The parameters that were part of the spec inputs params can be used in the steps using the notation $(<variable-name>).

Task Deploy

  • The application test task could be created using the command:

    kubectl apply -f task-test.yaml -n $NAMESPACE
    

  • We will use the Tekton cli to inspect the created resources

    tkn task ls -n $NAMESPACE
    

  • The above command should list one Task as shown below:

    NAME        AGE
    java-test   22 seconds ago
    

TaskRun

  • The TaskRun is used to run a specific task independently. In the following section we will run the build-app task created in the previous step

TaskRun Creation

  • The following snippet shows what a Tekton TaskRun YAML looks like:
  • Create the file taskrun-test.yaml
    apiVersion: tekton.dev/v1beta1
    kind: TaskRun
    metadata:
    generateName: test-task-run-
    spec:
    taskRef:
        name: java-test
    params:
        - name: url
        value: https://github.com/ibm-cloud-architecture/cloudnative_sample_app
    
  • generateName - since the TaskRun can be run many times, in order to have unqiue name across the TaskRun ( helpful when checking the TaskRun history) we use this generateName instead of name. When Kubernetes sees generateName it will generate unquie set of characters and suffix the same to build-app-, similar to how pod names are generated
  • taskRef - this is used to refer to the Task by its name that will be run as part of this TaskRun. In this example we use build-app Task.
  • As described in the earlier section that the Task inputs and outputs could be overridden via TaskRun.
  • params - this are the parameter values that are passed to the task
  • The application test task(java-maven-test) could be run using the command:
    kubectl create -n $NAMESPACE -f taskrun-test.yaml
    
  • Note - As tasks will use generated name, never use kubectl apply -f taskrun-test.yaml
  • We will use the Tekton cli to inspect the created resources:

    tkn tr ls -n $NAMESPACE
    
    The above command should list one TaskRun as shown below:
    NAME                       STARTED        DURATION   STATUS
    test-task-run-q6s8c        1 minute ago   ---        Running(Pending)
    
    Note - It will take few seconds for the TaskRun to show status as Running as it needs to download the container images.

  • To check the logs of the Task Run using the tkn:

    tkn tr logs -f -a -n $NAMESPACE
    
    Note - Each task step will be run within a container of its own. The -f or -a allows to tail the logs from all the containers of the task. For more options run tkn tr logs --help

  • If you see the TaskRun status as Failed or Error use the following command to check the reason for error:
    tkn tr describe --last -n $NAMESPACE
    
  • If it is successful, you will see something like below.
    tkn tr ls -n $NAMESPACE
    
    The above command should list one TaskRun as shown below:
    NAME                  STARTED          DURATION     STATUS
    test-task-run-q6s8c   47 seconds ago   34 seconds   Succeeded
    

Creating additional tasks and deploying them

  • Create a Task to build a container image and push to the registry
  • This task will be later used by the pipeline.
  • Download the task file task-buildah.yaml to build the image, push the image to the registy:
  • Create task buildah
  • Create the buildah Task using the file and the command:
    kubectl apply -f task-buildah.yaml -n $NAMESPACE
    
  • Use the Tekton cli to inspect the created resources
    tkn task ls -n $NAMESPACE
    
  • The above command should list one Task as shown below:

    NAME              AGE
    buildah            4 seconds ago
    java-test         46 minutes ago
    

  • To access the container registry, create the required secret as follows.

  • If using IBM Container registry use iamapikey for REGISTRY_USERNAME and get a API Key for REGISTRY_PASSWORD, use the domain name for the region IBM CR service like us.icr.io
  • Create the environment variables to be use, replace with real values and include the single quotes:

    export REGISTRY_USERNAME='<REGISTRY_USERNAME>'
    
    export REGISTRY_PASSWORD='<REGISTRY_PASSWORD>'
    
    export REGISTRY_SERVER='docker.io'
    

  • Run the following command to create a secret regcred in the namespace NAMESPACE

    kubectl create secret docker-registry regcred \
    --docker-server=${REGISTRY_SERVER} \
    --docker-username=${REGISTRY_USERNAME} \
    --docker-password=${REGISTRY_PASSWORD} \
    -n ${NAMESPACE}
    

    Before creating, replace the values as mentioned above. Note: If your docker password contains special characters in it, please enclose the password in double quotes or place an escape character before each special character.

    • (Optional) Only if you have problems with the credentials you can recreate it, but you have to deleted first
      kubectl delete secret regcred -n $NAMESPACE
      
  • Before we run the Task using TaskRun let us create the Kubernetes service account and attach the needed permissions to the service account, the following Kubernetes resource defines a service account called pipeline in namespace $NAMESPACE who will have administrative role within the $NAMESPACE namespace.

  • Create the file sa.yaml
    apiVersion: v1
    kind: ServiceAccount
    metadata:
    name: pipeline
    secrets:
    - name: regcred
    
  • Create sa role as follows:

    kubectl create -n $NAMESPACE -f sa.yaml
    

  • Create an environment variable for location to push the image to be build. Replace NAMESPACE for the dockerhub username, or IBM CR Namespace

    export NAMESPACE='<REGISTRY_NAMESPACE>'
    export IMAGE_URL=${REGISTRY_SERVER}/${REGISTRY_NAMESPACE}/cloudnative_sample_app
    

  • Lets create a Task Run for buildah Task using the tkn CLI passing the inputs, outputs and service account.

    tkn task start buildah --showlog \
    -p url=https://github.com/ibm-cloud-architecture/cloudnative_sample_app \
    -p image=${IMAGE_URL} \
    -s pipeline \
    -n $NAMESPACE
    

    The task will start and logs will start printing automatically

    Taskrun started: buildah-run-vvrg2
    Waiting for logs to be available...
    

  • Verify the status of the Task Run

    tkn tr ls -n $NAMESPACE
    
    Output should look like this
    NAME                  STARTED          DURATION     STATUS
    buildah-run-zbsrv      2 minutes ago    1 minute     Succeeded
    

  • To clean up all Pods associated with all Task Runs, delete all the task runs resources
    kubectl delete taskrun --all -n $NAMESPACE
    
  • (Optional) Instead of starting the Task via tkn task start you could also use yaml TaskRun, create a file taskrun-buildah.yaml Make sure update value for parameter image with your registry info.
    apiVersion: tekton.dev/v1beta1
    kind: TaskRun
    metadata:
    generateName: buildah-task-run-
    spec:
    serviceAccountName: pipeline
    taskRef:
        name: buildah
    params:
        - name: url
        value: https://github.com/ibm-cloud-architecture/cloudnative_sample_app
        - name: image
        value: docker.io/csantanapr/cloudnative_sample_app
    
    Then create the TaskRun with generateName
    kubectl create -f taskrun-buildah.yaml -n $NAMESPACE
    
    Follow the logs with:
    tkn tr logs --last -f -n $NAMESPACE
    

Pipelines

Pipeline Creation

  • Pipelines allows to start multiple Tasks, in parallel or in a certain order

  • Create the file pipeline.yaml, the Pipeline contains two Tasks

    apiVersion: tekton.dev/v1beta1
    kind: Pipeline
    metadata:
    name: test-build
    spec:
    params:
        - name: repo-url
        default: https://github.com/ibm-cloud-architecture/cloudnative_sample_app
        - name: revision
        default: master
        - name: image-server
        - name: image-namespace
        - name: image-repository
        default: cloudnative_sample_app
    tasks:
        - name: test
        taskRef:
            name: java-test
        params:
            - name: url
            value: $(params.repo-url)
            - name: revision
            value: $(params.revision)
        - name: build
        runAfter: [test]
        taskRef:
            name: buildah
        params:
            - name: image
            value: $(params.image-server)/$(params.image-namespace)/$(params.image-repository)
            - name: url
            value: $(params.repo-url)
            - name: revision
            value: $(params.revision)
    

  • Pipeline defines a list of Tasks to execute in order, while also indicating if any outputs should be used as inputs of a following Task by using the from field and also indicating the order of executing (using the runAfter and from fields). The same variable substitution you used in Tasks is also available in a Pipeline.

  • Create the Pipeline using the command:
    kubectl apply -f pipeline.yaml -n $NAMESPACE
    
  • Use the Tekton cli to inspect the created resources
    tkn pipeline ls -n $NAMESPACE
    
    The above command should list one Pipeline as shown below:
    NAME              AGE              LAST RUN   STARTED   DURATION   STATUS
    test-build-push   31 seconds ago   ---        ---       ---        ---
    

PipelineRun

PipelineRun Creation

  • To execute the Tasks in the Pipeline, you must create a PipelineRun. Creation of a PipelineRun will trigger the creation of TaskRuns for each Task in your pipeline.
  • Create the file pipelinerun.yaml replace the values for image-server and image-namespace with your own.
    apiVersion: tekton.dev/v1beta1
    kind: PipelineRun
    metadata:
    generateName: test-build-run-
    spec:
    serviceAccountName: pipeline
    pipelineRef:
        name: test-build
    params:
        - name: image-server
        value: us.icr.io
        - name: image-namespace
        value: student01-registry
    
    serviceAccount - it is always recommended to have a service account associated with PipelineRun, which can then be used to define fine grained roles. Replace the values for image-server and image-namespace
  • Create the PipelineRun using the command:
    kubectl create -f pipelinerun.yaml -n $NAMESPACE
    
  • We will use the Tekton cli to inspect the created resources

    tkn pipelinerun ls -n $NAMESPACE
    

  • The above command should list one PipelineRun as shown below:

    NAME                        STARTED         DURATION   STATUS
    test-build-push-run-c7zgv   8 seconds ago   ---        Running
    

  • Get the logs of the pipeline using the following command

    tkn pipelinerun logs --last -f
    

  • Wait for few minutes for your pipeline to complete all the tasks. If it is successful, you will see something like below.
    tkn pipeline ls -n $NAMESPACE
    
    NAME              AGE              LAST RUN                    STARTED         DURATION    STATUS
    test-build-push   33 minutes ago   test-build-push-run-c7zgv   2 minutes ago   2 minutes   Succeeded
    
  • Run again the pipeline ls command

    tkn pipelinerun ls -n $NAMESPACE
    
    NAME                        STARTED         DURATION    STATUS
    test-build-push-run-c7zgv   2 minutes ago   2 minutes   Succeeded
    
    If it is successful, go to your container registry account and verify if you have the cloudnative_sample_app image pushed.

  • (Optional) Run the pipeline again using the tkn CLI

    tkn pipeline start test-build --last -n $NAMESPACE
    

  • (Optional) Re-run the pipeline using last pipelinerun values
    tkn pipeline start test-build-push --last -f -n $NAMESPACE
    

Deploy Application

  • Add the imagePullSecret to the default Service Account
    kubectl patch sa default -p '"imagePullSecrets": [{"name": "regcred" }]' -n $NAMESPACE
    
  • Create a deployment
    kubectl create deployment cloudnative --image=${IMAGE_URL} -n $NAMESPACE
    
  • Verify if the pods are running:
    kubectl get pods -l app=cloudnative -n $NAMESPACE
    
  • Expose the deployment
    kubectl expose deployment cloudnative --type=NodePort --port=9080 -n $NAMESPACE
    
  • Now access the compose the URL of the App using IP and NodePort
    export APP_EXTERNAL_IP=$(kubectl get nodes -o jsonpath='{.items[0].status.addresses[?(@.type=="ExternalIP")].address}')
    export APP_NODEPORT=$(kubectl get svc cloudnative -n $NAMESPACE -o jsonpath='{.spec.ports[0].nodePort}')
    export APP_URL="http://${APP_EXTERNAL_IP}:${APP_NODEPORT}/greeting?name=Carlos"
    echo APP_URL=$APP_URL
    
    http://192.168.64.30:30632//greeting?name=Carlos
    
  • Now access the app from terminal or browser
    curl $APP_URL
    
    Output should be
    {"id":4,"content":"Welcome to Cloudnative bootcamp !!! Hello, Carlos :)"}
    
    open $APP_URL