如何使用Pipeline引用Jenkinsfile目录?

时间:2016-05-20 18:54:59

标签: jenkins groovy jenkins-workflow jenkins-pipeline jenkinsfile

我有一个groovy文件,我想从Jenkins文件运行。

即。 load script.groovy

但是,如果它存储在与Jenkinsfile相同的目录中,我不确定如何引用该文件。我从git加载Jenkinsfile。我注意到它创建了一个名为workspace@script的文件夹。它不会将其放在工作空间目录中。我可以对文件夹进行硬编码,但我不确定这个规则,再次检查代码似乎有点多余。

java.io.FileNotFoundException: /opt/jenkins_home/jobs/my_job/workspace/script.groovy (No such file or directory)

默认情况下,它从工作区加载,而不是workspace@script

我正在尝试将BuildFlow脚本转换为Pipeline(工作流)脚本。但我发现,它并不像复制和粘贴那么容易。

Jenkinsfile

node {

//get parameters from Job
def builds = builds.tokenize(",")
def ip_address_node = ip_address_node.trim()
def port_node = port_node.trim()
def branch = branch.trim()
def workspace = pwd()

stage 'Checking out code from esb repository'
git branch: branch, url: 'ssh://git@giturl/integration_bus.git'

load '../workspace@script/esb_deploybar_pipeline/deploy_esb.groovy'

}

deploy_esb.groovy(这是来自旧的buildflow,试图在管道中运行)

import groovy.transform.ToString
import groovy.transform.EqualsAndHashCode
@EqualsAndHashCode
@ToString
class BarDeploy {
    String barFile
    String app
    String integrationServer
}


//parse csv
def csvItemsApps = new HashSet<BarDeploy>();
def csvItemsLibs = new HashSet<BarDeploy>();
def deploymentMapFile = new File(workspace + "/ESB_Deployment_Map.csv")
def isFirstLine = true

stage 'Parsing ESB Deployment CSV'
deploymentMapFile.withReader { reader ->
    while(line = reader.readLine()) {
        if(isFirstLine)
        {
          isFirstLine = false
          continue
        }

        csvLine = line.split(",")
        app = csvLine[0]
        intServer = csvLine[1]

        def barDeploy = new BarDeploy()
        barDeploy.app = app
        barDeploy.integrationServer = intServer
        csvItemsApps.add(barDeploy)


        //get shared libs
        if(csvLine.length > 2 && csvLine[2] != null)
        {
            def sharedLibs = csvLine[2].split(";")
            sharedLibs.each { libString ->
                if(!libString.isAllWhitespace())
                {
                    def lib = new BarDeploy()
                    lib.app = libString
                    lib.integrationServer = intServer
                    csvItemsLibs.add(lib)
                }
            };
        }
    }
};

//get list of bar files to deploy from html and consolidate bar files to deploy with apps in csv 
for (int i = 0; i < builds.size(); i+=3)
{
    if(builds[i].equals("false"))
    {
        //Don't deploy bar if checkbox isn't selected
        continue
    }

    foundInCSV = false

    appToDeploy = builds[i + 1]
    barFileToDeploy = builds[i + 2]

    iterator = csvItemsApps.iterator()
    while (iterator.hasNext())
    {
        barDeploy = iterator.next()
        if(appToDeploy.equalsIgnoreCase(barDeploy.app))
        {
            barDeploy.barFile = barFileToDeploy
            foundInCSV = true
        }
    }

    iterator = csvItemsLibs.iterator()
    while (iterator.hasNext())
    {
        barDeploy = iterator.next()
        if(appToDeploy.equalsIgnoreCase(barDeploy.app))
        {
            barDeploy.barFile = barFileToDeploy
            foundInCSV = true
        }
    }

    if(foundInCSV == false)
    {
        throw new RuntimeException("App: " + appToDeploy + " not found in ESB_Deployment_Map.csv. Please add CSV Entry.")
    }
}


//Do deploy, deploy shared libs first
deployCSVItemsInParallel(ip_address_node,port_node,branch,env_key,csvItemsLibs)
deployCSVItemsInParallel(ip_address_node,port_node,branch,env_key,csvItemsApps)


def deploy(ip_address_node,port_node,branch,deployItem,env_key)
{
    def integrationServer = deployItem.integrationServer
    def app = deployItem.app
    def barFile = deployItem.barFile

    if(barFile == null)
    {
        return;
    }

    println("Triggering Build -> ESB App = " + app +  ", Branch = " 
            + branch + ", Barfile: " + barFile + ", Integration Server = " + integrationServer + ", IP Address: " + ip_address_node 
            + ", Port: " + port_node + ", Env_Key: " + env_key)

    build_closure = { ->
        build("esb_deploybar", 
                      ip_address_node: ip_address_node, port_node: port_node,
                      integrationServer: integrationServer, branch: branch, app: app, barFile: barFile, env_key: env_key)
    }

    return build_closure
}

