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

2 编写jenkinsfile文件
import hudson.model.*;
pipeline{ agent any
stages{
stage("Hello Pipeline") {
steps {
script {
println "Hello Pipeline!"
println env.JOB_NAME
println env.BUILD_NUMBER
}
}
}
} }
3 设置pipeline

4 结果
Started by user darren ning
Obtained pipeline.jenkinsfile from git http://192.168.132.132/root/pipeline-parameter-test.git
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /root/.jenkins/workspace/pipeline_parameter_project
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
No credentials specified
Cloning the remote Git repository
Cloning repository http://192.168.132.132/root/pipeline-parameter-test.git
> git init /root/.jenkins/workspace/pipeline_parameter_project # timeout=
Fetching upstream changes from http://192.168.132.132/root/pipeline-parameter-test.git
> git --version # timeout=
> git fetch --tags --progress http://192.168.132.132/root/pipeline-parameter-test.git +refs/heads/*:refs/remotes/origin/*
> git config remote.origin.url http://192.168.132.132/root/pipeline-parameter-test.git # timeout=10
> git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
> git config remote.origin.url http://192.168.132.132/root/pipeline-parameter-test.git # timeout=10
Fetching upstream changes from http://192.168.132.132/root/pipeline-parameter-test.git
> git fetch --tags --progress http://192.168.132.132/root/pipeline-parameter-test.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 256f52910dbbc98008973b177ef533f21fd10a1f (refs/remotes/origin/master)
> git config core.sparsecheckout # timeout=10
> git checkout -f 256f52910dbbc98008973b177ef533f21fd10a1f
Commit message: "Add new file"
First time build. Skipping changelog.
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello Pipeline)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
Hello Pipeline!
[Pipeline] echo
pipeline_parameter_project
[Pipeline] echo
1
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
上面就是简单的参数调用,但是当项目的西安舒比较多的时候,需要一个很长篇幅的构建页面,
假如有十来个可变的参数,其中有几个是可以提取出来,没必要暴露出来,那么我们就可以把其他参数放入到一个json文件。这个json文件,可以存放在linux服务器上。
原则上,每一个有权限去构建这个job的人,需要去linux服务器上创建自己的名称的文件夹,这个文件夹下然后放一个json文件。
准备一个json文件,在这个文件定义一些需要的参数,为了方便,就直接从博主那里粘贴了
5 使用json调用参数
vi /tmp/test.json
{
"NAME":"Lucy",
"AGE":"",
"PHONE_NUMBER":"",
"ADDRESS":"Haidian Beijing",
"EMAIL":"lucy@demo.com",
"GENDER":"male",
"IS_MARRY":"false"
}
jenkinsfile文件
import hudson.model.*;
pipeline{
agent any
environment {
INPUT_JSON = "/tmp/test.json" #原本的文档使用参数化构建,我这里直接使用pipeline的环境变量,效果一样
}
stages{
stage("Hello Pipeline") {
steps {
script {
println "Hello Pipeline!"
println env.JOB_NAME
println env.BUILD_NUMBER
}
}
}
stage("Init paramters in json") {
steps {
script { println "read josn input file"
json_file = INPUT_JSON? INPUT_JSON.trim() : ""
prop = readJSON file : json_file
name = prop.NAME? prop.NAME.trim() : ""
println "Name:" + name
}
}
}
}
}
6 安装插件
需要安装这个插件

