2017-04-20 104 views
1

我有一個多分支管道,在我的回購庫中有一個Jenkinsfile,我能夠擁有我的CI工作流程(構建&單元測試 - > deploy-dev - >批准 - >部署 - QA - >批准 - >部署 - 生產)每個提交。 我想要做的是在第一階段構建&單元測試的每晚構建中添加SonarQube分析。 由於我的版本是由Gitlab triggerd我定義我的管道觸發如下:如何在Jenkins聲明式管道中處理每晚構建

pipeline { 
    ... 
    triggers { 
     gitlab(triggerOnPush: true, triggerOnMergeRequest: true, branchFilterType: 'All') 
    } 
    ... 
} 

設置我每晚構建我已經加入

triggers { 
    ... 
    cron('H H * * *') 
} 

但現在,如何執行分析步驟,如果我們只有在晚上建立由cron表達引發的工作?

我簡化構建階段看起來如下:

stage('Build & Tests & Analysis') { 
    // HERE THE BEGIN SONAR ANALYSIS (to be executed on nightly builds) 
    bat 'msbuild.exe ...' 
    bat 'mstest.exe ...' 
    // HERE THE END SONAR ANALYSIS (to be executed on nightly builds) 
} 

回答

2

還有就是如何讓打造觸發信息的方式。在此描述: https://jenkins.io/doc/pipeline/examples/#get-build-cause

這是對你有好處檢查這個問題,以及: how to get $CAUSE in workflow

爲你的情況很好的參考是https://hopstorawpointers.blogspot.com/2016/10/performing-nightly-build-steps-with.html。下面是從源頭恰好符合您需要的功能:

// check if the job was started by a timer 
@NonCPS 
def isJobStartedByTimer() { 
    def startedByTimer = false 
    try { 
     def buildCauses = currentBuild.rawBuild.getCauses() 
     for (buildCause in buildCauses) { 
      if (buildCause != null) { 
       def causeDescription = buildCause.getShortDescription() 
       echo "shortDescription: ${causeDescription}" 
       if (causeDescription.contains("Started by timer")) { 
        startedByTimer = true 
       } 
      } 
     } 
    } catch(theError) { 
     echo "Error getting build cause" 
    } 

    return startedByTimer 
} 
+0

如何使用從* *聲明管道這個方法/步驟? – schnatterer

+0

它使用這種方法很好用!謝謝 – Vincent

0

你可以檢查生成的原因,像這樣:

stage('Build & Tests & Analysis') { 
    when { 
     expression { 
      for (Object currentBuildCause : script.currentBuild.rawBuild.getCauses()) { 
       return currentBuildCause.class.getName().contains('TimerTriggerCause') 
      } 
     } 
     steps { 
      bat 'msbuild.exe ...' 
      bat 'mstest.exe ...' 
     } 
    } 
} 

然而,這需要下面的條目script-approval.xml

<approvedSignatures> 
    <string>method hudson.model.Run getCauses</string> 
    <string>method org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper getRawBuild</string> 
</approvedSignatures> 

這也可以通過https://YOURJENKINS/scriptApproval/批准。
希望在修復JENKINS-41272之後,這將不再需要。

在此之前,一個解決辦法可能是檢查一天的小時在when表達(記住,這些時間是指對詹金斯的時區)

when { expression { return Calendar.instance.get(Calendar.HOUR_OF_DAY) in 0..3 } } 
0

對我來說,最簡單的方法是定義在建觸發一個cron和使用when expression覈實夜間舞臺小時:

pipeline { 
    agent any 
    triggers { 
     pollSCM('* * * * *') //runs this pipeline on every commit 
     cron('30 23 * * *') //run at 23:30:00 
    } 

    stages { 
     stage('nightly') { 
      when {//runs only when the expression evaluates to true 
       expression {//will return true when the build runs via cron trigger (also when there is a commit at night between 23:00 and 23:59) 
        return Calendar.instance.get(Calendar.HOUR_OF_DAY) in 23 
       } 
      } 

      steps { 
       echo "Running the nightly stage only at night..." 
      } 
     } 
    } 
} 
+0

不幸的是,這個解決方案有一個主要的缺點。如果您只有一個構建處理器正在另一個分支上運行構建作業,並且您在分支中提交了更改,則會每分鐘在隊列中創建一個新作業。我懷疑這會不停地排隊。 – phivo

+0

您可以通過停止運行同一作業的構建來解決該問題,請參閱此處的示例:https://github.com/cloudbees/jenkins-scripts/blob/master/cancel-builds-same-job.groovy 我們使用諸如共享庫這樣的groovy腳本,並在流水線開始時調用它。 – rmpestano

相關問題