Jenkins入门教程(五):Pipeline基础入门
Jenkins Pipeline是Jenkins 2.0的核心特性,它允许你用代码的方式定义整个CI/CD流程。本文将介绍Pipeline的基础概念、语法和完整示例。
什么是Pipeline
Pipeline是一套插件,支持将持续交付流程定义为代码。Pipeline代码使用Groovy DSL编写,可以存储在版本控制系统中,与项目代码一起管理。
Pipeline的优势
- 版本控制:Pipeline作为代码存储在Git中,变更可追溯
- 可复用:通过共享库在多个项目间复用流程
- 可恢复:支持断点续传,Pipeline可以暂停和恢复
- 灵活:支持复杂的流程控制,如并行、条件、循环
两种Pipeline语法
声明式Pipeline(推荐)
声明式Pipeline使用更简洁、结构化的语法,易于阅读和维护:
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
}
}
stage('Test') {
steps {
echo 'Testing...'
}
}
stage('Deploy') {
steps {
echo 'Deploying...'
}
}
}
}
脚本式Pipeline
脚本式Pipeline提供更大的灵活性,使用完整的Groovy语法:
node {
stage('Build') {
echo 'Building...'
}
stage('Test') {
echo 'Testing...'
}
stage('Deploy') {
if (env.BRANCH_NAME == 'main') {
echo 'Deploying to production...'
}
}
}
Pipeline核心概念
- pipeline:整个Pipeline的定义容器
- agent:指定执行环境(节点或Docker容器)
- stages:包含一系列stage的容器
- stage:Pipeline的一个阶段,如构建、测试
- steps:stage中的具体执行步骤
完整Pipeline示例
下面是一个功能完整的Pipeline示例,展示了常用的指令和步骤:
pipeline {
agent any
// 定义环境变量
environment {
APP_NAME = 'my-app'
VERSION = '1.0.0'
DEPLOY_ENV = 'staging'
}
// Pipeline选项
options {
timeout(time: 30, unit: 'MINUTES') // 超时设置
buildDiscarder(logRotator(numToKeepStr: '10')) // 保留10个构建
disableConcurrentBuilds() // 禁止并发构建
}
// 构建参数
parameters {
string(name: 'BRANCH', defaultValue: 'main', description: '要构建的分支')
choice(name: 'ENV', choices: ['dev', 'staging', 'prod'], description: '部署环境')
booleanParam(name: 'SKIP_TESTS', defaultValue: false, description: '跳过测试')
}
stages {
stage('Checkout') {
steps {
echo "Checking out branch: ${params.BRANCH}"
checkout scm
// 或者指定Git仓库
// git branch: params.BRANCH, url: 'https://github.com/example/repo.git'
}
}
stage('Build') {
steps {
echo "Building ${APP_NAME} version ${VERSION}..."
sh '''
echo "Current directory: $(pwd)"
echo "Files:"
ls -la
'''
}
}
stage('Test') {
when {
expression { params.SKIP_TESTS != true }
}
steps {
echo 'Running tests...'
sh 'echo "Test 1: PASSED"'
sh 'echo "Test 2: PASSED"'
sh 'echo "Test 3: PASSED"'
}
}
stage('Deploy') {
when {
anyOf {
branch 'main'
expression { params.ENV == 'prod' }
}
}
steps {
echo "Deploying to ${params.ENV}..."
sh 'echo "Deployment successful!"'
}
}
}
// 构建后操作
post {
always {
echo 'Pipeline finished.'
}
success {
echo 'Build succeeded!'
}
failure {
echo 'Build failed!'
}
cleanup {
echo 'Cleaning up workspace...'
cleanWs() // 清理工作空间
}
}
}
执行结果示例
运行上述Pipeline后,控制台输出类似:
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/my-pipeline
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Checkout)
[Pipeline] echo
Checking out branch: main
[Pipeline] checkout
...
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] echo
Building my-app version 1.0.0...
[Pipeline] sh
+ echo 'Current directory: /var/lib/jenkins/workspace/my-pipeline'
Current directory: /var/lib/jenkins/workspace/my-pipeline
+ echo Files:
Files:
+ ls -la
total 16
drwxr-xr-x 4 jenkins jenkins 4096 Feb 8 12:00 .
drwxr-xr-x 8 jenkins jenkins 4096 Feb 8 12:00 ..
-rw-r--r-- 1 jenkins jenkins 500 Feb 8 12:00 Jenkinsfile
-rw-r--r-- 1 jenkins jenkins 1200 Feb 8 12:00 pom.xml
drwxr-xr-x 3 jenkins jenkins 4096 Feb 8 12:00 src
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] echo
Running tests...
[Pipeline] sh
+ echo 'Test 1: PASSED'
Test 1: PASSED
+ echo 'Test 2: PASSED'
Test 2: PASSED
+ echo 'Test 3: PASSED'
Test 3: PASSED
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Deploy)
[Pipeline] echo
Deploying to staging...
[Pipeline] sh
+ echo 'Deployment successful!'
Deployment successful!
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] echo
Pipeline finished.
[Pipeline] echo
Build succeeded!
[Pipeline] echo
Cleaning up workspace...
[Pipeline] cleanWs
[Pipeline] }
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
创建Pipeline Job
在Jenkins中创建Pipeline Job的步骤:
- 点击"新建任务"
- 输入任务名称
- 选择"流水线"(Pipeline)
- 在Pipeline部分选择定义方式:
- Pipeline script:直接在界面编写脚本
- Pipeline script from SCM:从版本控制系统读取Jenkinsfile
Jenkinsfile
Jenkinsfile是存储Pipeline定义的文件,通常放在项目根目录。这样Pipeline可以和代码一起版本控制:
my-project/
├── Jenkinsfile # Pipeline定义
├── pom.xml
├── src/
│ ├── main/
│ └── test/
└── README.md
常用steps命令
// 输出信息
echo 'Hello, World!'
// 执行Shell命令
sh 'ls -la'
sh '''
cd build
make all
'''
// 执行Windows批处理
bat 'dir'
// 克隆Git仓库
git branch: 'main', url: 'https://github.com/example/repo.git'
// 读取文件
def content = readFile 'config.txt'
// 写入文件
writeFile file: 'output.txt', text: 'Hello'
// 归档构建产物
archiveArtifacts artifacts: 'target/*.jar', fingerprint: true
// 发布测试报告
junit 'target/surefire-reports/*.xml'
总结
本文介绍了Pipeline的基础概念和两种语法。Pipeline是Jenkins的核心特性,掌握它对于构建现代CI/CD流程至关重要。
下一篇我们将深入学习声明式Pipeline的各种指令。
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。







