Jenkins installation, configuration and operation to realize automatic construction

Time:2021-4-18

Jenkins installation, configuration and operation to realize automatic construction

1、 Preparation

Personal environment and tools: Jenkins 2.190.3 war package, Tomcat 9. X, Maven 3.60, JDK 13.0.1, GIT 2.22.1

  1. Download and unzip Tomcat to/opt/tomcat/
  2. Download and unzip Maven to/opt/maven/
  3. Download and unzip JDK to/usr/java/
  4. Download and unzip git to/usr/src/
  5. Put Jenkins.war Put it in the unzip directory of Tomcatwebapps/lowerwebapps/Jenkins.war

2、 Configure Tomcat

Because by default, Tomcat manager can only run from a browser running on the same machine.
At this time, we need to modify some configuration of tomcat, so that we can have access to the manager app and other pages of Tomcat remotely

Jenkins installation, configuration and operation to realize automatic construction

1. Configuration file location: opt / Tomcat / conf / Tomcat- users.xml

<! -- add at the end -- >
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx" />
<role rolename="manager-status"/>
<user username="tomcat" password="123456"  roles="manager-gui,manager-script,manager-jmx,manager-status" />

2. Add in both configuration files|\d+\.\d+\.\d+\.\d+

Two profile locations:

/opt/tomcat/webapps/manager/META-INF/context.xml
/opt/tomcat/webapps/host-manager/META-INF/context.xml
<! -- FA 1: direct annotation, invalid for me -- >
<!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->

<! -- Method 2: change IP to all "^. * $", which is invalid for me -- >
<!--
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="^.*$" />
 -->

