下面给你一个从入门到常用写法的 Jenkins 流水线(Pipeline)教程,基于 Jenkinsfile(最推荐方式)。
Jenkins 流水线是一段定义构建、测试、部署流程的代码,通常写在 Jenkinsfile 里,放在代码仓库中。
两种类型:
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
}
}
stage('Test') {
steps {
echo 'Testing...'
}
}
stage('Deploy') {
steps {
echo 'Deploying...'
}
}
}
}
✅ 保存为 Jenkinsfile,在 Jenkins 中新建 Pipeline 任务 即可运行。
agent any
或指定节点:
agent { label 'linux' }
stages {
stage('Clone') {
steps {
git 'https://github.com/your/repo.git'
}
}
}
常见命令:
steps {
sh 'mvn clean package'
bat 'gradlew build' // Windows
echo 'Hello'
}
environment {
VERSION = '1.0.0'
}
steps {
echo "Version: ${VERSION}"
}
stage('Checkout') {
steps {
git branch: 'main', url: 'https://github.com/xxx/xxx.git'
}
}
pipeline {
agent any
environment {
JAVA_HOME = '/usr/lib/jvm/java-11'
}
stages {
stage('Checkout') {
steps {
git 'https://github.com/your/repo.git'
}
}
stage('Build') {
steps {
sh 'mvn clean package'
}
}
stage('Test') {
steps {
sh 'mvn test'
}
}
stage('Deploy') {
steps {
sh 'scp target/app.jar server:/app/'
}
}
}
}
stage('Deploy') {
when {
branch 'main'
}
steps {
echo 'Deploy only on main'
}
}
post {
success {
echo 'Build success'
}
failure {
echo 'Build failed'
}
}
environment {
TOKEN = credentials('my-token-id')
}
steps {
sh 'curl -H "Token: $TOKEN" ...'
}
✅ 从 声明式 Pipeline 开始
✅ 把 Jenkinsfile 放进 Git
✅ 逐步加:测试 → 构建 → 部署 → 通知
如果你愿意,可以告诉我:
我可以直接帮你写一份可用的 Jenkinsfile。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。