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