首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Jenkins可锁定资源插件中获取锁定资源的名称

如何在Jenkins可锁定资源插件中获取锁定资源的名称
EN

Stack Overflow用户
提问于 2020-04-09 14:19:14
回答 1查看 3K关注 0票数 5

我正在使用Jenkins Lockable Resources插件来决定在我的声明性管道中使用哪个服务器来进行各种构建操作。我已经设置了我的Lockable Resources,如下表所示:

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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/

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-09 15:06:57

您可以使用lock工作流步骤的variable参数获取锁定资源的名称。此选项定义将存储锁定资源名称的环境变量的名称。考虑下面的例子。

代码语言:javascript
运行
复制
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变量访问锁定的资源名称。以下是管道运行的输出。

代码语言:javascript
运行
复制
[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

您可以看到,env.resource_name变量被赋值为Win_Res_1

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61115066

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档