Hello everyone, I am xiao CAI, a desire to do CAI Not CAI xiao CAI in the Internet industry. Soft but just, soft praise, white piao just! Ghost ~ remember to give me a three – even oh!

Jenkins is mainly introduced in this paper

Refer to it if necessary

If it is helpful, do not forget the Sunday

Wechat public number has been opened, xiao CAI Liang, did not pay attention to the students remember to pay attention to oh!

“Alas, every day after submitting the code, I have to package it and deploy it to the test environment and development environment. It’s turning into operations.”

“Ah? Oh! It’s true, packaging and deploying every day has become a tool man.”

A simple conversation quickly disappeared in the office, but triggered my thinking, “Such a troublesome process must have a good solution, after all, programmers are oriented to lazy programming, I have heard about Jenkins tool for a long time, it seems that I have to start it today.”

Let’s talk about JenKins today. After that, you might as well complete your project and give yourself more time for paddling.

Notice before reading: this article is long, from installation to use, step by step with you super god!

Wechat public attention: [Xiao CAI Liang Ji], take you to appreciate the technology coquetty!

What is Jenkins

Jenkins is an open source software project. It is a Continuous integration tool developed based on Java for monitoring continuous repetitive work. It aims to provide an open and easy to use software platform to make continuous integration of software possible.

In short, it is a continuous integration tool!

1. Continuous integration

Continuous Integration (CI). Before frequently integrating code into the trunk, automated tests must be passed, and integration cannot happen if only one test case fails. With continuous integration, teams can move quickly from one feature to another.

Benefits:

  • Risk reduction, problems can be found early due to continuous integration to build, compile, and test
  • Reduce repetitive tasks
  • Continuous deployment, providing deployable unit packages
  • Continuously deliver available versions

2. Jenkins continuous integration

Let’s take a look at Jenkins’ role here:

  • First, the developer commits the code to a Git repository
  • Then Jenkins used the Git plug-in to pull the code from the Git repository, and then cooperated with JDK and Maven to complete the code compilation, test, review, test and packaging
  • Finally Jenkins pushed the generated JAR/WAR to the test/production server for user access

After the whole process, all we had to do as developers was submit the code, and Jenkins did the rest of the work.

Two, Jenkins installation

Sharpening knife does not mistakenly cut wood workers, no knife said more are virtual. Let’s take a look at how Jenkins installs it.

1. Install the JDK

Since Jenkins is written in Java, the Java runtime environment must be configured to run it. The JDK installation process will not be discussed here

2. Download and install Jenkins

  • download

We can go to the download page to select the version we want to install: download address, we use the version is: Jenkins-2.190.3-1.1.noarch.rpm

  • The installation

Then upload the downloaded RPM package to our server and install it through RPM -ivh jenkins-2.190.3-1.1.noarch. RPM. Then edit the Jenkins configuration file in etc directory: Vim /etc/sysconfig/jenkins

JENKINS_USER="root"
JENKINS_PORT="8888"
Copy the code
  • Start the

systemctl start jenkins

  • access

Open a browser and visit http://server IP address :8888/. If the following page is displayed, the server is successfully started

We then get the password from the specified file on the server and proceed to the next step.

This step we can skip the plug-in installation, because Jenkins plug-in needs to be connected to the default official website to download, which is very slow:

Then we add an administrator account to manage:

If you see the following page, the setup is successful:

Wechat public attention: small dishes, take you to enjoy the technology of coquettish!

Third, Jenkins use

1. Plug-in acceleration

To do a good job, he must sharpen his tools

First go to Jenkins -> Manage Jenkins -> Manage Plugins and click Install

Then we in Jenkins server installation into the/var/lib/Jenkins/updates directory, you can see there is a default. The json file, the first step: * * * * we need to replace the inside part of the field, type the command is as follows:

sudo sed -i 's#updates.jenkins.io/download/plugins#mirrors.tuna.tsinghua.edu.cn/jenkins/plugins#g' default.json && sudo sed -i 's#www.google.com#www.baidu.com#g' default.json
Copy the code

** Step 2: * * we enter to the directory/var/lib/Jenkins, edit Hudson. Model. UpdateCenter. Xm, Will be changed to https://updates.jenkins.io/update-center.json http://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json

Final step: Enter the following command to restart Jenkins:

systemctl restart jenkins
Copy the code

After the above steps, we are happy to install the plug-in!

2. User management

In Jenkins, we can also manage user authority, and at this time, we need to use plug-in role-based Authorization Strategy

  • First installationRole-based Authorization StrategyThe plug-in

  • Enable global security configuration