def deployCSVItemsInParallel(ip_address_node,port_node,branch,env_key,csvItems)
{
    def build_closures = []
    iterator = csvItems.iterator()
    while (iterator.hasNext())
    {
      barDeploy = iterator.next()
      def build_closure = deploy(ip_address_node,port_node,branch,barDeploy,env_key)

      if(build_closure != null)
      {
          build_closures.add(build_closure)
      }
    }

    if(build_closures?.size() > 0)
    {
         parallel(build_closures)
    }
}

6 个答案:

答案 0 :(得分:12)

如果deploy_esb.groovy文件与Jenkins文件存储在同一个SCM中,则可以执行以下操作:

node {       
   def workspace = pwd() 
   load "${workspace}@script/esb_deploybar_pipeline/deploy_esb.groovy"
}

答案 1 :(得分:11)

有一种情况我没见过有人提过。当作业应该在Jenkins 代理/从属上而不是在上运行时,如何加载Groovy脚本。

由于master是从SCM检出Jenkins管道项目的那个,只能在master的文件系统中找到Groovy脚本。所以虽然这会奏效:

node {       
    def workspace = pwd() 
    def Bar = load "${workspace}@script/Bar.groovy"
    Bar.doSomething()
}

这只是一个幸福的巧合,因为从SCM克隆管道的节点与尝试在其中加载groovy脚本的节点相同。但是,只需添加要执行的其他代理的名称:

node("agent1"){
    def workspace = pwd() 
    def Bar = load "${workspace}@script/Bar.groovy"
    Bar.doSomething()
}

会失败,导致:

java.io.IOException: java.io.FileNotFoundException: /Jenkins/workspace/Foo_Job@script/Bar.groovy (No such file or directory)

这是因为这条路径:

/Jenkins/workspace/Foo_Job@script/

仅存在于主Jenkins框中。不在运行 agent1 的框中。

因此,如果您遇到此问题,请确保将master中的groovy脚本加载到全局声明的变量中,以便代理可以使用它们:

def Bar
node {       
    def workspace = pwd() 
    if(isUnix()){
        Bar = load "${workspace}@script/Bar.groovy"
    }
    else{
        Bar = load("..\\workspace@script\\Bar.groovy")
    }
}
node("agent1"){
    Bar.doSomething()
}

注意:用于在节点之间传递模块的变量必须声明 节点块。

答案 2 :(得分:1)

如果这个script.groovy文件位于项目的根目录中,比如Jenkins文件,它将从git中提取到与Jenkinsfile相同的文件夹中。所以你使用的命令应该可以正常工作。

你收到一些错误吗?如果有,请提供更多详细信息。

编辑:现在我可以看到Jenkins文件中的内容,我可以看到你正在查看一个名为integration_bus的git项目,这是groovy脚本所在的位置。您可以指定检出的位置,如下所示:

checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'esb_deploy']], submoduleCfg: [], userRemoteConfigs: [[url: 'ssh://git@giturl/integration_bus.git']]])

与你拥有的相反

git branch: branch, url: 'ssh://git@giturl/integration_bus.git'

然后你应该可以像这样引用esb_deploy文件夹中的groovy脚本

load 'esb_deploy/esb_deploybar_pipeline/deploy_esb.groovy'

答案 3 :(得分:0)

您可以假设Jenkinsfile中的所有文件操作都是相对于当前工作空间(在load内使用node时的默认工作空间)。

因此,如果目标文件(例如deploy_esb.groovy)位于SCM中的文件夹foo内,则无需其他配置即可使用:

git branch: branch, url: 'ssh://git@giturl/integration_bus.git'
load 'foo/deploy_esb.groovy'

或者如果要加载的文件与Jenkinsfile

位于同一个存储库中
checkout scm
load 'foo/deploy_esb.groovy'

答案 4 :(得分:0)

这应该有效

load "${WORKSPACE}/../${JOB_NAME}@script/esb_deploy/esb_deploybar_pipeline/deploy_esb.groovy"

答案 5 :(得分:0)

有同样的问题。最后进行了额外的克隆操作,以在工作区dir下获取脚本仓库的副本,这样我就可以可靠地访问其中的常规文件:

dir ('SCRIPTS_DIR') {
    checkout scm
    commonScripts = load 'subdir/Common.groovy' // no def, so script is global
}