groovy - 如何在 Jenkins Lockable Resource 插件中获取锁定资源的名称
问题描述
我正在使用 Jenkins Lockable Resources 插件来决定在我的声明式管道中将哪个服务器用于各种构建操作。我已经设置了我的可锁定资源,如下表所示:
Resource Name Labels
Win_Res_1 Windows
Win_Res_2 Windows
Win_Res_3 Windows
Lx_Res_1 Linux
Lx_Res_2 Linux
Lx_Res_3 Linux
我想锁定一个label
然后获取对应锁定的名称resource
。
我正在编写以下代码,但无法获得所需的值r
int num_resources = 1;
def label = "Windows"; /* I have hardcoded it here for simplicity. In actual code, I will get ${lable} from my code and its value can be either Windows or Linux. */
lock(label: "${label}", quantity: num_resources)
{
def r = org.jenkins.plugins.lockableresources.LockableResourcesManager; /* I know this is incomplete but unable to get correct function call */
println (" Locked resource r is : ${r} \n");
/* r should be name of resource for example Win_Res_1. */
}
文档Lockable Resources Plugin
可在此处获得:https ://jenkins.io/doc/pipeline/steps/lockable-resources/
和
https://plugins.jenkins.io/lockable-resources/
解决方案
您可以使用工作流步骤variable
的参数获取锁定资源的名称。lock
此选项定义将存储锁定资源名称的环境变量的名称。考虑以下示例。
pipeline {
agent any
stages {
stage("Lock resource") {
steps {
script {
int num = 1
String label = "Windows"
lock(label: label, quantity: num, variable: "resource_name") {
echo "Locked resource name is ${env.resource_name}"
}
}
}
}
}
}
在此示例中,可以使用env.resource_name
变量访问锁定的资源名称。这是管道运行的输出。
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Lock resource)
[Pipeline] script
[Pipeline] {
[Pipeline] lock
Trying to acquire lock on [Label: Windows, Quantity: 1]
Lock acquired on [Label: Windows, Quantity: 1]
[Pipeline] {
[Pipeline] echo
Locked resource name is Win_Res_1
[Pipeline] }
Lock released on resource [Label: Windows, Quantity: 1]
[Pipeline] // lock
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
您可以看到该值Win_Res_1
已分配给env.resource_name
变量。
推荐阅读
- php - 正则表达式在 . , : 字符(和一些例外)
- android - Kotlin - 如何传递一个具有类构造函数的函数,它有自己的参数作为可组合的参数?
- python-3.x - 如何在文章中添加图片?
- mysql - Symfony(带有教义)迁移不适用于有效的 mysql 函数创建请求
- javascript - 当 webscraping youtube 页面时,视频返回和空对象,但 video.src 返回一个值
- java - Java Spring Boot PersistenceException (OneToMany)
- if-statement - 自动化单元格引用
- laravel - 从 checkox 附加多个值,在 Laravel 中具有多对多关系
- python - 从源代码构建 Python 时用于 ./configure 的标志
- php - CodeIgniter 图像验证