Install docker
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
# Add the repository to Apt sources:
echo \
"deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
Install docker
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Set the permissions on ubuntu host
sudo usermod -a -G docker jenkins sudo chown jenkins:docker /var/run/docker.sock sudo chmod 660 /var/run/docker.sock
Install docker and docker pipeline plugin from manage jenkins
Lab 1: with single agent
pipeline { agent { docker { image 'node:18.18.0-alpine3.18' } } stages { stage('Test') { steps { sh 'node --version' } } } }
Docker Volumes to mount, which can be used for caching data on the agent between Pipeline runs. The following example will cache ~/.m2
between Pipeline runs utilizing the maven
container, avoiding the need to re-download dependencies for subsequent Pipeline runs.
lab2:
pipeline { agent { docker { image 'maven:3.9.3-eclipse-temurin-17' args '-v $HOME/.m2:/root/.m2' } } stages { stage('Build') { steps { sh 'mvn --version' } } } }
Combining Docker and Pipeline allows a Jenkinsfile
to use multiple types of technologies, by combining the agent {}
directive with different stages.
lab3:
pipeline { agent { docker { image 'maven:3.9.3-eclipse-temurin-17' args '-v $HOME/.m2:/root/.m2' } } parameters { choice( name: 'ENVIRONMENT', choices: ['dev', 'qa', 'prod'], description: 'Select the deployment environment' ) } stages { stage('SCM code') { steps { git 'https://github.com/hellokaton/java11-examples.git' } } stage('Build') { steps { sh 'mvn clean package' } } stage('Test') { steps { sh 'mvn test' } } stage('Deploy') { when { expression { params.ENVIRONMENT == 'prod' } } steps { // Add steps to deploy the application for 'prod' echo 'Deploying to production...' } } } post { success { echo 'Pipeline succeeded!' // Add additional success steps if needed } failure { echo 'Pipeline failed!' // Add additional failure steps if needed } } }
lab4:
Create Dockerfile [add to git]
FROM node:20.17.0-alpine3.20 RUN apk add -U subversion
Create jenkinsfiledoc [add to git]
pipeline { agent { dockerfile true } stages { stage('Test') { steps { sh 'node --version' sh 'svn --version' } } } }