我正在努力为我的项目设置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变量来找到分支名称。
例如。当将更改推送到分支时,我的jenskins工作就会运行。但是我想获取已签出(推送更改的位置)并以此为基础的分支名称,我想在branch为“ stage”时运行一些额外的脚本。
我尝试使用“ env.BRANCH_NAME”等,但都显示为空。
任何人都可以帮助我,以获得分支名称。
答案 0 :(得分:0)
env.BRANCH_NAME
仅在 multiBranchPipeline 上起作用,因此切换项目,您将获得分支名称:)
答案 1 :(得分:0)
env.GIT_BRANCH
应该可以解决单分支管道作业
答案 2 :(得分:0)
env.gitlabSourceBranch 将用于获取触发 jenkins 作业的分支。