<! -- Method 3: at the end, add | [D + \. [D + \. [D + \. [D + \. [D + -- >
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|\d+\.\d+\.\d+\.\d+" />

3. Open the Tomcat decompression directory Tomcat / conf/ server.xml Modify the encoding and decoding character set of URL address
add toURIEncoding="UTF-8"
In JavaWeb, the get request has Chinese request parameters, which is not garbled

<Connector port="8080" protocol="HTTP/1.1"
    connectionTimeout= "20000"
    redirectPort="8443" URIEncoding="UTF-8"/>

3、 Install Git

Step 1: install the package needed to compile GIT

yum install -y curl-devel expat-devel gettext-devel openssl-devel zlib-devel
yum install -y gcc perl-ExtUtils-MakeMaker

Step 2: delete the existing GIT

yum remove git

Step 3: download the latest version of GIT tar package from git official website and move to/usr/srcUnder the directory

cd /usr/src
tar -zxvf git-2.9.3.tar.gz

Step 4: compile and install

cd git-2.9.3
make prefix=/usr/local/git all
make prefix=/usr/local/git install
echo "export PATH=$PATH:/usr/local/git/bin" >> /etc/bashrc
source /etc/bashrc

Step 5: check the version number

git --version

4、 Run Jenkins

1. Start Tomcatbin/startup.sh

2. Visitip:8080/jenkinsRun Jenkins initialization

Jenkins installation, configuration and operation to realize automatic construction

3. You can follow the promptscatView password, and then the next step

Jenkins installation, configuration and operation to realize automatic construction

4. You need to connect the agent to install the plug-in[Select recommended], which can be installed offline

Jenkins installation, configuration and operation to realize automatic construction

5. After installation, come here

Jenkins installation, configuration and operation to realize automatic construction

Note:
If the administrator is not created here, the user will log out later. Jenkins uses the password of admin + cat above to log in
If the lankeren administrator is created, the original admin will be invalid. In the future, lankeren will be used to log in Jenkins

5、 Preliminary configuration of Jenkins

Global security configurationAllow users to register, so that when the password forgot to register, but also to do some operation
Global tool configuration
  1. Example maven_ HOME:opt/apache-maven-3.6.0
  2. Example Java_ HOEM:/opt/jdk1.8.0_121
  3. Specify Maven configuration locationopt/apache-maven-3.6.0/conf/settings.xml
  4. Add git location (alias: mygit)/usr/local/git/bin/git
  5. Location of new JDK (alias: myjdk)/opt/jdk1.8.0_121
    ………

Jenkins installation, configuration and operation to realize automatic construction

Jenkins installation, configuration and operation to realize automatic construction

New maven
Alias: mymaven
opt/apache-maven-3.6.0

Jenkins installation, configuration and operation to realize automatic construction

Install plug-insdeploy to container[also want to connect agents], may fail, the network is unstable, try several times

Note:To create a maven (WEB) project, it is required to upload the project name on GitHub / gitee to the directory with pom.xml file

6、 Initially create a Jenkins project

Create a new task on Jenkins[Building a free style software project]
Project configuration

1. Source code management

None
Git √ 
Subverion

Choose Git
Then configure git related content

If you choose GitHub 
1. Configuration project Git
2. At the same time, configure the system configuration and add git server

If you choose gitee
1. Gitee in configuration project
2. Configure gitee in system configuration at the same time

[1] Jenkins + github

The URL of the GitHub repository configured in the project is the link between the current project clone and the download http

Jenkins installation, configuration and operation to realize automatic construction

To get the access token value in GitHub, you need an account with write permission to the project

GitHub > setting > developer setting > personal access tokens > Click generate new token

The contents are as follows:

Jenkins installation, configuration and operation to realize automatic construction

After creation, the token is as follows:

Jenkins installation, configuration and operation to realize automatic construction

Add a GitHub server for global configuration

Jenkins installation, configuration and operation to realize automatic construction

Adding token voucher to GitHub configuration in Jenkins

System Management > add GitHub server > add secret text certificate > connection testHere, when I connect to the test, I return error, and then there is no tube, so I can build it automatically

Jenkins installation, configuration and operation to realize automatic construction

The secret text voucher is added as follows:

Select secret text as the type, and enter the token value generated above in GitHub

Jenkins installation, configuration and operation to realize automatic construction

Hook URL default:ip:port/jenkins/github-webhookWrite to webhook of GitHub project setting

settings->webhooks->Add webhook

Jenkins installation, configuration and operation to realize automatic construction

[2] Jenkins + gitee

  • Plug in installation
  • Go to manage Jenkins > Manage plugs > available
  • Right filter input: gitee
Add gitee code cloud link configuration (system configuration)
  1. Go to Jenkins > Manage Jenkins > configure system > gitee configuration > gitee connections
  2. stayConnection nameInput inGiteeOr the name you want
  3. Gitee host URLEnter the full URL address of the code cloud in:https://gitee.com(code cloud privatization customer enters the deployed domain name)
  4. CredentialsIf the code cloud apiv5 private token has not been configured, clickAdd– >Jenkins

    1. DomainchoiceGlobal credentials
    2. KindchoiceGitee API Token
    3. ScopeChoose the range you need
    4. Gitee API TokenEnter your code cloud private token to get the address: https://gitee.com/profile/personal_ access_ tokens
    5. ID,DescripitonEnter the ID and description you want in.
  5. CredentialsSelect the configured gitee apiv5 token
  6. clickAdvanced, which can be configured to ignore SSL errors (depending on your Jenkins environment), and set the link test timeout (depending on your network environment)
  7. clickTest ConnectionTest whether the link is successful. If it fails, please check steps 3, 5 and 6 above.

The configuration is as shown in the figure
Jenkins installation, configuration and operation to realize automatic construction

Git configuration in project
  1. According to the GitHub configuration above, you only need to change itWarehouse linkbyHTTP link of gitee warehouse, account changed togiteeYour account

2. Enter the code cloud project set in the source code management configuration, and enter Management > webhooks

  1. Add webhook, fill in URLTrigger configuration: build when a change is pushed to gitee. Gitee webhook URLThe URL shown in
  2. Password filling: the webhook password configured in point 5 of trigger configuration can be omitted without setting password
  3. Check push, pull request

Jenkins installation, configuration and operation to realize automatic construction
Jenkins installation, configuration and operation to realize automatic construction

3. Build and operation after build (need deploy plug-in, willWar package generated in targetDeploy to / webapps of Tomcat
Jenkins installation, configuration and operation to realize automatic construction

Apple is the access path name of the project
Example:127.0.0.1:8080/apple/index.jsp

4. In addition, it is said thatTurn off to prevent cross Site Request ForgeryAnd then I shut it down. I don’t know if it will work the same

7、 Idea

In idea

The first step is to create a local warehouse

Jenkins installation, configuration and operation to realize automatic construction

The second step is to submit documents / projects

Jenkins installation, configuration and operation to realize automatic construction

Third, push to GitHub / gitee

Jenkins installation, configuration and operation to realize automatic construction

The fourth step is to visit IP: port / xiangmumingcheng/ test.html

ok