DEVOPS技术实践_02:jenkins自动构建项目

一、用户名密码错误

打开jenkins发现用户名密码错误,解决

1.1 找到config.xml文件

[root@jenkins-master ~]# ll -a

drwxr-xr-x. 12 root root  4096 Apr 14 15:30 .jenkins

[root@jenkins-master ~]# cd .jenkins/

[root@jenkins-master .jenkins]# ll

-rw-r--r--.  1 root root 1869 Apr 14 15:30 config.xml    #找到这个文件
-rw-r--r--.  1 root root  156 Apr 14 05:18 hudson.model.UpdateCenter.xml
-rw-r--r--.  1 root root 1230 Apr 14 15:20 hudson.plugins.emailext.ExtendedEmailPublisher.xml
-rw-r--r--.  1 root root  370 Apr 14 05:25 hudson.plugins.git.GitTool.xml
-rw-------.  1 root root 1712 Apr 14 05:18 identity.key.enc
-rw-r--r--.  1 root root   94 Apr 14 15:30 jenkins.CLI.xml
-rw-r--r--.  1 root root    7 Apr 14 05:43 jenkins.install.InstallUtil.lastExecVersion
-rw-r--r--.  1 root root    7 Apr 14 05:43 jenkins.install.UpgradeWizard.state
-rw-r--r--.  1 root root  138 Apr 14 15:30 jenkins.model.DownloadSettings.xml
-rw-r--r--.  1 root root  184 Apr 14 05:43 jenkins.model.JenkinsLocationConfiguration.xml
-rw-r--r--.  1 root root  357 Apr 14 15:30 jenkins.security.apitoken.ApiTokenPropertyConfiguration.xml
-rw-r--r--.  1 root root  169 Apr 14 15:30 jenkins.security.QueueItemAuthenticatorConfiguration.xml
-rw-r--r--.  1 root root  162 Apr 14 15:30 jenkins.security.UpdateSiteWarningsConfiguration.xml
-rw-r--r--.  1 root root  171 Apr 14 05:18 jenkins.telemetry.Correlator.xml
drwxr-xr-x.  2 root root    6 Apr 14 05:18 jobs
drwxr-xr-x.  4 root root   33 Apr 14 15:34 logs
-rw-r--r--.  1 root root  907 Apr 14 05:18 nodeMonitors.xml
drwxr-xr-x.  3 root root   20 Apr 14 15:34 nodes
drwxr-xr-x. 76 root root 8192 Apr 14 05:29 plugins
-rw-r--r--.  1 root root   64 Apr 14 05:18 secret.key
-rw-r--r--.  1 root root    0 Apr 14 05:18 secret.key.not-so-secret
drwx------.  4 root root 4096 Apr 14 15:35 secrets
drwxr-xr-x.  2 root root  182 Apr 14 05:23 updates
drwxr-xr-x.  2 root root   24 Apr 14 05:18 userContent
drwxr-xr-x.  3 root root   56 Apr 14 05:18 users
drwxr-xr-x. 11 root root 4096 Apr 14 05:17 war
drwxr-xr-x.  2 root root    6 Apr 14 05:25 workflow-libs

1.2 备份修改文件

[root@jenkins-master .jenkins]# cp config.xml config.xml.bak

[root@jenkins-master .jenkins]# vim config.xml