安装完成后,构建
构建结果
Started by user darren ning
Obtained pipeline.jenkinsfile from git http://192.168.132.132/root/pipeline-parameter-test.git
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /root/.jenkins/workspace/pipeline_parameter_project
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
No credentials specified
> git rev-parse --is-inside-work-tree # timeout=
Fetching changes from the remote Git repository
> git config remote.origin.url http://192.168.132.132/root/pipeline-parameter-test.git # timeout=10
Fetching upstream changes from http://192.168.132.132/root/pipeline-parameter-test.git
> git --version # timeout=
> git fetch --tags --progress http://192.168.132.132/root/pipeline-parameter-test.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 139f409fd00ad11c293ce9726c7a638f73212062 (refs/remotes/origin/master)
> git config core.sparsecheckout # timeout=
> git checkout -f 139f409fd00ad11c293ce9726c7a638f73212062
Commit message: "Update pipeline.jenkinsfile"
> git rev-list --no-walk 9fc1715852a27f40d5fa3277d6f58ac150c972da # timeout=
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello Pipeline)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
Hello Pipeline!
[Pipeline] echo
pipeline_parameter_project
[Pipeline] echo [Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Init paramters in json)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
read josn input file
[Pipeline] readJSON
[Pipeline] echo
Name:Lucy
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
7 用完全的Jenkinsfile调用所有参数
import hudson.model.*;
pipeline{
agent any
environment {
INPUT_JSON = "/tmp/test.json"
}
stages{
stage("Hello Pipeline") {
steps {
script {
println "Hello Pipeline!"
println env.JOB_NAME
println env.BUILD_NUMBER
}
}
} stage("Init paramters in json") {
steps {
script { println "read josn input file"
json_file = INPUT_JSON? INPUT_JSON.trim() : ""
prop = readJSON file : json_file
name = prop.NAME? prop.NAME.trim() : ""
println "Name:" + name
age = prop.AGE? prop.AGE.trim() : ""
println "Age:" + age
phone = prop.PHONE_NUMBER? prop.PHONE_NUMBER.trim() : ""
println "Phone:" + phone
address = prop.ADDRESS? prop.ADDRESS.trim() : ""
println "Address:" + address
email = prop.EMAIL? prop.EMAIL.trim() : ""
println "Email:" + email
gender = prop.GENDER? prop.GENDER.trim() : ""
println "Gender:" + gender
is_marry = prop.IS_MARRY? prop.IS_MARRY.trim() : false
println "is_marry:" + is_marry
}
}
}
} }
8 最终构建结果
Started by user darren ning
Obtained pipeline.jenkinsfile from git http://192.168.132.132/root/pipeline-parameter-test.git
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /root/.jenkins/workspace/pipeline_parameter_project
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
No credentials specified
> git rev-parse --is-inside-work-tree # timeout=
Fetching changes from the remote Git repository
> git config remote.origin.url http://192.168.132.132/root/pipeline-parameter-test.git # timeout=10
Fetching upstream changes from http://192.168.132.132/root/pipeline-parameter-test.git
> git --version # timeout=
> git fetch --tags --progress http://192.168.132.132/root/pipeline-parameter-test.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 1f532ef638de79563acdabb39378602140ebd2e1 (refs/remotes/origin/master)
> git config core.sparsecheckout # timeout=
> git checkout -f 1f532ef638de79563acdabb39378602140ebd2e1
Commit message: "Update pipeline.jenkinsfile"
> git rev-list --no-walk 139f409fd00ad11c293ce9726c7a638f73212062 # timeout=
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello Pipeline)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
Hello Pipeline!
[Pipeline] echo
pipeline_parameter_project
[Pipeline] echo [Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Init paramters in json)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
read josn input file
[Pipeline] readJSON
[Pipeline] echo
Name:Lucy
[Pipeline] echo
Age:
[Pipeline] echo
Phone:
[Pipeline] echo
Address:Haidian Beijing
[Pipeline] echo
Email:lucy@demo.com
[Pipeline] echo
Gender:male
[Pipeline] echo
is_marry:false
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
实验基本完成
参考文档:https://blog.csdn.net/u011541946/article/details/88937201
感谢博主的分享,让我在jenkins上又进了一步
DEVOPS技术实践_18:Jenkins的Pinpeline对于参数的使用的更多相关文章
- DEVOPS技术实践_17:Jenkins使用扩展邮件功能发送邮件
一 环境准备 1.1 安装插件Email Extension 系统管理-管理插件-安装Email Extension插件 1.2 配置 配置jenkins邮箱的全局配置:系统管理-系统设置-完成邮箱配 ...
- DEVOPS技术实践_11:Jenkins集成Sonar
前言 前面已经有介绍sonar的安装,简单应用,下面在简答的研究一下sonar和jenkins集成的简单使用,对于sonar的安装不做介绍 一 sonar的简单介绍 持续检查避免了低质量的代码,比如S ...
- DEVOPS技术实践_09:Jenkins多分支管道
简介 多分支的管道是在jenkins2.x中新增的功能 . 多分支管道允许你针对分布式的控制器的每个分支创建一个管道. 下图是对它的一个描述.使用jenkinsfile去创建多分支的管道,jenkin ...
- DEVOPS技术实践_07:Jenkins 管道工作
一 简介 由于在公司构建很多工作,都是使用的maven工作构建的,这种构建方式很大缺点就是所有的工作都需要一步一步的配置,当项目较多,需求变动时,需要很大的精力去修改配置,很费劲 Pipeline即为 ...
- DEVOPS技术实践_02:jenkins自动构建项目
一.用户名密码错误 打开jenkins发现用户名密码错误,解决 1.1 找到config.xml文件 [root@jenkins-master ~]# ll -a drwxr-xr-x. root r ...
- DEVOPS技术实践_01:jenkins集成平台
一.准备环境 准备三台机器 角色 IP地址 用户名 密码 jenkins-master 172.25.254.130 admin meiyoumima gitlab 172.25.254 ...
- DEVOPS技术实践_19:Pipeline的多参数json调用
在上一篇学习了把参数写进Json文件,然后通过去Json文件,调用参数的方法 1. 三元运算符介绍 调用的方法是通过一个三元运算符实现的 gender = prop.GENDER? prop.GEND ...
- DEVOPS技术实践_04:Jenkins参数化构建
一.参数化构建 1.1 各个参数的信息 凭据参数存储一个用户的账号密码信息,等等,运用最多的是选项参数 1.2 使用选项参数 构建已经变成参数化构建 1.3 获取这个值,修改Jenkinsfile文件 ...
- DEVOPS技术实践_03:Jenkins自动构建
一.提交代码自动构建 当开发人员在gitlab提交代码后,会自动触发jenkin构建 点击项目---->点击diy_maven-TEST----->点击配置--->构建触发器---- ...
随机推荐
- pycharm 快捷键使用
1.Ctrl+/?键 = 选中行全部注释/解封: 2.Ctrl+D = 复制前一行: 3.Ctrl+Z = 撤销: 1.编辑(Editing) Ctrl + Space 基本的代码完成(类.方法.属性 ...
- [C#] 如何分析stackoverflow等clr错误
有时候由于无限递归调用等代码错误,w3wp.exe会报错退出,原因是clr.exe出错了. 这种错误比较难分析,因为C#代码抓不住StackOverflowException等异常. 处理方法是:生成 ...
- corn表达式——用于设置定时任务[转载]
原文地址https://blog.csdn.net/xiaopihai86/article/details/50756306 http://www.cronmaker.com/ cron表达式验证网 ...
- @codechef - BIKE@ Chef and Bike
目录 @description@ @solution@ @accepted code@ @details@ @description@ 输入 n(n ≤ 22) 个点,m(m ≤ 8000) 个边.每 ...
- 解决ViewState过于庞大的问题
这里是我将ViewState持久化保持在服务器端的代码,这样ViewState不占用网络带宽,因此其存取只是服务器的磁盘读取时间.并且它很 小,可以说是磁盘随便转一圈就能同时读取好多ViewState ...
- HZOJ string
正解炸了…… 考试的时候想到了正解,非常高兴的打出来了线段树,又调了好长时间,对拍了一下发现除了非常大的点跑的有点慢外其他还行.因为复杂度算着有点高…… 最后正解死于常数太大……旁边的lyl用同样的算 ...
- uva 10566 Crossed Ladders (二分)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- springboot&mybatis 增删改查系列(一)
创建父项目 首先,我们需要创建一个Maven项目. 在这个项目的pom文件中加入以下几个依赖: <!-- spring boot --> <parent> <groupI ...
- poj 1039 Pipe (Geometry)
1039 -- Pipe 理解错题意一个晚上._(:з」∠)_ 题意很容易看懂,就是要求你求出从外面射进一根管子的射线,最远可以射到哪里. 正解的做法是,选择上点和下点各一个,然后对于每个折点位置竖直 ...
- laravel博客后台操作步骤