-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
85 lines (78 loc) · 3.22 KB
/
Jenkinsfile
File metadata and controls
85 lines (78 loc) · 3.22 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
def ENV_CONFIG = [
dev : [branch: 'dev', namespace: 'project-dev', cloud: 'k8s-dev', hub: 'dev.harbor.example.com'],
test: [branch: 'test', namespace: 'project-test', cloud: 'k8s-test', hub: 'test.harbor.example.com'],
uat : [branch: 'uat', namespace: 'project-uat', cloud: 'k8s-uat', hub: 'uat.harbor.example.com'],
prod: [branch: 'release', namespace: 'project-prod', cloud: 'k8s-prod', hub: 'harbor.example.com']
]
properties([
parameters([
choice(name: 'ENV', choices: ['dev', 'test', 'uat', 'prod'], description: '部署环境'),
string(name: 'BUILD_TAG', defaultValue: '', description: '自定义镜像 tag(默认使用 BUILD_ID)'),
booleanParam(name: 'SKIP_BUILD', defaultValue: false, description: '跳过构建'),
booleanParam(name: 'SKIP_DEPLOY', defaultValue: false, description: '跳过部署'),
booleanParam(name: 'DRY_RUN', defaultValue: false, description: 'Dry Run 模式:只测试流程,不执行')
])
])
def envCfg = ENV_CONFIG[params.ENV]
def namespace = envCfg.namespace
def cloud = envCfg.cloud
def hub = envCfg.hub
def branch = envCfg.branch
def project = 'your-project'
def imageTag = params.BUILD_TAG?.trim() ? params.BUILD_TAG : BUILD_ID
def label = "agent-${params.ENV}-${UUID.randomUUID().toString().take(8)}"
def buildStatus = 'SUCCESS'
podTemplate(label: label, cloud: cloud) {
node(label) {
try {
stage("Git 拉取 (${branch})") {
checkout([$class: 'GitSCM', branches: [[name: "*/${branch}"]],
userRemoteConfigs: [[url: 'git@your.git.repo/your-project.git', credentialsId: 'git-ssh']]])
}
if (!params.SKIP_BUILD && !params.DRY_RUN) {
stage('Maven 构建') {
container('maven') {
sh "mvn clean install -Dmaven.test.skip=true"
}
}
stage('构建镜像 (Kaniko)') {
container('kaniko') {
withCredentials([usernamePassword(credentialsId: 'harbor-cred', usernameVariable: 'REG_USER', passwordVariable: 'REG_PASS')]) {
sh """
mkdir -p /kaniko/.docker
cat > /kaniko/.docker/config.json <<EOF
{ "auths": { "http://${hub}": { "username": "${REG_USER}", "password": "${REG_PASS}" } } }
EOF
/kaniko/executor \
--context=. \
--dockerfile=Dockerfile \
--destination=${hub}/${project}:${imageTag} \
--insecure --skip-tls-verify
"""
}
}
}
} else if (params.DRY_RUN) {
echo "Dry Run 模式:跳过构建"
}
if (!params.SKIP_DEPLOY && !params.DRY_RUN) {
stage("部署到 Kubernetes") {
sh """
kubectl set image deployment/${project}-deployment \
${project}=${hub}/${project}:${imageTag} -n ${namespace}
kubectl rollout status deployment/${project}-deployment -n ${namespace}
"""
}
} else if (params.DRY_RUN) {
echo "Dry Run 模式:跳过部署"
}
} catch (err) {
buildStatus = 'FAILURE'
currentBuild.result = 'FAILURE'
throw err
} finally {
echo "构建状态: ${buildStatus}"
// 你可在这里集成钉钉通知等
}
}
}