2015-11-14 22 views
10

Workflow komut dosyasına harici kod eklemeye çalışıyorum ama bir şeyleri özlüyorum. İlk adım işe yarıyor gibi görünüyor. yolu geçersiz Bu başarısız:Jenkins Workflow'da bir dış kod dosyasını nasıl ekleyebilirim?

evaluate(new File('/home/larry.fast/Wkflo.groovy')) 

Ama şu üzerinde söz dizimi varyasyonları bir dizi denedim ve çalışan bir sihir bulamadı. Tüm girişimleri sınıf mycode.Wkflo çözemediği" konulu varyantları ürettiler

def z = mycode.Wkflo() 

Wkflo.groovy içeriyor:.

package mycode; 
def hello() { 
    echo "Hello from workflow" 
} 

Ben de kaçak gibi varyasyonları denedim (Dosya()) veya kaldırma paket beyanı.

cevap

15

Jenkins İş Akışı belgelerine şimdi https://github.com/jenkinsci/workflow-plugin/blob/master/TUTORIAL.md ama zor takip bulundu. Burada komple iş akışları ve dış dosyadaki diğer yöntemlerin nasıl oluşturulacağını gösteren basit bir örnek "Senaryo Metin yükleniyor" ilgili bir bölümün de s. Jenkins içinde

İş Akışı kodu:

// Loading code requires a NODE context 
// But we want the code accessible outside the node Context 
// So declare extcode (object created by the LOAD operation) outside the Node block. 
def extcode 

node { 
    // paths are relative to your workspace 
    // you may need to run git to populate your workspace 
    git url: 'ssh://mygitrepo' 
    extcode = load 'myExtCode.wkflo' 

    // or you can directly access the file system 
    // Eg. when developing your code in a local git clone 
    extcode = load '/some/absolute/path/myExtCode.wkflo' 

    // extcode is now a groovy object containing everything declared in the file 
    extcode.hello('world') 
} 
// If your code contains Stage and Node blocks, 
// you'll want to initiate that code outside of the node() block above 
extcode.extMain() 

------ myExtCode.wkflo

// Any command placed at the root of the file get executed during the load operation 
echo "hello from loading myExtCode" 

// Methods declared in external code are accessible 
// directly from other code in the external file 
// indirectly via the object created by the load operation 
// eg. extcode.hello('use this syntax from your main code') 
def hello(whom) { 
    echo "Hello ${whom}" 
} 

// Complete workflows should be created inside a controlling method 
// else they will run nested inside the node() block when the load takes place (edit: added missing "def" keyword 
def extMain() { 
    stage 'External Code is running' 
    node() { 
     hello('from external node block') 
    } 
} 

// !!Important Boilerplate!!  
// The external code must return it's contents as an object 
return this;