簡體   English   中英

Jenkins Groovy Postbuild使用靜態文件而不是腳本

[英]Jenkins Groovy Postbuild use static file instead of script

是否可以將外部groovy腳本加載到groovy post build插件中,而不是將腳本內容粘貼到每個作業中? 我們有大約200個工作崗位,因此更新所有工作相當費時。 我知道我可以編寫一個腳本來直接更新配置文件(如本文所示: 將Jenkins Groovy Postbuild步驟添加到所有作業 ),但這些作業全天候運行,因此當我可以重新啟動Jenkins或重新加載配置時找到一個窗口是有問題的。

謝謝!

更新 :這是我真正解決的問題: https//issues.jenkins-ci.org/browse/JENKINS-21480

“我可以通過執行以下操作來做到這一點。在”Groovy腳本“框中輸入這些代替腳本:”

// Delegate to an external script
// The filename must match the class name
import JenkinsPostBuild
def postBuild = new JenkinsPostBuild(manager)
postBuild.run()

“然后在”Additional groovy classpath“框中輸入該文件的路徑。”

只需將以下內容放在“Groovy腳本:”字段中:

evaluate(new File(“... groovy script file name ...”));

此外,您可能想要更進一步。 如果腳本名稱或路徑發生變化怎么辦 使用模板插件,您可以創建一個“模板”作業,在那里定義對groovy腳本(上面一行)的調用,並在需要它的所有作業中添加引用此模板項目的名為“使用另一個項目的發布者”的構建后操作。

我們以下列方式進行。

我們有一個文件c:\\somepath\\MyScriptLibClass.groovy (Jenkins可訪問),它包含一個groovy類MyScriptLibClass的代碼。 該類包含許多設計用於靜態方法的函數(稍后將混合使用)。

我們在sytem groovy和postbuild groovy步驟的開頭包含了這個函數編寫以下語句:

[ // include lib scripts
    'MyScriptLibClass'
].each{ this.metaClass.mixin(new GroovyScriptEngine('c:\\somepath').loadScriptByName(it+'.groovy')) }

這可能看起來有點難看,但你只需要為腳本編寫一次。 您可以包含多個腳本,也可以在庫類之間使用繼承。

在這里,您可以看到庫類中的所有方法都混合在當前腳本中。 所以,如果你的班級如下:

class MyScriptLibClass {
    def setBuildName( String str ){
        binding?.variables['manager'].build.displayName = str
    }
}

在Groovy Postbuild中你可以寫:

[ // include lib scripts
    'MyScriptLibClass'
].each{ this.metaClass.mixin(new GroovyScriptEngine('c:\\somepath').loadScriptByName(it+'.groovy')) }

setBuildName( 'My Greatest Build' )

它會改變你當前版本的名稱。

還有其他方法可以加載外部groovy類,並且沒有必要使用混合。例如,您可以看一下在運行時從Java編譯和使用Groovy類?

我是怎么解決的:

使用以下內容創建文件$ JENKINS_HOME / scripts / PostbuildActions.groovy:

public class PostbuildActions {
  void setBuildName(Object manager, String str ){
    binding?.variables['manager'].build.displayName = str
  }
}

在這種情況下,在Groovy Postbuild中你可以寫:

File script = new File("${manager.envVars['JENKINS_HOME']}/scripts/PostbuildActions.groovy")
Object actions = new GroovyClassLoader(getClass().getClassLoader()).parseClass(script).newInstance();

actions.setBuildName(manager, 'My Greatest Build');

如果您希望在代碼存儲庫中安裝Groovy腳本,並將其加載到工作區中的構建/測試從屬服務器上,那么您需要注意Groovy Postbuild在主服務器上運行。

對我們來說,主服務器是Unix服務器,而構建/測試從服務器是本地網絡上的Windows PC。 因此,在使用腳本之前,我們必須打開從主服務器到從服務器的通道,並使用FilePath到該文件。

以下為我們工作:

// Get an Instance of the Build object, and from there
// the channel from the Master to the Workspace
build = Thread.currentThread().executable
channel = build.workspace.channel;

// Open a FilePath to the script
fp = new FilePath(channel, build.workspace.toString() + "<relative path to the script in Unix notation>")

// Some have suggested that the "Not NULL" check is redundant
// I've kept it for completeness
if(fp != null)
{
    // 'Evaluate' requires a string, so read the file contents to a String
    script = fp.readToString();
    // Execute the script
    evaluate(script);
} 

我剛剛面臨同樣的任務,並嘗試使用@Blaskovicz方法。 不幸的是它對我不起作用,但我在這里找到升級的代碼(Zach Auclair)

在這里發布一些細微的變化:

后期任務

//imports
import hudson.model.*
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;
import java.io.File;

// define git file
def postBuildFile = manager.build.getEnvVars()["WORKSPACE"] + "/Jenkins/SimpleTaskPostBuildReporter.GROOVY"
def file = new File(postBuildFile)
// load custom class from file
Class groovy = this.class.classLoader.parseClass(file);
// create custom object
GroovyObject groovyObj = (GroovyObject) groovy.newInstance(manager);
// do report
groovyObj.report();

git repo中的postbuild類文件(SimpleTaskPostBuildReporter.GROOVY)

class SimpleTaskPostBuildReporter {
  def manager

  public SimpleTaskPostBuildReporter(Object manager){
    if(manager == null) {
      throw new RuntimeException("Manager object can't be null")
    }
    this.manager = manager
  }

  public def report() {
    // do work with manager object
  }
}

我沒有試過這個。

您可以嘗試使用Jenkins Job DSL插件 ,它允許您使用Groovy DSL從jenkins內部重建作業,並直接從wiki支持構建時的groovy步驟

Groovy Postbuild

在構建之后執行Groovy腳本。

groovyPostBuild(String script,Behavior behavior = Behavior.DoNothing)參數:

script構建后要執行的Groovy腳本。 有關可執行操作的詳細信息,請參閱插件頁面。 行為可選。 如果腳本失敗,則允許您將構建標記為失敗,不穩定或不執行任何操作。 行為參數使用枚舉,當前有三個值:DoNothing,MarkUnstable和MarkFailed。

例子:

這個例子將運行一個打印hello,world的groovy腳本,如果失敗,它將不會影響構建的狀態:

 groovyPostBuild('println "hello, world"') This example will run a groovy script, and if that fails will mark the build as failed: groovyPostBuild('// some groovy script', Behavior.MarkFailed) This example will run a groovy script, and if that fails will mark the 

建立不穩定:

 groovyPostBuild('// some groovy script', Behavior.MarkUnstable) (Since 1.19) 

有一個工具可以使用模板作業(這是我沒有嘗試過的),這可能是作業本身所以你只需要添加后期構建步驟。 如果您不使用模板,則需要重新編碼整個項目。

我的方法是讓腳本從頭開始重新生成或創建所有作業,這樣我就不必多次應用相同的升級。 重新生成的作業保留其構建歷史記錄

我能夠得到以下工作(我也發布了這個jira問題 )。

在我的postbuild任務中

this.class.classLoader.parseClass("/home/jenkins/GitlabPostbuildReporter.groovy")
GitlabPostbuildReporter.newInstance(manager).report()

在我的文件磁盤上/home/jenkins/GitlabPostbuildReporter.groovy

class GitlabPostbuildReporter {
  def manager
  public GitlabPostbuildReporter(manager){
    if(manager == null) {
      throw new RuntimeException("Manager object musn't be null")
    }
    this.manager = manager
  }
  public def report() {
    // do work with manager object
  }
}

暫無
暫無

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

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