feat:213

This commit is contained in:
linzhongyan 2025-10-28 18:41:55 +08:00
parent 50e9f7b741
commit 5f2ab42c37

54
Jenkinsfile vendored
View File

@ -1,26 +1,58 @@
pipeline { pipeline {
agent any agent any
environment {
APP_NAME = "go-dy"
IMAGE_NAME = "go-dy:latest"
WORKDIR = "/opt/go-dy"
DOCKER_REGISTRY = "" // 如果你需要推送到镜像仓库可以填
}
stages { stages {
stage('Checkout') { stage('Checkout Code') {
steps { steps {
git branch: 'main', url: 'http://47.95.203.241:3000/ReeseLin/golang-dy-back.git' git branch: 'main',
url: 'http://47.95.203.241:3000/ReeseLin/golang-dy-back.git',
credentialsId: '932fcb89-1c39-4005-b664-15e862f09e20'
} }
} }
stage('Build') {
stage('Build Binary') {
steps { steps {
sh 'go mod tidy' // 使用 golang 官方镜像构建
sh 'go build -o go-dy .' sh """
docker run --rm -v ${WORKDIR}:/app -w /app golang:1.22-alpine \
sh -c "go mod tidy && go build -o ${APP_NAME} ."
"""
} }
} }
stage('Build Docker Image') {
steps {
sh """
docker build -t ${IMAGE_NAME} ${WORKDIR}
"""
}
}
stage('Deploy') { stage('Deploy') {
steps { steps {
sh ''' // 停掉旧容器,启动新容器
docker stop go-dy || true sh """
docker rm go-dy || true docker stop ${APP_NAME} || true
docker build -t go-dy . docker rm ${APP_NAME} || true
docker run -d --name go-dy -p 8090:18080 go-dy docker run -d --name ${APP_NAME} -p 8090:8090 ${IMAGE_NAME}
''' """
} }
} }
} }
post {
success {
echo "流水线执行成功,服务已更新!"
}
failure {
echo "流水线失败,请检查日志"
}
}
} }