> 文章列表 > 50-Jenkins-Lockable Resources插件实现资源锁定

50-Jenkins-Lockable Resources插件实现资源锁定

50-Jenkins-Lockable Resources插件实现资源锁定

Lockable Resources插件实现资源锁定

      • 前言
      • 安装插件
      • 使用插件
        • 资源配置
        • Pipeline中使用

前言

  • 用来阻止多个构建在同一时间试图使用同一个资源。这里的资源可能是一个节点、一个代理节点、一组节点或代理节点的集合,或者仅仅是一个用于上锁的名字。如果指定的资源没有在全局配置中定义,那么它将会被自动地添加到系统中。

安装插件

  • Manage Jenkins --> Mangage Plugins --> 可选插件 --> 输出框输入 Lockable Resources

使用插件

资源配置

  • Manage jenkins --> System configuration --> 找到Lockable Resources Manager
    50-Jenkins-Lockable Resources插件实现资源锁定

参数说明:

  1. Name :资源的名称,只有定义的名称创建项目的时候才可以使用它,比如printers定义了一个名叫作printers类型的lockable资源
  2. Description:描述,可以添加对资源的描述信息
  3. Labels:要选择的节点,多个之间用空格分割.比如printer1 printer2 printer3
  4. Reserved by:被某个对象预留,这里可以填写任意名称,如果有值,则资源不可用,此选项用于对资源的维护

Pipeline中使用

lock(inversePrecedence: true, label: 'printer1', quantity: 1, variable: 'resource_name')

参数说明:

  1. label:在全局设置中定义的要锁定的资源的标签
  2. quantity:在全局设置中定义的具有指定标签的资源被锁定的数量。可以把这个理解成“我必须拥有多少这种资源才能继续前行?” 如果你指定了标签但没有指定数量,那么标记该标签的所有资源都会被锁定,也就是空值或0表示锁定所有匹配的资源
  3. variable:获取资源名称
  4. inversePrecedence:表示是否可以后来居上,后进先出;如果这个参数被设置为true,那么最新的构建优先获得资源(资源可用时)。否则,所有构建将按照申请资源的顺序依次获得资源
  • stage 中使用
  1. 新建两个Pipeline – pipeline-lock
pipeline {agent anystages {stage("Lock resource") {steps {script {lock(label: 'printer1',quantity: 1 ,variable: "resource_name") {echo "Locked resource name is ${env.resource_name}"sleep 30}}sh 'echo hello world!'}}}
}
  • 新建两个Pipeline – pipeline-lock-block
pipeline {agent anystages {stage("Lock resource") {steps {script {lock(label: 'printer1',quantity: 1 ,variable: "resource_name") {echo "Locked resource name is ${env.resource_name}"}}sh 'echo hello world!'}}}
}
  1. 先运行pipeline-lock,再运行 pipeline-lock-block,下图为日志输出
    50-Jenkins-Lockable Resources插件实现资源锁定

50-Jenkins-Lockable Resources插件实现资源锁定

  • option中使用
  • pipeline-lock
pipeline {agent anyoptions {lock(inversePrecedence: true, label: 'printer1', quantity: 1, variable: 'resource_name')}stages {stage("Lock resource") {steps {script {echo "Locked resource name is ${env.resource_name}"sleep 30}sh 'echo hello world!'}}}
}
  • pipeline-lock-block
pipeline {agent anyoptions {lock(inversePrecedence: true, label: 'printer1', quantity: 1, variable: 'resource_name')}stages {stage("Lock resource") {steps {script {echo "Locked resource name is ${env.resource_name}"}sh 'echo hello world!'}}}
}
  • 日志输出

50-Jenkins-Lockable Resources插件实现资源锁定
50-Jenkins-Lockable Resources插件实现资源锁定