本地jenkins自动部署远程服务

一、准备账号与信息

1.阿里云

* 开通 ACR(容器镜像服务),创建命名空间和镜像仓库(例如 myapp)。

* 记下 Registry 地址(如 registry.cn-hangzhou.aliyuncs.com)。

* 在 ACR 控制台设置 访问凭证(固定密码或推荐方式按控制台说明)

 

2.Codeup

*  代码推到仓库,确认分支名(如 main)。

* 决定 Jenkins 拉代码方式:HTTPS + 令牌 或 SSH 公钥(在 Codeup 配 Deploy Key 或账号 SSH Key)。

 

3.远程 Linux

* 安装 Docker(官方文档:https://docs.docker.com/engine/install/ 对应发行版)

* 确认能 SSH 登录(密钥登录更安全)

 

 

二、jenkins配置

 

1.安装

* mac安装: brew install jenkins-lts

 

* 启动: /opt/homebrew/opt/openjdk@21/bin/java -Dmail.smtp.starttls.enable\=true -jar /opt/homebrew/opt/jenkins-lts/libexec/jenkins.war --httpListenAddress\=127.0.0.1 --httpPort\=8080

 

 

1.凭证配置

968ab78b2b628b7f6d847a2f836f0885 

 

2.流水线任务配置

 

10e8848f11b01f08b0352f59b03ec688

 

 

三、工程项目根目录中的Jenkinsfile

 

6c1b5508dd52a53e6cccea26c96d80d4

 

* 直接使用账户|密码

pipeline {
agent any
environment {
// ====================== 你只需要改这里 ======================
APP_NAME = "go-demo" // 你的应用名
SERVER_PORT = "18080" // 容器映射端口
VERSION = "${BUILD_NUMBER}"

// 阿里云 ACR 镜像地址(从阿里云控制台复制)
ACR_HOST = "crpi-mtra9i4lc1z87q28.cn-shanghai.personal.cr.aliyuncs.com"
ACR_REPO = "code_common/lizhi"
ACR_IMAGE = "${ACR_HOST}/${ACR_REPO}:${VERSION}"

// 远程 Linux 服务器
// REMOTE_HOST = "47.100.126.154"
REMOTE_HOST = "106.15.203.107"
REMOTE_USER = "root"
// ==========================================================
}

stages {
// 1. 拉取云效代码
stage('拉取云效Codeup代码') {
steps {
git url: 'https://codeup.aliyun.com/69d8bd1c405bafb07e128c21/jinxu/app/server.git',
branch: 'master',
credentialsId: 'codeup-creds'
}
}

// 3. 本地构建 Docker 镜像
stage('构建 Docker 镜像') {
steps {
sh '''
docker build --platform linux/amd64 -t ${ACR_IMAGE} .
'''
}
}

// 4. 推送到阿里云 ACR
stage('推送到阿里云 ACR') {
steps {
withCredentials([usernamePassword(
credentialsId: 'acr-creds',
usernameVariable: 'ACR_USER',
passwordVariable: 'ACR_PWD'
)]) {
sh '''
echo "${ACR_PWD}" | docker login ${ACR_HOST} -u ${ACR_USER} --password-stdin
docker push ${ACR_IMAGE}
'''
}
}
}

// 5. 远程服务器拉取镜像 + 启动容器
stage('远程 Linux 部署') {
steps {
// 原:用户名密码 SSH(Username with password),已改用下方密钥认证
/*
withCredentials([
usernamePassword(
credentialsId: 'remote-ssh',
usernameVariable: 'SSH_USER',
passwordVariable: 'SSH_PASS'
),
usernamePassword(
credentialsId: 'acr-creds',
usernameVariable: 'ACR_USER',
passwordVariable: 'ACR_PWD'
)
]) {
script {
def remote = [:]
remote.name = "server"
remote.host = "${REMOTE_HOST}"
remote.user = "${SSH_USER}"
remote.password = "${SSH_PASS}"
remote.port = 22
remote.allowAnyHosts = true

sshCommand remote: remote, command: """
set -e
echo '${ACR_PWD}' | docker login ${ACR_HOST} -u '${ACR_USER}' --password-stdin
docker pull ${ACR_IMAGE}
docker rm -f ${APP_NAME} || true

if ! docker run -d \
--name ${APP_NAME} \
-p ${SERVER_PORT}:8080 \
--restart always \
${ACR_IMAGE}; then
echo 'docker run failed, printing diagnostics...'
docker ps -a --filter "name=${APP_NAME}" --no-trunc || true
docker logs ${APP_NAME} --tail 200 || true
docker image inspect ${ACR_IMAGE} >/dev/null 2>&1 || true
exit 125
fi
"""
}
}
*/

// 新:SSH 密钥认证(改用系统 ssh,避免 ssh-steps/JSch 认证兼容问题)
withCredentials([
sshUserPrivateKey(
credentialsId: 'remote-ssh2',
keyFileVariable: 'SSH_KEY',
usernameVariable: 'SSH_USER'
),
usernamePassword(
credentialsId: 'acr-creds',
usernameVariable: 'ACR_USER',
passwordVariable: 'ACR_PWD'
)
]) {


// ⚠️⚠️⚠️⚠️⚠️shell脚本顶格写,避免构建时语法出错
sh '''
set -euo pipefail
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \
-i "$SSH_KEY" "$SSH_USER@${REMOTE_HOST}" <<EOF
set -e
echo '${ACR_PWD}' | docker login ${ACR_HOST} -u '${ACR_USER}' --password-stdin
docker pull ${ACR_IMAGE}
docker rm -f ${APP_NAME} || true

if ! docker run -d \
--name ${APP_NAME} \
-p ${SERVER_PORT}:8080 \
--restart always \
${ACR_IMAGE}; then
echo 'docker run failed, printing diagnostics...'
docker ps -a --filter "name=${APP_NAME}" --no-trunc || true
docker logs ${APP_NAME} --tail 200 || true
docker image inspect ${ACR_IMAGE} >/dev/null 2>&1 || true
exit 125
fi
EOF
'''
}
}
}

}

post {
success {
echo "✅ 部署成功!镜像:${ACR_IMAGE}"
}
failure {
echo "❌ 构建或部署失败!"
}
}
}

 

 

 

 

直接运行构建即可

 

 

 

 

 

 

posted @ 2026-04-15 09:38  Wolf_Stark  阅读(29)  评论(0)    收藏  举报