Jenkins starts on port 8080 by default
Two ways to install and run Jenkins:
- install the Jenkins app on the OS directly
- run Jenkins image and start Jenkins as a docker container
--> Open port 50000 on Jenkins: here Jenkins master and worker nodes communicate --> Jenkins can be built and started as a cluster if you have large workloads that you are running with Jenkins --> Initial Jenkins password is located at: /var/volume_name/secrets/initialadminpassword
-
spin up an EC2 instance on AWS
-
open port 22 (for ssh) and port 8080(access jenkins externally) in inbound rules for the security group attached
-
we will run Jenkins's official image to start Jenkins on the Ubuntu server
-
For this, we need the installation of docker on the server
-
sudo apt update
-
By default, the Docker daemon socket is only accessible to users in the "docker" group. You can add your current user to this group using the following command:
-
sudo usermod -aG docker $USER
-
docker login
-
Starting Jenkins on the server
sudo docker run -d -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home jenkins/jenkins
-
setup password by giving the initial password stored at the location mentioned in the UI
cat /var/volume_name/secrets/initialadminpassword
-
when we log in to the Jenkins container --> we are logged in as a Jenkins user instead of a root user as by default Jenkins is started as a Jenkins user (security best practice)
-
setup the first admin user
-
inspect the Jenkins mount volume location
docker volume inspect jenkins_home
-
we can manage and configure build tools and package managers from the Tools tab under the manage Jenkins option:
-
To log in as a root user to the Jenkins container, use -u flag :
-
installing nodejs and NPM in the container, we logged in as root user
-
download node: https://deb.nodesource.com/
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
sudo apt-get install -y nodejs
-
Creating a job:
-
under new item -> create a job or directly from the UI itself
-
freestyle for demo purposes/ small projects, learning Jenkins etc
-
for the Production environment, we will be using pipeline and multipipeline jobs
-
integrate with the GitHub account to start the build process as soon as changes are committed to the repo
-
the build now takes longer than the previous one because of this integration
-
added new branch "jenkins-job", created test.sh script to print npm version
-
in the shell command give execute permission to Jenkins user to run test.sh
-
to run and build docker artifacts inside jenkins , we need to make docker available inside the jenkins container
docker run -d -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock -v $(which docker):/usr/bin/docker jenkins/jenkins
these two volumes make docker commands available inside the jenkins container -
now we have docker commands available inside the jenkins container
-
the Jenkins service user doesnt have permission to read, execute docker commands yet now we are able to execute all the docker commands in the jenkins container
-
-
Running a simple test from maven plugin
-
we integrate the git repo, give the branch name where the code is pushed recently , run maven test (runs a simple test file ) and maven package (packages if test is successful into a jar file) git repo:
-
To make docker available inside the jenkins container, (docker commands available while building jobs), we need to mount the docker runtime directory from the server/local host to the jenkins container as a volume ie(need to mount additional two volumes: docker volume and docker runtime volume (cmds gets executed from here/ docker executable binary location)
-
stop the jenkins container and run
docker run -p 8080:8080 -p 50000:50000 -d -v /var/run/docker.sock:/var/run/docker.sock -v jenkins_home:/var/jenkins_home jenkins/jenkins
-
login to the jenkins container and install docker
docker exec -it — user root <container id> /bin/bash
-
installing docker
curl https://get.docker.com/ > dockerinstall && chmod 777 dockerinstall && ./dockerinstall
-
The above command downloads and executes the Docker rapid installation script from https://get.docker.com/, which further installs Docker within the container.
-
adding sufficient permissions to execute docker commands as jenkins service user
sudo chmod 666 /var/run/docker.sock
ignore