Jenkins参数化设置,取参数化值应用到Shell脚本或Pipeline中


Jenkins 项目参数化配置如下:

 

Jenkins 配置的Pipeline scripts如下:

pipeline {
agent any

#定义全局环境变量
environment{
E_COUNTRY="${params.COUNTRY}" #获取jenkins配置的参数化值,赋值给环境变量
E_BRANCH="${params.BRANCH}"
E_git_repo="${params.git_repo}"

#执行shell命令,将执行的shell命令结果赋值给环境变量
E_CMD_RESULT="${sh(returnStdout: true,script: 'echo `date +%Y%m%d`').trim()}"
}
stages {
stage('pull') {
steps {
sh"git clone -b ${params.BRANCH} ${params.git_repo} ${params.COUNTRY}/${env.BUILD_NUMBER} && cd ${params.COUNTRY}/${env.BUILD_NUMBER}"
}
}
stage('execute shell_1') {
steps {
sh "sh /var/lib/jenkins/workspace/scripts/deploy_pre_build.sh"   #执行shell脚本
}
}
stage('Build') {
when {
expression{
currentBuild.currentResult == "SUCCESS"
}
}
steps {
echo "${currentBuild.result}"
echo "${currentBuild.currentResult}"
echo "${env.E_CMD_RESULT}"
}
}
stage('execute shell_2') {
when {
expression{
currentBuild.currentResult == "SUCCESS"
}
environment name: 'E_CMD_RESULT', value: "${env.E_CMD_RESULT}"
}

steps {
sh("bash /var/lib/jenkins/workspace/scripts/deploy_step_1.sh")  #执行shell脚本
}
post {
success {
echo 'test post...'
}
}
}
stage('execute shell_3') {
when {
expression{
currentBuild.currentResult == "SUCCESS"
}
}
steps {
sh("bash /var/lib/jenkins/workspace/scripts/deploy_step_2.sh")  #执行shell脚本
}

}

}
post {
always {
echo 'always post...'
}
}

}

 

 

Shell脚本如下:

vim deploy_pre_build.sh

#!/bin/sh
echo "$Test"
echo "$BRANCH"
echo "111111111"
echo "${BRANCH}"
echo "${E_COUNTRY}"
echo "${env.E_COUNTRY}"

 

vim deploy_step_1.sh 

#!/bin/bash
VERSION="${E_CMD_RESULT}"
PASSWORD="${E_COUNTRY}admin"
echo "${VERSION}"
echo "$PASSWORD"

 

#!/bin/bash
VERSION="${E_CMD_RESULT}"
PASSWORD="${E_COUNTRY}admin"
echo "${VERSION}"
echo "$PASSWORD"

#!/bin/bash
VERSION="${E_CMD_RESULT}"
PASSWORD="${E_COUNTRY}admin"
echo "${VERSION}"
echo "$PASSWORD"

 

 

 

具体如何获取Jenkins中参数值,reference官网链接如下:

jenkinsfile: https://www.jenkins.io/doc/book/pipeline/jenkinsfile/

pipeline syntax:  https://www.jenkins.io/doc/book/pipeline/syntax/

environment avriables(环境变量) : https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#using-environment-variables

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM