我来详细介绍 Jenkins Pipeline(流水线)的使用方法。
一、Jenkins Pipeline 基础概念
Jenkins Pipeline 是一套插件,支持将持续交付过程用代码方式(Infrastructure as Code)定义。主要优势:
可版本控制:Jenkinsfile 可以放入 Git 等版本控制系统
可重用:支持共享库(Shared Libraries)
持久化:即使 Jenkins 重启,构建状态也能保持
可视化:提供 Stage View 和 Blue Ocean 界面
二、创建 Pipeline Job 的步骤
1. 安装必要插件
确保已安装:
Pipeline(核心插件)
Git(如果需要从 Git 拉取代码)
2. 新建 Pipeline 任务
点击 "新建任务" → 输入任务名 → 选择 "流水线" → 确定
3. 配置 Pipeline 来源
在 Pipeline 配置页,有三种主要方式:
方式一:直接在 Jenkins 中定义(最简单)
groovy
复制
// 在 "Pipeline" 部分的脚本框中直接编写 pipeline { agent any stages { stage('Build') { steps { echo 'Hello World' } } } }方式二:从 SCM(推荐)
选择 "Pipeline script from SCM"
SCM:选择 Git/SVN 等
Repository URL:填写仓库地址
Credentials:添加认证信息
Script Path:Jenkinsfile 在仓库中的路径(默认为
Jenkinsfile)
方式三:使用 Blue Ocean
安装 Blue Ocean 插件
提供可视化编辑器,自动生成 Jenkinsfile
三、Pipeline 语法类型
1. Declarative Pipeline(声明式 - 推荐)
结构清晰,易于理解和维护:
groovy
复制
pipeline { agent any // 在任何可用的agent上执行 environment { // 定义环境变量 NAME = 'World' BUILD_VERSION = "${env.BUILD_NUMBER}" } parameters { // 构建参数 string(name: 'DEPLOY_ENV', defaultValue: 'staging', description: '部署环境') } triggers { // 触发器 cron('H */4 * * 1-5') // 工作日每4小时 } stages { stage('检出代码') { steps { git 'https://github.com/example/repo.git' } } stage('构建') { steps { sh 'mvn clean package' } } stage('测试') { parallel { // 并行执行 stage('单元测试') { steps { sh 'mvn test' } } stage('集成测试') { steps { sh 'mvn integration-test' } } } } stage('部署') { when { // 条件判断 branch 'main' } steps { sh "./deploy.sh ${params.DEPLOY_ENV}" } } } post { // 构建后操作 always { echo '构建结束' archiveArtifacts artifacts: 'target/*.jar', fingerprint: true } success { echo '构建成功!' } failure { echo '构建失败!' mail to: 'team@example.com', subject: '构建失败', body: "构建 ${env.BUILD_NUMBER} 失败" } } }2. Scripted Pipeline(脚本式)
更灵活,基于 Groovy 语法:
groovy
复制
node('linux') { def mvnHome stage('检出') { git 'https://github.com/example/repo.git' } stage('构建') { mvnHome = tool 'M3' sh "${mvnHome}/bin/mvn clean package" } stage('测试') { parallel '单元测试': { sh "${mvnHome}/bin/mvn test" }, '集成测试': { sh "${mvnHome}/bin/mvn integration-test" } } stage('部署') { if (env.BRANCH_NAME == 'main') { sh './deploy.sh' } } }四、常用功能示例
1. Docker 集成
groovy
复制
pipeline { agent { docker { image 'maven:3.8.4-jdk-11' args '-v /root/.m2:/root/.m2' } } stages { stage('Build') { steps { sh 'mvn clean package' } } } }2. 凭证管理
groovy
复制
environment { // 使用 Jenkins 中定义的凭证 AWS_ACCESS_KEY = credentials('aws-access-key') } steps { withCredentials([usernamePassword( credentialsId: 'docker-hub', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD' )]) { sh "docker login -u $USERNAME -p $PASSWORD" } }3. 超时和重试
groovy
复制
steps { timeout(time: 30, unit: 'MINUTES') { retry(3) { sh ' flaky_command.sh' } } }五、最佳实践
使用 Declarative Pipeline:结构更清晰,易于维护
将 Jenkinsfile 放入版本控制:实现 "Pipeline as Code"
保持 Stage 职责单一:每个 Stage 只做一件事
使用 Shared Libraries:复用公共逻辑
不要硬编码凭证:使用 Jenkins Credentials 插件
添加足够的日志:便于排查问题
利用
when指令:实现条件执行使用
post块:统一处理构建结果
六、调试技巧
使用
echo语句输出变量值在 Blue Ocean 中查看可视化流程
使用
replay功能修改并重新运行检查 "Pipeline Syntax" 链接,生成代码片段
这样你就可以开始使用 Jenkins Pipeline 了!建议先从简单的 Declarative Pipeline 开始,逐步添加复杂功能。