<?xml version='1.1' encoding='UTF-8'?>
<hudson>
  <disabledAdministrativeMonitors/>
  <version>2.164.2</version>
  <installStateName>RUNNING</installStateName>
  <numExecutors>2</numExecutors>
  <mode>NORMAL</mode>
  <useSecurity>true</useSecurity>    #删除
  <authorizationStrategy class="hudson.security.FullControlOnceLoggedInAuthorizationStrategy">
    <denyAnonymousReadAccess>true</denyAnonymousReadAccess>
  </authorizationStrategy>
  <securityRealm class="hudson.security.HudsonPrivateSecurityRealm">
    <disableSignup>true</disableSignup>
    <enableCaptcha>false</enableCaptcha>
  </securityRealm>
  <disableRememberMe>false</disableRememberMe>
  <projectNamingStrategy class="jenkins.model.ProjectNamingStrategy$DefaultProjectNamingStrategy"/>
  <workspaceDir>${JENKINS_HOME}/workspace/${ITEM_FULL_NAME}</workspaceDir>
  <buildsDir>${ITEM_ROOTDIR}/builds</buildsDir>
  <markupFormatter class="hudson.markup.EscapedMarkupFormatter"/>
  <jdks/>
  <viewsTabBar class="hudson.views.DefaultViewsTabBar"/>
  <myViewsTabBar class="hudson.views.DefaultMyViewsTabBar"/>
  <clouds/>
  <scmCheckoutRetryCount>0</scmCheckoutRetryCount>
  <views>
    <hudson.model.AllView>
      <owner class="hudson" reference="../../.."/>
      <name>all</name>
      <filterExecutors>false</filterExecutors>
      <filterQueue>false</filterQueue>
      <properties class="hudson.model.View$PropertyList"/>
    </hudson.model.AllView>
  </views>
  <primaryView>all</primaryView>
  <slaveAgentPort>8181</slaveAgentPort>
  <enabledAgentProtocols>
    <string>JNLP-connect</string>
    <string>JNLP2-connect</string>
    <string>JNLP3-connect</string>
  </enabledAgentProtocols>
  <label></label>
  <crumbIssuer class="hudson.security.csrf.DefaultCrumbIssuer">
    <excludeClientIPFromCrumb>false</excludeClientIPFromCrumb>
  </crumbIssuer>
  <nodeProperties/>
  <globalNodeProperties/>
</hudson>

1.3 重启jenkins

[root@jenkins-master .jenkins]# ps -ef |grep  jenkins

[root@jenkins-master .jenkins]# kill -9 105823

[root@jenkins-master ~]# cd /usr/local/src/

[root@jenkins-master src]# nohup java -jar /usr/local/src/jenkins.war --httpPort=9000 &

1.4 修改安全配置

进入首页>“系统管理”>全局安全配置>启用安全>Jenkins专有用户数据库>保存,如下图:

 重新点击首页-系统管理-管理用户

 1.5 重新设置密码

 

登陆成功

 二、jenkins自动构建

 2.1 创建一个任务

选择保存后填下面信息

使用git,地址要填准确,同时在jenkins的服务器上要安装git,否则会报错

2.2 构建项目

构建完成,点击立即构建

点击#1,打开控制台,看输出消息

2.3 配置Mzven

选择系统管理>全局工具配置>新增Maven

[root@node5 ~]# echo $M2_HOME

/usr/local/maven/apache-maven-3.6.1

2.4 在重新构建测试

Started by user unknown or anonymous
Building remotely on slave1 in workspace /var/jenkins/workspace/diy-maven_TEST   #构建的机器和构建的位置
No credentials specified
Cloning the remote Git repository
Cloning repository http://172.25.254.131/tester/mvn-test01.git
 > git init /var/jenkins/workspace/diy-maven_TEST # timeout=10
Fetching upstream changes from http://172.25.254.131/tester/mvn-test01.git
 > git --version # timeout=10
 > git fetch --tags --progress http://172.25.254.131/tester/mvn-test01.git +refs/heads/*:refs/remotes/origin/*
 > git config remote.origin.url http://172.25.254.131/tester/mvn-test01.git # timeout=10
 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git config remote.origin.url http://172.25.254.131/tester/mvn-test01.git # timeout=10
