首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Jenkins2 学习系列21 -- pipeline 中 agent 使用介绍

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

作者头像
mafeifan
发布2019-08-12 16:31:33
6.1K0
发布2019-08-12 16:31:33
举报
文章被收录于专栏:finleyMafinleyMa
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执行的目的。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.08.10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • agent label 标签
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档