一、参数化构建
1.1 各个参数的信息
凭据参数存储一个用户的账号密码信息,等等,运用最多的是选项参数
1.2 使用选项参数
构建已经变成参数化构建
1.3 获取这个值,修改Jenkinsfile文件
String srcType = "${env.srcType}" #使用env变量获取参数 node { def mvnHome stage('Checkout') { // for display purposes println('checkcode') } stage('Build') { println("${srcType}") } stage('Test') { println('test') } stage('Deploy'){ println('Deploy') } stage('CodeScan'){ println('code') } }
1.4 jenkins点击构建
控制台信息
Started by user unknown or anonymous Obtained Jenkinsfile from git http://172.25.254.131/devops/jenkins.git Running in Durability level: MAX_SURVIVABILITY [Pipeline] Start of Pipeline [Pipeline] node Running on Jenkins in /root/.jenkins/workspace/java_pipeline-Test [Pipeline] { [Pipeline] stage [Pipeline] { (Checkout) [Pipeline] echo checkcode [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Build) [Pipeline] echo Git #输出参数的信息是Git [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Test) [Pipeline] echo test [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Deploy) [Pipeline] echo Deploy [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (CodeScan) [Pipeline] echo code [Pipeline] } [Pipeline] // stage [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline Finished: SUCCESS
1.5 给pipeline加几个功能性参数
顶一个字符串类型的参数,使用版本
构建的时候就会出现1版本,而且这个参数可以修改
二、远程构建
使用pipeline作为例子
2.1 配置构建触发器
身份验证令牌
Use the following URL to trigger build remotely: JENKINS_URL/job/java_pipeline-Test/build?token=TOKEN_NAME 或者 /buildWithParameters?token=TOKEN_NAME Optionally append &cause=Cause+Text to provide text that will be included in the recorded build cause.
2.2 构建
[root@jenkins-master ~]# curl -s -X POST "http://172.25.254.130:9000/job/java_pipeline-Test/build?token=testabc"
<html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> <title>Error 403 No valid crumb was included in the request</title> </head> <body><h2>HTTP ERROR 403</h2> <p>Problem accessing /job/java_pipeline-Test/build. Reason: <pre> No valid crumb was included in the request</pre></p><hr><a href="http://eclipse.org/jetty">Powered by Jetty:// 9.4.z-SNAPSHOT</a><hr/> </body> </html>
关闭防止跨站点请求
点击系统配置--->点击全局安全配置
[root@jenkins-master ~]# curl -s -X POST "http://172.25.254.130:9000/job/java_pipeline-Test/build?token=testabc" a=b
<html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> <title>Error 400 This page expects a form submission</title> </head> <body><h2>HTTP ERROR 400</h2> <p>Problem accessing /job/java_pipeline-Test/build. Reason: <pre> This page expects a form submission</pre></p><hr><a href="http://eclipse.org/jetty">Powered by Jetty:// 9.4.z-SNAPSHOT</a><hr/> </body> </html>
因为我们使用参数构建,需要使用另一个
[root@jenkins-master ~]# curl -s -X POST "http://172.25.254.130:9000/job/java_pipeline-Test/buildWithParameters?token=testabc"
jenkins正在构建
控制台输出
Started by remote host 172.25.254.130 Obtained Jenkinsfile from git http://172.25.254.131/devops/jenkins.git Running in Durability level: MAX_SURVIVABILITY [Pipeline] Start of Pipeline [Pipeline] node Running on Jenkins in /root/.jenkins/workspace/java_pipeline-Test [Pipeline] { [Pipeline] stage [Pipeline] { (Checkout) [Pipeline] echo checkcode [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Build) [Pipeline] echo Git #不设置参数的参数构建 [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Test) [Pipeline] echo test [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Deploy) [Pipeline] echo Deploy [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (CodeScan) [Pipeline] echo code [Pipeline] } [Pipeline] // stage [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline Finished: SUCCESS
2.3 设置参数构建
[root@jenkins-master ~]# curl -s -X POST "http://172.25.254.130:9000/job/java_pipeline-Test/buildWithParameters?token=testabc" -d srcType=SVN
控制台输出
Started by remote host 172.25.254.130 Obtained Jenkinsfile from git http://172.25.254.131/devops/jenkins.git Running in Durability level: MAX_SURVIVABILITY [Pipeline] Start of Pipeline [Pipeline] node Running on Jenkins in /root/.jenkins/workspace/java_pipeline-Test [Pipeline] { [Pipeline] stage [Pipeline] { (Checkout) [Pipeline] echo checkcode [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Build) [Pipeline] echo SVN #带参数的参数构建结果 [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Test) [Pipeline] echo test [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Deploy) [Pipeline] echo Deploy [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (CodeScan) [Pipeline] echo code [Pipeline] } [Pipeline] // stage [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline Finished: SUCCESS
2.4 不使用参数构建
换另一个任务,不使用参数构建
身份验证令牌
Use the following URL to trigger build remotely: JENKINS_URL/job/diy-maven_TEST/build?token=TOKEN_NAME 或者 /buildWithParameters?token=TOKEN_NAME Optionally append &cause=Cause+Text to provide text that will be included in the recorded build cause.
设置权限
错误参数构建
[root@jenkins-master ~]# curl -s -X POST "http://172.25.254.130:9000/job/diy-maven_TEST/build?token=testabc"
报错
<html><head><meta http-equiv='refresh' content='1;url=/login?from=%2Fjob%2Fdiy-maven_TEST%2Fbuild%3Ftoken%3Dtestabc'/><script>window.location.replace('/login?from=%2Fjob%2Fdiy-maven_TEST%2Fbuild%3Ftoken%3Dtestabc');</script></head><body style='background-color:white; color:white;'> Authentication required <!-- --> </body></html>
正确构建
[root@jenkins-master ~]# curl -s -X POST "http://172.25.254.130:9000/job/diy-maven_TEST/build?token=testmvn"
开支构建
控制台信息
Started by remote host 172.25.254.130 Building remotely on slave1 in workspace /var/jenkins/workspace/diy-maven_TEST No credentials specified > git rev-parse --is-inside-work-tree # timeout=10 Fetching changes from the remote Git repository > 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 --version # timeout=10 > 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 6454cc57fd8252c2859ff8ebd5cf78d087bbdc0d (refs/remotes/origin/master) > git config core.sparsecheckout # timeout=10 > git checkout -f 6454cc57fd8252c2859ff8ebd5cf78d087bbdc0d Commit message: "Add CHANGELOG" > git rev-list --no-walk 6454cc57fd8252c2859ff8ebd5cf78d087bbdc0d # timeout=10 Parsing POMs Established TCP socket on 39000 maven35-agent.jar already up to date maven35-interceptor.jar already up to date maven3-interceptor-commons.jar already up to date [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 39000 <===[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.013 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: 12.620 s [INFO] Finished at: 2019-04-12T11:42:56+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
带用户名密码
[root@jenkins-master ~]# curl -s -u admin:meiyoumima -X POST "http://172.25.254.130:9000/job/diy-maven_TEST/build?token=testmvn"
获取后见结果
[root@jenkins-master ~]# curl -s -u admin:meiyoumima -X POST "http://172.25.254.130:9000/job/diy-maven_TEST/lastBuild/api/json?token=testmvn"
{"_class":"hudson.maven.MavenModuleSetBuild","actions":[{"_class":"hudson.model.CauseAction","causes":[{"_class":"hudson.model.Cause$RemoteCause","shortDescription":"Started by remote host 172.25.254.130","addr":"172.25.254.130","note":null}]},{"_class":"hudson.plugins.git.util.BuildData","buildsByBranchName":{"refs/remotes/origin/master":{"_class":"hudson.plugins.git.util.Build","buildNumber":7,"buildResult":null,"marked":{"SHA1":"6454cc57fd8252c2859ff8ebd5cf78d087bbdc0d","branch":[{"SHA1":"6454cc57fd8252c2859ff8ebd5cf78d087bbdc0d","name":"refs/remotes/origin/master"}]},"revision":{"SHA1":"6454cc57fd8252c2859ff8ebd5cf78d087bbdc0d","branch":[{"SHA1":"6454cc57fd8252c2859ff8ebd5cf78d087bbdc0d","name":"refs/remotes/origin/master"}]}}},"lastBuiltRevision":{"SHA1":"6454cc57fd8252c2859ff8ebd5cf78d087bbdc0d","branch":[{"SHA1":"6454cc57fd8252c2859ff8ebd5cf78d087bbdc0d","name":"refs/remotes/origin/master"}]},"remoteUrls":["http://172.25.254.131/tester/mvn-test01.git"],"scmName":""},{"_class":"hudson.plugins.git.GitTagAction"},{},{"_class":"hudson.maven.reporters.SurefireAggregatedReport","failCount":0,"skipCount":0,"totalCount":1,"urlName":"testReport"},{"_class":"hudson.maven.reporters.MavenAggregatedArtifactRecord"},{},{},{},{}],"artifacts":[],"building":false,"description":null,"displayName":"#7","duration":16654,"estimatedDuration":22772,"executor":null,"fullDisplayName":"diy-maven_TEST #7","id":"7","keepLog":false,"number":7,"queueId":30,"result":"SUCCESS","timestamp":1555364270313,"url":"http://172.25.254.130:9000/job/diy-maven_TEST/7/","builtOn":"slave1","changeSet":{"_class":"hudson.plugins.git.GitChangeSetList","items":[],"kind":"git"},"culprits":[],"mavenArtifacts":{},"mavenVersionUsed":"3.6.1"}
过滤
[root@jenkins-master ~]# curl -s -u admin:meiyoumima -X POST "http://172.25.254.130:9000/job/diy-maven_TEST/lastBuild/api/json?token=testmvn" |grep -Po '"result":".*?"'|awk -F : '{print $2}'|sed 's/"/ /'g SUCCESS
2.5 使用xml输出结果
root@jenkins-master ~]# curl -s -u admin:meiyoumima -X POST "http://172.25.254.130:9000/job/diy-maven_TEST/lastBuild/api/xml?token=testmvn"
<mavenModuleSetBuild _class='hudson.maven.MavenModuleSetBuild'><action _class='hudson.model.CauseAction'><cause _class='hudson.model.Cause$RemoteCause'><shortDescription>Started by remote host 172.25.254.130</shortDescription><addr>172.25.254.130</addr></cause></action><action _class='hudson.plugins.git.util.BuildData'><buildsByBranchName><refsremotesoriginmaster _class='hudson.plugins.git.util.Build'><buildNumber>7</buildNumber><marked><SHA1>6454cc57fd8252c2859ff8ebd5cf78d087bbdc0d</SHA1><branch><SHA1>6454cc57fd8252c2859ff8ebd5cf78d087bbdc0d</SHA1><name>refs/remotes/origin/master</name></branch></marked><revision><SHA1>6454cc57fd8252c2859ff8ebd5cf78d087bbdc0d</SHA1><branch><SHA1>6454cc57fd8252c2859ff8ebd5cf78d087bbdc0d</SHA1><name>refs/remotes/origin/master</name></branch></revision></refsremotesoriginmaster></buildsByBranchName><lastBuiltRevision><SHA1>6454cc57fd8252c2859ff8ebd5cf78d087bbdc0d</SHA1><branch><SHA1>6454cc57fd8252c2859ff8ebd5cf78d087bbdc0d</SHA1><name>refs/remotes/origin/master</name></branch></lastBuiltRevision><remoteUrl>http://172.25.254.131/tester/mvn-test01.git</remoteUrl><scmName></scmName></action><action _class='hudson.plugins.git.GitTagAction'></action><action></action><action _class='hudson.maven.reporters.SurefireAggregatedReport'><failCount>0</failCount><skipCount>0</skipCount><totalCount>1</totalCount><urlName>testReport</urlName></action><action _class='hudson.maven.reporters.MavenAggregatedArtifactRecord'></action><action></action><action></action><action></action><action></action><building>false</building><displayName>#7</displayName><duration>16654</duration><estimatedDuration>22772</estimatedDuration><fullDisplayName>diy-maven_TEST #7</fullDisplayName><id>7</id><keepLog>false</keepLog><number>7</number><queueId>30</queueId><result>SUCCESS</result><timestamp>1555364270313</timestamp><url>http://172.25.254.130:9000/job/diy-maven_TEST/7/</url><builtOn>slave1</builtOn><changeSet _class='hudson.plugins.git.GitChangeSetList'><kind>git</kind></changeSet><mavenArtifacts></mavenArtifacts><mavenVersionUsed>3.6.1</mavenVersionUsed></mavenModuleSetBuild>
过滤
[root@jenkins-master ~]# curl -s -u admin:meiyoumima -X POST "http://172.25.254.130:9000/job/diy-maven_TEST/lastBuild/api/xml?token=testmvn" |grep -Po '<result>.*?</result>'|sed 's/<result>//'g|awk -F '<' '{print $1}' SUCCESS