2016-09-16 78 views

回答

6

我有一個類似的問題。我必須通過讓作業J1,J2創建屬性文件然後使用主管道P1中的「複製工件」來獲取這些文件來完成。然後將這些屬性轉換爲Java屬性(這可能需要Jenkins中的一些腳本批准)。如果Jenkins管道可以直接在代碼中返回參數(可能有這樣做,但我不知道),那將會很好。從構建步驟返回的結果是RunWrapper,它似乎沒有辦法返回我可以看到的自定義結果(除非我們使用一些現有的屬性,如構建描述)。

所以我有這樣的事情:

// Pipeline code in P1 

// Build J1 and get result. 
def j1BuildResult = build job: 'J1', parameters: [string(name: 'J1_PROP', value: 'FOO')], propagate: true, wait: true 

// Get results of J1 
step([$class    : 'CopyArtifact', filter: 'j1-result.properties', 
     fingerprintArtifacts: true, 
     flatten    : true, 
     projectName   : 'J1', 
     selector   : [$class  : 'SpecificBuildSelector', buildNumber: buildResult.getNumber().toString()]]) 

// Load J1 properties (you may need to turn off sandbox or approve this in Jenkins) 
Properties j1Props = new Properties() 
j1Props.load(new StringReader(readFile('j1-result.properties'))) 

// Build J2 
def j2BuildResult = build job: 'J2', parameters: [string(name: 'J2_PROP', value: j1Props.someProperty)], propagate: true, wait: true 

// Get results of J2 
step([$class    : 'CopyArtifact', filter: 'j2-result.properties', 
     fingerprintArtifacts: true, 
     flatten    : true, 
     projectName   : 'J2', 
     selector   : [$class  : 'SpecificBuildSelector', buildNumber: buildResult.getNumber().toString()]]) 

// Load J2 properties (you may need to turn off sandbox or approve this in Jenkins) 
Properties j2Props = new Properties() 
j1Props.load(new StringReader(readFile('j2-result.properties'))) 
7

它可以通過 「ENV」 來完成。如果您設法使j1將其信息添加到版本的env

如果j1是一個pieleline,你可以env.MYKEY=MYVALUE。 對於自由式的工作,我想它應該使用EnvInject plugin(沒有嘗試)。 在p1那麼你將得到一個包含這些信息的地圖,如果你超出了構建結果。

所以,如果你在p1東西線這事:

// somewhere in your pipeline, i.e. p1: 
def j1BuildResult = build job: 'J1' 
def j1EnvVariables = j1BuildResult.getBuildVariables(); 

然後j1EnvVariables將包含您在j1設置的變量的地圖。

PS:如何將該信息傳遞給參數p2是例如涵蓋了here

+0

它會爲我生成一個異常:org.kohsuke.stapler.NoStaplerConstructorException:java.lang.String的任何構造函數上沒有@DataBoundConstructor 修復任何想法 – OlivierTerrien

+0

我嘗試使用'EnvInject plugin',它不起作用。 – igreen

3

你可以得到的環境變量建立參數一起使用

def buildProperties = runWrapper.rawBuild.getEnvironment() 

這是一個常規的地圖。目標參數可與

String someProperty = buildProperties.someProperty 

限制接收:需要讓method hudson.model.Run getEnvironment在「進程內腳本審批」,並調用(因爲rawBuildnode關閉這裏面的代碼。

我也試過runWrapper.rawBuild.getAction(ParametersAction.class)但它需要很多導入到Jenkinsfile中。

注意:runWrapper.getBuildVariables()對我沒有任何回報。