Reader small image

You're reading from  Building CI/CD Systems Using Tekton

Product typeBook
Published inSep 2021
PublisherPackt
ISBN-139781801078214
Edition1st Edition
Right arrow
Author (1)
Joel Lord
Joel Lord
author image
Joel Lord

Joel Lord (joel__lord on Twitter) is passionate about the web and technology in general. He likes to learn new things, but most of all, he wants to share his discoveries. He does so by traveling to various conferences all across the globe. He graduated from college with a degree in computer programming in the last millennium. Apart from a little break to get his BSc in computational astrophysics, he has always worked in the industry. In his daily job, Joel is a developer advocate with MongoDB, where he connects with software engineers to help them make the web better by using best practices around JavaScript. In his free time, he can be found stargazing on a campground somewhere or brewing a fresh batch of beer in his garage.
Read more about Joel Lord

Right arrow

Chapter 13: Building a Deployment Pipeline

You are almost there. It is now time to build your first complete CI/CD pipeline to automate your application deployment in your minikube cluster. In the previous chapter, you saw how to deploy the application manually. In this one, you will create all the necessary components to automate this deployment.

First, you will start by analyzing the required steps and determining the necessary components to build your pipelines. Once you have decided on the tasks that you will need for your pipeline, you will need to write those tasks. Thankfully, some tasks are already available for you to use, and you will learn about the Tekton Catalog and how it can help you here.

For most of your tasks, you will be able to use some pre-written ones from the official catalog, yet you will still need to write at least one. This will act as a good refresher on authoring your own tasks.

Finally, once you've established your parameters and workspaces...

Technical requirements

You can find all of the examples described in this chapter in the chapter-13 folder of the Git repository:

https://github.com/PacktPublishing/Building-CI-CD-systems-using-Tekton

You can also see the Code in Action videos at the following link: https://bit.ly/3ybaLV6

Identifying the components

Before writing your pipeline, the first step is to identify the various components required for your deployment. This step will help you decide the necessary tasks and the order in which they should occur.

Let's think about what operations are needed every time you perform a commit on your source code:

  1. Clone the repository: The CI/CD pipeline will need a fresh copy of your code base to prepare the next steps.
  2. Install the required libraries: The containers that are running the testing and linting processes will need to download the necessary modules to perform these operations.
  3. Test the code: The test suite should be executed and halt the deployment process if the tests do not pass.
  4. Lint the code: The code should follow the coding standards for this project, or the application should not be allowed to be deployed.
  5. Build and push the image: Build a container image and push it to a registry.
  6. Deploy the application...

Using the task catalog

So far, you've created all of your tasks from scratch, but here's a little secret that's been kept from you all this time. There is a list of tasks available out there that are well tested and available for you to use in your pipelines. This list of tasks is called Tekton Hub and was just released out of beta recently. You can access Tekton Hub at https://hub.tekton.dev.

Tasks are meant to be reusable whenever possible. For this reason, it makes sense that operations such as a git clone have a pre-written task in the catalog. Using such tasks makes it much easier to write your pipeline, as you don't need to reinvent the wheel every time you need a new, common task.

By using the text box in the upper-right corner, you can search for tasks. Clicking on the matching card will open up a description of the task, along with the installation instructions and usage.

Even better, you can install tasks from Tekton Hub directly by using...

Adding an additional task

For the build-push task, you could've tried to use the docker build task, but the image used relies on binding a socket to the Docker daemon, which might not work in all environments. Instead, you will write your task using the Buildah tool (https://buildah.io/) to build and push the image to a registry. This image does not require access to the Docker daemon and will work in any context.

For this task, you will have three parameters. The name of the image to be built and pushed should be provided, along with the credentials to connect to the appropriate image registry.

This task will also need a workspace that will contain the source code that should be packaged up as an image.

Finally, the task will have a single step to build the image, log in to the registry, and push the image to it. These operations will require privileged access in the container, so you will also need to specify this in the step description.

First, start with a...

Creating the pipeline

You are now ready to write your new pipeline that will take your source code, run testing and linting, build the image, push it to a registry, and do a rollout on your Kubernetes cluster. It might sound like a lot of work, but let's look at it step by step.

Start with a new file called pipeline.yaml. In there, create a new pipeline named tekton-deploy:

apiVersion: tekton.dev/v1beta1 
kind: Pipeline 
metadata: 
  name: tekton-deploy 

In the spec field, add the parameters and workspace that will be needed for your tasks:

spec: 
  params: 
    - name: repo-url 
    - name: deployment-name 
    - name: image 
    - name: docker-username 
    - name: docker-password 
  workspaces: 
    - name: source 

Create your tasks list and add a first task called clone. This task refers to the git-clone task...

Creating the trigger

With your pipeline ready, you need to set up your trigger to automatically start the pipeline when someone pushes code to your repository. This trigger will be very similar to the one you wrote in Chapter 11, Triggering Tekton.

Start by creating your secret key, which will be shared between your trigger and GitHub:

$ export TEKTON_SECRET=$(head -c 24 /dev/random | base64) 
$ kubectl create secret generic git-secret --from-literal=secretToken=$TEKTON_SECRET

Note this secret key somewhere, as you will need it later to configure your GitHub webhook. If you need to see it again later, you can use the echo command:

$ echo $TEKTON_SECRET 

To add a Tekton trigger to your cluster, you will need three components. You can put all of them in a single file called trigger.yaml.

Start with a trigger binding. This binding will be named event-binding and will bind the repository.url object from the JSON payload to the gitrepositoryurl parameter:

apiVersion...

Summary

In this chapter, you've built your first complete pipeline that will have an actual use case. Starting from scratch, you've seen how to plan for your task, parameters, and workspaces.

With this plan in hand, you could use the knowledge you've got from this book to build a complete pipeline. Starting from the Tekton Hub tasks, you could reuse some components that the Tekton community has created. You have also made a task that you used in the pipeline. You've then created a large pipeline that reproduces all the steps you did manually in the previous chapter to deploy your application.

Ultimately, you created a trigger so that this pipeline would automatically start every time some code is pushed to your repository. This pipeline is still somewhat simple. With what you've learned across this book, you can make it your own and build on top of it.

For example, if you need different tasks based on the branch that someone made the push on...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Building CI/CD Systems Using Tekton
Published in: Sep 2021Publisher: PacktISBN-13: 9781801078214
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Joel Lord

Joel Lord (joel__lord on Twitter) is passionate about the web and technology in general. He likes to learn new things, but most of all, he wants to share his discoveries. He does so by traveling to various conferences all across the globe. He graduated from college with a degree in computer programming in the last millennium. Apart from a little break to get his BSc in computational astrophysics, he has always worked in the industry. In his daily job, Joel is a developer advocate with MongoDB, where he connects with software engineers to help them make the web better by using best practices around JavaScript. In his free time, he can be found stargazing on a campground somewhere or brewing a fresh batch of beer in his garage.
Read more about Joel Lord