Jenkins Job 类型配置详解文档
目录
- 流水线 (Pipeline)
- 自由风格项目 (Freestyle project)
- 构建一个多配置项目 (Multi-configuration project)
- 组织文件夹 (Organization Folder)
- 多分支流水线 (Multibranch Pipeline)
- 文件夹 (Folder)
一、流水线 (Pipeline)
1.1 概述
流水线是 Jenkins 现代化的构建方式,使用 Jenkinsfile 定义完整的 CI/CD 流程,支持代码化配置和版本控制。
1.2 配置页面详解
1.2.1 General(基本配置)
描述 (Description)
- 功能:为 job 添加描述信息
- 用途:说明 job 的目的、功能、负责人等信息
- 最佳实践 :
- 包含项目名称和构建目的
- 指明负责人和联系方式
- 记录重要的配置说明
Discard old builds
- 功能:自动清理旧的构建历史
- 配置 :
- 保持构建的天数:设置保留构建的天数
- 保持构建的最大个数:设置保留的最大构建数量
- 最佳实践 :
- 生产环境:保留 7-30 天或 10-50 个构建
- 测试环境:保留 3-7 天或 5-20 个构建
Do not allow concurrent builds
- 功能:禁止同一 job 的并发构建
- 用途:防止资源争用,确保构建顺序执行
- 适用场景:需要独占资源的构建、依赖共享文件的构建
Do not allow the pipeline to resume if the controller restarts
- 功能:控制器重启后是否恢复流水线
- 用途:防止不完整的流水线继续执行
- 适用场景:关键的生产部署流水线
Pipeline speed/durability override
- 功能:调整流水线的速度和持久性
- 选项 :
- Performance Optimized:性能优先,减少磁盘 I/O
- Balanced:平衡模式(默认)
- Durability Optimized:可靠性优先,确保数据不丢失
- 选择建议 :
- 短期流水线:Performance Optimized
- 关键流水线:Durability Optimized
This project is parameterized
- 功能:启用参数化构建
- 参数类型 :
- String Parameter:字符串参数
- Boolean Parameter:布尔参数
- Choice Parameter:选择参数
- File Parameter:文件参数
- Password Parameter:密码参数
- Run Parameter:运行参数
- Credentials Parameter:凭据参数
Throttle builds
- 功能:限制构建的并发数
- 配置 :
- Throttle Category:节流类别
- Maximum total concurrent builds:最大总并发构建数
- Maximum concurrent builds per node:每个节点的最大并发构建数
1.2.2 Build Triggers(构建触发器)
触发远程构建 (Trigger builds remotely)
- 功能:通过 HTTP 请求触发构建
- 配置:设置身份验证令牌
- 使用方式 :
curl http://jenkins/job/jobname/build?token=TOKEN
Build after other projects are built
- 功能:在其他项目构建完成后触发
- 配置 :
- Projects to watch:要监视的项目
- Trigger only if build is stable:仅在构建稳定时触发
- Trigger even if the build is unstable:即使构建不稳定也触发
- Trigger even if the build fails:即使构建失败也触发
Build periodically
- 功能:按照 cron 表达式定时构建
- 格式 :
分 时 日 月 周 - 示例 :
H/15 * * * *(每 15 分钟构建一次)
GitHub hook trigger for GITScm polling
- 功能:GitHub webhook 触发构建
- 配置:在 GitHub 仓库设置 webhook,指向 Jenkins 的 webhook URL
Poll SCM
- 功能:定期检查代码变更并触发构建
- 配置:设置 cron 表达式
- 注意:比 webhook 方式效率低,不推荐用于大型项目
1.2.3 Pipeline(流水线配置)
Definition(定义方式)
- Pipeline script:直接在页面中编写 Jenkinsfile
- Pipeline script from SCM:从版本控制系统中获取 Jenkinsfile
Pipeline script 配置
- Script:在文本框中输入 Jenkinsfile 内容
- Use Groovy Sandbox:是否在沙箱模式下运行
Pipeline script from SCM 配置
- SCM:选择版本控制系统(Git、SVN 等)
- Repository URL:代码仓库地址
- Credentials:认证信息
- Branch Specifier :分支指定(如
*/main、*/develop) - Script Path :Jenkinsfile 路径(默认:
Jenkinsfile) - Lightweight checkout:只检出 Jenkinsfile,不检出整个仓库
1.2.4 环境变量和凭据
This build requires lockable resources
- 功能:锁定共享资源
- 用途:防止多个构建同时使用同一资源
Execute concurrent builds if necessary
- 功能:必要时执行并发构建
- 配置:设置并发构建的数量限制
1.3 Jenkinsfile 示例
Declarative Pipeline 示例
groovy
pipeline {
agent any
parameters {
string(name: 'VERSION', defaultValue: '1.0.0', description: '版本号')
choice(name: 'ENV', choices: ['dev', 'test', 'prod'], description: '环境')
booleanParam(name: 'RUN_TESTS', defaultValue: true, description: '是否运行测试')
}
environment {
BUILD_DATE = sh(returnStdout: true, script: 'date +%Y%m%d').trim()
ARTIFACT_NAME = "app-${params.VERSION}.jar"
}
options {
buildDiscarder(logRotator(numToKeepStr: '10'))
disableConcurrentBuilds()
timeout(time: 30, unit: 'MINUTES')
timestamps()
}
stages {
stage('Checkout') {
steps {
git branch: 'main', url: 'https://github.com/example/project.git'
echo "检出代码完成"
}
}
stage('Build') {
steps {
sh 'mvn clean package -DskipTests'
echo "构建完成"
}
}
stage('Test') {
when {
expression { params.RUN_TESTS }
}
steps {
sh 'mvn test'
junit 'target/surefire-reports/*.xml'
echo "测试完成"
}
}
stage('Deploy') {
steps {
script {
if (params.ENV == 'prod') {
input message: '确认部署到生产环境?', ok: '确认'
}
sh "./deploy.sh -e ${params.ENV} -v ${params.VERSION}"
}
}
}
}
post {
always {
echo '构建结束'
cleanWs()
}
success {
echo '构建成功!'
archiveArtifacts artifacts: 'target/*.jar', allowEmptyArchive: true
}
failure {
echo '构建失败!'
mail to: 'dev@example.com', subject: "构建失败: ${env.JOB_NAME}"
}
}
}
1.4 最佳实践
- 使用 Jenkinsfile 纳入版本控制
- 合理使用参数化构建提高灵活性
- 设置适当的超时时间防止构建卡住
- 使用
post部分处理构建后的操作 - 利用 Blue Ocean 界面可视化构建流程
二、自由风格项目 (Freestyle project)
2.1 概述
自由风格项目是 Jenkins 最经典、最通用的构建项目类型,通过 Web 界面配置构建步骤,适合简单的构建任务。
2.2 配置页面详解
2.2.1 General(基本配置)
描述 (Description)
- 同 Pipeline 类型的描述配置
Discard old builds
- 同 Pipeline 类型的配置
This project is parameterized
- 功能:启用参数化构建
- 参数类型 :
- String Parameter
- Boolean Parameter
- Choice Parameter
- File Parameter
- Password Parameter
- Run Parameter
- Credentials Parameter
Disable this project
- 功能:暂时禁用项目
- 用途:在维护或调试时暂停构建
Execute concurrent builds if necessary
- 功能:必要时执行并发构建
- 配置:设置并发构建的数量限制
Restrict where this project can be run
- 功能:限制项目运行的节点
- 配置:输入节点标签表达式
- 示例 :
linux && java8、windows || mac
2.2.2 Source Code Management(源代码管理)
None
- 功能:不使用源代码管理
- 用途:不需要从 SCM 获取代码的构建任务
Git
- 功能:配置 Git 仓库
- 配置项 :
- Repository URL:Git 仓库地址
- Credentials:Git 认证信息
- Branches to build :要构建的分支(如
*/main) - Repository browser:仓库浏览器(如 GitHub、GitLab)
- Additional Behaviors :额外行为
- Clean before checkout:检出前清理
- Create a tag for every build:为每个构建创建标签
- Wipe out repository & force clone:强制重新克隆
- Advanced clone behaviors:高级克隆选项
- Advanced sub-modules behaviors:子模块选项
- Path restriction:路径限制
- Polling ignores commits from certain users:轮询忽略特定用户的提交
- Strategy for choosing what to build:构建选择策略
Subversion
- 功能:配置 SVN 仓库
- 配置项 :
- Repository URL:SVN 仓库地址
- Credentials:SVN 认证信息
- Local module directory:本地模块目录
- Repository depth:检出深度
- Ignore externals:忽略外部引用
2.2.3 Build Triggers(构建触发器)
同 Pipeline 类型的构建触发器配置,包括:
- 触发远程构建
- Build after other projects are built
- Build periodically
- GitHub hook trigger for GITScm polling
- Poll SCM
2.2.4 Build Environment(构建环境)
Delete workspace before build starts
- 功能:构建前删除工作空间
- 用途:确保构建环境干净
Use secret text(s) or file(s)
- 功能:使用凭据
- 配置:选择预定义的凭据,绑定到环境变量
Provide Configuration files
- 功能:提供配置文件
- 配置:从 Managed Files 中选择配置文件
Send files or execute commands over SSH before the build starts
- 功能:构建前通过 SSH 发送文件或执行命令
- 配置:选择 SSH 服务器,配置传输和执行参数
Send files or execute commands over SSH after the build runs
- 功能:构建后通过 SSH 发送文件或执行命令
Abort the build if it's stuck
- 功能:设置构建超时
- 配置:设置超时时间和策略
Add timestamps to the Console Output
- 功能:在控制台输出中添加时间戳
- 建议:始终启用
Inspect build log for published Gradle build scans
- 功能:检查 Gradle 构建扫描
Terminate a build if it's stuck
- 功能:构建卡住时终止构建
- 配置:设置超时策略
2.2.5 Build(构建步骤)
Execute Windows batch command
-
功能:执行 Windows 批处理命令
-
配置:输入批处理脚本
-
示例 :
batch@echo off echo 开始构建 mvn clean package echo 构建完成
Execute shell
-
功能:执行 shell 命令
-
配置:输入 shell 脚本
-
示例 :
bash#!/bin/bash echo "开始构建" mvn clean package echo "构建完成"
Invoke Ant
- 功能:执行 Ant 构建
- 配置 :
- Ant Version:Ant 版本
- Targets:目标
- Build file:构建文件(默认 build.xml)
- Properties:属性
- Java Options:Java 选项
Invoke Gradle script
- 功能:执行 Gradle 脚本
- 配置 :
- Gradle Version:Gradle 版本
- Switches:开关选项
- Tasks:任务
- Root Build script:根构建脚本目录
- Build File:构建文件
Invoke top-level Maven targets
- 功能:执行 Maven 目标
- 配置 :
- Maven Version:Maven 版本
- Goals:目标(如 clean package)
- POM:POM 文件路径
- Properties:属性
- JVM Options:JVM 选项
Execute Python script
- 功能:执行 Python 脚本
- 配置:输入 Python 代码或选择 Python 文件
Execute Groovy script
- 功能:执行 Groovy 脚本
- 配置:输入 Groovy 代码或选择 Groovy 文件
Execute managed script
- 功能:执行托管脚本
- 配置:从 Managed Scripts 中选择脚本
Run with timeout
- 功能:设置步骤超时
- 配置:设置超时时间和策略
Set build status to "pending" on GitHub commit
- 功能:在 GitHub 提交上设置构建状态为 pending
2.2.6 Post-build Actions(构建后操作)
Aggregate downstream test results
- 功能:聚合下游测试结果
- 配置:指定下游项目
Archive the artifacts
- 功能:归档构建产物
- 配置 :
- Files to archive:要归档的文件(支持通配符)
- Excludes:排除的文件
- Archive artifacts only if build is successful:仅在构建成功时归档
- Fingerprint all archived artifacts:为所有归档文件生成指纹
Build other projects
- 功能:触发其他项目构建
- 配置 :
- Projects to build:要构建的项目
- Trigger only if build succeeds:仅在构建成功时触发
- Trigger only if build is stable:仅在构建稳定时触发
Publish JUnit test result report
- 功能:发布 JUnit 测试结果
- 配置 :
- Test report XMLs:测试报告 XML 文件路径
- Health report amplification factor:健康报告放大因子
- Allow empty results:允许空结果
Publish TestNG Results
- 功能:发布 TestNG 测试结果
Record fingerprints of files to track usage
- 功能:记录文件指纹以跟踪使用情况
- 配置 :
- Files to fingerprint:要生成指纹的文件
- Fingerprint all archived artifacts:为所有归档文件生成指纹
Git Publisher
- 功能:Git 发布
- 配置 :
- Push Only If Build Succeeds:仅在构建成功时推送
- Merge Results:合并结果
- Tags:标签配置
- Branches:分支配置
E-mail Notification
- 功能:发送邮件通知
- 配置 :
- Recipients:收件人
- Send e-mail for every unstable build:每个不稳定构建都发送邮件
- Send separate e-mails to individuals who broke the build:给破坏构建的人发送单独邮件
- Send e-mail for each failed module:为每个失败的模块发送邮件
Editable Email Notification
- 功能:可编辑的邮件通知(Email Extension Plugin)
- 配置 :
- Project Recipient List:项目收件人列表
- Project Reply-To List:项目回复列表
- Content Type:内容类型
- Default Subject:默认主题
- Default Content:默认内容
- Attachments:附件
- Triggers:触发器(Success、Failure、Unstable 等)
Set GitHub commit status (universal)
- 功能:设置 GitHub 提交状态
Delete workspace when build is done
- 功能:构建完成后删除工作空间
Deploy artifacts to Maven repository
- 功能:部署构件到 Maven 仓库
- 配置 :
- Maven repository URL:Maven 仓库 URL
- Repository ID:仓库 ID
- Unique Version:唯一版本
- Deploy even if the build is unstable:即使构建不稳定也部署
Send build artifacts over SSH
- 功能:通过 SSH 发送构建产物
- 配置 :
- SSH Server:SSH 服务器
- Transfers:传输配置
- Source files:源文件
- Remove prefix:移除前缀
- Remote directory:远程目录
- Exec command:执行命令
Deploy war/ear to a container
- 功能:部署 WAR/EAR 到容器
- 配置 :
- WAR/EAR files:WAR/EAR 文件
- Context path:上下文路径
- Containers:容器配置(Tomcat、JBoss 等)
2.3 最佳实践
- 合理使用构建步骤,保持构建流程清晰
- 使用参数化构建提高灵活性
- 设置适当的构建历史保留策略
- 配置邮件通知及时了解构建状态
- 归档重要的构建产物
三、构建一个多配置项目 (Multi-configuration project)
3.1 概述
多配置项目(也称为矩阵项目)允许在多个配置组合上运行相同的构建,适用于需要在不同环境、平台或参数组合下测试的场景。
3.2 配置页面详解
3.2.1 General(基本配置)
同 Freestyle project 的基本配置,包括:
- 描述
- Discard old builds
- This project is parameterized
- Disable this project
- Execute concurrent builds if necessary
- Restrict where this project can be run
3.2.2 Configuration Matrix(配置矩阵)
Add axis(添加轴)
User-defined Axis(用户定义轴)
- 功能:定义自定义配置轴
- 配置 :
- Name:轴名称
- Values:轴值(每行一个值)
- 示例 :
- Name:
BROWSER - Values:
chrome、firefox、safari
- Name:
JDK Axis(JDK 轴)
- 功能:在不同 JDK 版本上运行
- 配置:选择已配置的 JDK 版本
Slaves Axis(节点轴)
- 功能:在不同节点上运行
- 配置:选择节点或节点标签
Label Expression Axis(标签表达式轴)
- 功能:根据标签表达式选择节点
- 配置:输入标签表达式
Run each combination sequentially(顺序运行每个组合)
- 功能:顺序执行每个配置组合
- 用途:避免资源冲突
Run each combination in parallel(并行运行每个组合)
- 功能:并行执行配置组合
- 用途:提高执行效率
Combination Filter(组合过滤器)
- 功能:过滤不需要执行的组合
- 语法:Groovy 布尔表达式
- 示例 :
!(JDK == "jdk-1.6" && BROWSER == "safari")
Execute touchstone builds first(首先执行 touchstone 构建)
- 功能:首先执行特定的 touchstone 构建
- 配置 :
- Touchstone filter:Touchstone 过滤器
- Required result:要求的结果
3.2.3 Source Code Management(源代码管理)
同 Freestyle project 的源代码管理配置。
3.2.4 Build Triggers(构建触发器)
同 Freestyle project 的构建触发器配置。
3.2.5 Build Environment(构建环境)
同 Freestyle project 的构建环境配置。
3.2.6 Build(构建步骤)
同 Freestyle project 的构建步骤配置。
3.2.7 Post-build Actions(构建后操作)
Aggregate downstream test results
- 功能:聚合下游测试结果
Archive the artifacts
- 功能:归档构建产物
Publish JUnit test result report
- 功能:发布 JUnit 测试结果
Record fingerprints of files to track usage
- 功能:记录文件指纹
Build other projects
- 功能:触发其他项目构建
E-mail Notification
- 功能:发送邮件通知
Editable Email Notification
- 功能:可编辑的邮件通知
3.3 使用场景
场景 1:多浏览器测试
Axis 1 - BROWSER: chrome, firefox, safari, edge
Axis 2 - OS: windows, linux, mac
总组合数:4 × 3 = 12 个构建
场景 2:多 JDK 版本测试
Axis 1 - JDK: jdk8, jdk11, jdk17
Axis 2 - DATABASE: mysql, postgresql, oracle
总组合数:3 × 3 = 9 个构建
场景 3:多环境部署
Axis 1 - ENV: dev, staging, production
Axis 2 - REGION: us-east, us-west, eu-west, ap-south
总组合数:3 × 4 = 12 个构建
3.4 最佳实践
- 合理设置轴的组合数量,避免过多组合导致构建时间过长
- 使用组合过滤器排除不需要的组合
- 考虑资源限制,合理设置并发数
- 使用 touchstone 构建快速验证关键配置
四、组织文件夹 (Organization Folder)
4.1 概述
组织文件夹通过扫描代码仓库自动创建多分支项目子文件夹,适用于管理大量代码仓库的组织。
4.2 配置页面详解
4.2.1 General(基本配置)
描述 (Description)
- 同其他类型的描述配置
Projects(项目)
Repository Sources(仓库源)
GitHub Organization
- 功能:扫描 GitHub 组织下的仓库
- 配置 :
- API endpoint:GitHub API 端点
- Credentials:GitHub 认证凭据
- Owner:组织名称
- Behaviors:行为配置
- Discover branches:发现分支
- Discover pull requests from origin:发现来自源仓库的 PR
- Discover pull requests from forks:发现来自 fork 的 PR
- Filter by name (with wildcards):按名称过滤
- Build strategies:构建策略
- Project Recognizers:项目识别器
- Pipeline Jenkinsfile:识别 Jenkinsfile
- Property strategy:属性策略
- Build configuration:构建配置
Bitbucket Team/Project
- 功能:扫描 Bitbucket 团队或项目下的仓库
- 配置 :
- Server URL:Bitbucket 服务器 URL
- Credentials:认证凭据
- Team/Project:团队或项目名称
- Behaviors:行为配置
GitLab Group
- 功能:扫描 GitLab 群组下的仓库
- 配置 :
- Server URL:GitLab 服务器 URL
- Credentials:认证凭据
- Group:群组名称
- Behaviors:行为配置
Gitea Organization
- 功能:扫描 Gitea 组织下的仓库
Gogs Organization
- 功能:扫描 Gogs 组织下的仓库
4.2.2 Orphaned Item Strategy(孤儿项目策略)
Discard old items(丢弃旧项目)
- 功能:自动删除不再存在的仓库对应的项目
- 配置 :
- Days to keep old items:保留旧项目的天数
- Max # of old items to keep:保留的最大旧项目数量
4.3 工作原理
- 配置组织文件夹,指定代码仓库源(GitHub、GitLab、Bitbucket 等)
- Jenkins 定期扫描组织下的所有仓库
- 为每个包含 Jenkinsfile 的仓库创建多分支流水线项目
- 自动为每个分支和 PR 创建构建任务
- 当仓库被删除或不再包含 Jenkinsfile 时,自动删除对应的项目
4.4 最佳实践
- 确保仓库中包含有效的 Jenkinsfile
- 合理设置扫描频率,避免过于频繁的扫描
- 配置适当的孤儿项目策略,自动清理不再需要的项目
- 使用统一的 Jenkinsfile 模板,确保构建一致性
五、多分支流水线 (Multibranch Pipeline)
5.1 概述
多分支流水线根据 SCM 仓库中检测到的分支自动创建流水线,为每个分支维护独立的构建历史,支持分支特定的构建配置。
5.2 配置页面详解
5.2.1 General(基本配置)
描述 (Description)
- 同其他类型的描述配置
Branch Sources(分支源)
Add source(添加源)
Git
- 功能:从 Git 仓库获取分支
- 配置 :
- Project Repository:项目仓库 URL
- Credentials:认证凭据
- Behaviors:行为配置
- Discover branches:发现分支
- Exclude branches that are also filed as PRs:排除同时作为 PR 的分支
- Only branches that are also filed as PRs:仅作为 PR 的分支
- All branches:所有分支
- Discover pull requests from origin:发现来自源仓库的 PR
- Discover pull requests from forks:发现来自 fork 的 PR
- Discover tags:发现标签
- Filter by name (with wildcards):按名称过滤
- Build strategies:构建策略
- Discover branches:发现分支
- Property strategy:属性策略
- Build configuration:构建配置
- Mode:模式(Jenkinsfile 或 by Jenkinsfile)
- Script Path:脚本路径
GitHub
- 功能:从 GitHub 仓库获取分支
- 配置 :
- Repository HTTPS URL:仓库 HTTPS URL
- Credentials:认证凭据
- Behaviors:行为配置
Bitbucket
- 功能:从 Bitbucket 仓库获取分支
GitLab
- 功能:从 GitLab 仓库获取分支
SCM API 支持的其他源
Build Configuration(构建配置)
Mode
- by Jenkinsfile:使用仓库中的 Jenkinsfile
- by inline script:使用内联脚本
Script Path
- Jenkinsfile 在仓库中的路径(默认:Jenkinsfile)
Lightweight checkout
- 只检出 Jenkinsfile,不检出整个仓库
5.2.2 Build Triggers(构建触发器)
Scan Multibranch Pipeline Triggers(扫描多分支流水线触发器)
- 功能:定期扫描分支变化
- 配置 :
- Interval:扫描间隔(1 minute、5 minutes、15 minutes、30 minutes、1 hour、2 hours、4 hours、8 hours、12 hours、1 day、1 week)
5.2.3 Orphaned Item Strategy(孤儿项目策略)
同 Organization Folder 的孤儿项目策略配置。
5.2.4 Properties(属性)
Suppress automatic SCM triggering(抑制自动 SCM 触发)
- 功能:禁用自动 SCM 触发
Pipeline Speed/Durability Override
- 同 Pipeline 类型的配置
Override hook management(覆盖钩子管理)
- 功能:自定义 webhook 管理
5.3 工作原理
- 配置多分支流水线,指定分支源和仓库信息
- Jenkins 定期扫描仓库中的所有分支
- 为每个包含 Jenkinsfile 的分支创建独立的流水线项目
- 自动检测新分支并创建对应的流水线
- 当分支被删除时,自动删除对应的流水线项目
- 支持 PR 构建,可以在合并前验证代码
5.4 分支特定的 Jenkinsfile
可以在不同分支中使用不同的 Jenkinsfile 配置:
main 分支的 Jenkinsfile:
groovy
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'mvn clean package'
}
}
stage('Deploy to Production') {
steps {
sh './deploy-production.sh'
}
}
}
}
develop 分支的 Jenkinsfile:
groovy
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'mvn clean package'
}
}
stage('Deploy to Staging') {
steps {
sh './deploy-staging.sh'
}
}
}
}
feature 分支的 Jenkinsfile:
groovy
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'mvn clean package'
}
}
stage('Test') {
steps {
sh 'mvn test'
}
}
}
}
5.5 最佳实践
- 在每个分支中包含适当的 Jenkinsfile
- 合理设置扫描频率,平衡及时性和资源消耗
- 使用分支过滤器排除不需要构建的分支
- 配置适当的孤儿项目策略
- 利用 PR 构建进行代码审查前的验证
六、文件夹 (Folder)
6.1 概述
文件夹是一个可以嵌套存储的容器,用于组织和分组项目。与视图不同,文件夹是一个独立的命名空间,可以在不同文件夹中创建相同名称的项目。
6.2 配置页面详解
6.2.1 General(基本配置)
名称 (Name)
- 功能:文件夹的名称
- 注意:名称在父级命名空间中必须唯一
描述 (Description)
- 功能:文件夹的描述信息
- 用途:说明文件夹的用途、包含的项目类型等
Display Name(显示名称)
- 功能:文件夹的显示名称
- 用途:可以设置与名称不同的显示名称,用于更好的可读性
6.2.2 视图配置
Default View(默认视图)
- 功能:设置文件夹的默认视图
- 选项 :
- All:显示所有项目
- 自定义视图
Views(视图)
- 功能:创建和管理文件夹内的视图
- 类型 :
- List View:列表视图
- My View:我的视图
- Dashboard View:仪表板视图
- Nested View:嵌套视图
6.2.3 权限配置
Enable project-based security(启用基于项目的安全)
- 功能:为文件夹配置独立的权限控制
- 配置 :
- 添加用户或组
- 分配权限(Overall、Job、Run、View、SCM 等)
Inheritance strategy(继承策略)
- 功能:设置权限继承策略
- 选项 :
- Inherit permissions from parent ACL:从父级 ACL 继承权限
- Do not inherit permission grants from other ACLs:不从其他 ACL 继承权限
6.2.4 属性配置
Health metrics(健康指标)
- 功能:配置文件夹的健康指标
Pipeline libraries(流水线库)
- 功能:为文件夹内的项目配置共享的流水线库
- 配置 :
- Library Name:库名称
- Default version:默认版本
- Retrieval method:检索方法(Modern SCM 或 Legacy SCM)
- Source Code Management:源代码管理配置
Environment variables(环境变量)
- 功能:为文件夹内的项目配置环境变量
- 配置 :
- 变量名和变量值
Tool Locations(工具位置)
- 功能:为文件夹内的项目配置工具位置
- 配置 :
- 工具名称和路径
6.3 文件夹结构示例
Jenkins
├── 开发团队A
│ ├── 项目A1
│ ├── 项目A2
│ └── 共享库
├── 开发团队B
│ ├── 项目B1
│ ├── 项目B2
│ └── 项目B3
├── 基础设施
│ ├── 部署流水线
│ └── 监控任务
└── 测试
├── 自动化测试
└── 性能测试
6.4 最佳实践
- 按照团队、项目类型或环境组织文件夹结构
- 使用清晰的命名规范
- 配置适当的权限控制,确保安全性
- 利用文件夹级别的环境变量和工具配置
- 为常用配置创建共享库
七、Job 类型选择指南
7.1 选择决策树
是否需要代码化配置?
├── 是 → Pipeline
│ └── 是否需要多分支管理?
│ ├── 是 → Multibranch Pipeline
│ └── 否 → Pipeline
├── 否 → 是否需要多环境/多配置测试?
│ ├── 是 → Multi-configuration project
│ └── 否 → 是否需要简单配置?
│ ├── 是 → Freestyle project
│ └── 否 → 是否需要组织管理?
│ ├── 是 → Organization Folder
│ └── 否 → Folder
7.2 场景推荐
| 场景 | 推荐 Job 类型 | 原因 |
|---|---|---|
| 简单的 CI/CD 流程 | Freestyle project | 配置简单,快速上手 |
| 复杂的 CI/CD 流程 | Pipeline | 代码化配置,版本控制,灵活性高 |
| 多分支开发 | Multibranch Pipeline | 自动管理分支,支持 PR 构建 |
| 多环境测试 | Multi-configuration project | 矩阵构建,多配置组合 |
| 多仓库管理 | Organization Folder | 自动发现和创建项目 |
| 项目组织 | Folder | 清晰的组织结构,权限管理 |
7.3 迁移建议
- Freestyle → Pipeline:将构建步骤转换为 Jenkinsfile,实现代码化配置
- Pipeline → Multibranch:添加分支源配置,实现自动分支管理
- 单项目 → Folder:创建文件夹,将相关项目移动到文件夹中
八、总结
Jenkins 提供了多种 Job 类型,每种类型都有其特定的用途和优势:
- Pipeline:现代化的代码化配置方式,适合复杂的 CI/CD 流程
- Freestyle project:经典的通用构建方式,适合简单的构建任务
- Multi-configuration project:矩阵构建,适合多环境/多配置测试
- Organization Folder:自动管理多仓库,适合大型组织
- Multibranch Pipeline:自动管理多分支,适合现代 Git 工作流
- Folder:项目组织和分组,提高可维护性
选择合适的 Job 类型可以提高构建效率、简化配置管理、增强可维护性。建议根据项目需求和团队规模选择最适合的类型。