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

agent label 标签

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

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

pipeline {
   agent any
}
复制代码

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

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

经过标签指定 agent,好比某项目须要在JDK8中环境中构建bash

pipeline {
  agent {
    label 'jdk8'
  }
  stages {
     stage ('build') {
         steps {
            echo 'build'
         }
     }
  }
}
复制代码

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

label 支持过滤多标签

agent {
  label 'windows && jdk8'
}
复制代码

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

agent {
  node {
      label 'jdk8'
      customWorkspace '/var/lib/custom'
   }
}
复制代码

不分配 agent

agent none ,这样能够在具体的stages中定义spa

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…code

相关文章
相关标签/搜索