DEVOPS技術實踐_02:jenkins自動構建項目
一、用戶名密碼錯誤
打開jenkins發現用戶名密碼錯誤,解決
1.1 找到config.xml文件
[[email protected] ~]# ll -a
drwxr-xr-x. 12 root root 4096 Apr 14 15:30 .jenkins
[[email protected] ~]# cd .jenkins/
[[email protected] .jenkins]# ll
-rw-r--r--. 1 root root 1869 Apr 14 15:30 config.xml #找到這個文件 -rw-r--r--. 1root 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 94Apr 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 184Apr 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 備份修改文件
[[email protected] .jenkins]# cp config.xml config.xml.bak
[[email protected] .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
[[email protected] .jenkins]# ps -ef |grep jenkins
[[email protected] .jenkins]# kill -9 105823
[[email protected] ~]# cd /usr/local/src/
[[email protected] 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
[[email protected] ~]# 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 去構建機器驗證
[[email protected] ~]# cd /var/jenkins/workspace/diy-maven_TEST
[[email protected] 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
[[email protected] diy-maven_TEST]# cd target/
[[email protected] 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
基本構建完成
DEVOPS技術實踐_02:jenkins自動構建項目