Jenkins as a CI/CD tool offers way more than just being able to build and compile code. It can integrate and utilize a host of other technologies, one of which is Docker, a tool for containerizing your workloads.
In this hands-on lab we will be creating a Jenkins Pipeline job which will create and deploy Docker containers with an application.
Learning Objectives
Successfully complete this lab by achieving the following learning objectives:
- Jenkins UI
Once you access landing page for Jenkins, click New Item on the left side of the screen to proceed towards selecting the type of Jenkins job you want to create.
Once the new page loads, click on Pipeline and on the input box give a name to your job, such as MyDeclarativePipeline.
After selecting the type of job, Pipeline, and giving it a name, click OK to proceed with configuring the job.
- Configuring the Jenkins Pipeline Job (Docker in Jenkins)
On the new page which loads for configuring the pipeline job, scroll down to the Pipeline section and ensure that Pipeline script is selected in the Definition drop-down. An input area should be visible. This could also be done using a Scripted Pipeline, however we will be using a Declarative Pipeline in this lab.
Copy the following body of text (which is basically in Groovy DSL format, a format understood by Jenkins) into the input area:pipeline{ agent { docker { image 'python:alpine' args '-u root' } } stages{ stage('Prepare environment'){ steps{ sh 'apk add git' } } stage('Clone code from Git repository and setup python env'){ steps{ sh 'git clone https://github.com/linuxacademy/content-pipelines-cje-labs.git' sh 'pip install -r content-pipelines-cje-labs/lab3_lab4/dog_pics_downloader/requirements.txt' } } stage('test code'){ steps{ sh 'python content-pipelines-cje-labs/lab3_lab4/dog_pics_downloader/dog_pic_get_class.py' } } } post{ always{ echo "Job execution complete." } success{ archiveArtifacts artifacts : '*.jpg' } unsuccessful{ echo "Job execution status is failed, please check error logs" } cleanup{ echo 'Cleaning up environment' sh 'rm -rf content-pipelines-cje-labs' } } }
Leave everything else at the default settings and click on Save.
Note: This Declarative Pipeline will pull down and run a Docker container which has
python3
andpip
installed already.Additionally, we are installing
git
inside the container to pull down the code we want to test.- Running/Building the Jenkins Pipeline Job
After hitting Save in the previous step, you will be taken to the control page for the job you just configured. There you can see the job status and other job statistics, including being able to edit the configuration again.
To kick off a build, click on Build Now. This should kick off the build and you should see the output of build status in a couple of places on your current screen.
- Under the Build History box to the left of your screen you’ll see a build number and a progress bar, if the build is currently running.
- The page will also be updated with a Stage View giving details of execution success (green) and time taken by each build stage that was defined in our Declarative Pipeline. Subsequent builds will add more data to average run times in the same area.
If you click on the build number under Build History, #1 for example, you will be taken to the details of the specific build. This will show things such as the Console Output of executed commands that were defined in the pipeline for each step.
On this page you’ll also find the build output files or Build Artifacts, which are the outcome of the pipeline stages that you built. In this case, if everything goes right, you should get all the downloaded
.jpg
files as artifacts that the execution/testing of the code produced.