1+ pipeline {
2+ agent {label ' dockeragent' }
3+ // 构建逻辑已迁移到 Dockerfile,Jenkins 不再进行本地 go build
4+
5+ environment {
6+ GO111MODULE = ' on' // 开启 Modules 模式
7+ CGO_ENABLED = ' 0'
8+ APP_NAME = ' Sensitive-lexicon'
9+ REGISTRY = ' crpi-vqe38j3xeblrq0n4.cn-hangzhou.personal.cr.aliyuncs.com/go-mctown'
10+ }
11+
12+ stages {
13+ stage(' Checkout' ) {
14+ steps {
15+ checkout scm
16+ }
17+ }
18+
19+ // 使用 Dockerfile 完成编译与打包,仅保留镜像构建与推送
20+ stage(' Docker Build & Push' ) {
21+ steps {
22+ withCredentials([usernamePassword(
23+ credentialsId : ' aliyun-docker-login' ,
24+ usernameVariable : ' DOCKER_USERNAME' ,
25+ passwordVariable : ' DOCKER_PASSWORD'
26+ )]) {
27+ sh """
28+ echo "\$ DOCKER_PASSWORD" | docker login --username \$ DOCKER_USERNAME --password-stdin ${ env.REGISTRY.split('/')[0]}
29+ """
30+ }
31+
32+ script {
33+ def imageTag = " ${ env.REGISTRY} /${ env.APP_NAME} :${ env.BUILD_NUMBER} "
34+ def latestTag = " ${ env.REGISTRY} /${ env.APP_NAME} :latest"
35+
36+ sh """
37+ ls -l
38+ docker build -t ${ imageTag} --network=host .
39+ docker tag ${ imageTag} ${ latestTag}
40+ docker push ${ imageTag}
41+ docker push ${ latestTag}
42+ """
43+ }
44+ }
45+ }
46+
47+ stage(' Deploy All Compose Projects' ) {
48+ parallel {
49+ stage(' Deploy compose1' ) {
50+ agent {label ' dockeragent' }
51+ steps {
52+
53+ checkout scm
54+ sh """
55+ pwd
56+ ls -l
57+ """
58+ dir(' deploy/compose' ) {
59+ script {
60+ withCredentials([usernamePassword(
61+ credentialsId : ' aliyun-docker-login' ,
62+ usernameVariable : ' DOCKER_USERNAME' ,
63+ passwordVariable : ' DOCKER_PASSWORD'
64+ )]) {
65+ sh """
66+ echo "$DOCKER_PASSWORD " | docker login --username "$DOCKER_USERNAME " --password-stdin ${ env.REGISTRY.split('/')[0]}
67+ """
68+ }
69+ sh """
70+ pwd
71+ ls -l
72+ docker compose -f docker-compose.yml down || true
73+ docker compose -f docker-compose.yml pull
74+ docker compose -f docker-compose.yml up -d --remove-orphans
75+ """
76+ }
77+ }
78+ }
79+ }
80+
81+ }
82+ }
83+
84+
85+ }
86+
87+ post {
88+ always {
89+ cleanWs()
90+ }
91+ success {
92+ echo " ✅ 构建成功!"
93+ }
94+ failure {
95+ echo " 🔥 构建失败,请检查日志。"
96+ }
97+ }
98+ }
0 commit comments