add: Jenkinscicd

This commit is contained in:
RookieCuzz
2025-10-08 11:42:09 +08:00
parent dbf0ffc752
commit ba1d8a32ff
2 changed files with 101 additions and 0 deletions

View File

@@ -9,6 +9,9 @@ RUN go mod download
# Copy source
COPY . .
# Normalize and tidy modules inside build context
RUN go mod tidy
# Build static binary for target platform
ARG TARGETOS
ARG TARGETARCH

98
Jenkinsfile vendored Normal file
View File

@@ -0,0 +1,98 @@
pipeline {
agent {label 'dockeragent'}
// 构建逻辑已迁移到 DockerfileJenkins 不再进行本地 go build
environment {
GO111MODULE = 'on' // 开启 Modules 模式
CGO_ENABLED = '0'
APP_NAME = 'Sensitive-lexicon'
REGISTRY = 'crpi-vqe38j3xeblrq0n4.cn-hangzhou.personal.cr.aliyuncs.com/go-mctown'
}
stages {
stage('Checkout') {
steps {
checkout scm
}
}
// 使用 Dockerfile 完成编译与打包,仅保留镜像构建与推送
stage('Docker Build & Push') {
steps {
withCredentials([usernamePassword(
credentialsId: 'aliyun-docker-login',
usernameVariable: 'DOCKER_USERNAME',
passwordVariable: 'DOCKER_PASSWORD'
)]) {
sh """
echo "\$DOCKER_PASSWORD" | docker login --username \$DOCKER_USERNAME --password-stdin ${env.REGISTRY.split('/')[0]}
"""
}
script {
def imageTag = "${env.REGISTRY}/${env.APP_NAME}:${env.BUILD_NUMBER}"
def latestTag = "${env.REGISTRY}/${env.APP_NAME}:latest"
sh """
ls -l
docker build -t ${imageTag} --network=host .
docker tag ${imageTag} ${latestTag}
docker push ${imageTag}
docker push ${latestTag}
"""
}
}
}
stage('Deploy All Compose Projects') {
parallel {
stage('Deploy compose1') {
agent {label 'dockeragent'}
steps {
checkout scm
sh """
pwd
ls -l
"""
dir('deploy/compose') {
script {
withCredentials([usernamePassword(
credentialsId: 'aliyun-docker-login',
usernameVariable: 'DOCKER_USERNAME',
passwordVariable: 'DOCKER_PASSWORD'
)]) {
sh """
echo "$DOCKER_PASSWORD" | docker login --username "$DOCKER_USERNAME" --password-stdin ${env.REGISTRY.split('/')[0]}
"""
}
sh """
pwd
ls -l
docker compose -f docker-compose.yml down || true
docker compose -f docker-compose.yml pull
docker compose -f docker-compose.yml up -d --remove-orphans
"""
}
}
}
}
}
}
}
post {
always {
cleanWs()
}
success {
echo "✅ 构建成功!"
}
failure {
echo "🔥 构建失败,请检查日志。"
}
}
}