任务类型 #

Jenkins支持多种任务类型,每种类型都有其特定的用途和适用场景。本节将详细介绍各种任务类型及其特点。

任务类型概览 #

类型 英文名称 适用场景 复杂度
自由风格项目 Freestyle Project 简单构建任务
流水线 Pipeline 复杂CI/CD流程
多分支流水线 Multibranch Pipeline Git分支管理
多配置项目 Multi-configuration Project 多平台构建
文件夹 Folder 组织管理任务
组织文件夹 Organization Folder GitHub/GitLab组织

自由风格项目 (Freestyle Project) #

特点 #

  • 图形化配置界面
  • 简单易用
  • 适合简单构建任务
  • 不支持复杂流程控制

适用场景 #

text
✓ 简单的编译构建
✓ 执行Shell脚本
✓ 定时任务
✓ 快速原型验证
✗ 复杂的CI/CD流程
✗ 需要条件判断的流程

创建示例 #

  1. New Item → 输入名称 → 选择 Freestyle project

  2. 配置源码管理

text
Source Code Management: Git
Repository URL: https://github.com/user/repo.git
Branch: */main
  1. 配置构建触发器
text
Build Triggers: 
  ☑ Build periodically
  Schedule: H 2 * * *
  1. 配置构建步骤
bash
#!/bin/bash
echo "开始构建..."
mvn clean package
echo "构建完成!"
  1. 配置构建后操作
