目录
1. **声明式 Pipeline (Declarative Pipeline)**
2. **脚本式 Pipeline (Scripted Pipeline)**
Jenkins Pipeline 脚本可以包括多种内容,用于定义和控制 CI/CD 流程的各个方面。根据 Jenkins Pipeline 的类型(Declarative 或 Scripted),脚本的内容和结构会有所不同。以下是详细的说明和示例:
1. **Declarative Pipeline**
Declarative Pipeline 是一种结构化、声明式的语法,适合大多数 CI/CD 场景。它使用 `pipeline` 块来定义整个流水线。
主要部分
- **agent**:指定执行构建的环境。
- **stages**:定义多个阶段,每个阶段包含一个或多个步骤。
- **steps**:定义具体的构建步骤。
- **environment**:设置环境变量。
- **tools**:指定需要的工具版本。
- **post**:定义构建完成后的操作(如清理工作空间、发送通知)。
- **triggers**:定义触发构建的方式。
- **parameters**:定义用户输入的参数。
- **options**:设置一些选项,如超时时间、并发控制等。
示例
pipeline {
agent any
environment {
NPM_CONFIG_REGISTRY = 'https://registry.npmmirror.com'
}
tools {
nodejs 'node-14'
}
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Install Dependencies') {
steps {
sh 'npm install'
}
}
stage('Build') {
steps {
sh 'npm run build'
}
}
stage('Test') {
steps {
sh 'npm test'
}
}
}
post {
always {
cleanWs()
}
success {
echo 'Build succeeded!'
&n