Jenkins - 如何将变量传递给Bitbucket状态通知插件?

时间:2018-06-08 05:25:44

标签: git jenkins bitbucket jenkins-plugins

我正在尝试将变量传递给Bitbucketstatusnotify插件,但它无法正常工作。我尝试过不同的迭代,并使用转义字符等无济于事。这甚至可能吗?如果是,怎么样?

我的jenkins管道片段如下:

REPO_SLUG = sh(script: "echo ${GIT_URL} | grep -oP '(?<=/).*[^.git]'", returnStdout: true)

// Update Bitbucket commit build status to In Progress  
bitbucketStatusNotify(buildState: 'INPROGRESS', repoSlug: "${REPO_SLUG}", commitId: "${GIT_COMMIT}")

try {

//build code here

// Update Bitbucket commit build status to Successful  
bitbucketStatusNotify(buildState: 'SUCCESSFUL', repoSlug: "${REPO_SLUG}", commitId: "${GIT_COMMIT}")


} catch (Exception e) {

echo 'Error occured: ' + e

// Update Bitbucket commit build status to Failed  
bitbucketStatusNotify(buildState: 'FAILED', repoSlug: "${REPO_SLUG}", commitId: "${GIT_COMMIT}")

}

注意 GIT_COMMIT是一个jenkins环境变量。我尝试回应这些变量,并按预期看到正确的值,但是将它们传递给这个插件似乎没有用。

1 个答案:

答案 0 :(得分:0)

请使用该脚本:
导入hudson.tasks.test.AbstractTestResultAction

node {
    stage 'Checkout'
        notifyBitbucket('INPROGRESS')
        cleanWs()
        checkout scm
    enter code here
    stage 'Build'
        bat "nuget restore \"${workspace}/PlaygroundForVaults.sln\""
        bat "\"C:/Program Files/dotnet/dotnet.exe\" restore \"${workspace}/PlaygroundForVaults.sln\""
        bat "\"C:/Program Files/dotnet/dotnet.exe\" build \"${workspace}/PlaygroundForVaults.sln\""
    stage 'UnitTests'
        bat returnStatus: true, script: "\"C:/Program Files/dotnet/dotnet.exe\" test \"${workspace}/PlaygroundForVaults.sln\" --logger \"trx;LogFileName=unit_tests.xml\" --no-build"
        step([$class: 'MSTestPublisher', testResultsFile:"**/unit_tests.xml", failOnError: true, keepLongStdio: true])
        def allTestPassed = allTestPass()
        if(allTestPassed == false)
        {
            notifyBitbucket('FAILED')
            assert false
        }
        else
        {
            notifyBitbucket('SUCCESS')
        }
}
def allTestPass() {
    def testStatus = ""
    def allTestPass = 0
    AbstractTestResultAction testResultAction = currentBuild.rawBuild.getAction(AbstractTestResultAction.class)
    if (testResultAction != null) {
        def total = testResultAction.totalCount
        def failed = testResultAction.failCount
        def skipped = testResultAction.skipCount
        def passed = total - failed - skipped
        testStatus = "Test Status:\n  Passed: ${passed}, Failed: ${failed} ${testResultAction.failureDiffString}, Skipped: ${skipped}"
        if (failed == 0) {
            currentBuild.result = 'SUCCESS'
        }
        allTestPass = (failed == 0)
    }
    else
    {
        testStatus = "Didn't find any tests..."
        allTestPass = false
    }
    println testStatus
    return allTestPass
}
def notifyBitbucket(String state) {
    println "Notify Bitbucket With " + state + ", Commit = " + "${Commit}"
    if('SUCCESS' == state || 'FAILED' == state) {
        currentBuild.result = state         // Set result of currentBuild !Important!
    }

    notifyBitbucket commitSha1: '${Commit}', 
                    credentialsId: 'Bitbucket_PG_Global_For_Notification_User_Pass', 
                    disableInprogressNotification: false, 
                    considerUnstableAsSuccess: false, 
                    ignoreUnverifiedSSLPeer: true, 
                    includeBuildNumberInKey: false, `enter code here`
                    prependParentProjectKey: false, 
                    projectKey: '', 
                    stashServerBaseUrl: '<your server ip>:7990'