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”
stage(‘Cloning Code Repo’)
{
when { branch “${sourcecodeBranch}” }
steps
{
sshagent (credentials: [“${sourcecodeCredential}”]) {
sh(“git clone — single-branch — branch ${sourcecodeBranch} ${sourcecodeRepoUrl}”)
}
}
}
- Then we need to Bump version number and store it in a variable to be reusable
stage(‘Bump Version — Using build#’) {
when { branch “${sourcecodeBranch}” }steps {
script
{
appVersion = “test-${BUILD_NUMBER}”
buildNumber = BUILD_NUMBER
}
}
}
- After that build a new docker image and add a tag using the build number
stage(‘Build Docker Image’) {
when { branch “${sourcecodeBranch}” }
steps {
script
{
sh “echo ${appVersion}”
dockerImage = docker.build(“$registryUrl/$imageName:$appVersion”, ‘$DockerFIlePath’)
}
}
}
- Then Push your Docker image to docker registry
stage(‘Push Docker Image’) {
when { branch “${sourcecodeBranch}” }
steps {
script
{
docker.withRegistry(‘https://’ + registryUrl, registryCredential)
{
dockerImage.push()
}
}
}
}
- After that, you need to remove the local image to avoid low disk space in your Jenkins server
stage(‘Remove Recent Image from Jenkins’) {
when { branch “${sourcecodeBranch}” }
steps {
sh “docker rmi $registryUrl/$imageName:$appVersion”
}
}
- Then we need to clone the Helm chart repository like step one
stage(‘Clone Rancher Chart Repo’) {
when { branch “${sourcecodeBranch}” }
steps {
script {
sshagent (credentials: [“${sourcecodeCredential}”]) {
sh(“git clone ${chartRepoUrl}”)
}
}
}
}
- 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
stage(‘Push Rancher Chart Repo With New Version’) {
when { branch “${sourcecodeBranch}” }
environment
{
valuesData = ‘’
chartData = ‘’
}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
stage(‘Update Rancher Catalog and Upgrade App’) {
when { branch “${sourcecodeBranch}” }
steps {
script {
sh “docker run — rm -v /tmp:/root/.rancher/ rancher/cli2 login https://$rancherUrl/v3 — token $rancherApiToken — skip-verify"
sh “docker run — rm -v /tmp:/root/.rancher/ rancher/cli2 catalog refresh $rancherCatalogName — wait”
sh “docker run — rm -v /tmp:/root/.rancher/ rancher/cli2 app upgrade $rancherAppName $appVersion”
}
}
}
}
Note: If you encounter a problem in SSH use a cleanws plugin to clean the Environment before the first stage in your pipeline
stage(‘clean up’) {
steps {
script {
cleanWs()
}
}
}
Now your app is upgraded and the workload is updated with a new docker image and you saved your time.