Skip to content

Instantly share code, notes, and snippets.

@ljaraque
Last active October 10, 2018 22:01
Show Gist options
  • Save ljaraque/9b74542be629e06f85b3de1535d86a7a to your computer and use it in GitHub Desktop.
Save ljaraque/9b74542be629e06f85b3de1535d86a7a to your computer and use it in GitHub Desktop.
Jenkins Cheatsheet

Jenkins Cheatsheet

[email protected]

1. Installing Jenkins

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

2. Start/Stop/Check 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

3. Create Create a Free Style Job in 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.

3. Create other types of Job in Jenkins

(Under construction)

4. Using Plugin in Jenkins

(Under construction)

References:

  1. How To Install Jenkins on Ubuntu 18.04
  2. Automatically Dockerize a Flask Project on Git Push with Jenkins
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment