Build and deploy your app in Rancher using Jenkins pipeline

  • Clone your code from the git repository
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.
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”
}
}
}
}
stage(‘clean up’) {
steps {
script {
cleanWs()
}
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store