Jenkins 联动参数流水线配置

Jenkins Plugin

plugin name:

Jenkins Config

新建任务 ——> 输入一个任务名称: Active-choice-test ——> 选择"流水线" ——> "确认"

选择"参数化构建过程" ——>选择"Active Choices Parameter" ——> 选择"Groovy Script" (使用groovy 脚本进行动态生成构建参数)——> Name: BUSINESS_NAME ——>

groovy script:

def cmd = """/bin/sh  /var/lib/jenkins/script/get_business.sh"""
def proc = cmd.execute()
proc.waitFor()

def out = new File(/tmp,.business.txt).newPrintWriter()
out.write(proc.text)
out.flush()
out.close()

def file = new File(/tmp/.business.txt)
return file.readLines()

通过groovy 运行shell 脚本灵活生成构建参数变量。

——> 添加参数 "Active Choices Reactive Parameter"

Name: HOST

Script 选择 "Groovy"

def cmd = """/bin/sh  /var/lib/jenkins/script/get_host.sh ${BUSINESS_NAME}"""
def proc = cmd.execute()
proc.waitFor()

def out = new File(/tmp,.host.txt).newPrintWriter()
out.write(proc.text)
out.flush()
out.close()

def file = new File(/tmp/.host.txt)
return file.readLines()

——> Choice Type: Check Boxes ——> Referenced parameters: BUSSINESS_NAME

——> 流水线 ——> Piepline script

pipeline {
    agent any
    stages {
        stage(Checkout) {
              steps {
                  echo "Checkout code ..."
              }
        }
        stage(Test) {
             steps {
                   echo "Testing ..."
            }
        }
       stage(Deploy) {
             steps {
                   echo "Deploying ${HOST} ..."
                   
            }
        }
    }
}
经验分享 程序员 微信小程序 职场和发展