قالب وردپرس درنا توس
Home / Tips and Tricks / How to install and use Jenkins to build a CI / CD pipeline – CloudSavvy IT

How to install and use Jenkins to build a CI / CD pipeline – CloudSavvy IT



jenkins logo

For software companies that follow a smooth development process, it is an intensive process to release software every day. Jenkins is a tool that can speed up your workflow by automating many of the repetitive tasks, such as building, testing and releasing.

What is Jenkins?

Jenkins was originally built to serve a single purpose ̵

1; to automate the construction and testing of nightly software buildings. New commitments must be regularly integrated into the master branch (often called “continuous integration”), which often means heavy testing to ensure that everything goes smoothly. Doing it once a week is one thing, but when you integrate several times a day, it is better for everyone that it should be an automatic process.

Jenkins and other CI / CD solutions as it speeds up this process. You can think of it as an automated shell script. For example, to release a new version of a React app, you may need to run npm install, npm run build, then runs a test suite like Jest to verify that the new build passes all tests. If successful, you may want to send it to a test environment for manual review or simply publish a new version immediately. All of these are commands that you can script quite easily.

Jenkins can handle all of this information as part of a pipeline. When Jenkins detects changes in your source control (either on the master or a feature branch), it will start the automatic pipeline and run through each task you have given it. Some tasks are as simple as bash commands, other tasks can interface with an external service like Jira, Git or your email provider. Jenkins is also fully extendable with plugins, and can really be made to do whatever you want.

Jenkins has two major releases, Blue Ocean and Classic. Blue Ocean is newer and contains a streamlined user experience that makes creating pipelines much easier. We will use Blue Ocean for this guide, but most of the same concepts will apply to both versions of Jenkins.

Install Jenkins

Jenkin binaries are available for multiple operating systems, but usually not from standard package managers. If you are on Ubuntu, you can download and install with the following commands:

wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
sudo sh -c 'echo deb https://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt-get update
sudo apt-get install jenkins

To make things independent, we run Jenkins with Docker, which Jenkins still recommends.

Install Docker

Docker is a platform for running apps in “containers;” The container contains all the necessary dependencies and will ensure that the app runs the same regardless of the basic operating system – all you need to do is install Docker for your system and run some commands.

For Ubuntu, you need to install some prerequisites:

sudo apt install apt-transport-https ca-certificates curl software-properties-common

Then add the Dockers GPG key:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

And add repo yourself:

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"

Update your sources:

apt update

And finally install Docker from apt:

sudo apt install docker-ce

Docker should now run on your system, which you can check with systemctl.

Set up Jenkins

When Docker is running, you can set up the Docker container for Jenkins. You first need a bridged network for the containers to communicate on:

docker network create jenkins

Docker is of course volatile – all data stored on containers will be deleted when these containers are stopped. To prevent this, you want to store data on Docker volumes, which will continue on the disk. You will need two volumes, one for some TLS certificates that Jenkins needs to connect to Docker, and the other for all of your Jenkins data. These will be tied to the container while driving.

docker volume create jenkins-docker-certs
docker volume create jenkins-data

Jenkins must actually be able to run Docker as part of his business, to create construction environments. This is not possible with normal Docker, so for the function to work properly, you must run “Docker in Docker” or DinD. The following command runs Dockers official docker:dind container, bind the network and volumes you created in the previous steps to it and publish it as a service running on port 2376 for the Jenkins container to use. You are free to change this port if you want, although you will have to change it in the next step as well.

docker container run --name jenkins-docker --rm --detach 
--privileged --network jenkins --network-alias docker 
--env DOCKER_TLS_CERTDIR=/certs 
--volume jenkins-docker-certs:/certs/client 
--volume jenkins-data:/var/jenkins_home 
--publish 2376:2376 docker:dind

With that setting, you can drive jenkinsci/blueocean container with the following command:

docker container run --name jenkins-blueocean --rm --detach 
--network jenkins --env DOCKER_HOST=tcp://docker:2376 
--env DOCKER_CERT_PATH=/certs/client --env DOCKER_TLS_VERIFY=1 
--volume jenkins-data:/var/jenkins_home 
--volume jenkins-docker-certs:/certs/client:ro 
--publish 8080:8080 --publish 50000:50000 jenkinsci/blueocean

This will mount the network and devices, set DOCKER_HOST variable to the port that DinD runs on and publish the service on port 8080, which you can change if it is not free. (The format is host:container.) It also publishes the Jenkins administrative connection on port 50000, if you plan to set up a master Jenkins server with multiple distributed build connections to it.

Once Jenkins is up and running, it will be accessible via a web browser on the host’s port 8080. You will need to do some installation before Jenkins is fully usable, the first of which will authenticate yourself to prove you are the owner of the server and not a bot attacking a vulnerable web interface.

jenkins log in

You must enter a password stored in /var/jenkins_home/, which is part of the Docker volume. To access it, you have to drive cat inside the Docker container:

docker container exec -it jenkins-blueocean cat /var/jenkins_home/secrets/initialAdminPassword

This will print the password, which you can copy and begin the rest of the installation.

You will be asked to configure your admin username and password, install various plugins. Selecting “Install recommended plugins” will only install a variety of recommended community to boot. You can always install more later.

Create a pipeline

Once you have installed Jenkins, you will be greeted with the following welcome screen. Click “Create a new pipeline” to get started.

jenkins creates a new pipeline

You must choose where your code is to be stored. You can link your Github or BitBucket account directly with an access key.

jenkins joins git

But a better solution is to simply choose the generic “Git.” Enter in your URL, and Jenkins will give you a public key. Because Jenkins can make commitments (and always make changes to the pipeline configuration), you should create a new service user and add the public key to it.

jenkins git config

Jenkins takes a second to connect to Git, then goes to a page where you can edit the pipeline settings. Jenkins stores the entire pipeline configuration in a Jenkins file, located at the root of your archive. When you make pipeline updates in the editor, Jenkins will make the change to your Jenkins file.

jenkins pipeline builder

Each pipeline will have a few different steps, such as Build, Test or Deploy, which will contain individual steps. These can do all sorts of things, like sending emails, interacting with other services like Jira and Git, and coordinating the flow of other steps, but you usually use these to run simple shell scripts. Any failure to return these scripts will cause the pipeline to fail.

Before adding any steps, you want to configure the environment settings. Usually you use a Docker container, e.g. node:latest.

In this example, we are building a Node-based web app. Just add two steps for npm install and npm run build is all that is necessary. Jenkins will execute these commands and move on to the next step, with the building items in place. For the test phase, it requires that all tests must pass for the construction to be successful if you install a shell script to run Jest.

Jenkins can run multiple steps in parallel, which is useful if you need to test on several different platforms. You can set up a “Test on Linux” and “Test on Windows” scene and let them run at the same time so that you do not wait for one to start the other. If any of them fail, the pipeline will still fail.

When you are done editing, Jenkins automatically starts your pipeline. If you click on it for more information, you can see the development of the construction through the stages.

jenkins builds the screen

The top bar turns red if there is an error, and green if everything is successful. If you encounter errors, you can click on the infringing stages to see the console run on the commands that cause the pipeline to fail. You also want to check your environmental configuration to make sure all the necessary tools are installed. (You may want to set up your own Docker container for your builds.)


Source link