Jenkins is an open source automation server. It is majorly used for CICD pipelines.
I am writing this cheatsheet while learning about Jenkins from multiple platforms like Udemy, Pluralsight, LinkedIn Learning and multiple articles. Download Jenkins using homebrew in MacOS and Linux, and exe file from the official website for Windows.
- Install Docker and Docker compose
- Pull docker images using
docker pull jenkins/jenkins.
- Create a docker compose file that has the following content.
volumesshould have the path where the data from the container is to be stored. Docker compose is used for making the container persistent.
version: '3'
services:
jenkins:
container_name: jenkins
image: jenkins/jenkins
ports:
- "8080:8080"
volumes:
- $PWD/jenkins_home:/var/jenkins_home
networks:
- net
networks:
net:
-
Create a directory
jenkins. Place the docker compose file in it. -
Create a new directory named
jenkins_homeinsidejenkinsdir. -
Inside
jenkinsdir, do the following:sudo chown 502:502 jenkins_home -Rgive permissions to write to the folder.docker-compose up -dspin up the containerdocker logs -f jenkinsgives you password
-
Hit localhost:8080 in a browser and login using this password.
-
The next screen asks for which plugins to install. Choose custom or recommended install (as per your wish). This takes a while.
-
After installation is complete, Jenks will ask you to create the first admin account. Assign username and password to the admin account. It will also ask for Jenkins URL. Preferrable leave as it is.
-
Once setup is complete, Jenkins might ask you to login using the admin credentials.
To start a container, docker-compose start and to stop it use docker-compose stop.
To restart docker-compose restart jenkins
To delete the container, dcoker-compose down. This will not delete the container data.
New Item: Create a new jenkins job. This has a number of templates.People: List of people who can access this account along with their permission levels.Build History: History of builds. Very intuitive :PCredentials: Manage credentials to be used in the jenkins jobsManage Jenkins: Manage the jenkins account.
- Click on
New Item. Give a name to this job and select Freestyle Project. - Under
Buildsection, there is an optionExecute shell. This will be executed in the same shell asdocker exec -ti jenkins bashi.e. the container's shell. - A text box appears. Here type
echo Hello World. And save. - On the left side, there will be an option
Build Now. This will trigger the job and you can see the output inConsole Outputoption on the left in the build's page. Reconfigure the job by clickingConfigurein the jobs page. Please take not of the wordsjobsandbuilds. Jobs produce builds. In the place you entered shell script, you can use other commands or env variables compatible to the container shell, likedate, who ami, etc. Few examples are as follows:
NAME=aanimish
echo "Hello aanimish. Current date is $(date)"
echo "Hello aanimish. Current date is $(date)" > /temp/dataThe file written in the last line resides in the container.
You execute a script file in a job, the file should be present in the container. The Jenkins container does not vi ninstalled in it. To move a file from local system,
to the container use docker cp <filepath in local system> jenkins:<full filepath in container>. For example, docker cp script.sh jenkins:/tmp/script.sh.
The script might need parameters from the user. To add parameters to the job, go to Configure. Under the general tab, there must be an option similar to This project is parameterised. Here, the parameters name, default value and description is specified. There a number of different data type variables that can be
defined. Most commonly used to string type. Adding parameters, change the Build option in the left side of the job changes to Build with Parameters.
To create a list parameter, choose Choice Parameter. This creates a dropdown from the user. Mention each choice in a new line while adding parameters.
While setting boolean parameters, there is a checkboc Default Value. If checked, the default value will be True, otherwise no default values. Boolean parameter is shown as checkbox to the user.