Jenkins - Nexus and Helm
This segment will focus on working with an artifact repository - in our case, this will be Nexus. This means we want to extend our pipeline and create images for our application which will then be pushed to a nexus instance that is deployed within the training environment.
stage('Push image to nexus') {
container('docker-builder') {
env.REGISTRY_HOST = 'https://cpjswd02.nexus.cloudtrainings.online'
env.REPOSITORY = 'todobackend'
env.TAG = 'studentX-${VERSION}'
withCredentials([usernamePassword(credentialsId: 'nexus-credentials', usernameVariable: 'NEXUS_USER', passwordVariable: 'NEXUS_PASSWORD')]) {
sh 'podman login ${REGISTRY_HOST} --username ${NEXUS_USER} --password ${NEXUS_PASSWORD}'
sh "podman tag technologyconsulting-containerexerciseapp-todobackend:${VERSION} ${env.REGISTRY_HOST}/${env.REPOSITORY}:${env.TAG}"
sh "podman push ${env.REGISTRY_HOST}/${env.REPOSITORY}:${env.TAG}"
}
}
}Warning
Please make sure to substitute your studentId for studentX in the sample file displayed above, e.g. student1.
Info
Why having one sh line in single quotes and the others in double quotes?
This is to avoid leaking sensitive credential data when doing [Groovy string interpolation][groovy-string-interpolation].
Feel free trying to put the podman login line in double quotes (and prepend the variables with env.) to see Jenkins warn about this.
Once saved and executed take a look around in Nexus and see whether you can find your artifact.
Deploy to Kubernetes with Jenkins and Helm
Now we want to use our pipeline to deploy our application to the kubernetes cluster. We will utilize helm to do so.
After the previous build and push stage, add the following steps to have helm install the backend using the provided helmcharts under todobackend/helmchart, right at the end of the stages.
stage('Deploy container') {
container('helm') {
env.REGISTRY_HOST = 'https://cpjswd02.nexus.cloudtrainings.online'
env.REPOSITORY = 'todobackend'
env.TAG = 'studentX-${VERSION}'
dir('todobackend/helmchart') {
sh "helm upgrade --install todobackend-studentX --set image.repository=${env.REGISTRY_HOST}/${env.REPOSITORY} --set image.tag=${env.TAG} --set springProfiles=dev ."
}
}
}Here we are using the helm container. But as you might have noticed, we have not defined that container yet. Therefore, we need to add a new container template to our Jenkins pipeline.
podTemplate(containers: [
...
containerTemplate(name: 'helm', image: 'alpine/helm', command: 'sleep', args: '99d')
...and to add the Jenkins worker as serviceAccount
podTemplate(containers: [
...
],
serviceAccount: 'jenkinsworker',
volumes: [
...
]
)