[英]Get name of the changed branch where changes are pushed, Jenkins pipeline script
I am struggling with setting up my CI/CD
with my project.我正在努力为我的项目设置我的
CI/CD
。 My SCM is gitlab
and CI is jenkins
.我的 SCM 是
gitlab
, CI 是jenkins
。
I have created a pipeline script which works very well.我创建了一个运行良好的管道脚本。 Also I am able to set up the
gitlab
webhook
which triggers the build in jenkins
without any issue.此外,我还可以设置
gitlab
webhook
,它可以毫无问题地触发jenkins
中的构建。
Here is my sample pipeline script:这是我的示例管道脚本:
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()
}
}
}
}
}
But the problem with above code is I am not able to find the branch name by the help of any ENV variables.但是上面代码的问题是我无法通过任何 ENV 变量找到分支名称。
For exa.例如。 My
Jenkins
job runs when a changes are pushed to a branch.我的
Jenkins
作业在将更改推送到分支时运行。 But I want to get the branch name that has been checkout (where changes are pushed) and baed on that, I want to run some extra scripts when branch is "stage".但是我想获取已结帐的分支名称(推送更改的位置)并以此为基础,我想在分支为“阶段”时运行一些额外的脚本。
I tried to use "env.BRANCH_NAME"
etc, but all are showing null.我尝试使用
"env.BRANCH_NAME"
等,但都显示为空。
Can anyone please help me, to just get the branch name.任何人都可以请帮助我,以获得分支名称。
env.GIT_BRANCH
应该可以解决单分支管道作业
env.BRANCH_NAME
仅适用于multiBranchPipeline所以切换你的项目,你会得到分支名称:)
env.gitlabSourceBranch将用于获取触发 jenkins 作业的分支。
这个命令对我有用: $env.gitlabSourceBranch
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.