Jenkins 流水线(Pipeline)通常有两种写法:声明式 Pipeline(Declarative) 和 脚本式 Pipeline(Scripted)。最常见、推荐使用的是声明式 Pipeline,通过 Jenkinsfile 来配置。
下面我从 基本概念 → 创建方式 → 示例配置 → 常用配置项 给你讲清楚。
Jenkins Pipeline 是一套用代码描述 CI/CD 流程的方式,通常写在仓库根目录的 Jenkinsfile 中,支持:
Jenkinsfile)pipeline {
agent any
stages {
stage('拉取代码') {
steps {
git 'https://github.com/your/repo.git'
}
}
stage('构建') {
steps {
sh 'mvn clean package'
}
}
stage('测试') {
steps {
sh 'mvn test'
}
}
}
}
✅ 这是最基础的 3 步流水线
agent any
常见写法:
agent { label 'linux' }
agent { docker 'maven:3.8' }
environment {
BRANCH = 'main'
TOKEN = credentials('git-token')
}
stages {
stage('Build') {
steps {
sh 'make'
}
}
}
post {
success {
echo '构建成功'
}
failure {
echo '构建失败'
}
}
Pipeline 配置中选择:
JenkinsfileJenkinsfile✅ 适合:
pipeline {
agent any
environment {
APP_NAME = 'demo'
}
stages {
stage('Checkout') {
steps {
git branch: 'main', url: 'https://github.com/xx/demo.git'
}
}
stage('Build') {
steps {
sh 'npm install'
sh 'npm run build'
}
}
stage('Deploy') {
steps {
sh 'bash deploy.sh'
}
}
}
post {
always {
cleanWs()
}
}
}
sh 'pwd' / sh 'env' 排查环境withCredentials如果你愿意,可以告诉我:
我可以直接给你一份可复制使用的 Jenkinsfile。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。