Powershell 将生成的值传递给下游作业
我正在努力找到一种方法,根据管道运行期间生成的值填充下游自由式项目的参数 一个简化的例子可能最能说明这个问题:Powershell 将生成的值传递给下游作业,powershell,jenkins,jenkins-pipeline,Powershell,Jenkins,Jenkins Pipeline,我正在努力找到一种方法,根据管道运行期间生成的值填充下游自由式项目的参数 一个简化的例子可能最能说明这个问题: //other stuff... stage('Environment Creation') { steps { dir(path: "${MY_DIR}") { powershell ''' $generatedProps = New-Instance @instancePropsSplat $generated
//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???
'''
}
我在一个阶段中创建了一些资源,然后在下一个阶段中,我想使用这些资源作为参数开始一项工作。我可以修改下游的工作,但我想知道怎么做
我尝试过的事情:
- 文件参数(仅取消分级并传递)
- 显然不适合
- :使用可能不安全,且显然不适用于管道
- 定义一个“全局”变量,比如,但我不确定powershell如何改变它
那么,实现这一目标的最佳方式是什么?我有一个在管道的一个阶段中生成的值,然后我需要将该值(或包含该值的文件)作为参数传递给下游作业。因此,我发现了一种适合我的方法 在我的阶段(取决于在前一阶段中创建的文件),我将执行以下操作:
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 {
copyArtifactPermission('EnvironmentCleanup') // or just '*' if you want
}
现在,这就像一个符咒,我可以在遵循相同工作流的管道中使用它。您想要一个Powershell对象可用于另一个下游Powershell上下文吗?使用Export/Import-Clixml
,有什么不起作用?@bendersegregate我想要一个powershell对象,或者在启动另一个buildExport-Clixml时,该对象中包含的字符串值可用作参数。Clixml导出powershell对象的Xml表示,并且可以在另一个powershell会话中导入。我不明白为什么这对你不起作用。@BendertheGreatest因为我需要在powershell会话之外访问它:我需要将它传递给构建调用,我已经想出了一种不同的方法来完成这项工作,我会将该解决方案作为我自己的答案发布。我以为你想在Powershell环境中更进一步。我可以通过解析Export-Clixml
中的XML对象,或者将您想要的任何数据序列化到稍后可以读取的文件来解决这个问题。或者,如果信息本质上不敏感,您可以利用环境变量存储信息以供以后读取。