一、参数化构建

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 No valid crumb was included in the request</title>
</head>
<body><h2>HTTP ERROR </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 This page expects a form submission</title>
</head>
<body><h2>HTTP ERROR </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=
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=
> 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=
> git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=
Checking out Revision 6454cc57fd8252c2859ff8ebd5cf78d087bbdc0d (refs/remotes/origin/master)
> git config core.sparsecheckout # timeout=
> git checkout -f 6454cc57fd8252c2859ff8ebd5cf78d087bbdc0d
Commit message: "Add CHANGELOG"
> git rev-list --no-walk 6454cc57fd8252c2859ff8ebd5cf78d087bbdc0d # timeout=
Parsing POMs
Established TCP socket on
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./boot/plexus-classworlds-2.6..jar:/usr/local/maven/apache-maven-3.6./conf/logging jenkins.maven3.agent.Maven35Main /usr/local/maven/apache-maven-3.6. /usr/local/jenkins-slave/agent.jar /var/jenkins/maven35-interceptor.jar /var/jenkins/maven3-interceptor-commons.jar
<===[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- 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-, i.e. build is platform dependent!
[INFO] Compiling 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- 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-, i.e. build is platform dependent!
[INFO] Compiling source file to /var/jenkins/workspace/diy-maven_TEST/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.: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: , Failures: , Errors: , Skipped: , Time elapsed: 0.013 sec Results : Tests run: , Failures: , Errors: , Skipped: [JENKINS] Recording test results
[WARNING] Attempt to (de-)serialize anonymous class hudson.maven.reporters.SurefireArchiver$; see: https://jenkins.io/redirect/serialization-of-anonymous-classes/
[WARNING] Attempt to (de-)serialize anonymous class hudson.maven.reporters.BuildInfoRecorder$; 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: --12T11::+:
[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":,"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":,"skipCount":,"totalCount":,"urlName":"testReport"},{"_class":"hudson.maven.reporters.MavenAggregatedArtifactRecord"},{},{},{},{}],"artifacts":[],"building":false,"description":null,"displayName":"#7","duration":,"estimatedDuration":,"executor":null,"fullDisplayName":"diy-maven_TEST #7","id":"","keepLog":false,"number":,"queueId":,"result":"SUCCESS","timestamp":,"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></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

DEVOPS技术实践_04:Jenkins参数化构建的更多相关文章

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

    一.用户名密码错误 打开jenkins发现用户名密码错误,解决 1.1 找到config.xml文件 [root@jenkins-master ~]# ll -a drwxr-xr-x. root r ...

  2. DEVOPS技术实践_18:Jenkins的Pinpeline对于参数的使用

    因为最近使用Pipeline声明式语法构建项目,但是最近项目的参数设置较多,特地的来学习一下关于参数的调用和测试,主要式从一个大神那里学习的,结尾回贴上大神的博客链接 1 构建一个pipeline项目 ...

  3. DEVOPS技术实践_17:Jenkins使用扩展邮件功能发送邮件

    一 环境准备 1.1 安装插件Email Extension 系统管理-管理插件-安装Email Extension插件 1.2 配置 配置jenkins邮箱的全局配置:系统管理-系统设置-完成邮箱配 ...

  4. DEVOPS技术实践_11:Jenkins集成Sonar

    前言 前面已经有介绍sonar的安装,简单应用,下面在简答的研究一下sonar和jenkins集成的简单使用,对于sonar的安装不做介绍 一 sonar的简单介绍 持续检查避免了低质量的代码,比如S ...

  5. DEVOPS技术实践_09:Jenkins多分支管道

    简介 多分支的管道是在jenkins2.x中新增的功能 . 多分支管道允许你针对分布式的控制器的每个分支创建一个管道. 下图是对它的一个描述.使用jenkinsfile去创建多分支的管道,jenkin ...

  6. DEVOPS技术实践_07:Jenkins 管道工作

    一 简介 由于在公司构建很多工作,都是使用的maven工作构建的,这种构建方式很大缺点就是所有的工作都需要一步一步的配置,当项目较多,需求变动时,需要很大的精力去修改配置,很费劲 Pipeline即为 ...

  7. DEVOPS技术实践_01:jenkins集成平台

    一.准备环境 准备三台机器 角色 IP地址 用户名 密码 jenkins-master   172.25.254.130    admin   meiyoumima gitlab 172.25.254 ...

  8. Jenkins 利用Build With Parameters Plugin实现Jenkins参数化构建

    利用Build With Parameters Plugin实现Jenkins参数化构建   by:授客 QQ:1033553122 测试环境 Build With Parameters Plugin ...

  9. Jenkins参数化构建插件,实现构建前输入自定义参数

    插件: [Build with Parameters]:https://wiki.jenkins-ci.org/display/JENKINS/Build+With+Parameters+Plugin ...

随机推荐

  1. idea 使用优化

    1.创建类的模板 /** * Copyright (C), 2015-${YEAR}, XXX有限公司 * FileName: ${NAME} * Author: ${USER} * Date: ${ ...

  2. Java发送邮件的工具类

    package com.csice.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileNot ...

  3. uni-app获取dom元素到顶部的距离以及操作dom元素的一些样式

    一. 1.首先有一个元素 <view class="activity" ref="btn"></view> 2.确认指针指向 this. ...

  4. iptables 防止DoS攻击

    SYN洪水是攻击者发送海量的SYN请求到目标服务器上的一种DoS攻击方法,下面的脚本用于预防轻量级的DoS攻击:ipt-tcp.sh: iptables -N syn-flood (如果您的防火墙默认 ...

  5. .net Framework 源代码 · ScrollViewer

    本文是分析 .net Framework 源代码的系列,主要告诉大家微软做 ScrollViewer 的思路,分析很简单. 看完本文,可以学会如何写一个 ScrollViewer ,如何定义一个 IS ...

  6. java DOM 操作xml

    1 代码如下: package dom.pasing; import java.io.IOException; import java.io.StringWriter; import javax.xm ...

  7. Js中“==”和“===”的区别

    从字面上来讲,‘==’代表相等,‘===’代表严格相等. 具体来讲,比较过程如下: 比较过程: ‘==’:      1. 首先判断两个值的类型是否相同,如果相同,进行‘===’判断.      2. ...

  8. P1101 走迷宫一

    题目描述 大魔王抓住了爱丽丝,将她丢进了一口枯井中,并堵住了井口. 爱丽丝在井底发现了一张地图,他发现他现在身处一个迷宫当中,从地图中可以发现,迷宫是一个N*M的矩形,爱丽丝身处迷宫的左上角,唯一的出 ...

  9. linux scull 中的设备注册

    在内部, scull 使用一个 struct scull_dev 类型的结构表示每个设备. 这个结构定义为: struct scull_dev { struct scull_qset *data;  ...

  10. 1626 - Brackets sequence——[动态规划]

    Let us define a regular brackets sequence in the following way: Empty sequence is a regular sequence ...