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

Posted zyxnhr

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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=10
Fetching upstream changes from http://192.168.132.132/root/pipeline-parameter-test.git
 > git --version # timeout=10
 > 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":"18",
    "PHONE_NUMBER":"13912345678",
    "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=10
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=10
 > 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 139f409fd00ad11c293ce9726c7a638f73212062 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 139f409fd00ad11c293ce9726c7a638f73212062
Commit message: "Update pipeline.jenkinsfile"
 > git rev-list --no-walk 9fc1715852a27f40d5fa3277d6f58ac150c972da # timeout=10
[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
10
[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=10
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=10
 > 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 1f532ef638de79563acdabb39378602140ebd2e1 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 1f532ef638de79563acdabb39378602140ebd2e1
Commit message: "Update pipeline.jenkinsfile"
 > git rev-list --no-walk 139f409fd00ad11c293ce9726c7a638f73212062 # timeout=10
[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
11
[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:18
[Pipeline] echo
Phone:13912345678
[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技术实践_10:安装部署Artifactory

DEVOPS技术实践_08:Jenkins多分支管道

DEVOPS技术实践_12:创建持续集成的管道

DEVOPS技术实践_11:Jenkins集成Sonar

DEVOPS技术实践_13:使用Jenkins持续传送设计-CD基础

DEVOPS技术实践_19:Pipeline的多参数json调用