In this step by step tutorial we are going to build a CI/CD solution for a modern cloud application based on microservices with Jenkins X. By the end of this tutorial, we will have a working CI/CD solution that will enable us to develop, test and deliver our application in a fast and reliable way.
- Continuous Deployment with Kubernetes and Jenkins X (Overview)
- Jenkins X Serveless Architecture (Chapter 1)
- Install & Setup Jenkins X Serverless (Chapter 2)
- Import Existing Project into Jenkins X (Chapter 3)
- You’re here → Create Custom Build Pack for Jenkins X (Chapter 4)
- Integration Testing in Jenkins X (Chapter 5)
In this chapter, we will create a custom build pack for Jenkins X and include unit testing as part of it.
Build packs define how to build, test and deliver your project each time a developer makes changes to the source code of your project. Jenkins X provides basic build packs, but in real life, you need to create a custom build pack for your project.
It is recommended to keep your build packs in a central repository and share them between the developers in your company.
In order to create a custom build pack for the tutorial project, the first thing you need to do is fork the official repository with community build packs. Then, clone the repository to your development environment using the following commands:
GIT_USER=[...] git clone https://github.com/$GIT_USER/jenkins-x-kubernetes cd jenkins-x-kubernetes
It’s advised to add an upstream repository to receive future updates from the official repository, in order to stay up-to-date. Use the following command to do so:
git remote add upstream https://github.com/jenkins-x-buildpacks/jenkins-x-kubernetes
Next, in order to create a custom build pack, we need to create a branch of the repository. Use the following commands:
git checkout -b tutorial-build-pack master git push -u origin tutorial-build-pack
Selecting the Source for Build Packs in Jenkins X
By default, the source of build packs is in the following folder:
To change the source to point to your newly created repository, run the following commands:
jx edit buildpack \ -u https://github.com/$GIT_USER/jenkins-x-kubernetes \ -r master \ -b
From now on, when you import a new project, Jenkins X will use the build packs from your newly created repository.
By default Jenkins X provides build packs for the popular development languages. As a base for our custom build pack, we will use the Python build pack since our project is written in Python.
Copy the python build pack folder to a folder called tutorial-build-pack. To do this run the following command:
cp -R packs/python packs/tutorial-build-pack
Now let’s look at the files inside the new build pack folder we’ve just created:
- Dockerfile – Contains the build of the docker image.
- charts – Contains Helm charts and Kubernetes files that describe how to deploy the project to staging and production environments.
- pipeline.yaml – Contains the steps for building, testing and delivering the project.
- preview – Contains Helm charts and Kubernetes files that describe how to deploy the project to a preview environment.
- skaffold.yaml – Contains the steps for building and storing the docker image.
Adding Unit Test Support as part of a Custom Build Pack in Jenkins X
In order to implement unit testing in the custom build pack, you need to extend the basic build pack. To extend a build pack you simply need to modify the pipeline.yaml file that is located in the home directory of the build pack.
Let’s say you want to add a script that runs some unit tests before building the image. For the sake of example, you will run an echo command “Running unit tests!”. But in reality, you should run a command or script that will run your tests.
Now add a new step to the pipeline.yaml to execute the new script before we build our service. Use the following commands to do so:
… release: build: preSteps: - sh: echo “Running unit tests!” …
The new file should look like this: https://github.com/testproject-io/jenkinsx-tutorial/blob/master/build-pack-pipeline.yaml
In order for the changes to take effect, we need to commit our changes to the build pack. However, the newly created build pack is not associated with any project.
If you have not already cloned the main tutorial repository, then run the following commands:
GIT_USER=[...] PROJECT_NAME=[...] git clone https://github.com/$GIT_USER/$PROJECT_NAME cd $PROJECT_NAME/backend2
Now we have a new repository to work with. To import the new project with the build pack you’ve created, run the following command:
jx import --pack tutorial-build-pack --batch-mode
In order to check if the build was correctly executed with our new unit test, run the following command:
jx get build logs
Congrats! Your unit tests are now being executed 🎉
In the next chapter, we will learn how to implement integration testing using Jenkins X 😎