Showing posts with label Jenkins. Show all posts
Showing posts with label Jenkins. Show all posts

Using Docker in Jenkins Pipeline Job | Jenkins Docker Plugin

Using Docker with Jenkins offers several advantages within our continuous integration and deployment(CI/CD) pipelines. Well, docker allows for containerization, so our applications and their dependencies can be bundled together in a lightweight, and isolated environment.




Here are a few points on why we should use Dockers with Jenkins Pipeline Jobs:

Easy Environment Setup: Docker provides a consistent and stable environment for doing our build, test, and deployment processes. With Docker, we can package our application and its dependencies into a container, ensuring that it runs consistently across different environments wheater QA or Dev, or Production.

Scalability: Docker will enable easy scaling of our Jenkins infrastructure. We can create and manage multiple Jenkins docker containers as agents to distribute the workloads and handle increased job demands efficiently.

Isolation and Resource Efficiency: Each Jenkins job can be executed in its own Docker container, providing isolation from other jobs in our Jenkins server. This isolation ensures that any changes or dependencies required by a specific job won't impact others. Additionally, Docker allows you to make optimal use of system resources by allocating them precisely to each container.

Faster Build and Test Cycles: Docker's are lightweight in nature and as such will have fast startup time, this contributes to faster build and test cycles. With Docker, you can easily spin up multiple containers, run tests, and tear them down quickly, leading to improved overall development speed and productivity.

Reproducible Builds: Docker enables reproducible builds by encapsulating the application code, dependencies, and environment configuration within a container. This ensures that the build process remains consistent, regardless of the underlying infrastructure.

Simplified Deployment: Docker containers can be easily deployed to various environments, including QA or development, staging, and production. This simplifies the deployment process and helps ensure that the application runs consistently across different environments, regardless of the underlying infrastructure.

In this following video let's see how to use docker with Jenkins Pipeline Jobs. Note that you will need to install docker in the host where you will be running the job.


Thank you! Cheers!


Read More »

Jenkins - How To Add a Agent node or Slave Node

1. Check the slave node and necessary packages(java) are installed:
 [root@nx-psowifi ~]# cat /etc/redhat-release  
 CentOS Linux release 7.4.1708 (Core)

 [root@nx-psowifi ~]# java -version  
 openjdk version "1.8.0_151"  
 OpenJDK Runtime Environment (build 1.8.0_151-b12)  
 OpenJDK 64-Bit Server VM (build 25.151-b12, mixed mode)  
If Java-8 is not installed, you may install it with the command: yum -y install java-1.8.0-openjdk

2. Create a user(jenkins) on the slave node:
 [root@nx-psowifi ~]# useradd -d /var/lib/jenkins jenkins  
 [root@nx-psowifi ~]# id jenkins  
 uid=454(jenkins) gid=100(users) groups=100(users)  
This user and the folder path will be used in Jenkins side configuration below.

3. Generate ssh key for server to login:
In the slave node go to some folder, maybe /tmp, and type ssh-keygen. Follow the prompts, and do note down the passphrase entered.
 [root@nx-psowifi tmp]# ssh-keygen  
 Generating public/private rsa key pair.  
 Enter file in which to save the key (/root/.ssh/id_rsa):  
 Enter passphrase (empty for no passphrase):  
 Enter same passphrase again:  
 Your identification has been saved in /root/.ssh/id_rsa.  
 Your public key has been saved in /root/.ssh/id_rsa.pub.  
 The key fingerprint is:  
 SHA256:wZ/hs075xboarhPAJyFsiXz9PdMAw4CDCbphgguRSkk root@nx-psowifi  
 The key's randomart image is:  
 +---[RSA 2048]----+  
 |+E.= +.oo.    |  
 |++= O o....   |  
 |O. o + oo..o   |  
 |=+  + o++o.  |  
 |o   +S =o   |  
 |    . + .  |  
 |     .=  o |  
 |    .+ o o  |  
 |    .o+.+.  |  
 +----[SHA256]-----+  
The above will generate 2 key files in .ssh folder in the location(specified above).
[general@nx-psowifi ~]$ ls /root/.ssh/
id_rsa  id_rsa.pub

The "id_rsa.pub" is the public key.
The "id_rsa" private key will be used in Jenkins UI while configuring the slave node.

4. Copy public key to the authorized_keys file of jenkins user, and change owner of the file:
 [root@nx-psowifi ~]# mkdir /var/lib/jenkins/.ssh  
 [root@nx-psowifi ~]# cp /root/.ssh/id_rsa.pub /var/lib/jenkins/.ssh/authorized_keys  
 [root@nx-psowifi ~]# cd /var/lib/jenkins/.ssh/; chown jenkins:users authorized_keys  

5. Configure Jenkins to add the slave node:
Log in to Jenkins UI, go to Manage Jenkins, then Manage Nodes, then click New Node, give your node a name, then select Permanent Agent and click OK.
Fill up or select all the appropriate values as in the screenshot below, and wait before clicking save.

For Credentials, select the button Add -> Jenkins, and you'll get a screen as below:

Enter as below:
Username = jenkins
Private Key = select "Enter directly". Copy the contents of private key "id_rsa" from step3 above, into the Key field.
Passphrase = Enter the passpharse, noted down during step3.
You may enter or leave blank a descriptive id and description, then click Add.
You may now save the New Node creation.

6. Check the new slave node is working fine:
Go to Jenkins UI, go to Manage Jenkins, then Manage Nodes, then click our just created node name, then in the subsequent page click "Launch agent" button.
If all configuration are fine, you should see output in the same page something like below:
 ...........  
 ...........  
 [12/20/18 00:05:05] [SSH] Checking java version of java  
 [12/20/18 00:05:05] [SSH] java -version returned 1.8.0_151.  
 [12/20/18 00:05:05] [SSH] Starting sftp client.  
 [12/20/18 00:05:05] [SSH] Copying latest slave.jar...  
 [12/20/18 00:05:06] [SSH] Copied 745,674 bytes.  
 Expanded the channel window size to 4MB  
 [12/20/18 00:05:06] [SSH] Starting slave process: cd "/var/lib/jenkins" && exec java -jar slave.jar  
 <===[JENKINS REMOTING CAPACITY]===>channel started  
 Remoting version: 3.14  
 This is a Unix agent  
 Evacuated stdout  
 Agent successfully connected and online  
 ...  
Read More »