jenkins - 如何在詹金斯管道的触发器部分使用环境变量?
问题描述
读取节点标签和 triggerConfigURL 的属性文件,节点标签有效,但我无法从环境读取和设置 triggerConfigURL。
def propFile = "hello/world.txt" //This is present in workspace, and it works.
pipeline {
environment {
nodeProp = readProperties file: "${propFile}"
nodeLabel = "$nodeProp.NODE_LABEL"
dtcPath = "$nodeProp.DTC"
}
agent { label env.nodeLabel } // this works!! sets NODE_LABEL value from the properties file.
triggers {
gerrit dynamicTriggerConfiguration: 'true',
triggerConfigURL: env.dtcPath, // THIS DON'T WORK, tried "${env.dtcPath}" and few other notations too.
serverName: 'my-gerrit-server',
triggerOnEvents: [commentAddedContains('^fooBar$')]
}
stages {
stage('Print Env') {
steps {
script {
sh 'env' // This prints "dtcPath=https://path/of/the/dtc/file", so the dtcPath env is set.
}
}
}
运行作业后,配置如下:
解决方案
在env
andtriggers
子句中,Jenkins 一个接一个地运行,看起来您已经通过实验证明了triggers
首先运行和env
第二个运行。它看起来也像是agent
在追赶env
。
虽然我不知道程序员为什么做出这个具体决定,但我认为您处于一种先有鸡还是先有蛋的问题中,您想使用文件定义管道但只能在管道一次读取文件已定义并正在运行。
话虽如此,以下可能有效:
def propFile = "hello/world.txt"
def nodeProp = null
node {
nodeProp = readProperties file: propFile
}
pipeline {
environment {
nodeLabel = nodeProp.NODE_LABEL
dtcPath = nodeProp.DTC
}
agent { label env.nodeLabel }
triggers {
gerrit dynamicTriggerConfiguration: 'true',
triggerConfigURL: nodeProp.DTC,
//etc.
推荐阅读
- python-3.x - 从postgres数据库n python中获取所有行的问题
- android - 当 React 本机应用程序启动时,如何在设备上保存/缓存所有数据以便在后台快速加载和更新?
- c++ - 没有名为“stoi”的成员
- spring-boot - SpringBoot项目中的CORS配置错误
- amazon-web-services - AWS中的BaseUrl映射不一致
- sql - 如何在sql server中将连续的序列号顺序排列为两列或多列?
- flutter - 添加 Firestore 文档时出现 PlatformException 错误(java.lang.IllegalArgumentException:无效数据。不支持嵌套数组)
- html - CSS 弹性布局
- c - 自由函数的动态内存问题。应用程序在堆缓冲区结束后写入内存
- python - Python:通过基于搜索的索引页面进行抓取