Switch the authorization Policy to “Role-based Strategy”

  • Create a user

After changing the authorization policy, we are ready to create Users. Go to Manage Users on the System administration page

Here we create two users, cbuc1 and cbuc2

  • Create the role

Once the user is created, we can create Roles. Go to Manage and Assign Roles on the admin page

Roles are divided into Global roles and Item Roles.

Global roles: Advanced users such as administrators can create globally-based roles

Item Roles: Roles that are specific to one or more projects

Our system now has two users, and we can bind the roles to these two users

3. Voucher management

What is a voucher? Credentials can be used to store database passwords that need ciphertext protection, GitLab password information, and Docker private warehouse login passwords. With this information saved, Jenkins can interact with these third-party apps. Of course, this is still with Jenkins plugins!

1) installation

First install the Credentials Binding plug-in

After installing the plug-in, you will have a credentials menu in the primary menu bar of the system

Click in, we can see that there are 5 kinds of credentials that can be added:

  1. Username with password: indicates the Username and password
  2. SSH Username with private key: The SSH user and key are used
  3. Secret file: the text file that needs to be kept Secret. When Jenkins uses it, he will copy the file to a temporary directory and set the file path to a variable. After construction, the Secret file copied will be deleted
  4. Secret text: An encrypted string of text that needs to be saved, such as a Pegbot or GitHub API token
  5. Certificate: Upload the Certificate file

The commonly used types are Username with password and SSH Username with private key

2) Git certificate management

If we were to use Jenkins to pull the project code from GitLab, we would have to use credentials to verify it.

  • Installing the Git plug-in

We need to install the Git plug-in in Jenkins to pull the project code

Then we need to install Git tools on the server as well:

#Install command
yum install git -y
#Validation command
git --version
Copy the code
1. Mode 1: Indicates the password type

We can log in with the user password and pull the project code. In this case, we need to use the Username with password type of the credential:

Once it has been created, we can test whether it is available. Let’s create a FreeStyle project first

Then copy our project URL in GitLab

Select the Credentials we just created in the Credentials, and after saving the configuration, we click Build Now to Build the project:

At this point you can see the output on the console

And then on into the server/var/lib/Jenkins/see we pull project workspace directory:

We have successfully pulled the Git project using the user password credential mode

2. Mode 2: SSH key type

In addition to using an account password to verify Git, you can also use SSH keys to verify Git. The procedure is as follows:

As can be seen from the figure, the public and private keys need to be generated in the first step, which is generated by inputting the following commands on Jenkins server:

Ssh-keygen -t rsa Enter the command and press Enter to generate public and private keys in the /root/.ssh/ directory.

  • Id_rsa: indicates the private key file
  • Id_rsa. pub: public key file

>Settings >SSH Keys. Copy id_rsa.pub and click “Add key”.

Then go back to Jenkins system page to add credentials, select SSH Username with private key, and copy the content of the private file just generated

Once added, a credential is generated

Once it has been created, we can test whether it is available. Let’s create a FreeStyle project first

Then copy our project URL in GitLab

Select the Credentials we just created in the Credentials, and after saving the configuration, we click Build Now to Build the project:

At this point you can see the output on the console

And then on into the server/var/lib/Jenkins/see we pull project workspace directory:

The SSH Username with private key authentication mode has been successfully used to pull Git project

4. Project management

1) Maven installation

Most of the projects we are currently developing are Maven projects. Using Maven projects, we need to do dependency management, so we should install Maven on the server to download project dependencies.

  • Install Maven

You can download the compressed package from the Maven website and upload it to the server for decompression

The tar - XZF apache maven - 3.6.0 - bin. Tar. Gz

  • Configuring environment Variables

vim /etc/profile

Export JAVA_HOME=/usr/lib/ JVM/java-1.8.0-openJDK export MAVEN_HOME=/home/maven/apache-maven-3.6.2 export PATH=$PATH:$JAVA_HOME/bin:$MAVEN_HOME/binCopy the code

To make the configuration file take effect after editing:

source /etc/profile
Copy the code

Validation:

mvn -v
Copy the code

Then set Maven’s settings.xml

#Create a local repository directoryMkdir/data/localRepo vim/home/maven/apache maven - 3.6.2 / conf/Settings. The XMLCopy the code

Change the local repository to: /root/repo/

Add ali cloud server address: alimaven aliyun maven central at http://maven.aliyun.com/nexus/content/groups/public/

  • Jenkins configuration

In Jenkins we also need to configure the JDK and Maven association.

Enter Jenkins -> Global Tool Configuration -> JDK

Enter Jenkins -> Global Tool Configuration -> Maven

