Skip to content

hemu07/Jenkins-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 

Repository files navigation

Jenkins-demo

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

  • sudo apt install docker.io image

  • 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

  • enter username & password or PAT to login image

  • Starting Jenkins on the server sudo docker run -d -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home jenkins/jenkins

    image image

  • Access Jenkins UI from the browser image

  • setup password by giving the initial password stored at the location mentioned in the UI cat /var/volume_name/secrets/initialadminpassword image

  • 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)

  • install the plugins manually or automatically image

  • setup the first admin user

  • Jenkins is up and running image

  • inspect the Jenkins mount volume location docker volume inspect jenkins_home image

  • we can manage and configure build tools and package managers from the Tools tab under the manage Jenkins option: image

  • To log in as a root user to the Jenkins container, use -u flag : image

  • installing nodejs and NPM in the container, we logged in as root user

  • checking the Linux distro container is using image

  • download node: https://deb.nodesource.com/ curl -fsSL https://deb.nodesource.com/setup_20.x | bash - sudo apt-get install -y nodejs image

    apt-get install -y nodejs image

  • 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

    • on clicking the build now button, the job gets run image

    • we can see the console output on clicking that tab image

    • integrate with the GitHub account to start the build process as soon as changes are committed to the repo image

    • the build now takes longer than the previous one because of this integration image image

    • added new branch "jenkins-job", created test.sh script to print npm version image

    • build this branch in Jenkins image

    • in the shell command give execute permission to Jenkins user to run test.sh image

    • run the build image

    • 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 image

    • the Jenkins service user doesnt have permission to read, execute docker commands yet image now we are able to execute all the docker commands in the jenkins container image

  • 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: image

    image

    image

    build results: image image

  • 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)

    image

  • Add permission to jenkins user to execute docker commands image

    -- not working, getting docker cli not found error image

  • 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

  • login as jenkins user and try to run docker run hello-world image

ignore

  • lets try other way:

  • install docker cli inside the jenkins conatiner using below documentation, did

  • https://www.jenkins.io/doc/book/installing/docker/

  • now after exec in the jenkins container, we are able to use docker cmd now ignore

  • adding docker commands in job config now image

  • add docker hub creds in jenkins image

  • use the creds to login to docker hub for pushing the image image

  • add docker commands in job to build and push the image image

  • pushed successfully image image

  • we see a warning to secure creds, so we now give the password as stdin inplace of cli parameter image image

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published