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

Assessments

Some chapters across the book had exercises for you to practice and improve your Tekton Pipelines authoring skills. In this section, you will find the solutions to those exercises. Your own implementation might vary, but these examples should provide you with enough context to fine-tune your own answers.

The solutions are printed here for easy reference, or you can find them in GitHub, as described in the next section.

Technical requirements

You can find all of the examples described in this chapter in the assessments folder of the Git repository at https://github.com/PacktPublishing/Building-CI-CD-systems-using-Tekton.

Chapter 4

This chapter was all about tasks, and so are these exercises. The goal of these exercises is to let you become familiar with the basic concepts around steps and tasks.

More than Hello World

In this first challenge, build a task with three steps. The first step should output a log message stating that the task has started. Then, the task will sleep for the number of seconds that the user specifies. In the third step, the task should log a string that the user provides. Be sure to add default values to those two parameters. Now, try running the task with the tkn command-line interface (CLI) tool. Run it a second time using the default values this time. Start this task a third time, specifying the parameter values directly in the command line.

Here is a possible solution for the exercise:

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: more-than-hello
spec:
  params:
    - name: log
     ...

Chapter 5

In this chapter, you learned about pipelines. The goal of the following exercises is to help you improve your pipeline authoring skills.

Back to the basics

For your first challenge, start by building a pipeline that will output a hello message. To do so, use the logger task that you created in this chapter. The pipeline will take a parameter to indicate who to say hello to, and this should default to World. Run this pipeline with different parameter values using the CLI. Then, run it with the default values. Do one last run with the parameter value specified in the CLI command directly. Here is a possible solution for the exercise:

apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: back-to-basics
spec:
  params: 
    - name: who
      default: "World"
      type: string
      description: Who should we say hello...

Chapter 6

In this chapter, you learned one important concept called finally tasks. In this section, you will put this knowledge to work and use exit codes to change the final output of your pipelines.

Fail if root

Create a pipeline with a simple task. This task will check whether the container is running as the root user. If it is indeed running as the root user, the task will fail.

Here is a possible solution for the exercise:

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: fail-if-root
spec:
  steps:
    - name: fail-if-root
      image: registry.access.redhat.com/ubi8/ubi
      script: |
        if [ $(whoami) == "root" ]
          then
            echo "User is root"
    ...

Chapter 7

In this chapter, you learned how to share large quantities of data across tasks in a pipeline. This will be useful when you start dealing with Git repositories. The following exercises will give you some practice with the concept of workspaces.

Write and read

Create a task that uses a workspace to share information across its two steps. The first step will write a message, specified in a parameter, to a file in the workspace. The second step will output the content of the file in the logs. Try running the task using the -w parameter in the tkn CLI tool.

Here is a possible solution for the exercise:

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: write-read-workspace
spec:
  workspaces:
    - name: data
  params:
    - name: message
      default: "Hello World"
      type: string
     ...

Chapter 8

Sometimes, your pipeline will need to execute tasks based on certain conditions. In this chapter, you learned how to use when expressions to add conditional flows to your pipelines. You now have the opportunity to get some experience with this concept here.

Hello Admin

Build a pipeline that will take a username as a parameter. If the username is admin, log the Hello Admin text. For any other username, output a simple Hello message.

Here is a possible solution for the exercise:

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: logger
spec:
  params:
    - name: text
      type: string
  steps:
    - name: log
      image: registry.access.redhat.com/ubi8/ubi-minimal
      script: |
        DATE=$(date +%d/%m/%Y\ %T)
      &...

Why subscribe?

  • Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals
  • Improve your learning with Skill Plans built especially for you
  • Get a free eBook or video every month
  • Fully searchable for easy access to vital information
  • Copy and paste, print, and bookmark content

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at customercare@packtpub.com for more details.

At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.

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 €14.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