Adding global variables

Go to Manage Jenkins->Configure System->Global Properties and add three Global variables

JAVA_HOME, M2_HOME, PATH+EXTRA

Then we go to the project and click Configure

Then add the shell execution script:

Save and rebuild, check the console, you can see MVN build successfully:

2) War package deployment

If our project is a WAR package and we need a Tomcat container to run it, we will install Tomcat first

Tomcat installation

Upload the Tomcat installation package to the server, decompress it using tar -xzf apache-tomcat-8.5.47.tar.gz, and run start.sh in the bin directory to start Tomcat. If the following information is displayed, Tomcat is successfully started:

Next, you need to configure Tomcat user role permissions. By default, Tomcat does not have user role permissions

First we need to modify the tomcat/conf/tomcat-users.xml file:

(Copy) contents as follows:

<role rolename="tomcat"/>
<role rolename="role1"/>
<role rolename="manager-script"/>
<role rolename="manager-gui"/>
<role rolename="manager-status"/>
<role rolename="admin-gui"/>
<role rolename="admin-script"/>
<user username="tomcat" password="tomcat" roles="manager-gui,manager-script,tomcat,admin-gui,admin-script"/>
Copy the code

Then modify/tomcat/webapps/manager/meta-inf/context. The XML file, add the following content:

Then enter the Tomcat page and click enter:

The account password is Tomcat

The success page is as follows:

With that done, tomcat is installed and ready to deploy

Tomcat deployment
  • injenkinsInstalled in theDeploy to containerThe plug-in
  • Add Tomcat credentials

  • Build configuration

Configure in the project’s Configure

Then click Build to see the console output:

Then visit the project page and see that the WAR package project is successfully deployed:

3) JAR package deployment

The deployment of war package project is described above, but we still use SpringBoot most of the project now. At this time, the typed jar type is printed, but The Tomcat container is built in SpringBoot. This way we do not need to rely on the use of external Tomcat containers.

  • First we download the Maven plugin from Jenkins, which will give you an option to create a New Project

Then configure the project as follows:

Repository URL: library address Credentials: Branch Specifier (blank for ‘any’) : Branch

Run only if build succeeds: Perform subsequent steps upon successful build Add post-build step: Add post-build steps Send files or execute Commands over SSH: Send files or run commands over SSH

  • The installationPublish Over SSHThe plug-in

Since the server we are deploying to is not on the same server as Jenkins, we need this plug-in for remote deployment

After installing the plug-in, we need to configure the remote server first. Enter ssh-copy-id remote server IP address on the Jenkins server to copy the public key to the remote server, and then add the server information in the Jenkins system configuration as follows:

After completing the above steps, we can go back to the project’s configure and add the server information we just configured:

Name: server configured in SSH Servers Source files: Source file Remove prefix: deletes the prefix Remote directory: uploads to the server Exec Command: executes the script

Complete the above steps and we can happily click Build Now!

4) Assembly line projects

There are many types of automatic build projects in Jenkins, and the following three are commonly used:

  • FreeStyle Project

  • Maven Project

  • Pipeline Project

Each type of build can actually complete the same build process and result, but there are differences in operation mode, flexibility, etc. Among them, the pipeline type is more flexible. We have tried the other two types in the above example. Let’s introduce how to build pipeline projects.

1. The concept

Pipeline is a set of workflow framework running on Jenkins, which connects the original independent operation with tasks of a single or multiple nodes to achieve complex process choreography and visualization that is difficult to be completed by a single task

2. The advantages
  • Code: Pipelines are implemented as code, usually checked into source control, enabling the team to edit, review, and iterate over their shipping process.
  • Persistence: Pipeline is recoverable from planned and unplanned server restarts
  • Stopable: A Pipeline can receive interactive input to determine whether to continue Pipeline execution
  • Multifunctional: Pipelines support the complex continuous delivery requirements of the real world. They support fork/join, loop execution, and parallel execution of tasks
  • Extensible: The Pipeline plug-in supports custom extensions to its DSL, as well as multiple options for integration with other plug-ins
3. Create a

Before creating the Pipeline project we need to install the Pipeline plug-in:

Then, when creating the project, there will be more Pipeline types:

After selecting the project type, we can configure in the project:

  • First, configure git address as usual, as above, here is not much redundant
  • Then configure the Pipeline script

Pipeline projects are managed through Pipeline scripts for greater flexibility

Hello World template:

pipeline {
    agent any
    stages {
        stage('Hello') {
            steps {
                echo 'Hello World'}}}}Copy the code

“Stages” : Refers to all stages of an assembly line. Usually, there is only one “stages” consisting of multiple stages

