Ich habe ein Jenkinsfile in die Wurzel meines Projekts gelegt und möchte eine groovige Datei für meine Pipeline einlesen und ausführen. Die einzige Möglichkeit, dies zu erreichen, besteht darin, ein separates Projekt zu erstellen und den Befehl fileLoader.fromGit
zu verwenden. ich möchte zu tun
def pipeline = load 'groovy-file-name.groovy'
pipeline.pipeline()
Wenn Ihre Jenkinsfile
- und groovy-Datei in einem Repository und Jenkinsfile
von SCM geladen wird, müssen Sie Folgendes tun:
Beispiel.Groovy
def exampleMethod() {
//do something
}
def otherExampleMethod() {
//do something else
}
return this
JenkinsFile
node {
def rootDir = pwd()
def example = load "${rootDir}@script/Example.Groovy "
example.exampleMethod()
example.otherExampleMethod()
}
Sie müssen checkout scm
(oder eine andere Methode zum Überprüfen des Codes von SCM) ausführen, bevor Sie load
ausführen.
Danke @anton und @Krzysztof Krasori, Es hat gut funktioniert, wenn ich checkout scm
und genaue Quelldatei kombiniert habe
Beispiel.Groovy
def exampleMethod() {
println("exampleMethod")
}
def otherExampleMethod() {
println("otherExampleMethod")
}
return this
JenkinsFile
node {
// Git checkout before load source the file
checkout scm
// To know files are checked out or not
sh '''
ls -lhrt
'''
def rootDir = pwd()
println("Current Directory: " + rootDir)
// point to exact source file
def example = load "${rootDir}/Example.Groovy"
example.exampleMethod()
example.otherExampleMethod()
}
Wenn Sie eine Pipeline haben, die mehr als eine groovige Datei lädt, und diese groovigen Dateien auch Dinge untereinander teilen:
JenkinsFile.groovy
def modules = [:]
pipeline {
agent any
stages {
stage('test') {
steps {
script{
modules.first = load "first.groovy"
modules.second = load "second.groovy"
modules.second.init(modules.first)
modules.first.test1()
modules.second.test2()
}
}
}
}
}
first.groovy
def test1(){
//add code for this method
}
def test2(){
//add code for this method
}
return this
second.groovy
import groovy.transform.Field
@Field private First = null
def init(first) {
First = first
}
def test1(){
//add code for this method
}
def test2(){
First.test2()
}
return this
Ciao, ein sehr nützlicher Thread, hatte das gleiche Problem, das Ihnen folgend gelöst wurde.
Mein Problem war: Jenkinsfile
-> Aufruf eines first.groovy
-> Aufrufs second.groovy
Hier meine Lösung:
Jenkinsfile
node {
checkout scm
//other commands if you have
def runner = load pwd() + '/first.groovy'
runner.whateverMethod(arg1,arg2)
}
first.groovy
def first.groovy(arg1,arg2){
//whatever others commands
def caller = load pwd() + '/second.groovy'
caller.otherMethod(arg1,arg2)
}
NB: Argumente sind optional, fügen Sie sie hinzu oder lassen Sie sie leer.
Hoffe das könnte weiter helfen.