first install java:
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer
install jenkins:
wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt update
sudo apt install jenkins
if jenkins does not start automaticalle run it with:
sudo service jenkins start
go to localhost:8080
.
check the requested password with:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
input the password.
choose install suggested plugins
. This will take several minutes.
input user data.
choose url for accessing jenkins. Default: locahost:8080
In order to let Jenkins
interact with Docker
, we need to add it to the corresponding group:
sudo usermod -aG docker jenkins
start jenkins service with:
sudo systemctl start jenkins
stop jenkins service with:
sudo systemctl stop jenkins
check the status of jenkins with:
sudo systemctl status jenkins
We will create a Free Style Project with Jenkins in order to complete the useful task of automatically building the new docker image and put it online, triggered by any new change in the corresponding central Git repository of our project:
- Create new job.
- Choose Free Style Project.
- Choose Git in Source Code Management.
- Include the repository url.
- Provide credentials.
- Choose Poll SCM in Build Triggers (This will let Jenkins be watching out changes in our repository).
- Below Poll SCM we need to add a Cron expression to indicate the periodicity of the job.
- In the section Build, we will add all the actions we want Jenkins to execute once triggered. In our case we want:
# build the new image
docker build -t my-flask-image:latest .
# stop and remove the existing container
current_container_id=$(docker ps | grep flask-docker | grep -o "^[0-9a-z]*")
docker stop $current_container_id
docker rm $current_container_id
# delete previous image
docker rmi -f $(docker images -f "dangling=true" -q)
# create and run a new container
docker run -d -p 8000:5000 my-flask-image
With the previous configuration, Jenkins will be expecting a change in our repository, and once it is detected Jenkins will: get the updated code, build the new docker image, stop and remove the container that was active until the moment, remove the image and activate a container based on the new image.
We can check the success of the job going to localhost:8080
.
(Under construction)
(Under construction)