Installing and Using Jenkins

Author: 回首笑人间, a senior Java engineer, passionate about researching open source technologies, and a partner of the Architect Community!
Contents

What Is Continuous Integration
Introduction to Jenkins
Installing and Starting Jenkins
Installing Jenkins Plugins
Global Tool Configuration
Automatic Installation
Local Installation
Uploading Code to the Git Server
Creating and Running a Job
Go Project
JAVA Project
What Is Continuous Integration
Continuous integration, CI for short

As software development grows ever more complex, how team members collaborate better to ensure software quality has slowly become an unavoidable issue in the development process. Especially in recent years, as Agile has become more and more popular in the software engineering field, how to adapt quickly to constantly changing requirements while guaranteeing software quality has become particularly important.

Continuous integration is exactly a software development practice aimed at this class of problem. It advocates that team members integrate their work frequently — sometimes many times a day. Each integration is verified by an automated build, including automatic compilation, release, and testing, so that integration errors are found as early as possible and the team can develop cohesive software faster.

The characteristics of continuous integration:

It is an automated, periodic integration testing process: checking out code, compiling and building, running tests, recording results, and test statistics are all done automatically without human intervention;
It requires a dedicated integration server to run the integration builds;
It requires the support of code hosting tools, such as Git and the Gogs web interface
The role of continuous integration:

It guarantees the quality of the code developers commit, and relieves the pressure when releasing software;
Every stage of continuous integration is done automatically with little human intervention, which helps cut repetitive work and save time, cost, and effort;
Introduction to Jenkins
Jenkins, originally named Hudson, took its current name in 2011. It is an open source software tool for implementing continuous integration. Official website: http://jenkins-ci.org/.

Jenkins can monitor errors in integration in real time, provide detailed log files and notification features, and present the trends and stability of project builds visually in chart form.

Features:

Easy to install: just two docker commands download it from the official site and run it directly, with no extra installation, let alone installing a database;
Easy to configure: friendly GUI configuration interface;
Change support: Jenkins can pull from code repositories (Subversion/CVS) and produce a list of code updates, outputting it in the build output;
Permalink support: users access Jenkins over the web, and those web page links are all permanent addresses, so you can use the link directly in all kinds of documents;
E-Mail/RSS/IM integration: when an integration finishes, these tools tell you the result in real time (as far as I know, building an integration takes some time, and with this feature you can do other things while waiting for the result);
JUnit/TestNG test reports: that is, providing detailed test reports in chart form and so on;
Distributed builds: Jenkins can distribute integration builds and other work across multiple machines;
File fingerprinting: Jenkins keeps build records of which integration build produced which jars files, and which integration build used which version of the jars files;
Third-party plugin support: this makes Jenkins more and more powerful
Installing and Starting Jenkins
(1) Run the install command to download jenkins

docker pull jenkins/jenkins
(2) Start the service

docker run -p 8080:8080 -p 50000:50000 -v /mnt/data/jenkins:/var/jenkins_home —name “jenkins” jenkins/jenkins

If you get the following error:

touch: cannot touch ‘/var/jenkins_home/copy_reference_file.log’: Permission denied
Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?
You need to fix the directory permissions, because when mapping the local data volume, the owner of the /mnt/data/jenkins directory is the root user, while the jenkins user in the container has uid 1000

sudo chown -R 1000:1000 /mnt/data/jenkins
(3) Visit the link http://10.20.29.151:8080

If you forget the password, you can enter the container and run cat /var/lib/jenkins/secrets/initialAdminPassword to get the initial password string.
If the directory does not exist, you can use the find / -name “initialAdminPassword” -depth -print command to look for it.

(4) Install plugins

(5) Create a new user

The installation is complete; enter the main interface

Installing Jenkins Plugins
Let’s use installing the maven plugin as an example to demonstrate plugin installation

(1) Click the “System Management” menu on the left, then click

(2) Select the “Optional Plugins” tab, search for maven, choose Maven Integration in the list, and click the “Install directly” button

When you see the image below, it is done

Note: if your project is a GitHub, GitLib, or Go language project, then I think you will want a corresponding plugin like this to do the work for you.

