Imagine you have to build a special application, which requires lots of libraries and dependencies, at specific versions, and you can't install them on your Jenkins machines.
How to build it?
Using a Docker container!
First of all, you need to install the Docker Pipeline plugin:
To use the plugin with a Declarative pipeline, add this stage:
stage('Run command inside a container') {
steps {
script {
withDockerContainer("my-custom-image:1.0") {
echo 'This command is executed inside the container!'
}
}
}
}
Example with Python:
pipeline {
agent any
stages {
stage('Clone sources') {
steps {
git credentialsId: 'git-creds', url: 'https://git.example.com/myrepo.git'
}
}
//build a Docker image with a Dockerfile from sources
stage("Build Docker images") {
steps {
script {
docker_image_dev = docker.build("my-custom-python:3.6")
}
}
}
//Run tests inside a container based on previous image
stage('Run Tests: Coverage') {
steps {
script {
withDockerContainer("my-custom-python:3.6") {
sh 'pwd'
sh 'pylint source | tee test/results/pylint.txt'
}
}
}
}
}
}
No comments:
Post a Comment