标签与分组 #
标签(Label)用于组织和选择Jenkins节点,使构建任务能够灵活地分配到合适的节点上执行。
什么是标签? #
标签是节点的属性标记,用于:
- 标识节点特性(操作系统、工具等)
- 分组管理节点
- 选择合适的执行节点
text
节点标签示例:
linux-agent-1: linux, docker, java, maven
windows-agent-1: windows, dotnet, iis
macos-agent-1: macos, ios, xcode
设置标签 #
节点配置 #
text
Manage Jenkins → Manage Nodes → [节点名称] → Configure
Labels: linux docker java maven
多标签 #
使用空格分隔多个标签:
text
Labels: linux docker java maven
标签命名规范 #
text
推荐命名:
操作系统: linux, windows, macos
运行时: java, node, python, go
工具: docker, kubernetes, maven
环境: production, staging, development
功能: build, test, deploy
避免:
标签1, label-1, test123
使用标签 #
Pipeline中选择节点 #
groovy
pipeline {
agent { label 'linux' }
stages {
stage('Build') {
steps {
echo 'Building on Linux'
}
}
}
}
多标签条件 #
groovy
// AND条件 - 必须同时具有所有标签
agent { label 'linux && docker' }
// OR条件 - 具有任一标签
agent { label 'linux || windows' }
// 组合条件
agent { label '(linux || windows) && docker' }
动态标签选择 #
groovy
def nodeLabel = params.ENVIRONMENT == 'prod' ? 'production' : 'development'
pipeline {
agent { label nodeLabel }
stages {
stage('Deploy') {
steps {
sh 'deploy.sh'
}
}
}
}
阶段级标签 #
groovy
pipeline {
agent none
stages {
stage('Build') {
agent { label 'linux && docker' }
steps {
sh 'docker build -t myapp .'
}
}
stage('Test') {
agent { label 'linux && java' }
steps {
sh 'mvn test'
}
}
stage('Deploy') {
agent { label 'production' }
steps {
sh 'kubectl apply -f k8s/'
}
}
}
}
标签表达式 #
基本表达式 #
text
linux # 具有linux标签
linux && docker # 同时具有linux和docker标签
linux || windows # 具有linux或windows标签
!windows # 不具有windows标签
复杂表达式 #
text
(linux || windows) && docker
linux && (docker || kubernetes)
!(windows && iis)
表达式语法 #
| 操作符 | 说明 | 示例 |
|---|---|---|
&& |
AND | linux && docker |
|| |
OR | linux || windows |
! |
NOT | !windows |
() |
分组 | (linux || windows) && docker |
节点分组策略 #
按操作系统分组 #
text
Linux节点:
linux-agent-1: linux, docker, java
linux-agent-2: linux, docker, node
Windows节点:
windows-agent-1: windows, dotnet
windows-agent-2: windows, iis
macOS节点:
macos-agent-1: macos, ios, xcode
按功能分组 #
text
构建节点:
build-1: build, java, maven
build-2: build, node, npm
测试节点:
test-1: test, selenium, chrome
test-2: test, selenium, firefox
部署节点:
deploy-1: deploy, kubernetes
deploy-2: deploy, aws
按环境分组 #
text
开发环境:
dev-agent-1: development, docker
dev-agent-2: development, docker
测试环境:
test-agent-1: staging, kubernetes
生产环境:
prod-agent-1: production, kubernetes
视图分组 #
创建视图 #
- 点击首页的 + 号
- 选择视图类型
- 配置过滤条件
正则表达式过滤 #
text
Include: linux-.*
Exclude: .*-backup
标签过滤 #
text
Only show nodes with label: docker
文件夹分组 #
创建文件夹 #
text
New Item → Folder
Name: team-frontend
Description: 前端团队任务
文件夹结构 #
text
Jenkins Home
├── team-frontend/
│ ├── web-app/
│ ├── mobile-app/
│ └── shared-components/
├── team-backend/
│ ├── api-service/
│ ├── worker-service/
│ └── database-migration/
└── team-devops/
├── infrastructure/
└── monitoring/
文件夹级标签 #
groovy
// 在文件夹中设置默认标签
properties([
pipelineTriggers([
label: 'frontend'
])
])
标签最佳实践 #
1. 使用一致的命名 #
text
Good:
linux, windows, macos
java, node, python
docker, kubernetes
Bad:
Linux, LINUX, linux-node
java11, java-11, jdk11
2. 使用有意义的标签 #
text
Good:
docker, kubernetes, production
Bad:
node1, node2, test
3. 避免过度细分 #
text
Good:
linux, docker, java
Bad:
linux-ubuntu-20.04-docker-20.10-java-11
4. 文档化标签 #
text
在节点描述中说明标签含义:
Description: |
Linux构建节点
标签说明:
- linux: Ubuntu 20.04
- docker: Docker 20.10
- java: OpenJDK 11
- maven: Maven 3.8
标签管理脚本 #
列出所有标签 #
groovy
// Script Console
import jenkins.model.*
import hudson.model.*
def labels = [:]
Jenkins.instance.nodes.each { node ->
node.getLabelString().split(' ').each { label ->
labels[label] = labels.get(label, 0) + 1
}
}
labels.sort { -it.value }.each { label, count ->
println "${label}: ${count}"
}
查找具有特定标签的节点 #
groovy
import jenkins.model.*
def targetLabel = 'docker'
Jenkins.instance.nodes.each { node ->
if (node.getLabelString().contains(targetLabel)) {
println node.nodeName
}
}
下一步学习 #
小结 #
- 标签用于组织和选择节点
- 支持AND、OR、NOT表达式
- 按功能、环境、操作系统分组
- 使用一致的命名规范
- 文档化标签含义
- 可以使用脚本管理标签
最后更新:2026-03-28