代码之家  ›  专栏  ›  技术社区  ›  Sven Grosen

将生成的值传递给下游作业

  •  0
  • Sven Grosen  · 技术社区  · 7 年前

    一个简单的例子或许最能说明这个问题:

    //other stuff...
    stage('Environment Creation') {
    
      steps {
        dir(path: "${MY_DIR}") { 
           powershell '''
              $generatedProps = New-Instance @instancePropsSplat
    
              $generatedProps | Export-Clixml -Depth 99 -Path .\\props.xml
    
           '''
           stash includes: 'props.xml', name: 'props'
        }
      }
    }
    //...later
    stage('Cleanup') {
       unstash props
       // either pass props.xml 
       build job: 'EnvironmentCleanup', wait: false, parameters: [file: ???]
       // or i could read the xml and then pass as a string
       powershell '''
          $props = Import-Clixml -Path props.xml
          # how to get this out of this powershell script???
       '''
    }
    

    我在一个阶段中创建一些资源,然后在下一个阶段中,我希望使用这些资源作为参数启动作业。我可以随心所欲地修改下游的工作,但我正努力想办法做到这一点。

    我尝试过的事情:

    潜在路径:

    • EnvInject :使用起来可能不安全,而且显然不适用于管道?
    • 定义一个“全局”变量 here ,但我不确定powershell是如何改变的

    1 回复  |  直到 7 年前
        1
  •  0
  •   Sven Grosen    7 年前

    这就是我发现的对我有效的方法。

    在依赖于前一阶段中创建的文件的阶段中,我正在执行以下操作:

    stage ("Environment Cleanup') {
      unstash props
      archiveArtifacts "props.xml"
    
      build job: 'EnvironmentCleanup', parameters: [string(name: "PROJECT", value: "${JOB_NAME}")], wait: false
    }
    

    然后在依赖作业(freestyle)中,从触发生成复制“props.xml”文件,并使用传入的作业名称作为参数,然后执行powershell将xml反序列化到对象,然后读取所需的属性。

    最后,也是最令人困惑的,我错过的部分是 options 对于触发管道项目,我需要向下游作业授予复制权限:

    options {
      copyArtifactPermission('EnvironmentCleanup') // or just '*' if you want
    }
    

    现在,这就像一个魅力,我将能够使用我的管道,遵循这个相同的工作流程。

    推荐文章