Jenkins2 学习系列22 -- pipeline 中 agent 使用介绍

2,533 阅读1分钟

agent label 标签

当agnet数量变多时,如何区分这些agnet有哪些特点呢?比如哪些环境是node10,哪些是JDK8,为了区分,我们可以给不同的agent打标签(也叫tag)。一个agent可以拥有多个标签,为避免冲突,标签名不能包含空格,!&<>()|等这些特殊符号。打标签时可以考虑以下维度: 工具链: jdk, node, php 语言或工具的版本 操作系统:linux, windows, osx 系统位数: 32bit, 64bit

定义好标签后,可以在pipeline中指定他了,你可能见过

pipeline {
   agent any
}

agent any 告诉 Jenkins master 任意可用的agent都可以执行

agent 必须放在pipeline的顶层定义或stage中可选定义,放在stage中就是不同阶段使用不同的agent

通过标签指定 agent,比如某项目需要在JDK8中环境中构建

pipeline {
  agent {
    label 'jdk8'
  }
  stages {
     stage ('build') {
         steps {
            echo 'build'
         }
     }
  }
}

实际上agent { label 'jdk8' }agent { node { label 'jdk8' } } 的简写。

label 支持过滤多标签

agent {
  label 'windows && jdk8'
}

node 除了 label 选项,还支持自定义工作目录

agent {
  node {
      label 'jdk8'
      customWorkspace '/var/lib/custom'
   }
}

不分配 agent

agent none ,这样可以在具体的stages中定义

when 指令中的 beforeAgent 选项

pipeline {
   agent none
   stages {
     stage ('example build')  {
        steps {
           echo 'hello world'
        }
     }
     stage ('example deploy') {
       agent {
          label 'some-label'
       }
       when {
          beforeAgent true
          branch 'production' 
       }
       steps {
          echo  'deploying'
       }
     }
   }
}

只有当分支为 production时,才会进入 'example deploy' 阶段,这样避免了agent中拉取代码,从而达到加速pipeline执行的目的。

参考

www.jianshu.com/p/1ee7a828e…