Stage: Represents the logic that needs to be executed in a stage. Steps include shell script, Git pull code, SSH remote distribution, and any other content

Declarative Pipeline template:

pipeline {
    agent any
    stages {
        stage('Pull code') {
            steps {
                echo 'Pull code'
            }
        }
        stage('Compile build') {
            steps {
                echo 'Compile build'
            }
        }
        stage('Project Deployment') {
            steps {
                echo 'Project Deployment'}}}}Copy the code

You also don’t have to worry about not being able to write Pipeline scripts, we can click on [Pipeline Syntax] to jump to the Pipeline code generation page

Once the script is written, click Build and you can see the entire build process:

If we need to deploy to different environments, such as production and development, we can also configure in the project’s Configure:

  • First, you need to install the Extended Choice Parameter plug-in
  • Then add the Extended Choice Parameter Parameter to the configuration

After the above configuration is complete, click Save and at this point we can choose the server to deploy at build time

We can then read the selected parameters from the Pipeline script and paste the project’s build script as follows:

Node {//git certificate ID def git_auth = "7fdb3fa3-74EB-4862-b36f-c03701f71250" //git url def git_URL = "[email protected]:cbuc_group/cbuc_web.git" def selectedServers = "${publish_server}".split(",") Stage (' started to pull the code ') {checkout ([$class: 'GitSCM, branches: [[name:' * / v3.0]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: git_auth, url: Git_url]]])} stage(' start package ') {sh "mvn-dmaven.test.skip =true clean package"} stage(' start remote deployment ') { Deploy for(int j=0; j<selectedServers.length; Def currentServerName = selectedServers[j] // Production deployment directory def pro_address = "/home/pro/ Java "/ / development deployment directory If (currentServerName=="pro"){sshPublisher(publishers: [sshPublisherDesc(configName: 'pro_server', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: 'sh build.sh', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: pro_address, remoteDirectorySDF: false, removePrefix: 'target', sourceFiles: 'target/cbuc_web-0.0.1- snapshot.jar ')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)]) }else if(currentServerName=="dev"){ sshPublisher(publishers: [sshPublisherDesc(configName: 'dev_server', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "sh build.sh", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: dev_address, remoteDirectorySDF: false, removePrefix: 'target', sourceFiles: 'target/cbuc_web-0.0.1- snapshot.jar ')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)]) } } } }Copy the code

Another possibility is that if the server where Jenkins is deployed goes down, the Pipeline script file will be lost and rewriting will be troublesome, so we can put the script file in the root directory of our project. Then configure the location of the Pipeline script file in configure:

Then we click Build and see that the result is also successful:

5) Build triggers

We’ve covered several ways to build projects, all of which are built manually by clicking build, or we can also build through triggers

Commonly used are:

1. Build After Other Projects Are Built

Trigger after other projects build. Fill in the options for the items we care about. There are also 3 options to choose from:

Trigger only if build is stable: Executed only when the project is steadily built

Trigger even if the build is unstable: execute even if the project build is unstable

Trigger even if the build fails: Execute even if the project fails to build

2. Build Periodically

A timed build. The syntax type is cron expression, and the timing string is minute hour day month week from left to right

3. Poll SCM

Polling SCM. Specifies a time to scan the local repository for code changes and trigger a project build if the code changes.

4. Trigger builds remotely

Remotely trigger the build. Through the use of we define key, and then access building address: http://192.168.100.131:8888/job/test01/build? token=123123

5. Automatically trigger the build

We just saw that in Jenkins’ built-in build trigger, the polling SCM can implement Gitlab code updates and projects are built automatically, but the performance of this solution is not good. Is there a better plan? There is. Is to use Gitlab’s Webhook implementation code push to the warehouse, immediately trigger the automatic construction of the project.

To complete the auto-trigger build we need to install plugins in Jenkins: GitLab Hook and GitLab

Then we can see an additional option in the Build Trigger:

Copy the WebHook address and go to the GitLab page to set it:

Path steps: Admin Area -> Settings -> Network

Then we set it in the corresponding project:

Go back to the Jenkins page and do the following configuration: Manage Jenkins->Configure System

With the above configuration done, we are happy to have code that automatically triggers the build!

END

This article is long, full of dry goods, from installation to use, step by step to take you into the pit of operation and maintenance, after learning this article quickly to your project use it! The road is long, xiao CAI seeks together with you!

Today you work harder, tomorrow you will be able to say less words!

I am xiao CAI, a man who studies with you. 💋

Wechat public number has been opened, xiao CAI Liang, did not pay attention to the students remember to pay attention to oh!