簡體   English   中英

獲取推送更改的更改分支的名稱,Jenkins 管道腳本

[英]Get name of the changed branch where changes are pushed, Jenkins pipeline script

我正在努力為我的項目設置我的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 變量找到分支名稱。

例如。 我的Jenkins作業在將更改推送到分支時運行。 但是我想獲取已結帳的分支名稱(推送更改的位置)並以此為基礎,我想在分支為“階段”時運行一些額外的腳本。

我嘗試使用"env.BRANCH_NAME"等,但都顯示為空。

任何人都可以請幫助我,以獲得分支名稱。

env.GIT_BRANCH應該可以解決單分支管道作業

env.BRANCH_NAME僅適用於multiBranchPipeline所以切換你的項目,你會得到分支名稱:)

env.gitlabSourceBranch將用於獲取觸發 jenkins 作業的分支。

這個命令對我有用: $env.gitlabSourceBranch

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM