Jenkins管道:加载外部Jenkins管道脚本时重用工作空间 [英] Jenkins Pipelines: Re-use workspace when loading an external Jenkins pipeline script

查看:113
本文介绍了Jenkins管道:加载外部Jenkins管道脚本时重用工作空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下用例:

  1. 使用书面管道脚本签出/拉取某个Git修订版
    (我需要这样做是因为我可以动态检索修订版本)

  1. Checkout/pull a certain Git revision, using written pipeline script
    (I need this because I retrieve the revision dynamically)

从该修订版开始,加载先前检出的文件中的Jenkins-pipeline-file

From that revision, load a Jenkins-pipeline-file, located among the previously checked out files

此文件将依赖于同一签出的修订版中的文件
(因此,来自相同工作区)

This file would rely on files from the same checked out revision
(thus, from the same workspace)

问题:加载的Jenkins-pipeline-file在 new 工作区中执行.但它是空的.我需要该文件才能在相同的工作区中执行.

Problem: The loaded Jenkins-pipeline-file gets executed in a new workspace. But it is empty. I need that file to get executed in the same old workspace.

我认为,也许是因为node周围,因为node关键字创建了工作区,如文档中所述.但是,当我尝试将其 加载到node外部时,Jenkins却因为离开了沙箱"而不允许这样做.

I thought, perhaps it's because of surrounding node, because the node keyword creates workspaces, as mentioned in the docs. But when I tried to load it outside the node, Jenkins disallowed this because of "leaving the sandbox".

注意:找到了jenkins-pipeline-file并且确实执行了该文件.问题是执行期间.

Note: the jenkins-pipeline-file is found and really gets executed. The problem is during the execution.

请查看示例代码:

内联管道脚本

Inlined pipeline script

node('master') {
  def latestBuildableRevision = readFile '/my/LATEST-BUILDABLE-REVISION.txt'

  checkout poll:false,
   scm:[$class:'GitSCM', branches:[[name:latestBuildableRevision]],
   doGenerateSubmoduleConfigurations:false,
   extensions:[[$class: 'CleanBeforeCheckout']], submoduleCfg:[],
    userRemoteConfigs:[[credentialsId:'...', url:'...']]]

  load 'further-script-logic.jenkins'
}

文件:more-script-logic.jenkins

File: further-script-logic.jenkins

node('master') {
   // make use of certain files
   // assumption: pwd() is the *same* workspace which were checked-out before
   // problem: it's not, it's a new empty workspace
}

推荐答案

一种解决方法是

A kind of workaround is described here.

  1. 您必须在呼叫者脚本
  2. 的末尾使用{...}()大括号
  3. 您必须重写称为脚本才能返回闭包(lambda)
    {-> /* former code */ }
  1. You have to use {...}() braces at the end of the caller script
  2. You have to rewrite the called script to return a closure (a lambda)
    {-> /* former code */ }

这样,您就不会放弃"对执行脚本的程序流的控制.而是使用其返回的闭包并自己调用".这样可以防止Jenkins创建其他工作区.

This way, you don't "give away" the control of program flow to the executed script. Instead, you use its returned closure and "call it yourself". This prevents Jenkins from creating further workspaces.

可悲的是,我不知道这种解决方案是否允许在调用者脚本和/或被调用脚本中声明多个节点.

我已将这些更改合并到您的示例代码中.
查找标有"<--- CHANGE"的行.

I have incorporated these changes into your example code.
Look for lines marked with "<--- CHANGE".

内联管道脚本

Inlined pipeline script

node('master') {
  def latestBuildableRevision = readFile '/my/LATEST-BUILDABLE-REVISION.txt'

  checkout poll:false,
   scm:[$class:'GitSCM', branches:[[name:latestBuildableRevision]],
   doGenerateSubmoduleConfigurations:false,
   extensions:[[$class: 'CleanBeforeCheckout']], submoduleCfg:[],
    userRemoteConfigs:[[credentialsId:'...', url:'...']]]

  load 'further-script-logic.jenkins'
}()   // <--- CHANGE 1

文件:more-script-logic.jenkins

File: further-script-logic.jenkins

{->   // <--- CHANGE 2
  node('master') {
    // .....
  }
}

这篇关于Jenkins管道:加载外部Jenkins管道脚本时重用工作空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