In this lab we will create are own K8s Pod Template with nodejs and another Docker container for executing tests. We also want to use a different version of the node Docker image than the one provided by the Master managed Pod Template which is node:8.12.0-alpine
. So far we have been using the nodejs-app Kubernetes Pod Template defined for us at our Team Master level. In order to be able to control what containers
and what Docker image
version we use in our Pipeline we will update the Jenkinsfile Pipeline script with an Kubernetes Pod Template definition.
- The Jenkins Kubernetes plugin allows you to use standard Kubernetes Pod yaml configuration to define Pod Templates directly in your Pipeline script. We will do just that in a new
nodejs-pod.yaml
file. TheyamlFile
parameter value of thekubernetes
agent definition is a repository relative path to a yaml file representing the Pod spec you want to use as an agent Pod Template. At the top-level of the master branch of your forked copy of the helloworld-nodejs repository click on the Create new file button towards the top right of the screen. - Name the file
nodejs-pod.yaml
and add the following content:
kind: Pod
metadata:
name: nodejs-app
spec:
containers:
- name: nodejs
image: node:10.10.1-alpine
command:
- cat
tty: true
- name: testcafe
image: gcr.io/technologists/testcafe:0.0.2
command:
- cat
tty: true
securityContext:
runAsUser: 1000
- At the bottom of the screen enter a commit message, leave Commit directly to the
master
branch selected and click the Commit new file button - Now we need to update our Pipeline to use that file. Open the GitHub editor for the Jenkinsfile Pipeline script in the master branch of your forked helloworld-nodejs repository.
- Replace the
agent
section of the Teststage
with the following - note that the value of theyamlFile
parameter is the name of the file we created:
agent {
kubernetes {
label 'nodejs-app-pod'
yamlFile 'nodejs-pod.yaml'
}
}
- Commit the changes and then navigate to the master branch of your helloworld-nodejs job in Blue Ocean on your Team Master. The job will queue indefinitely, but why?
- The answer is provided by the CloudBees Kube Agent Management plugin. Exit to the classic UI on your Team Master and navigate up to the helloworld-nodejs Multibranch folder. On the bottom left of of the screen there is a dedicated widget that provides information about the ongoing provisioning of Kubernetes agents. It also highlights failures, allowing you to determine the root cause of a provisioning failure. Click on the link for the failed or pending pod template.
- The pod will be in pending state for a few minutes and eventually fail. Click in the name of the failed pod and you will see that the nodejs container has an error - it looks like there is not a node container image available with that tag. If you go to Docker Hub and look at the tags available for the node image you will see there is a 10.10.0-alpine but not a 10.10.1-alpine tag for the node image:
- Click Open Blue Ocean on the left and go to the latest build of helloworld-nodejs. Abort the current run (or it will keep trying to load that faulty pod template forever)
- Next, open the GitHub editor for the nodejs-pod.yaml Pipeline script in the master branch of your forked helloworld-nodejs repository. Update the
image
for the nodejscontainer
to benode:10.10.0-alpine
and commit the changes. - Next, open your Jenkinsfile and update
kubernetes
label
to nodejs-app-pod-2. - Commit the changes and then navigate to the master branch of your helloworld-nodejs job in Blue Ocean on your Team Master. The job will run successfully. Also, note the output of the
sh 'node --version'
step - it isv10.10.0
instead ofv8.12.0
:
pipeline {
agent none
options {
buildDiscarder(logRotator(numToKeepStr: '2'))
skipDefaultCheckout true
}
stages {
stage('Test') {
agent {
kubernetes {
label 'nodejs-app-pod-2'
yamlFile 'nodejs-pod.yaml'
}
}
steps {
checkout scm
container('nodejs') {
echo 'Hello World!'
sh 'node --version'
}
}
}
stage('Build and Push Image') {
when {
beforeAgent true
branch 'master'
}
steps {
echo "TODO - build and push image"
}
}
}
}
You may proceed to the next lab Lab 5. CloudBees Pipeline Template Catalogs or head back to the main list of the labs when you are ready.