Global Tool Configuration
Select System Management, then Global Tool Configuration

Automatic Installation

Jenkins provides two ways to configure tools; let’s again use maven as the example

The first is as shown above: you only need to select automatic installation and a version number. Jenkins also gives you a help button in the upper right corner, where you can see explanations and examples.

Local Installation

Compared with the first approach, the second is a bit more troublesome, but the advantage is that you don’t have to download again when packaging later, which shortens the packaging time.

Here is how to install Maven and a local repository:

(1) Upload the Maven archive to the server (virtual machine)

(2) Extract

tar zxvf apache-maven-3.5.4-bin.tar.gz
(3) Move the directory

mv apache-maven-3.5.4 /usr/local/maven
(4) Edit the settings.xml configuration file with vi /usr/local/maven/conf/settings.xml and configure the local repository directory, as follows

/usr/local/repository
(5) Upload the local repository from your development environment to the server (virtual machine) and move it to /usr/local/repository.

mv reponsitory_boot /usr/local/repository
Other examples:

(1) JDK configuration

Set javahome to /usr/java/jdk1.8.0_171-amd64

(2) Git configuration (Git is already installed locally)

(3) Maven configuration

Uploading Code to the Git Server
Steps:

(1) Install git locally (Windows version)

(2) In IDEA select the menu: File — settings, then in the window choose Version Control — Git

(3) Select the menu VCS —> Enable Version Control Integration…

Select Git

(4) Set the remote address: right-click the project and select the menu Git —> Repository —>Remotes…

(5) Right-click the project and select the menu Git —> Add

(6) Right-click the project and select the menu Git —> Commit Directory…

(7) Right-click the project and select the menu Git —> Repository —> Push …

Creating and Running a Job
Using the hottest Java and Go projects as examples, we will show you respectively how to build and run a project.

Go Project

(1) Go back to the home page and click the new item button. As shown below, enter a name, choose to create a freestyle project, and click OK

(2) General management, where you can add a project description and a GitHub project path, as well as some other configuration

(3) Source Code Management, select GitHub

(4) Build Triggers, configure the trigger rules; here we take scheduled and polling as examples, with the settings as

Scheduled build: build the job once on a schedule

Poll SCM: periodically check whether the code in Source Code Management has been updated; if it has, build, otherwise don’t

As shown in the image, the scheduled build builds once every 10 minutes, and Poll SCM checks once every 5 minutes.

The rule for the time * fields is: minute hour day month week
(5) Build Environment, configure timestamps for console output and specify the Go version

(6) Build, use a Shell script to test whether the project works after the code is uploaded

The Shell is as follows:

export GOPATH=$WORKSPACE/../ # Specify the GOPATH path; running Go requires a GOPATH path
export GOWORK=$GOPATH/src/github.com/Jenkins # Create the directory dependency structure for running the project
cp -rf $WORKSPACE/* $GOWORK/ # Keep the test run data isolated from the source data
cd $GOWORK && go build # Enter the project directory and run
Command notes:

$GOPATH: running Go requires a GOPATH to be specified, i.e. the project run path
$WORKSPACE /var/jenkins_home/workspace/Jenkins
GOWORK: create an execution directory that matches the code dependencies
Note: the Go plugin download sets GOROOT automatically for us during the build, but does not specify GOPATH, so it needs to be specified
Finally click the “Save” button

(7) Run the build and check the output in the console

The build succeeded, and it also output the WORKSPACE, GOPATH, and GOROOT directories, meaning the configuration took effect. Enter the docker container or the mounted directory to check whether there is an executable file:

Besides the methods above, you can also build, deploy, and run the project by configuring docker through a shell, and you can configure the project to run on this or another server. I won’t go through every other configuration method one by one — dig into them yourself.

JAVA Project

(1) Go back to the home page and click the new item button. As shown below, enter a name, choose to create a Maven project, and click OK

(2) Source Code Management, select Git

(3) Build

Command:

clean package docker:build -DpushImage
Used to clean, package, and build the docker image

Finally click the “Save” button

(4) Run the job

Refer to the Go project steps from here on.
Original post: https://mp.weixin.qq.com/s/PEupc5YIKuctZO1Tivy_ug