Build and deploy your app in Rancher using Jenkins pipeline

In this article, I will explain how to write Jenkins pipeline to automate the process to build your code as a docker image and upgrade your app in Rancher (The Kubernetes management platform)

First of all, I would to briefly explain what is Rancher

Rancher is an open-source software that combines everything an organization needs to adopt and run containers in production. Built on Kubernetes, Rancher makes it easy for DevOps teams to test, deploy, and manage their applications.

The following steps needed to upgrade your app in Rancher

1- Make your code change

2- Build a new Docker image from your code changes

3- Push the new Docker image to the registry

4- Update the helm charts by adding a new app version

5- Open Rancher and update the app catalog

6- Finally will see your app ready to upgrade and click upgrade

If you are doing these steps manually you will lose a lot of time so we need to build a pipeline to automate all of these steps instead of you so we will use Jenkins in this article you can use any other CI/CD technologies.

  • Clone your code from the git repository

we need to authenticate to git during cloning the code we will use “sshagent”

  • Then we need to Bump version number and store it in a variable to be reusable
steps {
appVersion = “test-${BUILD_NUMBER}”
buildNumber = BUILD_NUMBER

  • After that build a new docker image and add a tag using the build number
  • Then Push your Docker image to docker registry
  • After that, you need to remove the local image to avoid low disk space in your Jenkins server
  • Then we need to clone the Helm chart repository like step one
  • After that, we will copy from the old version and edit the version number and docker image in values.yaml and chart.yaml or edit your files as you need using Yaml Jenkins plugin and commit the changes and push it to git
steps {
script {
sh “cp -R ${intialchartFolder} ${currentchartFolder}”
valuesData = readYaml(file:currentvaluesFile)
sh “echo $valuesData” // Debug
valuesData.image.tag = “$appVersion”
sh “rm ${currentvaluesFile}”
writeYaml(file:currentvaluesFile, data:valuesData)
chartData = readYaml(file:currentchartFile)
sh “echo $chartData” // Debug
chartData.version = buildNumber
sh “rm ${currentchartFile}”
writeYaml(file:currentchartFile, data:chartData)
sshagent (credentials: [“${sourcecodeCredential}”]) {
sh(“cd AMLRT-Charts && git add . && git commit -m ‘Jenkins: bump docker image version to ${appVersion}’ && git push -u origin ${chartRepoBranch}”)
  • Finally, we need to upgrade the app in Rancher to do this we need Rancher CLI setup in Jenkins server, or using a Rancher CLI docker image on the fly it is a better solution.

notes: To authenticate to Rancher you need access token and make sure your Jenkins server is able to access your Rancher server

Note: If you encounter a problem in SSH use a cleanws plugin to clean the Environment before the first stage in your pipeline

Now your app is upgraded and the workload is updated with a new docker image and you saved your time.

Software engineer