In another step in configuring Jenkins for pipelines, in this post I will show how to use Docker Container as an Agent, K8s Pods can also be used, but I will show this in another post later on.
The use of containers as an agent opens up a huge variety and possibility of processes, in any language, in any way you want, whatever you can do in a conventional container. I use a lot of scripts in Python, and this way I can create many processes, including with Ansibles, accessing other services, taking advantage of many code modules that exist.
For this example, I created an image in Ubuntu, very simple, just with Python, which will serve as the base image for the pipelines. https://devops-db.com/base-image-for-docker-python/
Jenkins Settings.
Let’s start configuring Jenkins, the first step is to install the Docker Plugin.
With an administrative account in Jenkins, open Manage Jenkins / Cloud. Click Install Plugin: Docker
Then, in the same Manage Jenkins / Cloud, there will be one more option: New Cloud, click on the option.
Choose a name for the Cloud and choose the Docker type.
This option should open a series of configuration options. Enter the Docker Host URI, in my case the IP is 172.21.5.70 and as it is on the same host as Jenkins, I do not need to create credentials. Check the Enable option and do a Test Connection. Save, the configuration is that simple.
On your Docker host, you may need to change permissions due to unix:///var/run/docker.sock.
Adding the user jenkins to the docker group already solves the problem, in the worst case, for testing, apply chmod 777 to the /var/run/docker.sock file (This option should not be applied in a production environment)
Now you need to install a second plugin, again in Manage Jenkins / Plugins / Available Plugins: Install the Docker Pipeline plugin
Restart Jenkins to take effect the changes.
Pipeline as an agent a Container..
Now, let’s change the groovy pipeline script we made in the previous post. Change the script and merge it into Master from the repository http://gitlab.devops-db.internal/infrastructure/pipelines/dns
pipeline {
agent any
options { timestamps () }
stages {
stage('Build') {
when { not { changeRequest() } }
agent {
docker {
image 'ubuntu_python:3.10.6'
}
}
steps {
updateGitlabCommitStatus name: 'Finish', state: 'pending'
sh 'printenv'
sh 'ls -lah ${WORKSPACE}'
updateGitlabCommitStatus name: 'Finish', state: 'success'
}
}
stage('Finish') {
when { branch 'master' }
agent {
docker {
image 'ubuntu_python:3.10.6'
}
}
steps {
updateGitlabCommitStatus name: 'Finish', state: 'pending'
sh 'printenv'
updateGitlabCommitStatus name: 'Finish', state: 'success'
}
}
}
}
Make any commit / push to the GitLab project we use for testing this pipeline and see the result:
Leave a Reply