Jenkins in 5 Minutes

What Is Continuous Integration

Continuous integration, or CI for short. As software development grows ever more complex, how team members can better work together to ensure software quality has slowly become an unavoidable problem in the development process. Especially in recent years, as Agile has become increasingly popular in the software engineering field, being able to quickly adapt to constantly changing requirements while guaranteeing software quality has become particularly important. Continuous integration is a software development practice aimed precisely at this class of problems. It advocates that team members must integrate their work frequently, possibly many times a day. Each integration is verified through an automated build, including automatic compilation, release and testing, so that integration errors are found as quickly as possible and the team can develop cohesive software faster.

Characteristics of continuous integration:

  • It is an automated, periodic integration testing process: everything from checking out code, compiling and building, running tests, recording results to test statistics is done automatically, with no manual intervention required;

  • A dedicated integration server is needed to perform the integration builds;

  • Code hosting tools are needed for support, such as Git and the visual interface Gogs

The role of continuous integration:

  • It guarantees the quality of the code committed by team developers and eases the pressure when releasing software;
  • Every step in continuous integration is done automatically, without much manual intervention, which helps reduce repetitive processes and saves 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 as they occur, provide detailed log files and alerting, and also display the trend and stability of project builds visually in chart form.

Features:

  • Easy to install: just two docker commands and you can download and run it straight from the official site, with no extra installation and certainly no database to install;
  • Easy to configure: it provides a friendly GUI configuration interface;
  • Change support: Jenkins can fetch from code repositories (Subversion/CVS) and produce a list of code changes, outputting them in the build output information;
  • Permalink support: users access Jenkins through the web, and the link addresses of these web pages are all permalinks, so you can use those links directly in all kinds of documents;
  • E-Mail/RSS/IM integration: when an integration completes, these tools can tell you the result in real time (as far as I know, a build takes a certain amount of time, and with this feature you can do other things while waiting for the result);
  • JUnit/TestNG test reports: that is, the ability to provide detailed test reports in chart form and so on;
  • Distributed builds: Jenkins can distribute integration builds and other work across multiple machines;
  • File fingerprint information: Jenkins keeps build records of which integration build produced which jars files, which integration build used which version of the jars files, and so on;
  • Third-party plugin support: this makes Jenkins more and more powerfulJenkins installation and startup (1) Run the install command, download jenkins

Installing and Starting Jenkins

1) Run the install command to download jenkins

1
docker pull jenkins/jenkins

(2) Start the service
1
docker run -p 8080:8080 -p 50000:50000 -v /mnt/data/jenkins:/var/jenkins_home --name "jenkins" jenkins/jenkins

erik.xyz

If you get the following error:

1
2
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 the local data volume is mapped, the /mnt/data/jenkins directory is owned by the root user while the uid of the jenkins user inside the container is 1000

1
sudo chown -R 1000:1000 /mnt/data/jenkins

(3) Visit the link http://10.20.29.151:8080

If you forget the password, go into the container and run

1
cat /var/lib/jenkins/secrets/initialAdminPassword

to get the initial password string.

If the directory doesn’t exist, you can use

1
find / -name "initialAdminPassword" -depth -print

to look for it.
erik.xyz

(4) Install plugins
erik.xyz
erik.xyz

(5) Create a user
erik.xyz

Once installation is complete, enter the main interface
erik.xyz

Installing Jenkins Plugins

Let’s take installing the maven plugin as an example to demonstrate plugin installation

(1) Click the “Manage System” menu on the left, then click
erik.xyz

(2) Select the “Available plugins” tab, search for maven, choose Maven Integration in the list, and click the “Install without restart” button
erik.xyz

When you see the screen below, it means it’s done
erik.xyz

Global Tool Configuration

Select Manage System, Global Tool Configuration
erik.xyz

Automatic installation

Jenkins provides two ways to configure tools; let’s stick with maven as the example
erik.xyz

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

Local installation

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

Now let’s show you how to install Maven with a local repository:

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

(2) Extract it

1
tar zxvf apache-maven-3.5.4-bin.tar.gz

(3) Move the directory
1
mv apache-maven-3.5.4 /usr/local/maven

(4) Edit the setting.xml configuration file vi /usr/local/maven/conf/settings.xml and configure the local repository directory, with the following content
1
<localRepository>/usr/local/repository</localRepository>

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

Other examples:

(1) JDK configuration
erik.xyz

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

(2) Git configuration (Git software is already installed locally)
erik.xyz

(3) Maven configuration
erik.xyz

Uploading Code to the Git Server

Steps:

(1) Install git locally (Windows version)

(2) In IDEA select the menu : File — settings , and in the window select Version Control — Git
erik.xyz

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

Select Git

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

(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 Jobs

Let’s take the hottest Java projects and Go projects as examples, and demonstrate for 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
erik.xyz
(2) General management, where you can add a project description and a GitHub project path, along with some configuration
erik.xyz
(3) Source Code Management, select GitHub
erik.xyz
erik.xyz
erik.xyz

(4) Build Triggers, configure the trigger rules. Here we take scheduled and polling as examples; configure them respectively as
erik.xyz

Scheduled build: build the job once on a schedule

Poll SCM: check on a schedule whether the code in source code management has been updated; if so, build, otherwise don’t

As shown in the figure, the scheduled build builds once every 10 minutes, and Poll SCM polls for changes every 5 minutes.

The * time rule is: minute hour day month weekday

(5) Build Environment, configure console output timestamps and specify the Go language version
erik.xyz
(6) Build, use a Shell script to test whether the project is valid after the code is uploaded
erik.xyz

The Shell is as follows:

1
2
3
4
export GOPATH=$WORKSPACE/../                    # specify the GOPATH path; Go must have a GOPATH path to run
export GOWORK=$GOPATH/src/github.com/Jenkins # create the directory dependency structure for running the project
cp -rf $WORKSPACE/* $GOWORK/ # isolate the test run data from the source data
cd $GOWORK && go build # enter the project directory and execute

Command explanation:

$GOPATH Running Go requires specifying GOPATH, that is, the project run path
$WORKSPACE /var/jenkins_home/workspace/Jenkins
GOWORK creates an execution directory that matches the code dependencies
Note: when building, the Go plugin download will automatically set GOROOT for us, but it will 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
erik.xyz
erik.xyz

The build succeeded, and the WORKSPACE, GOPATH and GOROOT directories were also output, which shows the configuration took effect. Enter the docker container or the mounted directory to check whether there is an executable file:
erik.xyz
Besides the methods above, you can also build, deploy and run projects through shell-configured docker and so on, and you can configure a project to run on the current 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
erik.xyz

(2) Source Code Management, select Git
erik.xyz
(3) Build
erik.xyz

Command:

1
clean package docker:build -DpushImage

used to clean, package and build the docker image

Finally click the “Save” button

(4) Run the job

For the rest, please refer to the run steps for the Go project.

Reprinted from: 架构师社区