A Jenkinsfile keeps a project's CI job in source control beside its application, Dockerfiles, and Compose files. Its pipeline can call the same Docker Compose commands developers run locally, reducing differences between local builds and CI. section 11.4
Listing 11.5 Jenkinsfile build step that describes the CI job
# the build stage in the Jenkinsfile- it switches directory, then runs two
# shell commands - the first sets up a script file so it can be executed
# and the second calls the script:
stage('Build') {
steps {
dir('ch11/exercises') {
sh 'chmod +x ./ci/01-build.bat'
sh './ci/01-build.bat'
}
}
}
# and this is what's in 01-build.bat script:
docker compose
-f docker-compose.yml
-f docker-compose-build.yml
build --pull
A build stage changes into the exercise directory, makes a script executable, and runs it; the script builds with Docker Compose and pulls current base images. section 11.4
A practical Jenkinsfile workflow includes:
- Define a Build stage that invokes a small script rather than embedding every command directly. section 11.4
- Run docker compose build --pull so CI uses the latest required base images and exposes breaking image changes early. section 11.4
- Inspect Jenkins logs when a build fails; Docker Compose and Docker output is captured in the build step. section 11.4
Small CI lab
Complete a to-do application's pipeline by writing docker-compose-build.yml, creating a Jenkins job, and pointing it to the lab Jenkinsfile. Push the changes to Gogs, then inspect failed or successful builds and adjust the configuration. section 11.6
