This repository has been archived on 2026-01-07. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
Game/Server/gateway/Jenkinsfile

139 lines
5.0 KiB
Groovy
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
pipeline {
agent none
environment {
// 仓库
REPO_URL = 'http://47.108.184.184/gitea/HuXiaoHei/Game.git'
REPO_CREDENTIALS_ID = '80805ba2-f4ac-4d84-aee6-d4cce5fc0a96'
// Registry
REGISTRY_URL = 'registry.hlsq.asia'
REGISTRY_CREDENTIALS_ID = '0d79fc0b-a150-470b-bd0d-2639b2826031'
// 部署目标服务器
SERVER_HOST = 'www.hlsq.asia'
SERVER_USER = 'root'
REMOTE_DIR = '/opt/apps'
SSH_CREDENTIALS_ID = 'server-ssh-key'
// 基础信息
APP_NAME = 'server-gateway'
GO_MOD_CACHE_DIR = '/home/pi/Desktop/docker/jenkins/caches/go-mod'
GO_BUILD_CACHE_DIR = '/home/pi/Desktop/docker/jenkins/caches/go-build'
}
options {
skipDefaultCheckout true
}
stages {
stage('Checkout') {
agent any
steps {
checkout([
$class: 'GitSCM',
branches: [[name: '*/master']],
doGenerateSubmoduleConfigurations: false,
extensions: [],
userRemoteConfigs: [[
credentialsId: env.REPO_CREDENTIALS_ID,
url: env.REPO_URL
]]
])
// 立刻保存Git的Commit避免并发问题
script {
def shortCommit = sh(script: 'git rev-parse --short=8 HEAD', returnStdout: true).trim()
env.IMAGE_TAG = "${env.REGISTRY_URL}/${env.APP_NAME}:${shortCommit}"
echo "Checked out commit: ${env.IMAGE_TAG}"
}
}
}
stage('Build Go Binary') {
agent {
docker {
image 'golang:1.23.1-alpine'
reuseNode true
args '-u root:root -v $GO_MOD_CACHE_DIR:/go/pkg/mod -v $GO_BUILD_CACHE_DIR:/root/.cache/go-build'
}
}
steps {
dir('Server/gateway') {
sh """
export GOPROXY=https://goproxy.cn,direct
export CGO_ENABLED=0
export GOOS=linux
export GOARCH=amd64
go build -o ${env.APP_NAME} .
"""
}
}
}
stage('Push Docker Image') {
agent any
steps {
dir('Server/gateway') {
script {
withCredentials([usernamePassword(
credentialsId: env.REGISTRY_CREDENTIALS_ID,
usernameVariable: 'DOCKER_USER',
passwordVariable: 'DOCKER_PASS'
)]) {
sh """
echo "$DOCKER_PASS" | docker login --username "$DOCKER_USER" --password-stdin ${env.REGISTRY_URL}
docker build -t ${env.IMAGE_TAG} .
docker push ${env.IMAGE_TAG}
docker rmi ${env.IMAGE_TAG}
docker logout ${env.REGISTRY_URL}
"""
}
}
}
}
}
// stage('Deploy to Server') {
// steps {
// script {
// echo "Deploying image: ${IMAGE_TAG} to ${env.SERVER_HOST}"
//
// sshagent (credentials: [env.SSH_CREDENTIALS_ID]) {
// sh """
// ssh ${env.SERVER_USER}@${env.SERVER_HOST} << 'EOF'
// # 登录私有 registry使用 Jenkins 注入的凭据)
// echo "$REGISTRY_PASS" | docker login ${env.REGISTRY} --username "$REGISTRY_USER" --password-stdin
//
// # 拉取最新镜像
// docker pull ${IMAGE_TAG}
//
// # 停止并删除旧容器(如果存在)
// docker stop ${env.APP_NAME} 2>/dev/null || true
// docker rm ${env.APP_NAME} 2>/dev/null || true
//
// # 启动新容器(根据你的需求调整端口、环境变量等)
// docker run -d \\
// --name ${env.APP_NAME} \\
// --restart unless-stopped \\
// -p 8080:8080 \\
// ${IMAGE_TAG}
//
// # 可选:登出 registry
// docker logout ${env.REGISTRY}
// """
// }
// }
// }
// }
}
post {
success {
echo '✅ 构建、推送镜像与部署成功!'
}
failure {
echo '❌ 构建失败,请检查日志。'
}
}
}