-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJenkinsfile.example
More file actions
101 lines (90 loc) · 2.79 KB
/
Jenkinsfile.example
File metadata and controls
101 lines (90 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
pipeline {
agent any
environment {
MAVEN_OPTS = '-Xmx1024m'
BUILD_VERSION = '1.0.0'
DEPLOY_ENV = 'staging'
}
parameters {
string(name: 'BRANCH_NAME', defaultValue: 'main', description: '要构建的分支名称')
booleanParam(name: 'SKIP_TESTS', defaultValue: false, description: '是否跳过测试')
choice(name: 'DEPLOY_TARGET', choices: ['dev', 'staging', 'prod'], description: '部署目标环境')
}
options {
timeout(time: 30, unit: 'MINUTES')
retry(3)
skipDefaultCheckout()
}
tools {
maven 'Maven-3.8.6'
jdk 'JDK-17'
}
stages {
stage('Checkout') {
steps {
checkout scm
echo "检出代码完成,分支: ${params.BRANCH_NAME}"
echo "构建版本: ${env.BUILD_VERSION}"
}
}
stage('Build') {
steps {
sh 'mvn clean compile'
echo "编译完成"
}
}
stage('Test') {
when {
not { params.SKIP_TESTS }
}
steps {
sh 'mvn test'
publishTestResults testResultsPattern: 'target/test-reports/*.xml'
}
}
stage('Package') {
steps {
sh 'mvn package -DskipTests'
archiveArtifacts artifacts: 'target/*.jar', fingerprint: true
}
}
stage('Deploy') {
when {
anyOf {
branch 'main'
branch 'develop'
}
}
steps {
script {
if (params.DEPLOY_TARGET == 'prod') {
input message: '确认部署到生产环境?', ok: '部署'
}
echo "部署到 ${params.DEPLOY_TARGET} 环境"
sh "deploy.sh ${params.DEPLOY_TARGET}"
}
}
}
}
post {
always {
echo "清理工作空间"
deleteDir()
}
success {
echo "构建成功! 版本: ${env.BUILD_NUMBER}"
emailext subject: "构建成功 - ${env.JOB_NAME} #${env.BUILD_NUMBER}",
body: "构建成功完成",
to: "${env.CHANGE_AUTHOR_EMAIL}"
}
failure {
echo "构建失败"
emailext subject: "构建失败 - ${env.JOB_NAME} #${env.BUILD_NUMBER}",
body: "构建过程中发生错误,请检查日志",
to: "${env.CHANGE_AUTHOR_EMAIL}"
}
unstable {
echo "构建不稳定"
}
}
}