text
Post-build Actions:
  ☑ Archive the artifacts
  Files to archive: target/*.jar

优缺点 #

优点 缺点
配置简单直观 不支持复杂流程
学习曲线低 无法版本化管理
适合快速上手 难以复用
插件支持丰富 维护成本高

流水线 (Pipeline) #

特点 #

  • 代码化配置 (Pipeline as Code)
  • 支持复杂流程控制
  • 可版本化管理
  • 支持并行执行

适用场景 #

text
✓ 复杂CI/CD流程
✓ 多阶段构建
✓ 条件判断和循环
✓ 并行执行任务
✓ 需要版本化管理的流程

Declarative Pipeline示例 #

groovy
pipeline {
    agent any
    
    environment {
        APP_NAME = 'myapp'
        VERSION = "${BUILD_NUMBER}"
    }
    
    stages {
        stage('Checkout') {
            steps {
                checkout scm
            }
        }
        
        stage('Build') {
            steps {
                sh 'mvn clean package'
            }
        }
        
        stage('Test') {
            parallel {
                stage('Unit Tests') {
                    steps {
                        sh 'mvn test'
                    }
                }
                stage('Integration Tests') {
                    steps {
                        sh 'mvn verify -P integration'
                    }
                }
            }
        }
        
        stage('Deploy') {
            when {
                branch 'main'
            }
            steps {
                sh "kubectl set image deployment/${APP_NAME} ${APP_NAME}=${APP_NAME}:${VERSION}"
            }
        }
    }
    
    post {
        always {
            junit '**/target/surefire-reports/*.xml'
        }
        success {
            echo '构建成功!'
        }
        failure {
            echo '构建失败!'
        }
    }
}

Scripted Pipeline示例 #

groovy
node {
    try {
        stage('Checkout') {
            checkout scm
        }
        
        stage('Build') {
            sh 'mvn clean package'
        }
        
        stage('Test') {
            parallel(
                unitTest: {
                    sh 'mvn test'
                },
                integrationTest: {
                    sh 'mvn verify -P integration'
                }
            )
        }
        
        if (env.BRANCH_NAME == 'main') {
            stage('Deploy') {
                sh 'kubectl apply -f k8s/'
            }
        }
        
    } catch (Exception e) {
        currentBuild.result = 'FAILURE'
        throw e
    } finally {
        junit '**/target/surefire-reports/*.xml'
    }
}

优缺点 #

优点 缺点
代码化管理 学习曲线较高
支持复杂流程 需要Groovy知识
可版本化 调试相对困难
易于复用 -

多分支流水线 (Multibranch Pipeline) #

特点 #

  • 自动发现Git分支
  • 每个分支独立Pipeline
  • 支持Pull Request
  • 自动清理已删除分支

适用场景 #

text
✓ 多分支开发模式
✓ Pull Request构建
✓ 自动化分支管理
✓ Git Flow工作流

配置步骤 #

  1. New Item → 输入名称 → 选择 Multibranch Pipeline

  2. 配置分支源

text
Branch Sources:
  Add source: Git
  Repository URL: https://github.com/user/repo.git
  Credentials: github-credentials
  1. 配置构建策略
text
Build Configuration:
  Mode: by Jenkinsfile
  Script Path: Jenkinsfile
  1. 配置扫描策略
text
Scan Multibranch Pipeline Triggers:
  ☑ Periodically if not otherwise run
  Interval: 1 minute

分支命名策略 #

groovy
pipeline {
    agent any
    
    stages {
        stage('Build') {
            steps {
                echo "Building branch: ${env.BRANCH_NAME}"
            }
        }
    }
}

PR构建配置 #

text
Discover pull requests from origin:
  Strategy: Merging the pull request with the current target branch revision
  
Discover pull requests from forks:
  Strategy: Merging the pull request with the current target branch revision
  Trust: Trusted users

多配置项目 (Multi-configuration Project) #

特点 #

  • 支持多维度配置
  • 并行执行多配置
  • 适合多平台测试

适用场景 #

text
✓ 多平台测试
✓ 多版本兼容性测试
✓ 多环境部署
✓ 矩阵构建

配置示例 #

  1. 创建任务

New Item → 选择 Multi-configuration project

  1. 配置矩阵
text
Configuration Matrix:
  Add Axis:
    - Name: OS
      Values: linux windows macos
    - Name: JAVA_VERSION
      Values: 8 11 17
  1. 构建步骤
bash
#!/bin/bash
echo "Building on ${OS} with Java ${JAVA_VERSION}"
java -version
mvn clean package

执行结果 #

text
OS      JAVA_VERSION    Status
linux   8               ✓
linux   11              ✓
linux   17              ✓
windows 8               ✓
windows 11              ✓
windows 17              ✓
macos   8               ✓
macos   11              ✓
macos   17              ✓

文件夹 (Folder) #

特点 #

  • 组织管理任务
  • 支持嵌套结构
  • 可以设置权限

适用场景 #

text
✓ 组织大量任务
✓ 按项目/团队分类
✓ 权限隔离

创建文件夹 #

  1. New Item → 输入名称 → 选择 Folder

  2. 配置文件夹

text
Display Name: My Project
Description: 项目相关任务
  1. 在文件夹中创建任务

进入文件夹 → New Item

文件夹结构示例 #

text
Jenkins Home
├── team-frontend/
│   ├── web-app/
│   ├── mobile-app/
│   └── shared-components/
├── team-backend/
│   ├── api-service/
│   ├── worker-service/
│   └── database-migration/
└── team-devops/
    ├── infrastructure/
    └── monitoring/

组织文件夹 (Organization Folder) #

特点 #

  • 自动发现GitHub/GitLab组织
  • 自动创建多分支流水线
  • 统一管理多个仓库

适用场景 #

text
✓ 管理多个仓库
✓ GitHub/GitLab组织
✓ 自动发现新仓库

配置GitHub组织 #

  1. New Item → 选择 GitHub Organization

  2. 配置GitHub

text
GitHub:
  API URL: https://api.github.com
  Credentials: github-token
  Owner: organization-name
  1. 配置项目识别
text
Project Recognizers:
  ☑ Pipeline Jenkinsfile

任务类型选择指南 #

决策流程图 #

text
                    开始
                      │
                      ▼
            ┌─────────────────┐
            │ 是否需要复杂流程?│
            └─────────────────┘
                │         │
               是         否
                │         │
                ▼         ▼
        ┌───────────┐ ┌───────────┐
        │ Pipeline  │ │ Freestyle │
        └───────────┘ └───────────┘
                │
                ▼
        ┌─────────────────┐
        │ 是否多分支构建? │
        └─────────────────┘
                │         │
               是         否
                │         │
                ▼         │
    ┌───────────────────┐ │
    │ Multibranch       │ │
    │ Pipeline          │ │
    └───────────────────┘ │
                          │
                          ▼
                  ┌─────────────────┐
                  │ 是否多平台构建? │
                  └─────────────────┘
                          │         │
                         是         否
                          │         │
                          ▼         │
              ┌───────────────────┐ │
              │ Multi-config      │ │
              └───────────────────┘ │
                                    │
                                    ▼
                              使用 Pipeline

快速选择表 #

需求 推荐类型
简单构建任务 Freestyle Project
CI/CD流水线 Pipeline
多分支开发 Multibranch Pipeline
多平台测试 Multi-configuration Project
组织管理任务 Folder
多仓库管理 Organization Folder

下一步学习 #

小结 #

  • Freestyle适合简单任务
  • Pipeline是现代CI/CD的首选
  • Multibranch适合多分支开发
  • Multi-configuration适合多平台测试
  • Folder用于组织管理任务
  • 根据实际需求选择合适的类型
最后更新:2026-03-28