获取已推送更改的已更改分支的名称,Jenkins管道脚本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取已推送更改的已更改分支的名称,Jenkins管道脚本相关的知识,希望对你有一定的参考价值。
我正在努力用我的项目设置我的CI / CD。我的SCM是gitlab,CI是jenkins。
我创建了一个非常好用的管道脚本。此外,我能够设置gitlab webhook,它可以在没有任何问题的情况下触发jenkins中的构建。
这是我的示例管道脚本:
def running(gitlabBuildName) {
updateGitlabCommitStatus(name: "${gitlabBuildName}", state: 'running')
}
def success(gitlabBuildName) {
updateGitlabCommitStatus(name: "${gitlabBuildName}", state: 'success')
}
def failure(gitlabBuildName) {
updateGitlabCommitStatus(name: "${gitlabBuildName}", state: 'failed')
}
properties ([
[$class: 'GitLabConnectionProperty', gitLabConnection: 'gitlab'],
pipelineTriggers([[
$class: "GitLabPushTrigger",
triggerOnPush: true,
triggerOnMergeRequest: true,
// triggerOpenMergeRequestOnPush:"both",
//triggerOnNoteRequest: true,
//noteRegex: "CI_STAGE_BUILD_THIS",
// skipWorkInProgressMergeRequest: true,
// ciSkip:false ,
//setBuildDescription: true,
//addNoteOnMergeRequest:true,
addCiMessage: true,
addVoteOnMergeRequest: true,
acceptMergeRequestOnSuccess: true,
branchFilterType: "NameBasedFilter",
//targetBranchRegex: "some-branch",
includeBranchesSpec: "master,stage,prod",
excludeBranchesSpec: "dev"
]])
]);
pipeline {
agent {
label 'amazon-cloud'
}
options {
gitLabConnection('gitlab')
timeout(time:1, unit: 'HOURS')
}
environment {
WORK_DIR = "${WORKSPACE}/${BUILD_NUMBER}"
}
stages {
stage('PREDEPLOYMENT:: Cleanup the VM. '){
steps{
running("${JOB_NAME}")
echo "Running for ${JOB_NAME}"
echo "Deleting previous images. "
dir("$WORKSPACE"){
sh 'rm -rf *'
}
}
post{
success {
echo "Success: VM cleaned up for further tests "
}
failure {
echo "Error: Some error occured while cleaning up the system"
failure("${JOB_NAME}")
}
}
}
stage('CHECKOUT: Checkout the 1st project Repos '){
steps {
checkout([$class: 'GitSCM', branches: [[name: "my-dev"]],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: '$BUILD_NUMBER/my-node-1']],
submoduleCfg: [], userRemoteConfigs: [[credentialsId: "${GIT_SSH_KEY_ID}",
url: 'https://git_url/project_name1.git']]])
checkout([$class: 'GitSCM', branches: [[name: "my-dev"]],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: '$BUILD_NUMBER/my-node-2']],
submoduleCfg: [], userRemoteConfigs: [[credentialsId: "${GIT_SSH_KEY_ID}",
url: 'https://git_url/project_name2.git']]])
}
post {
success {
echo "Success: Git checkout done for repos. "
echo "=========="
echo "${env.GIT_BRANCH}" // prints null
}
failure {
echo "Error: Some error occured while Git checkout of repos."
failure("${JOB_NAME}")
}
}
}
stage('CHECKOUT: Checkout the project2 Repos '){
steps{
checkout([$class: 'GitSCM', branches: [[name: "new-ui"]],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: '$BUILD_NUMBER/project_name2']],
submoduleCfg: [], userRemoteConfigs: [[credentialsId: "${GIT_SSH_KEY_ID}",
url: 'https://git_url/project_name2.git']]])
}
post {
success {
echo "Success: Git checkout done for project_name2 repos. "
}
failure {
echo "Error: Some error occured while Git checkout of project_name2 repos."
failure("${JOB_NAME}")
}
}
}
stage('BUILD: Temp: Prints env variables. ') {
steps {
echo "${BRANCH_NAME}"
}
post {
success {
echo "Success: Successfully created the latest local build and tagged it."
}
failure {
echo "Error: Couldn't create the image. "
failure("${JOB_NAME}")
}
}
}
stage('Cleanup: Clean the VM Now. ') {
steps {
//sh 'docker rmi $(docker images -a -q) | echo "Not able to delete some images"'
cleancleanWs()
}
post {
success {
echo "Success: Cleaned up the vm. "
success("${JOB_NAME}")
}
failure {
echo "Error: Some error occured while cleaning up the vm. "
failure("${JOB_NAME}")
//cleanWs()
}
}
}
}
}
但上面代码的问题是我无法通过任何ENV变量找到分支名称。
对于exa。当更改被推送到分支时,我的jenskins作业会运行。但是我希望得到已经结帐的分支名称(推送更改的地方)并且依赖于此,我想在分支是“阶段”时运行一些额外的脚本。
我试图使用“env.BRANCH_NAME”等,但都显示为null。
任何人都可以帮助我,只需获得分支名称。
答案
env.BRANCH_NAME
只能在multiBranchPipeline上工作,所以切换你的项目,你将获得分支名称:)
以上是关于获取已推送更改的已更改分支的名称,Jenkins管道脚本的主要内容,如果未能解决你的问题,请参考以下文章