Fetching upstream changes from http://172.25.254.131/tester/mvn-test01.git
 > git fetch --tags --progress http://172.25.254.131/tester/mvn-test01.git +refs/heads/*:refs/remotes/origin/*
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision 026cbb9df8efcfc15ff98a27f0fa0c255716bc6a (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 026cbb9df8efcfc15ff98a27f0fa0c255716bc6a
Commit message: "test"
First time build. Skipping changelog.
Parsing POMs
Discovered a new module com.example.app:maven-app maven-app
Modules changed, recalculating dependency graph
Established TCP socket on 42398
Copied maven35-agent.jar
Copied maven35-interceptor.jar
Copied maven3-interceptor-commons.jar
[diy-maven_TEST] $ java -cp /var/jenkins/maven35-agent.jar:/usr/local/maven/apache-maven-3.6.1/boot/plexus-classworlds-2.6.0.jar:/usr/local/maven/apache-maven-3.6.1/conf/logging jenkins.maven3.agent.Maven35Main /usr/local/maven/apache-maven-3.6.1 /usr/local/jenkins-slave/agent.jar /var/jenkins/maven35-interceptor.jar /var/jenkins/maven3-interceptor-commons.jar 42398
<===[JENKINS REMOTING CAPACITY]===>channel started
Executing Maven:  -B -f /var/jenkins/workspace/diy-maven_TEST/pom.xml clean install
[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------< com.example.app:maven-app >----------------------
[INFO] Building maven-app 1.3-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-app ---
[INFO] Deleting /var/jenkins/workspace/diy-maven_TEST/target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-app ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /var/jenkins/workspace/diy-maven_TEST/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-app ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /var/jenkins/workspace/diy-maven_TEST/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-app ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /var/jenkins/workspace/diy-maven_TEST/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven-app ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /var/jenkins/workspace/diy-maven_TEST/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-app ---
[INFO] Surefire report directory: /var/jenkins/workspace/diy-maven_TEST/target/surefire-reports
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.example.app.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.016 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[JENKINS] Recording test results
[WARNING] Attempt to (de-)serialize anonymous class hudson.maven.reporters.SurefireArchiver$2; see: https://jenkins.io/redirect/serialization-of-anonymous-classes/
[WARNING] Attempt to (de-)serialize anonymous class hudson.maven.reporters.BuildInfoRecorder$1; see: https://jenkins.io/redirect/serialization-of-anonymous-classes/
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-app ---
[INFO] Building jar: /var/jenkins/workspace/diy-maven_TEST/target/maven-app-1.3-SNAPSHOT.jar
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ maven-app ---
[INFO] Installing /var/jenkins/workspace/diy-maven_TEST/target/maven-app-1.3-SNAPSHOT.jar to /root/.m2/repository/com/example/app/maven-app/1.3-SNAPSHOT/maven-app-1.3-SNAPSHOT.jar
[INFO] Installing /var/jenkins/workspace/diy-maven_TEST/pom.xml to /root/.m2/repository/com/example/app/maven-app/1.3-SNAPSHOT/maven-app-1.3-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  14.790 s
[INFO] Finished at: 2019-04-11T03:56:43+08:00
[INFO] ------------------------------------------------------------------------
Waiting for Jenkins to finish collecting data
[JENKINS] Archiving /var/jenkins/workspace/diy-maven_TEST/pom.xml to com.example.app/maven-app/1.3-SNAPSHOT/maven-app-1.3-SNAPSHOT.pom
[JENKINS] Archiving /var/jenkins/workspace/diy-maven_TEST/target/maven-app-1.3-SNAPSHOT.jar to com.example.app/maven-app/1.3-SNAPSHOT/maven-app-1.3-SNAPSHOT.jar
channel stopped
Finished: SUCCESS

2.5 去构建机器验证

[root@node5 ~]# cd /var/jenkins/workspace/diy-maven_TEST

[root@node5 diy-maven_TEST]# ls

-rw-r--r--. 1 root root 646 Apr 11 03:56 pom.xml
drwxr-xr-x. 4 root root  30 Apr 11 03:56 src
drwxr-xr-x. 7 root root 141 Apr 11 03:56 target

[root@node5 diy-maven_TEST]# cd target/

[root@node5 target]# ll

drwxr-xr-x. 3 root root   17 Apr 11 03:56 classes
-rw-r--r--. 1 root root 2275 Apr 11 03:56 maven-app-1.3-SNAPSHOT.jar
drwxr-xr-x. 2 root root   28 Apr 11 03:56 maven-archiver
drwxr-xr-x. 3 root root   35 Apr 11 03:56 maven-status
drwxr-xr-x. 2 root root   81 Apr 11 03:56 surefire-reports
drwxr-xr-x. 3 root root   17 Apr 11 03:56 test-classes 

基本构建完成 

猜你喜欢

转载自www.cnblogs.com/zyxnhr/p/10736014.html