新增流水线脚本
This commit is contained in:
@@ -34,6 +34,7 @@ pipeline {
|
||||
Deployment_name = "s1-lessie-ai-web"
|
||||
K8s_namespace = "sit"
|
||||
Pod_container_name = "lessie-ai-web"
|
||||
Pod_environment = "s2"
|
||||
}
|
||||
stages {
|
||||
stage('回滚上版') {
|
||||
@@ -168,16 +169,16 @@ pipeline {
|
||||
kubectl describe deployment ${Deployment_name} -n ${K8s_namespace} || true
|
||||
|
||||
echo '--- Pod 列表 ---'
|
||||
kubectl get pods -n ${K8s_namespace} -l app=${Pod_container_name} -o wide || true
|
||||
kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o wide || true
|
||||
|
||||
echo "--- Pod 描述 (describe) ---"
|
||||
for pod in \$(kubectl get pods -n ${K8s_namespace} -l app=${Pod_container_name} -o jsonpath='{.items[*].metadata.name}'); do
|
||||
for pod in \$(kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o jsonpath='{.items[*].metadata.name}'); do
|
||||
echo "-- Pod 描述 \$pod --"
|
||||
kubectl describe pod \$pod -n ${K8s_namespace} || true
|
||||
done
|
||||
|
||||
echo "--- 最近 200 行 Pod 日志 ---"
|
||||
for pod in \$(kubectl get pods -n ${K8s_namespace} -l app=${Pod_container_name} -o jsonpath='{.items[*].metadata.name}'); do
|
||||
for pod in \$(kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o jsonpath='{.items[*].metadata.name}'); do
|
||||
echo "-- logs \$pod --"
|
||||
kubectl logs \$pod -n ${K8s_namespace} --tail=200 || true
|
||||
done
|
||||
@@ -186,11 +187,11 @@ pipeline {
|
||||
}
|
||||
|
||||
echo "=== 检查 Pods 是否全部 Ready ==="
|
||||
sh "kubectl get pods -l app=${Pod_container_name} -n ${K8s_namespace} -o wide"
|
||||
sh "kubectl get pods -l 'app=${Pod_container_name},environment=${Pod_environment}' -n ${K8s_namespace} -o wide"
|
||||
|
||||
echo "=== 获取最新 Pod 名称 ==="
|
||||
NEW_POD = sh (
|
||||
script: "kubectl get pods -l app=${Pod_container_name} -n ${K8s_namespace} --sort-by=.metadata.creationTimestamp -o jsonpath='{.items[-1].metadata.name}'",
|
||||
script: "kubectl get pods -l 'app=${Pod_container_name},environment=${Pod_environment}' -n ${K8s_namespace} --sort-by=.metadata.creationTimestamp -o jsonpath='{.items[-1].metadata.name}'",
|
||||
returnStdout: true
|
||||
).trim()
|
||||
|
||||
|
||||
@@ -34,6 +34,7 @@ pipeline {
|
||||
Deployment_name = "s1-lessie-go-api"
|
||||
K8s_namespace = "sit"
|
||||
Pod_container_name = "lessie-go-api"
|
||||
Pod_environment = "s2"
|
||||
}
|
||||
stages {
|
||||
stage('回滚上版') {
|
||||
@@ -168,16 +169,16 @@ pipeline {
|
||||
kubectl describe deployment ${Deployment_name} -n ${K8s_namespace} || true
|
||||
|
||||
echo '--- Pod 列表 ---'
|
||||
kubectl get pods -n ${K8s_namespace} -l app=${Pod_container_name} -o wide || true
|
||||
kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o wide || true
|
||||
|
||||
echo "--- Pod 描述 (describe) ---"
|
||||
for pod in \$(kubectl get pods -n ${K8s_namespace} -l app=${Pod_container_name} -o jsonpath='{.items[*].metadata.name}'); do
|
||||
for pod in \$(kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o jsonpath='{.items[*].metadata.name}'); do
|
||||
echo "-- Pod 描述 \$pod --"
|
||||
kubectl describe pod \$pod -n ${K8s_namespace} || true
|
||||
done
|
||||
|
||||
echo "--- 最近 200 行 Pod 日志 ---"
|
||||
for pod in \$(kubectl get pods -n ${K8s_namespace} -l app=${Pod_container_name} -o jsonpath='{.items[*].metadata.name}'); do
|
||||
for pod in \$(kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o jsonpath='{.items[*].metadata.name}'); do
|
||||
echo "-- logs \$pod --"
|
||||
kubectl logs \$pod -n ${K8s_namespace} --tail=200 || true
|
||||
done
|
||||
@@ -186,11 +187,11 @@ pipeline {
|
||||
}
|
||||
|
||||
echo "=== 检查 Pods 是否全部 Ready ==="
|
||||
sh "kubectl get pods -l app=${Pod_container_name} -n ${K8s_namespace} -o wide"
|
||||
sh "kubectl get pods -l 'app=${Pod_container_name},environment=${Pod_environment}' -n ${K8s_namespace} -o wide"
|
||||
|
||||
echo "=== 获取最新 Pod 名称 ==="
|
||||
NEW_POD = sh (
|
||||
script: "kubectl get pods -l app=${Pod_container_name} -n ${K8s_namespace} --sort-by=.metadata.creationTimestamp -o jsonpath='{.items[-1].metadata.name}'",
|
||||
script: "kubectl get pods -l 'app=${Pod_container_name},environment=${Pod_environment}' -n ${K8s_namespace} --sort-by=.metadata.creationTimestamp -o jsonpath='{.items[-1].metadata.name}'",
|
||||
returnStdout: true
|
||||
).trim()
|
||||
|
||||
|
||||
277
SCM/部署镜像/s3/DM_s3_lessie_agent.groovy
Normal file
277
SCM/部署镜像/s3/DM_s3_lessie_agent.groovy
Normal file
@@ -0,0 +1,277 @@
|
||||
pipeline {
|
||||
agent any
|
||||
|
||||
parameters {
|
||||
imageTag(
|
||||
name: 'IMAGE_NAME',
|
||||
description: 'sit 仓库镜像 (除非输入 CUSTOM_IMAGE, 否则使用这里的)',
|
||||
registry: 'https://uswccr.ccs.tencentyun.com',
|
||||
image: 'lessiesit/lessie-sourcing-agents',
|
||||
credentialId: 'dxin_img_hub_auth',
|
||||
filter: '.*',
|
||||
defaultTag: '',
|
||||
verifySsl: true
|
||||
)
|
||||
string(
|
||||
name: 'CUSTOM_IMAGE',
|
||||
defaultValue: '',
|
||||
description: '手输完整镜像<registry>/<namespace>/<repo>:<tag>,例如: uswccr.ccs.tencentyun.com/lessiesit/lessie-sourcing-agents:v0.0.1 (填充后,则忽略镜像仓库选择)'
|
||||
)
|
||||
booleanParam(
|
||||
name: 'ROLLBACK_VERSION',
|
||||
defaultValue: false,
|
||||
description: '是否快速回滚上一个版本?'
|
||||
)
|
||||
choice(
|
||||
name: 'BRANCH_NAME',
|
||||
choices: ['dxin', 'opt'],
|
||||
description: '选择资源清单Yaml的分支'
|
||||
)
|
||||
}
|
||||
environment {
|
||||
KUBECONFIG = credentials('k8s-test-config-admin') // k8s 凭证 ID, Jenkins 中配置的凭证名称
|
||||
Deployment_yaml = "${WORKSPACE}/lessie-ai/sit/s3/lessie-agents.yaml"
|
||||
Deployment_name = "s3-lessie-agents"
|
||||
K8s_namespace = "sit"
|
||||
Pod_container_name = "lessie-agents"
|
||||
Pod_environment = "s3"
|
||||
}
|
||||
stages {
|
||||
stage('回滚上版') {
|
||||
when { expression { return params.ROLLBACK_VERSION == true } }
|
||||
steps {
|
||||
script {
|
||||
sh """
|
||||
echo "=== 开始回滚到上一个版本 ==="
|
||||
kubectl rollout undo deployment/${Deployment_name} -n ${K8s_namespace}
|
||||
echo "=== 回滚中... ==="
|
||||
kubectl rollout status deployment/${Deployment_name} -n ${K8s_namespace} --timeout=60s
|
||||
echo "=== 查看所使用的镜像 ==="
|
||||
kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}'
|
||||
echo "=== 回滚完成 ==="
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('决定镜像') {
|
||||
steps {
|
||||
script {
|
||||
def imageRegex = /^([a-zA-Z0-9.-]+)(:[0-9]+)?\/([a-zA-Z0-9._-]+)\/([a-zA-Z0-9._-]+):([a-zA-Z0-9._-]+)$/
|
||||
|
||||
if (params.CUSTOM_IMAGE?.trim()) {
|
||||
echo "检测手输镜像格式: ${params.CUSTOM_IMAGE}"
|
||||
def customImage = params.CUSTOM_IMAGE.trim()
|
||||
// 验证镜像格式
|
||||
def matcher = (customImage =~ imageRegex)
|
||||
if (!matcher.matches()) {
|
||||
error "CUSTOM_IMAGE 格式不正确,必须是 registry/namespace/repository:tag 格式\n" +
|
||||
"示例: uswccr.ccs.tencentyun.com/lessiesit/lessie-sourcing-agents:v1.0.0\n" +
|
||||
"当前输入: ${customImage}"
|
||||
}
|
||||
|
||||
echo "使用手输镜像: ${customImage}"
|
||||
env.IMAGE_FULL_NAME = customImage
|
||||
|
||||
} else {
|
||||
def img = "uswccr.ccs.tencentyun.com/${params.IMAGE_NAME}"
|
||||
echo "使用 imageTag 插件镜像: ${img}"
|
||||
env.IMAGE_FULL_NAME = img
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('查询镜像') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
echo "查询镜像: ${IMAGE_FULL_NAME}"
|
||||
|
||||
def result = sh(returnStdout: true, script: """
|
||||
/data/sh/get-image-labels-fast.sh \
|
||||
"${IMAGE_FULL_NAME}" \
|
||||
"100038894437" \
|
||||
'h8H1o6Fd!HLXn' | awk '/^{/,/^}\$/'
|
||||
""").trim()
|
||||
|
||||
if (result == "" || result == null) {
|
||||
error("查询镜像 label 失败,请检查镜像是否存在或凭证问题")
|
||||
}
|
||||
|
||||
echo "镜像 Label:\n${result}"
|
||||
env.IMAGE_LABEL = result
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('拉取yaml') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
git branch: "${params.BRANCH_NAME}",
|
||||
credentialsId: 'fly_gitlab_auth',
|
||||
url: 'http://172.24.16.20/opt/opt-config.git'
|
||||
}
|
||||
}
|
||||
stage('修改YAML') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
def oldImg = sh (
|
||||
script: """
|
||||
kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}' 2>/dev/null || echo "无(可能是首次部署)"
|
||||
""",
|
||||
returnStdout: true
|
||||
).trim()
|
||||
env.OLD_IMAGE_NAME = oldImg
|
||||
echo "--- 目前正常运行的旧镜像: ${OLD_IMAGE_NAME} ---"
|
||||
echo "--- 所选择新的镜像: ${params.IMAGE_NAME} ---"
|
||||
echo "--- 修改 Deployment YAML 中的镜像为新镜像版本 ---"
|
||||
sh """
|
||||
sed -i 's#image:.*#image: ${IMAGE_FULL_NAME}#' ${Deployment_yaml}
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('部署k8s') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
def ANNOTATION = "更新 image 为 ${params.IMAGE_NAME}"
|
||||
sh """
|
||||
echo "===Apply Deployment YAML ==="
|
||||
kubectl apply -f ${Deployment_yaml} -n ${K8s_namespace}
|
||||
|
||||
echo "=== 查看当前新使用的镜像 ==="
|
||||
kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}'
|
||||
|
||||
echo "=== 添加注解 ==="
|
||||
kubectl annotate deployment/${Deployment_name} kubernetes.io/change-cause="${ANNOTATION}" --overwrite -n ${K8s_namespace}
|
||||
|
||||
echo "=== 查看历史版本 ==="
|
||||
kubectl rollout history deployment/${Deployment_name} -n ${K8s_namespace}
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('部署情况') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
echo "=== 检测部署状态并验证新版本运行情况 ==="
|
||||
echo "--- 检查 Deployment 更新状态 ---"
|
||||
def rolloutStatus = sh(
|
||||
script: "kubectl rollout status deployment/${Deployment_name} -n ${K8s_namespace} --timeout=60s",
|
||||
returnStatus: true
|
||||
)
|
||||
|
||||
if (rolloutStatus != 0){
|
||||
echo "Deployment ${Deployment_name} 发布 **超时或失败**,开始排查..."
|
||||
sh """
|
||||
echo "--- Deployment 状态 ---"
|
||||
kubectl describe deployment ${Deployment_name} -n ${K8s_namespace} || true
|
||||
|
||||
echo '--- Pod 列表 ---'
|
||||
kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o wide || true
|
||||
|
||||
echo "--- Pod 描述 (describe) ---"
|
||||
for pod in \$(kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o jsonpath='{.items[*].metadata.name}'); do
|
||||
echo "-- Pod 描述 \$pod --"
|
||||
kubectl describe pod \$pod -n ${K8s_namespace} || true
|
||||
done
|
||||
|
||||
echo "--- 最近 200 行 Pod 日志 ---"
|
||||
for pod in \$(kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o jsonpath='{.items[*].metadata.name}'); do
|
||||
echo "-- logs \$pod --"
|
||||
kubectl logs \$pod -n ${K8s_namespace} --tail=200 || true
|
||||
done
|
||||
"""
|
||||
error("=== Deployment 发布失败,请检查以上输出定位问题 ===")
|
||||
}
|
||||
|
||||
echo "=== 检查 Pods 是否全部 Ready ==="
|
||||
sh "kubectl get pods -l 'app=${Pod_container_name},environment=${Pod_environment}' -n ${K8s_namespace} -o wide"
|
||||
|
||||
echo "=== 获取最新 Pod 名称 ==="
|
||||
NEW_POD = sh (
|
||||
script: "kubectl get pods -l 'app=${Pod_container_name},environment=${Pod_environment}' -n ${K8s_namespace} --sort-by=.metadata.creationTimestamp -o jsonpath='{.items[-1].metadata.name}'",
|
||||
returnStdout: true
|
||||
).trim()
|
||||
|
||||
echo "=== 新 Pod 启动日志(最近20行) ==="
|
||||
sh "kubectl logs ${NEW_POD} -n ${K8s_namespace} --tail=20 || true"
|
||||
|
||||
echo "部署成功:${NEW_POD} 已正常运行"
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('变更说明') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
def user = currentBuild.getBuildCauses()[0].userName ?: "SYSTEM"
|
||||
def now = sh(script: "date '+%Y-%m-%d %H:%M:%S'", returnStdout: true).trim()
|
||||
env.CHANGE_MSG = """
|
||||
jenkins执行人: ${user}
|
||||
修改时间: ${now}
|
||||
旧镜像: ${OLD_IMAGE_NAME}
|
||||
新镜像: ${IMAGE_FULL_NAME}
|
||||
部署对象: ${Deployment_name}
|
||||
镜像标签: ${env.IMAGE_LABEL}
|
||||
""".stripIndent().trim()
|
||||
echo env.CHANGE_MSG
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('提交变更') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
withCredentials([usernamePassword(credentialsId: 'fly_gitlab_auth', usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) {
|
||||
sh """
|
||||
cd ${WORKSPACE}
|
||||
git config user.name "jenkins"
|
||||
git config user.email "jenkins@local"
|
||||
|
||||
# 检查工作树是否有变化
|
||||
if ! git diff --exit-code ${Deployment_yaml} > /dev/null 2>&1; then
|
||||
echo "检测到更改,正在提交..."
|
||||
git add ${Deployment_yaml}
|
||||
git commit -m "更新镜像 \${CHANGE_MSG}"
|
||||
else
|
||||
echo "${Deployment_yaml} 没有变化, 无需commit"
|
||||
fi
|
||||
|
||||
# 检查是否需要推送(是否有新的提交)
|
||||
LOCAL=\$(git rev-parse @)
|
||||
REMOTE=\$(git rev-parse @{u} 2>/dev/null || true)
|
||||
BASE=\$(git merge-base @ @{u} 2>/dev/null || true)
|
||||
|
||||
if [ "\$LOCAL" = "\$REMOTE" ]; then
|
||||
echo "已与远程系统同步更新"
|
||||
elif [ "\$LOCAL" = "\$BASE" ]; then
|
||||
echo "需要从远程获取数据"
|
||||
git pull http://172.24.16.20/opt/opt-config.git ${params.BRANCH_NAME}
|
||||
elif [ "\$REMOTE" = "\$BASE" ]; then
|
||||
echo "将更改推送到远程服务器..."
|
||||
# 生成临时 .netrc 文件
|
||||
echo "machine 172.24.16.20 login \$GIT_USER password \$GIT_PASS" > ~/.netrc
|
||||
chmod 600 ~/.netrc
|
||||
git push http://172.24.16.20/opt/opt-config.git ${params.BRANCH_NAME}
|
||||
else
|
||||
echo "与远程模式不同,跳过推送操作"
|
||||
fi
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
post {
|
||||
success {
|
||||
echo "成功!"
|
||||
echo "=== 所选择镜像: ${params.IMAGE_NAME} ==="
|
||||
}
|
||||
failure {
|
||||
echo "有步骤失败,请检查!"
|
||||
}
|
||||
}
|
||||
}
|
||||
277
SCM/部署镜像/s3/DM_s3_lessie_ai_web.groovy
Normal file
277
SCM/部署镜像/s3/DM_s3_lessie_ai_web.groovy
Normal file
@@ -0,0 +1,277 @@
|
||||
pipeline {
|
||||
agent any
|
||||
|
||||
parameters {
|
||||
imageTag(
|
||||
name: 'IMAGE_NAME',
|
||||
description: 'sit 仓库镜像 (除非输入 CUSTOM_IMAGE, 否则使用这里的)',
|
||||
registry: 'https://uswccr.ccs.tencentyun.com',
|
||||
image: 'lessiesit/lessie-ai-web',
|
||||
credentialId: 'dxin_img_hub_auth',
|
||||
filter: '.*',
|
||||
defaultTag: '',
|
||||
verifySsl: true
|
||||
)
|
||||
string(
|
||||
name: 'CUSTOM_IMAGE',
|
||||
defaultValue: '',
|
||||
description: '手输完整镜像<registry>/<namespace>/<repo>:<tag>,例如: uswccr.ccs.tencentyun.com/lessiesit/lessie-ai-web:v0.0.1 (填充后,则忽略镜像仓库选择)'
|
||||
)
|
||||
booleanParam(
|
||||
name: 'ROLLBACK_VERSION',
|
||||
defaultValue: false,
|
||||
description: '是否快速回滚上一个版本?'
|
||||
)
|
||||
choice(
|
||||
name: 'BRANCH_NAME',
|
||||
choices: ['dxin', 'opt'],
|
||||
description: '选择资源清单Yaml的分支'
|
||||
)
|
||||
}
|
||||
environment {
|
||||
KUBECONFIG = credentials('k8s-test-config-admin') // k8s 凭证 ID, Jenkins 中配置的凭证名称
|
||||
Deployment_yaml = "${WORKSPACE}/lessie-ai/sit/s1/lessie-ai-web.yaml"
|
||||
Deployment_name = "s1-lessie-ai-web"
|
||||
K8s_namespace = "sit"
|
||||
Pod_container_name = "lessie-ai-web"
|
||||
Pod_environment = "s3"
|
||||
}
|
||||
stages {
|
||||
stage('回滚上版') {
|
||||
when { expression { return params.ROLLBACK_VERSION == true } }
|
||||
steps {
|
||||
script {
|
||||
sh """
|
||||
echo "=== 开始回滚到上一个版本 ==="
|
||||
kubectl rollout undo deployment/${Deployment_name} -n ${K8s_namespace}
|
||||
echo "=== 回滚中... ==="
|
||||
kubectl rollout status deployment/${Deployment_name} -n ${K8s_namespace} --timeout=60s
|
||||
echo "=== 查看所使用的镜像 ==="
|
||||
kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}'
|
||||
echo "=== 回滚完成 ==="
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('决定镜像') {
|
||||
steps {
|
||||
script {
|
||||
def imageRegex = /^([a-zA-Z0-9.-]+)(:[0-9]+)?\/([a-zA-Z0-9._-]+)\/([a-zA-Z0-9._-]+):([a-zA-Z0-9._-]+)$/
|
||||
|
||||
if (params.CUSTOM_IMAGE?.trim()) {
|
||||
echo "检测手输镜像格式: ${params.CUSTOM_IMAGE}"
|
||||
def customImage = params.CUSTOM_IMAGE.trim()
|
||||
// 验证镜像格式
|
||||
def matcher = (customImage =~ imageRegex)
|
||||
if (!matcher.matches()) {
|
||||
error "CUSTOM_IMAGE 格式不正确,必须是 registry/namespace/repository:tag 格式\n" +
|
||||
"示例: uswccr.ccs.tencentyun.com/lessie-ai-web:v1.0.0\n" +
|
||||
"当前输入: ${customImage}"
|
||||
}
|
||||
|
||||
echo "使用手输镜像: ${customImage}"
|
||||
env.IMAGE_FULL_NAME = customImage
|
||||
|
||||
} else {
|
||||
def img = "uswccr.ccs.tencentyun.com/${params.IMAGE_NAME}"
|
||||
echo "使用 imageTag 插件镜像: ${img}"
|
||||
env.IMAGE_FULL_NAME = img
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('查询镜像') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
echo "查询镜像: ${IMAGE_FULL_NAME}"
|
||||
|
||||
def result = sh(returnStdout: true, script: """
|
||||
/data/sh/get-image-labels-fast.sh \
|
||||
"${IMAGE_FULL_NAME}" \
|
||||
"100038894437" \
|
||||
'h8H1o6Fd!HLXn' | awk '/^{/,/^}\$/'
|
||||
""").trim()
|
||||
|
||||
if (result == "" || result == null) {
|
||||
error("查询镜像 label 失败,请检查镜像是否存在或凭证问题")
|
||||
}
|
||||
|
||||
echo "镜像 Label:\n${result}"
|
||||
env.IMAGE_LABEL = result
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('拉取yaml') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
git branch: "${params.BRANCH_NAME}",
|
||||
credentialsId: 'fly_gitlab_auth',
|
||||
url: 'http://172.24.16.20/opt/opt-config.git'
|
||||
}
|
||||
}
|
||||
stage('修改YAML') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
def oldImg = sh (
|
||||
script: """
|
||||
kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}' 2>/dev/null || echo "无(可能是首次部署)"
|
||||
""",
|
||||
returnStdout: true
|
||||
).trim()
|
||||
env.OLD_IMAGE_NAME = oldImg
|
||||
echo "--- 目前正常运行的旧镜像: ${OLD_IMAGE_NAME} ---"
|
||||
echo "--- 所选择新的镜像: ${params.IMAGE_NAME} ---"
|
||||
echo "--- 修改 Deployment YAML 中的镜像为新镜像版本 ---"
|
||||
sh """
|
||||
sed -i 's#image:.*#image: ${IMAGE_FULL_NAME}#' ${Deployment_yaml}
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('部署k8s') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
def ANNOTATION = "更新 image 为 ${params.IMAGE_NAME}"
|
||||
sh """
|
||||
echo "===Apply Deployment YAML ==="
|
||||
kubectl apply -f ${Deployment_yaml} -n ${K8s_namespace}
|
||||
|
||||
echo "=== 查看当前新使用的镜像 ==="
|
||||
kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}'
|
||||
|
||||
echo "=== 添加注解 ==="
|
||||
kubectl annotate deployment/${Deployment_name} kubernetes.io/change-cause="${ANNOTATION}" --overwrite -n ${K8s_namespace}
|
||||
|
||||
echo "=== 查看历史版本 ==="
|
||||
kubectl rollout history deployment/${Deployment_name} -n ${K8s_namespace}
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('部署情况') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
echo "=== 检测部署状态并验证新版本运行情况 ==="
|
||||
echo "--- 检查 Deployment 更新状态 ---"
|
||||
def rolloutStatus = sh(
|
||||
script: "kubectl rollout status deployment/${Deployment_name} -n ${K8s_namespace} --timeout=60s",
|
||||
returnStatus: true
|
||||
)
|
||||
|
||||
if (rolloutStatus != 0){
|
||||
echo "Deployment ${Deployment_name} 发布 **超时或失败**,开始排查..."
|
||||
sh """
|
||||
echo "--- Deployment 状态 ---"
|
||||
kubectl describe deployment ${Deployment_name} -n ${K8s_namespace} || true
|
||||
|
||||
echo '--- Pod 列表 ---'
|
||||
kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o wide || true
|
||||
|
||||
echo "--- Pod 描述 (describe) ---"
|
||||
for pod in \$(kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o jsonpath='{.items[*].metadata.name}'); do
|
||||
echo "-- Pod 描述 \$pod --"
|
||||
kubectl describe pod \$pod -n ${K8s_namespace} || true
|
||||
done
|
||||
|
||||
echo "--- 最近 200 行 Pod 日志 ---"
|
||||
for pod in \$(kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o jsonpath='{.items[*].metadata.name}'); do
|
||||
echo "-- logs \$pod --"
|
||||
kubectl logs \$pod -n ${K8s_namespace} --tail=200 || true
|
||||
done
|
||||
"""
|
||||
error("=== Deployment 发布失败,请检查以上输出定位问题 ===")
|
||||
}
|
||||
|
||||
echo "=== 检查 Pods 是否全部 Ready ==="
|
||||
sh "kubectl get pods -l 'app=${Pod_container_name},environment=${Pod_environment}' -n ${K8s_namespace} -o wide"
|
||||
|
||||
echo "=== 获取最新 Pod 名称 ==="
|
||||
NEW_POD = sh (
|
||||
script: "kubectl get pods -l 'app=${Pod_container_name},environment=${Pod_environment}' -n ${K8s_namespace} --sort-by=.metadata.creationTimestamp -o jsonpath='{.items[-1].metadata.name}'",
|
||||
returnStdout: true
|
||||
).trim()
|
||||
|
||||
echo "=== 新 Pod 启动日志(最近20行) ==="
|
||||
sh "kubectl logs ${NEW_POD} -n ${K8s_namespace} --tail=20 || true"
|
||||
|
||||
echo "部署成功:${NEW_POD} 已正常运行"
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('变更说明') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
def user = currentBuild.getBuildCauses()[0].userName ?: "SYSTEM"
|
||||
def now = sh(script: "date '+%Y-%m-%d %H:%M:%S'", returnStdout: true).trim()
|
||||
env.CHANGE_MSG = """
|
||||
jenkins执行人: ${user}
|
||||
修改时间: ${now}
|
||||
旧镜像: ${OLD_IMAGE_NAME}
|
||||
新镜像: ${IMAGE_FULL_NAME}
|
||||
部署对象: ${Deployment_name}
|
||||
镜像标签: ${env.IMAGE_LABEL}
|
||||
""".stripIndent().trim()
|
||||
echo env.CHANGE_MSG
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('提交变更') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
withCredentials([usernamePassword(credentialsId: 'fly_gitlab_auth', usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) {
|
||||
sh """
|
||||
cd ${WORKSPACE}
|
||||
git config user.name "jenkins"
|
||||
git config user.email "jenkins@local"
|
||||
|
||||
# 检查工作树是否有变化
|
||||
if ! git diff --exit-code ${Deployment_yaml} > /dev/null 2>&1; then
|
||||
echo "检测到更改,正在提交..."
|
||||
git add ${Deployment_yaml}
|
||||
git commit -m "更新镜像 \${CHANGE_MSG}"
|
||||
else
|
||||
echo "${Deployment_yaml} 没有变化, 无需commit"
|
||||
fi
|
||||
|
||||
# 检查是否需要推送(是否有新的提交)
|
||||
LOCAL=\$(git rev-parse @)
|
||||
REMOTE=\$(git rev-parse @{u} 2>/dev/null || true)
|
||||
BASE=\$(git merge-base @ @{u} 2>/dev/null || true)
|
||||
|
||||
if [ "\$LOCAL" = "\$REMOTE" ]; then
|
||||
echo "已与远程系统同步更新"
|
||||
elif [ "\$LOCAL" = "\$BASE" ]; then
|
||||
echo "需要从远程获取数据"
|
||||
git pull http://172.24.16.20/opt/opt-config.git ${params.BRANCH_NAME}
|
||||
elif [ "\$REMOTE" = "\$BASE" ]; then
|
||||
echo "将更改推送到远程服务器..."
|
||||
# 生成临时 .netrc 文件
|
||||
echo "machine 172.24.16.20 login \$GIT_USER password \$GIT_PASS" > ~/.netrc
|
||||
chmod 600 ~/.netrc
|
||||
git push http://172.24.16.20/opt/opt-config.git ${params.BRANCH_NAME}
|
||||
else
|
||||
echo "与远程模式不同,跳过推送操作"
|
||||
fi
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
post {
|
||||
success {
|
||||
echo "成功!"
|
||||
echo "=== 所选择镜像: ${params.IMAGE_NAME} ==="
|
||||
}
|
||||
failure {
|
||||
echo "有步骤失败,请检查!"
|
||||
}
|
||||
}
|
||||
}
|
||||
277
SCM/部署镜像/s3/DM_s3_lessie_go_api.groovy
Normal file
277
SCM/部署镜像/s3/DM_s3_lessie_go_api.groovy
Normal file
@@ -0,0 +1,277 @@
|
||||
pipeline {
|
||||
agent any
|
||||
|
||||
parameters {
|
||||
imageTag(
|
||||
name: 'IMAGE_NAME',
|
||||
description: 'sit 仓库镜像 (除非输入 CUSTOM_IMAGE, 否则使用这里的)',
|
||||
registry: 'https://uswccr.ccs.tencentyun.com',
|
||||
image: 'lessiesit/go_lessie-sourcing-api',
|
||||
credentialId: 'dxin_img_hub_auth',
|
||||
filter: '.*',
|
||||
defaultTag: '',
|
||||
verifySsl: true
|
||||
)
|
||||
string(
|
||||
name: 'CUSTOM_IMAGE',
|
||||
defaultValue: '',
|
||||
description: '手输完整镜像<registry>/<namespace>/<repo>:<tag>,例如: uswccr.ccs.tencentyun.com/lessiesit/go_lessie-sourcing-api:v0.0.1 (填充后,则忽略镜像仓库选择)'
|
||||
)
|
||||
booleanParam(
|
||||
name: 'ROLLBACK_VERSION',
|
||||
defaultValue: false,
|
||||
description: '是否快速回滚上一个版本?'
|
||||
)
|
||||
choice(
|
||||
name: 'BRANCH_NAME',
|
||||
choices: ['dxin', 'opt'],
|
||||
description: '选择资源清单Yaml的分支'
|
||||
)
|
||||
}
|
||||
environment {
|
||||
KUBECONFIG = credentials('k8s-test-config-admin') // k8s 凭证 ID, Jenkins 中配置的凭证名称
|
||||
Deployment_yaml = "${WORKSPACE}/lessie-ai/sit/s1/lessie-go-api.yaml"
|
||||
Deployment_name = "s1-lessie-go-api"
|
||||
K8s_namespace = "sit"
|
||||
Pod_container_name = "lessie-go-api"
|
||||
Pod_environment = "s3"
|
||||
}
|
||||
stages {
|
||||
stage('回滚上版') {
|
||||
when { expression { return params.ROLLBACK_VERSION == true } }
|
||||
steps {
|
||||
script {
|
||||
sh """
|
||||
echo "=== 开始回滚到上一个版本 ==="
|
||||
kubectl rollout undo deployment/${Deployment_name} -n ${K8s_namespace}
|
||||
echo "=== 回滚中... ==="
|
||||
kubectl rollout status deployment/${Deployment_name} -n ${K8s_namespace} --timeout=60s
|
||||
echo "=== 查看所使用的镜像 ==="
|
||||
kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}'
|
||||
echo "=== 回滚完成 ==="
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('决定镜像') {
|
||||
steps {
|
||||
script {
|
||||
def imageRegex = /^([a-zA-Z0-9.-]+)(:[0-9]+)?\/([a-zA-Z0-9._-]+)\/([a-zA-Z0-9._-]+):([a-zA-Z0-9._-]+)$/
|
||||
|
||||
if (params.CUSTOM_IMAGE?.trim()) {
|
||||
echo "检测手输镜像格式: ${params.CUSTOM_IMAGE}"
|
||||
def customImage = params.CUSTOM_IMAGE.trim()
|
||||
// 验证镜像格式
|
||||
def matcher = (customImage =~ imageRegex)
|
||||
if (!matcher.matches()) {
|
||||
error "CUSTOM_IMAGE 格式不正确,必须是 registry/namespace/repository:tag 格式\n" +
|
||||
"示例: uswccr.ccs.tencentyun.com/go_lessie-sourcing-api:v1.0.0\n" +
|
||||
"当前输入: ${customImage}"
|
||||
}
|
||||
|
||||
echo "使用手输镜像: ${customImage}"
|
||||
env.IMAGE_FULL_NAME = customImage
|
||||
|
||||
} else {
|
||||
def img = "uswccr.ccs.tencentyun.com/${params.IMAGE_NAME}"
|
||||
echo "使用 imageTag 插件镜像: ${img}"
|
||||
env.IMAGE_FULL_NAME = img
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('查询镜像') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
echo "查询镜像: ${IMAGE_FULL_NAME}"
|
||||
|
||||
def result = sh(returnStdout: true, script: """
|
||||
/data/sh/get-image-labels-fast.sh \
|
||||
"${IMAGE_FULL_NAME}" \
|
||||
"100038894437" \
|
||||
'h8H1o6Fd!HLXn' | awk '/^{/,/^}\$/'
|
||||
""").trim()
|
||||
|
||||
if (result == "" || result == null) {
|
||||
error("查询镜像 label 失败,请检查镜像是否存在或凭证问题")
|
||||
}
|
||||
|
||||
echo "镜像 Label:\n${result}"
|
||||
env.IMAGE_LABEL = result
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('拉取yaml') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
git branch: "${params.BRANCH_NAME}",
|
||||
credentialsId: 'fly_gitlab_auth',
|
||||
url: 'http://172.24.16.20/opt/opt-config.git'
|
||||
}
|
||||
}
|
||||
stage('修改YAML') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
def oldImg = sh (
|
||||
script: """
|
||||
kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}' 2>/dev/null || echo "无(可能是首次部署)"
|
||||
""",
|
||||
returnStdout: true
|
||||
).trim()
|
||||
env.OLD_IMAGE_NAME = oldImg
|
||||
echo "--- 目前正常运行的旧镜像: ${OLD_IMAGE_NAME} ---"
|
||||
echo "--- 所选择新的镜像: ${params.IMAGE_NAME} ---"
|
||||
echo "--- 修改 Deployment YAML 中的镜像为新镜像版本 ---"
|
||||
sh """
|
||||
sed -i 's#image:.*#image: ${IMAGE_FULL_NAME}#' ${Deployment_yaml}
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('部署k8s') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
def ANNOTATION = "更新 image 为 ${params.IMAGE_NAME}"
|
||||
sh """
|
||||
echo "===Apply Deployment YAML ==="
|
||||
kubectl apply -f ${Deployment_yaml} -n ${K8s_namespace}
|
||||
|
||||
echo "=== 查看当前新使用的镜像 ==="
|
||||
kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}'
|
||||
|
||||
echo "=== 添加注解 ==="
|
||||
kubectl annotate deployment/${Deployment_name} kubernetes.io/change-cause="${ANNOTATION}" --overwrite -n ${K8s_namespace}
|
||||
|
||||
echo "=== 查看历史版本 ==="
|
||||
kubectl rollout history deployment/${Deployment_name} -n ${K8s_namespace}
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('部署情况') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
echo "=== 检测部署状态并验证新版本运行情况 ==="
|
||||
echo "--- 检查 Deployment 更新状态 ---"
|
||||
def rolloutStatus = sh(
|
||||
script: "kubectl rollout status deployment/${Deployment_name} -n ${K8s_namespace} --timeout=60s",
|
||||
returnStatus: true
|
||||
)
|
||||
|
||||
if (rolloutStatus != 0){
|
||||
echo "Deployment ${Deployment_name} 发布 **超时或失败**,开始排查..."
|
||||
sh """
|
||||
echo "--- Deployment 状态 ---"
|
||||
kubectl describe deployment ${Deployment_name} -n ${K8s_namespace} || true
|
||||
|
||||
echo '--- Pod 列表 ---'
|
||||
kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o wide || true
|
||||
|
||||
echo "--- Pod 描述 (describe) ---"
|
||||
for pod in \$(kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o jsonpath='{.items[*].metadata.name}'); do
|
||||
echo "-- Pod 描述 \$pod --"
|
||||
kubectl describe pod \$pod -n ${K8s_namespace} || true
|
||||
done
|
||||
|
||||
echo "--- 最近 200 行 Pod 日志 ---"
|
||||
for pod in \$(kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o jsonpath='{.items[*].metadata.name}'); do
|
||||
echo "-- logs \$pod --"
|
||||
kubectl logs \$pod -n ${K8s_namespace} --tail=200 || true
|
||||
done
|
||||
"""
|
||||
error("=== Deployment 发布失败,请检查以上输出定位问题 ===")
|
||||
}
|
||||
|
||||
echo "=== 检查 Pods 是否全部 Ready ==="
|
||||
sh "kubectl get pods -l 'app=${Pod_container_name},environment=${Pod_environment}' -n ${K8s_namespace} -o wide"
|
||||
|
||||
echo "=== 获取最新 Pod 名称 ==="
|
||||
NEW_POD = sh (
|
||||
script: "kubectl get pods -l 'app=${Pod_container_name},environment=${Pod_environment}' -n ${K8s_namespace} --sort-by=.metadata.creationTimestamp -o jsonpath='{.items[-1].metadata.name}'",
|
||||
returnStdout: true
|
||||
).trim()
|
||||
|
||||
echo "=== 新 Pod 启动日志(最近20行) ==="
|
||||
sh "kubectl logs ${NEW_POD} -n ${K8s_namespace} --tail=20 || true"
|
||||
|
||||
echo "部署成功:${NEW_POD} 已正常运行"
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('变更说明') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
def user = currentBuild.getBuildCauses()[0].userName ?: "SYSTEM"
|
||||
def now = sh(script: "date '+%Y-%m-%d %H:%M:%S'", returnStdout: true).trim()
|
||||
env.CHANGE_MSG = """
|
||||
jenkins执行人: ${user}
|
||||
修改时间: ${now}
|
||||
旧镜像: ${OLD_IMAGE_NAME}
|
||||
新镜像: ${IMAGE_FULL_NAME}
|
||||
部署对象: ${Deployment_name}
|
||||
镜像标签: ${env.IMAGE_LABEL}
|
||||
""".stripIndent().trim()
|
||||
echo env.CHANGE_MSG
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('提交变更') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
withCredentials([usernamePassword(credentialsId: 'fly_gitlab_auth', usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) {
|
||||
sh """
|
||||
cd ${WORKSPACE}
|
||||
git config user.name "jenkins"
|
||||
git config user.email "jenkins@local"
|
||||
|
||||
# 检查工作树是否有变化
|
||||
if ! git diff --exit-code ${Deployment_yaml} > /dev/null 2>&1; then
|
||||
echo "检测到更改,正在提交..."
|
||||
git add ${Deployment_yaml}
|
||||
git commit -m "更新镜像 \${CHANGE_MSG}"
|
||||
else
|
||||
echo "${Deployment_yaml} 没有变化, 无需commit"
|
||||
fi
|
||||
|
||||
# 检查是否需要推送(是否有新的提交)
|
||||
LOCAL=\$(git rev-parse @)
|
||||
REMOTE=\$(git rev-parse @{u} 2>/dev/null || true)
|
||||
BASE=\$(git merge-base @ @{u} 2>/dev/null || true)
|
||||
|
||||
if [ "\$LOCAL" = "\$REMOTE" ]; then
|
||||
echo "已与远程系统同步更新"
|
||||
elif [ "\$LOCAL" = "\$BASE" ]; then
|
||||
echo "需要从远程获取数据"
|
||||
git pull http://172.24.16.20/opt/opt-config.git ${params.BRANCH_NAME}
|
||||
elif [ "\$REMOTE" = "\$BASE" ]; then
|
||||
echo "将更改推送到远程服务器..."
|
||||
# 生成临时 .netrc 文件
|
||||
echo "machine 172.24.16.20 login \$GIT_USER password \$GIT_PASS" > ~/.netrc
|
||||
chmod 600 ~/.netrc
|
||||
git push http://172.24.16.20/opt/opt-config.git ${params.BRANCH_NAME}
|
||||
else
|
||||
echo "与远程模式不同,跳过推送操作"
|
||||
fi
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
post {
|
||||
success {
|
||||
echo "成功!"
|
||||
echo "=== 所选择镜像: ${params.IMAGE_NAME} ==="
|
||||
}
|
||||
failure {
|
||||
echo "有步骤失败,请检查!"
|
||||
}
|
||||
}
|
||||
}
|
||||
277
SCM/部署镜像/s4/DM_s4_lessie_agent.groovy
Normal file
277
SCM/部署镜像/s4/DM_s4_lessie_agent.groovy
Normal file
@@ -0,0 +1,277 @@
|
||||
pipeline {
|
||||
agent any
|
||||
|
||||
parameters {
|
||||
imageTag(
|
||||
name: 'IMAGE_NAME',
|
||||
description: 'sit 仓库镜像 (除非输入 CUSTOM_IMAGE, 否则使用这里的)',
|
||||
registry: 'https://uswccr.ccs.tencentyun.com',
|
||||
image: 'lessiesit/lessie-sourcing-agents',
|
||||
credentialId: 'dxin_img_hub_auth',
|
||||
filter: '.*',
|
||||
defaultTag: '',
|
||||
verifySsl: true
|
||||
)
|
||||
string(
|
||||
name: 'CUSTOM_IMAGE',
|
||||
defaultValue: '',
|
||||
description: '手输完整镜像<registry>/<namespace>/<repo>:<tag>,例如: uswccr.ccs.tencentyun.com/lessiesit/lessie-sourcing-agents:v0.0.1 (填充后,则忽略镜像仓库选择)'
|
||||
)
|
||||
booleanParam(
|
||||
name: 'ROLLBACK_VERSION',
|
||||
defaultValue: false,
|
||||
description: '是否快速回滚上一个版本?'
|
||||
)
|
||||
choice(
|
||||
name: 'BRANCH_NAME',
|
||||
choices: ['dxin', 'opt'],
|
||||
description: '选择资源清单Yaml的分支'
|
||||
)
|
||||
}
|
||||
environment {
|
||||
KUBECONFIG = credentials('k8s-test-config-admin') // k8s 凭证 ID, Jenkins 中配置的凭证名称
|
||||
Deployment_yaml = "${WORKSPACE}/lessie-ai/sit/s4/lessie-agents.yaml"
|
||||
Deployment_name = "s4-lessie-agents"
|
||||
K8s_namespace = "sit"
|
||||
Pod_container_name = "lessie-agents"
|
||||
Pod_environment = "s4"
|
||||
}
|
||||
stages {
|
||||
stage('回滚上版') {
|
||||
when { expression { return params.ROLLBACK_VERSION == true } }
|
||||
steps {
|
||||
script {
|
||||
sh """
|
||||
echo "=== 开始回滚到上一个版本 ==="
|
||||
kubectl rollout undo deployment/${Deployment_name} -n ${K8s_namespace}
|
||||
echo "=== 回滚中... ==="
|
||||
kubectl rollout status deployment/${Deployment_name} -n ${K8s_namespace} --timeout=60s
|
||||
echo "=== 查看所使用的镜像 ==="
|
||||
kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}'
|
||||
echo "=== 回滚完成 ==="
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('决定镜像') {
|
||||
steps {
|
||||
script {
|
||||
def imageRegex = /^([a-zA-Z0-9.-]+)(:[0-9]+)?\/([a-zA-Z0-9._-]+)\/([a-zA-Z0-9._-]+):([a-zA-Z0-9._-]+)$/
|
||||
|
||||
if (params.CUSTOM_IMAGE?.trim()) {
|
||||
echo "检测手输镜像格式: ${params.CUSTOM_IMAGE}"
|
||||
def customImage = params.CUSTOM_IMAGE.trim()
|
||||
// 验证镜像格式
|
||||
def matcher = (customImage =~ imageRegex)
|
||||
if (!matcher.matches()) {
|
||||
error "CUSTOM_IMAGE 格式不正确,必须是 registry/namespace/repository:tag 格式\n" +
|
||||
"示例: uswccr.ccs.tencentyun.com/lessiesit/lessie-sourcing-agents:v1.0.0\n" +
|
||||
"当前输入: ${customImage}"
|
||||
}
|
||||
|
||||
echo "使用手输镜像: ${customImage}"
|
||||
env.IMAGE_FULL_NAME = customImage
|
||||
|
||||
} else {
|
||||
def img = "uswccr.ccs.tencentyun.com/${params.IMAGE_NAME}"
|
||||
echo "使用 imageTag 插件镜像: ${img}"
|
||||
env.IMAGE_FULL_NAME = img
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('查询镜像') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
echo "查询镜像: ${IMAGE_FULL_NAME}"
|
||||
|
||||
def result = sh(returnStdout: true, script: """
|
||||
/data/sh/get-image-labels-fast.sh \
|
||||
"${IMAGE_FULL_NAME}" \
|
||||
"100038894437" \
|
||||
'h8H1o6Fd!HLXn' | awk '/^{/,/^}\$/'
|
||||
""").trim()
|
||||
|
||||
if (result == "" || result == null) {
|
||||
error("查询镜像 label 失败,请检查镜像是否存在或凭证问题")
|
||||
}
|
||||
|
||||
echo "镜像 Label:\n${result}"
|
||||
env.IMAGE_LABEL = result
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('拉取yaml') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
git branch: "${params.BRANCH_NAME}",
|
||||
credentialsId: 'fly_gitlab_auth',
|
||||
url: 'http://172.24.16.20/opt/opt-config.git'
|
||||
}
|
||||
}
|
||||
stage('修改YAML') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
def oldImg = sh (
|
||||
script: """
|
||||
kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}' 2>/dev/null || echo "无(可能是首次部署)"
|
||||
""",
|
||||
returnStdout: true
|
||||
).trim()
|
||||
env.OLD_IMAGE_NAME = oldImg
|
||||
echo "--- 目前正常运行的旧镜像: ${OLD_IMAGE_NAME} ---"
|
||||
echo "--- 所选择新的镜像: ${params.IMAGE_NAME} ---"
|
||||
echo "--- 修改 Deployment YAML 中的镜像为新镜像版本 ---"
|
||||
sh """
|
||||
sed -i 's#image:.*#image: ${IMAGE_FULL_NAME}#' ${Deployment_yaml}
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('部署k8s') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
def ANNOTATION = "更新 image 为 ${params.IMAGE_NAME}"
|
||||
sh """
|
||||
echo "===Apply Deployment YAML ==="
|
||||
kubectl apply -f ${Deployment_yaml} -n ${K8s_namespace}
|
||||
|
||||
echo "=== 查看当前新使用的镜像 ==="
|
||||
kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}'
|
||||
|
||||
echo "=== 添加注解 ==="
|
||||
kubectl annotate deployment/${Deployment_name} kubernetes.io/change-cause="${ANNOTATION}" --overwrite -n ${K8s_namespace}
|
||||
|
||||
echo "=== 查看历史版本 ==="
|
||||
kubectl rollout history deployment/${Deployment_name} -n ${K8s_namespace}
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('部署情况') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
echo "=== 检测部署状态并验证新版本运行情况 ==="
|
||||
echo "--- 检查 Deployment 更新状态 ---"
|
||||
def rolloutStatus = sh(
|
||||
script: "kubectl rollout status deployment/${Deployment_name} -n ${K8s_namespace} --timeout=60s",
|
||||
returnStatus: true
|
||||
)
|
||||
|
||||
if (rolloutStatus != 0){
|
||||
echo "Deployment ${Deployment_name} 发布 **超时或失败**,开始排查..."
|
||||
sh """
|
||||
echo "--- Deployment 状态 ---"
|
||||
kubectl describe deployment ${Deployment_name} -n ${K8s_namespace} || true
|
||||
|
||||
echo '--- Pod 列表 ---'
|
||||
kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o wide || true
|
||||
|
||||
echo "--- Pod 描述 (describe) ---"
|
||||
for pod in \$(kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o jsonpath='{.items[*].metadata.name}'); do
|
||||
echo "-- Pod 描述 \$pod --"
|
||||
kubectl describe pod \$pod -n ${K8s_namespace} || true
|
||||
done
|
||||
|
||||
echo "--- 最近 200 行 Pod 日志 ---"
|
||||
for pod in \$(kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o jsonpath='{.items[*].metadata.name}'); do
|
||||
echo "-- logs \$pod --"
|
||||
kubectl logs \$pod -n ${K8s_namespace} --tail=200 || true
|
||||
done
|
||||
"""
|
||||
error("=== Deployment 发布失败,请检查以上输出定位问题 ===")
|
||||
}
|
||||
|
||||
echo "=== 检查 Pods 是否全部 Ready ==="
|
||||
sh "kubectl get pods -l 'app=${Pod_container_name},environment=${Pod_environment}' -n ${K8s_namespace} -o wide"
|
||||
|
||||
echo "=== 获取最新 Pod 名称 ==="
|
||||
NEW_POD = sh (
|
||||
script: "kubectl get pods -l 'app=${Pod_container_name},environment=${Pod_environment}' -n ${K8s_namespace} --sort-by=.metadata.creationTimestamp -o jsonpath='{.items[-1].metadata.name}'",
|
||||
returnStdout: true
|
||||
).trim()
|
||||
|
||||
echo "=== 新 Pod 启动日志(最近20行) ==="
|
||||
sh "kubectl logs ${NEW_POD} -n ${K8s_namespace} --tail=20 || true"
|
||||
|
||||
echo "部署成功:${NEW_POD} 已正常运行"
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('变更说明') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
def user = currentBuild.getBuildCauses()[0].userName ?: "SYSTEM"
|
||||
def now = sh(script: "date '+%Y-%m-%d %H:%M:%S'", returnStdout: true).trim()
|
||||
env.CHANGE_MSG = """
|
||||
jenkins执行人: ${user}
|
||||
修改时间: ${now}
|
||||
旧镜像: ${OLD_IMAGE_NAME}
|
||||
新镜像: ${IMAGE_FULL_NAME}
|
||||
部署对象: ${Deployment_name}
|
||||
镜像标签: ${env.IMAGE_LABEL}
|
||||
""".stripIndent().trim()
|
||||
echo env.CHANGE_MSG
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('提交变更') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
withCredentials([usernamePassword(credentialsId: 'fly_gitlab_auth', usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) {
|
||||
sh """
|
||||
cd ${WORKSPACE}
|
||||
git config user.name "jenkins"
|
||||
git config user.email "jenkins@local"
|
||||
|
||||
# 检查工作树是否有变化
|
||||
if ! git diff --exit-code ${Deployment_yaml} > /dev/null 2>&1; then
|
||||
echo "检测到更改,正在提交..."
|
||||
git add ${Deployment_yaml}
|
||||
git commit -m "更新镜像 \${CHANGE_MSG}"
|
||||
else
|
||||
echo "${Deployment_yaml} 没有变化, 无需commit"
|
||||
fi
|
||||
|
||||
# 检查是否需要推送(是否有新的提交)
|
||||
LOCAL=\$(git rev-parse @)
|
||||
REMOTE=\$(git rev-parse @{u} 2>/dev/null || true)
|
||||
BASE=\$(git merge-base @ @{u} 2>/dev/null || true)
|
||||
|
||||
if [ "\$LOCAL" = "\$REMOTE" ]; then
|
||||
echo "已与远程系统同步更新"
|
||||
elif [ "\$LOCAL" = "\$BASE" ]; then
|
||||
echo "需要从远程获取数据"
|
||||
git pull http://172.24.16.20/opt/opt-config.git ${params.BRANCH_NAME}
|
||||
elif [ "\$REMOTE" = "\$BASE" ]; then
|
||||
echo "将更改推送到远程服务器..."
|
||||
# 生成临时 .netrc 文件
|
||||
echo "machine 172.24.16.20 login \$GIT_USER password \$GIT_PASS" > ~/.netrc
|
||||
chmod 600 ~/.netrc
|
||||
git push http://172.24.16.20/opt/opt-config.git ${params.BRANCH_NAME}
|
||||
else
|
||||
echo "与远程模式不同,跳过推送操作"
|
||||
fi
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
post {
|
||||
success {
|
||||
echo "成功!"
|
||||
echo "=== 所选择镜像: ${params.IMAGE_NAME} ==="
|
||||
}
|
||||
failure {
|
||||
echo "有步骤失败,请检查!"
|
||||
}
|
||||
}
|
||||
}
|
||||
277
SCM/部署镜像/s4/DM_s4_lessie_ai_web.groovy
Normal file
277
SCM/部署镜像/s4/DM_s4_lessie_ai_web.groovy
Normal file
@@ -0,0 +1,277 @@
|
||||
pipeline {
|
||||
agent any
|
||||
|
||||
parameters {
|
||||
imageTag(
|
||||
name: 'IMAGE_NAME',
|
||||
description: 'sit 仓库镜像 (除非输入 CUSTOM_IMAGE, 否则使用这里的)',
|
||||
registry: 'https://uswccr.ccs.tencentyun.com',
|
||||
image: 'lessiesit/lessie-ai-web',
|
||||
credentialId: 'dxin_img_hub_auth',
|
||||
filter: '.*',
|
||||
defaultTag: '',
|
||||
verifySsl: true
|
||||
)
|
||||
string(
|
||||
name: 'CUSTOM_IMAGE',
|
||||
defaultValue: '',
|
||||
description: '手输完整镜像<registry>/<namespace>/<repo>:<tag>,例如: uswccr.ccs.tencentyun.com/lessiesit/lessie-ai-web:v0.0.1 (填充后,则忽略镜像仓库选择)'
|
||||
)
|
||||
booleanParam(
|
||||
name: 'ROLLBACK_VERSION',
|
||||
defaultValue: false,
|
||||
description: '是否快速回滚上一个版本?'
|
||||
)
|
||||
choice(
|
||||
name: 'BRANCH_NAME',
|
||||
choices: ['dxin', 'opt'],
|
||||
description: '选择资源清单Yaml的分支'
|
||||
)
|
||||
}
|
||||
environment {
|
||||
KUBECONFIG = credentials('k8s-test-config-admin') // k8s 凭证 ID, Jenkins 中配置的凭证名称
|
||||
Deployment_yaml = "${WORKSPACE}/lessie-ai/sit/s1/lessie-ai-web.yaml"
|
||||
Deployment_name = "s1-lessie-ai-web"
|
||||
K8s_namespace = "sit"
|
||||
Pod_container_name = "lessie-ai-web"
|
||||
Pod_environment = "s4"
|
||||
}
|
||||
stages {
|
||||
stage('回滚上版') {
|
||||
when { expression { return params.ROLLBACK_VERSION == true } }
|
||||
steps {
|
||||
script {
|
||||
sh """
|
||||
echo "=== 开始回滚到上一个版本 ==="
|
||||
kubectl rollout undo deployment/${Deployment_name} -n ${K8s_namespace}
|
||||
echo "=== 回滚中... ==="
|
||||
kubectl rollout status deployment/${Deployment_name} -n ${K8s_namespace} --timeout=60s
|
||||
echo "=== 查看所使用的镜像 ==="
|
||||
kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}'
|
||||
echo "=== 回滚完成 ==="
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('决定镜像') {
|
||||
steps {
|
||||
script {
|
||||
def imageRegex = /^([a-zA-Z0-9.-]+)(:[0-9]+)?\/([a-zA-Z0-9._-]+)\/([a-zA-Z0-9._-]+):([a-zA-Z0-9._-]+)$/
|
||||
|
||||
if (params.CUSTOM_IMAGE?.trim()) {
|
||||
echo "检测手输镜像格式: ${params.CUSTOM_IMAGE}"
|
||||
def customImage = params.CUSTOM_IMAGE.trim()
|
||||
// 验证镜像格式
|
||||
def matcher = (customImage =~ imageRegex)
|
||||
if (!matcher.matches()) {
|
||||
error "CUSTOM_IMAGE 格式不正确,必须是 registry/namespace/repository:tag 格式\n" +
|
||||
"示例: uswccr.ccs.tencentyun.com/lessie-ai-web:v1.0.0\n" +
|
||||
"当前输入: ${customImage}"
|
||||
}
|
||||
|
||||
echo "使用手输镜像: ${customImage}"
|
||||
env.IMAGE_FULL_NAME = customImage
|
||||
|
||||
} else {
|
||||
def img = "uswccr.ccs.tencentyun.com/${params.IMAGE_NAME}"
|
||||
echo "使用 imageTag 插件镜像: ${img}"
|
||||
env.IMAGE_FULL_NAME = img
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('查询镜像') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
echo "查询镜像: ${IMAGE_FULL_NAME}"
|
||||
|
||||
def result = sh(returnStdout: true, script: """
|
||||
/data/sh/get-image-labels-fast.sh \
|
||||
"${IMAGE_FULL_NAME}" \
|
||||
"100038894437" \
|
||||
'h8H1o6Fd!HLXn' | awk '/^{/,/^}\$/'
|
||||
""").trim()
|
||||
|
||||
if (result == "" || result == null) {
|
||||
error("查询镜像 label 失败,请检查镜像是否存在或凭证问题")
|
||||
}
|
||||
|
||||
echo "镜像 Label:\n${result}"
|
||||
env.IMAGE_LABEL = result
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('拉取yaml') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
git branch: "${params.BRANCH_NAME}",
|
||||
credentialsId: 'fly_gitlab_auth',
|
||||
url: 'http://172.24.16.20/opt/opt-config.git'
|
||||
}
|
||||
}
|
||||
stage('修改YAML') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
def oldImg = sh (
|
||||
script: """
|
||||
kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}' 2>/dev/null || echo "无(可能是首次部署)"
|
||||
""",
|
||||
returnStdout: true
|
||||
).trim()
|
||||
env.OLD_IMAGE_NAME = oldImg
|
||||
echo "--- 目前正常运行的旧镜像: ${OLD_IMAGE_NAME} ---"
|
||||
echo "--- 所选择新的镜像: ${params.IMAGE_NAME} ---"
|
||||
echo "--- 修改 Deployment YAML 中的镜像为新镜像版本 ---"
|
||||
sh """
|
||||
sed -i 's#image:.*#image: ${IMAGE_FULL_NAME}#' ${Deployment_yaml}
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('部署k8s') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
def ANNOTATION = "更新 image 为 ${params.IMAGE_NAME}"
|
||||
sh """
|
||||
echo "===Apply Deployment YAML ==="
|
||||
kubectl apply -f ${Deployment_yaml} -n ${K8s_namespace}
|
||||
|
||||
echo "=== 查看当前新使用的镜像 ==="
|
||||
kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}'
|
||||
|
||||
echo "=== 添加注解 ==="
|
||||
kubectl annotate deployment/${Deployment_name} kubernetes.io/change-cause="${ANNOTATION}" --overwrite -n ${K8s_namespace}
|
||||
|
||||
echo "=== 查看历史版本 ==="
|
||||
kubectl rollout history deployment/${Deployment_name} -n ${K8s_namespace}
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('部署情况') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
echo "=== 检测部署状态并验证新版本运行情况 ==="
|
||||
echo "--- 检查 Deployment 更新状态 ---"
|
||||
def rolloutStatus = sh(
|
||||
script: "kubectl rollout status deployment/${Deployment_name} -n ${K8s_namespace} --timeout=60s",
|
||||
returnStatus: true
|
||||
)
|
||||
|
||||
if (rolloutStatus != 0){
|
||||
echo "Deployment ${Deployment_name} 发布 **超时或失败**,开始排查..."
|
||||
sh """
|
||||
echo "--- Deployment 状态 ---"
|
||||
kubectl describe deployment ${Deployment_name} -n ${K8s_namespace} || true
|
||||
|
||||
echo '--- Pod 列表 ---'
|
||||
kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o wide || true
|
||||
|
||||
echo "--- Pod 描述 (describe) ---"
|
||||
for pod in \$(kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o jsonpath='{.items[*].metadata.name}'); do
|
||||
echo "-- Pod 描述 \$pod --"
|
||||
kubectl describe pod \$pod -n ${K8s_namespace} || true
|
||||
done
|
||||
|
||||
echo "--- 最近 200 行 Pod 日志 ---"
|
||||
for pod in \$(kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o jsonpath='{.items[*].metadata.name}'); do
|
||||
echo "-- logs \$pod --"
|
||||
kubectl logs \$pod -n ${K8s_namespace} --tail=200 || true
|
||||
done
|
||||
"""
|
||||
error("=== Deployment 发布失败,请检查以上输出定位问题 ===")
|
||||
}
|
||||
|
||||
echo "=== 检查 Pods 是否全部 Ready ==="
|
||||
sh "kubectl get pods -l 'app=${Pod_container_name},environment=${Pod_environment}' -n ${K8s_namespace} -o wide"
|
||||
|
||||
echo "=== 获取最新 Pod 名称 ==="
|
||||
NEW_POD = sh (
|
||||
script: "kubectl get pods -l 'app=${Pod_container_name},environment=${Pod_environment}' -n ${K8s_namespace} --sort-by=.metadata.creationTimestamp -o jsonpath='{.items[-1].metadata.name}'",
|
||||
returnStdout: true
|
||||
).trim()
|
||||
|
||||
echo "=== 新 Pod 启动日志(最近20行) ==="
|
||||
sh "kubectl logs ${NEW_POD} -n ${K8s_namespace} --tail=20 || true"
|
||||
|
||||
echo "部署成功:${NEW_POD} 已正常运行"
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('变更说明') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
def user = currentBuild.getBuildCauses()[0].userName ?: "SYSTEM"
|
||||
def now = sh(script: "date '+%Y-%m-%d %H:%M:%S'", returnStdout: true).trim()
|
||||
env.CHANGE_MSG = """
|
||||
jenkins执行人: ${user}
|
||||
修改时间: ${now}
|
||||
旧镜像: ${OLD_IMAGE_NAME}
|
||||
新镜像: ${IMAGE_FULL_NAME}
|
||||
部署对象: ${Deployment_name}
|
||||
镜像标签: ${env.IMAGE_LABEL}
|
||||
""".stripIndent().trim()
|
||||
echo env.CHANGE_MSG
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('提交变更') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
withCredentials([usernamePassword(credentialsId: 'fly_gitlab_auth', usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) {
|
||||
sh """
|
||||
cd ${WORKSPACE}
|
||||
git config user.name "jenkins"
|
||||
git config user.email "jenkins@local"
|
||||
|
||||
# 检查工作树是否有变化
|
||||
if ! git diff --exit-code ${Deployment_yaml} > /dev/null 2>&1; then
|
||||
echo "检测到更改,正在提交..."
|
||||
git add ${Deployment_yaml}
|
||||
git commit -m "更新镜像 \${CHANGE_MSG}"
|
||||
else
|
||||
echo "${Deployment_yaml} 没有变化, 无需commit"
|
||||
fi
|
||||
|
||||
# 检查是否需要推送(是否有新的提交)
|
||||
LOCAL=\$(git rev-parse @)
|
||||
REMOTE=\$(git rev-parse @{u} 2>/dev/null || true)
|
||||
BASE=\$(git merge-base @ @{u} 2>/dev/null || true)
|
||||
|
||||
if [ "\$LOCAL" = "\$REMOTE" ]; then
|
||||
echo "已与远程系统同步更新"
|
||||
elif [ "\$LOCAL" = "\$BASE" ]; then
|
||||
echo "需要从远程获取数据"
|
||||
git pull http://172.24.16.20/opt/opt-config.git ${params.BRANCH_NAME}
|
||||
elif [ "\$REMOTE" = "\$BASE" ]; then
|
||||
echo "将更改推送到远程服务器..."
|
||||
# 生成临时 .netrc 文件
|
||||
echo "machine 172.24.16.20 login \$GIT_USER password \$GIT_PASS" > ~/.netrc
|
||||
chmod 600 ~/.netrc
|
||||
git push http://172.24.16.20/opt/opt-config.git ${params.BRANCH_NAME}
|
||||
else
|
||||
echo "与远程模式不同,跳过推送操作"
|
||||
fi
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
post {
|
||||
success {
|
||||
echo "成功!"
|
||||
echo "=== 所选择镜像: ${params.IMAGE_NAME} ==="
|
||||
}
|
||||
failure {
|
||||
echo "有步骤失败,请检查!"
|
||||
}
|
||||
}
|
||||
}
|
||||
277
SCM/部署镜像/s4/DM_s4_lessie_go_api.groovy
Normal file
277
SCM/部署镜像/s4/DM_s4_lessie_go_api.groovy
Normal file
@@ -0,0 +1,277 @@
|
||||
pipeline {
|
||||
agent any
|
||||
|
||||
parameters {
|
||||
imageTag(
|
||||
name: 'IMAGE_NAME',
|
||||
description: 'sit 仓库镜像 (除非输入 CUSTOM_IMAGE, 否则使用这里的)',
|
||||
registry: 'https://uswccr.ccs.tencentyun.com',
|
||||
image: 'lessiesit/go_lessie-sourcing-api',
|
||||
credentialId: 'dxin_img_hub_auth',
|
||||
filter: '.*',
|
||||
defaultTag: '',
|
||||
verifySsl: true
|
||||
)
|
||||
string(
|
||||
name: 'CUSTOM_IMAGE',
|
||||
defaultValue: '',
|
||||
description: '手输完整镜像<registry>/<namespace>/<repo>:<tag>,例如: uswccr.ccs.tencentyun.com/lessiesit/go_lessie-sourcing-api:v0.0.1 (填充后,则忽略镜像仓库选择)'
|
||||
)
|
||||
booleanParam(
|
||||
name: 'ROLLBACK_VERSION',
|
||||
defaultValue: false,
|
||||
description: '是否快速回滚上一个版本?'
|
||||
)
|
||||
choice(
|
||||
name: 'BRANCH_NAME',
|
||||
choices: ['dxin', 'opt'],
|
||||
description: '选择资源清单Yaml的分支'
|
||||
)
|
||||
}
|
||||
environment {
|
||||
KUBECONFIG = credentials('k8s-test-config-admin') // k8s 凭证 ID, Jenkins 中配置的凭证名称
|
||||
Deployment_yaml = "${WORKSPACE}/lessie-ai/sit/s1/lessie-go-api.yaml"
|
||||
Deployment_name = "s1-lessie-go-api"
|
||||
K8s_namespace = "sit"
|
||||
Pod_container_name = "lessie-go-api"
|
||||
Pod_environment = "s4"
|
||||
}
|
||||
stages {
|
||||
stage('回滚上版') {
|
||||
when { expression { return params.ROLLBACK_VERSION == true } }
|
||||
steps {
|
||||
script {
|
||||
sh """
|
||||
echo "=== 开始回滚到上一个版本 ==="
|
||||
kubectl rollout undo deployment/${Deployment_name} -n ${K8s_namespace}
|
||||
echo "=== 回滚中... ==="
|
||||
kubectl rollout status deployment/${Deployment_name} -n ${K8s_namespace} --timeout=60s
|
||||
echo "=== 查看所使用的镜像 ==="
|
||||
kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}'
|
||||
echo "=== 回滚完成 ==="
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('决定镜像') {
|
||||
steps {
|
||||
script {
|
||||
def imageRegex = /^([a-zA-Z0-9.-]+)(:[0-9]+)?\/([a-zA-Z0-9._-]+)\/([a-zA-Z0-9._-]+):([a-zA-Z0-9._-]+)$/
|
||||
|
||||
if (params.CUSTOM_IMAGE?.trim()) {
|
||||
echo "检测手输镜像格式: ${params.CUSTOM_IMAGE}"
|
||||
def customImage = params.CUSTOM_IMAGE.trim()
|
||||
// 验证镜像格式
|
||||
def matcher = (customImage =~ imageRegex)
|
||||
if (!matcher.matches()) {
|
||||
error "CUSTOM_IMAGE 格式不正确,必须是 registry/namespace/repository:tag 格式\n" +
|
||||
"示例: uswccr.ccs.tencentyun.com/go_lessie-sourcing-api:v1.0.0\n" +
|
||||
"当前输入: ${customImage}"
|
||||
}
|
||||
|
||||
echo "使用手输镜像: ${customImage}"
|
||||
env.IMAGE_FULL_NAME = customImage
|
||||
|
||||
} else {
|
||||
def img = "uswccr.ccs.tencentyun.com/${params.IMAGE_NAME}"
|
||||
echo "使用 imageTag 插件镜像: ${img}"
|
||||
env.IMAGE_FULL_NAME = img
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('查询镜像') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
echo "查询镜像: ${IMAGE_FULL_NAME}"
|
||||
|
||||
def result = sh(returnStdout: true, script: """
|
||||
/data/sh/get-image-labels-fast.sh \
|
||||
"${IMAGE_FULL_NAME}" \
|
||||
"100038894437" \
|
||||
'h8H1o6Fd!HLXn' | awk '/^{/,/^}\$/'
|
||||
""").trim()
|
||||
|
||||
if (result == "" || result == null) {
|
||||
error("查询镜像 label 失败,请检查镜像是否存在或凭证问题")
|
||||
}
|
||||
|
||||
echo "镜像 Label:\n${result}"
|
||||
env.IMAGE_LABEL = result
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('拉取yaml') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
git branch: "${params.BRANCH_NAME}",
|
||||
credentialsId: 'fly_gitlab_auth',
|
||||
url: 'http://172.24.16.20/opt/opt-config.git'
|
||||
}
|
||||
}
|
||||
stage('修改YAML') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
def oldImg = sh (
|
||||
script: """
|
||||
kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}' 2>/dev/null || echo "无(可能是首次部署)"
|
||||
""",
|
||||
returnStdout: true
|
||||
).trim()
|
||||
env.OLD_IMAGE_NAME = oldImg
|
||||
echo "--- 目前正常运行的旧镜像: ${OLD_IMAGE_NAME} ---"
|
||||
echo "--- 所选择新的镜像: ${params.IMAGE_NAME} ---"
|
||||
echo "--- 修改 Deployment YAML 中的镜像为新镜像版本 ---"
|
||||
sh """
|
||||
sed -i 's#image:.*#image: ${IMAGE_FULL_NAME}#' ${Deployment_yaml}
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('部署k8s') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
def ANNOTATION = "更新 image 为 ${params.IMAGE_NAME}"
|
||||
sh """
|
||||
echo "===Apply Deployment YAML ==="
|
||||
kubectl apply -f ${Deployment_yaml} -n ${K8s_namespace}
|
||||
|
||||
echo "=== 查看当前新使用的镜像 ==="
|
||||
kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}'
|
||||
|
||||
echo "=== 添加注解 ==="
|
||||
kubectl annotate deployment/${Deployment_name} kubernetes.io/change-cause="${ANNOTATION}" --overwrite -n ${K8s_namespace}
|
||||
|
||||
echo "=== 查看历史版本 ==="
|
||||
kubectl rollout history deployment/${Deployment_name} -n ${K8s_namespace}
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('部署情况') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
echo "=== 检测部署状态并验证新版本运行情况 ==="
|
||||
echo "--- 检查 Deployment 更新状态 ---"
|
||||
def rolloutStatus = sh(
|
||||
script: "kubectl rollout status deployment/${Deployment_name} -n ${K8s_namespace} --timeout=60s",
|
||||
returnStatus: true
|
||||
)
|
||||
|
||||
if (rolloutStatus != 0){
|
||||
echo "Deployment ${Deployment_name} 发布 **超时或失败**,开始排查..."
|
||||
sh """
|
||||
echo "--- Deployment 状态 ---"
|
||||
kubectl describe deployment ${Deployment_name} -n ${K8s_namespace} || true
|
||||
|
||||
echo '--- Pod 列表 ---'
|
||||
kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o wide || true
|
||||
|
||||
echo "--- Pod 描述 (describe) ---"
|
||||
for pod in \$(kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o jsonpath='{.items[*].metadata.name}'); do
|
||||
echo "-- Pod 描述 \$pod --"
|
||||
kubectl describe pod \$pod -n ${K8s_namespace} || true
|
||||
done
|
||||
|
||||
echo "--- 最近 200 行 Pod 日志 ---"
|
||||
for pod in \$(kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o jsonpath='{.items[*].metadata.name}'); do
|
||||
echo "-- logs \$pod --"
|
||||
kubectl logs \$pod -n ${K8s_namespace} --tail=200 || true
|
||||
done
|
||||
"""
|
||||
error("=== Deployment 发布失败,请检查以上输出定位问题 ===")
|
||||
}
|
||||
|
||||
echo "=== 检查 Pods 是否全部 Ready ==="
|
||||
sh "kubectl get pods -l 'app=${Pod_container_name},environment=${Pod_environment}' -n ${K8s_namespace} -o wide"
|
||||
|
||||
echo "=== 获取最新 Pod 名称 ==="
|
||||
NEW_POD = sh (
|
||||
script: "kubectl get pods -l 'app=${Pod_container_name},environment=${Pod_environment}' -n ${K8s_namespace} --sort-by=.metadata.creationTimestamp -o jsonpath='{.items[-1].metadata.name}'",
|
||||
returnStdout: true
|
||||
).trim()
|
||||
|
||||
echo "=== 新 Pod 启动日志(最近20行) ==="
|
||||
sh "kubectl logs ${NEW_POD} -n ${K8s_namespace} --tail=20 || true"
|
||||
|
||||
echo "部署成功:${NEW_POD} 已正常运行"
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('变更说明') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
def user = currentBuild.getBuildCauses()[0].userName ?: "SYSTEM"
|
||||
def now = sh(script: "date '+%Y-%m-%d %H:%M:%S'", returnStdout: true).trim()
|
||||
env.CHANGE_MSG = """
|
||||
jenkins执行人: ${user}
|
||||
修改时间: ${now}
|
||||
旧镜像: ${OLD_IMAGE_NAME}
|
||||
新镜像: ${IMAGE_FULL_NAME}
|
||||
部署对象: ${Deployment_name}
|
||||
镜像标签: ${env.IMAGE_LABEL}
|
||||
""".stripIndent().trim()
|
||||
echo env.CHANGE_MSG
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('提交变更') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
withCredentials([usernamePassword(credentialsId: 'fly_gitlab_auth', usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) {
|
||||
sh """
|
||||
cd ${WORKSPACE}
|
||||
git config user.name "jenkins"
|
||||
git config user.email "jenkins@local"
|
||||
|
||||
# 检查工作树是否有变化
|
||||
if ! git diff --exit-code ${Deployment_yaml} > /dev/null 2>&1; then
|
||||
echo "检测到更改,正在提交..."
|
||||
git add ${Deployment_yaml}
|
||||
git commit -m "更新镜像 \${CHANGE_MSG}"
|
||||
else
|
||||
echo "${Deployment_yaml} 没有变化, 无需commit"
|
||||
fi
|
||||
|
||||
# 检查是否需要推送(是否有新的提交)
|
||||
LOCAL=\$(git rev-parse @)
|
||||
REMOTE=\$(git rev-parse @{u} 2>/dev/null || true)
|
||||
BASE=\$(git merge-base @ @{u} 2>/dev/null || true)
|
||||
|
||||
if [ "\$LOCAL" = "\$REMOTE" ]; then
|
||||
echo "已与远程系统同步更新"
|
||||
elif [ "\$LOCAL" = "\$BASE" ]; then
|
||||
echo "需要从远程获取数据"
|
||||
git pull http://172.24.16.20/opt/opt-config.git ${params.BRANCH_NAME}
|
||||
elif [ "\$REMOTE" = "\$BASE" ]; then
|
||||
echo "将更改推送到远程服务器..."
|
||||
# 生成临时 .netrc 文件
|
||||
echo "machine 172.24.16.20 login \$GIT_USER password \$GIT_PASS" > ~/.netrc
|
||||
chmod 600 ~/.netrc
|
||||
git push http://172.24.16.20/opt/opt-config.git ${params.BRANCH_NAME}
|
||||
else
|
||||
echo "与远程模式不同,跳过推送操作"
|
||||
fi
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
post {
|
||||
success {
|
||||
echo "成功!"
|
||||
echo "=== 所选择镜像: ${params.IMAGE_NAME} ==="
|
||||
}
|
||||
failure {
|
||||
echo "有步骤失败,请检查!"
|
||||
}
|
||||
}
|
||||
}
|
||||
277
SCM/部署镜像/s5/DM_s5_lessie_agent.groovy
Normal file
277
SCM/部署镜像/s5/DM_s5_lessie_agent.groovy
Normal file
@@ -0,0 +1,277 @@
|
||||
pipeline {
|
||||
agent any
|
||||
|
||||
parameters {
|
||||
imageTag(
|
||||
name: 'IMAGE_NAME',
|
||||
description: 'sit 仓库镜像 (除非输入 CUSTOM_IMAGE, 否则使用这里的)',
|
||||
registry: 'https://uswccr.ccs.tencentyun.com',
|
||||
image: 'lessiesit/lessie-sourcing-agents',
|
||||
credentialId: 'dxin_img_hub_auth',
|
||||
filter: '.*',
|
||||
defaultTag: '',
|
||||
verifySsl: true
|
||||
)
|
||||
string(
|
||||
name: 'CUSTOM_IMAGE',
|
||||
defaultValue: '',
|
||||
description: '手输完整镜像<registry>/<namespace>/<repo>:<tag>,例如: uswccr.ccs.tencentyun.com/lessiesit/lessie-sourcing-agents:v0.0.1 (填充后,则忽略镜像仓库选择)'
|
||||
)
|
||||
booleanParam(
|
||||
name: 'ROLLBACK_VERSION',
|
||||
defaultValue: false,
|
||||
description: '是否快速回滚上一个版本?'
|
||||
)
|
||||
choice(
|
||||
name: 'BRANCH_NAME',
|
||||
choices: ['dxin', 'opt'],
|
||||
description: '选择资源清单Yaml的分支'
|
||||
)
|
||||
}
|
||||
environment {
|
||||
KUBECONFIG = credentials('k8s-test-config-admin') // k8s 凭证 ID, Jenkins 中配置的凭证名称
|
||||
Deployment_yaml = "${WORKSPACE}/lessie-ai/sit/s5/lessie-agents.yaml"
|
||||
Deployment_name = "s5-lessie-agents"
|
||||
K8s_namespace = "sit"
|
||||
Pod_container_name = "lessie-agents"
|
||||
Pod_environment = "s5"
|
||||
}
|
||||
stages {
|
||||
stage('回滚上版') {
|
||||
when { expression { return params.ROLLBACK_VERSION == true } }
|
||||
steps {
|
||||
script {
|
||||
sh """
|
||||
echo "=== 开始回滚到上一个版本 ==="
|
||||
kubectl rollout undo deployment/${Deployment_name} -n ${K8s_namespace}
|
||||
echo "=== 回滚中... ==="
|
||||
kubectl rollout status deployment/${Deployment_name} -n ${K8s_namespace} --timeout=60s
|
||||
echo "=== 查看所使用的镜像 ==="
|
||||
kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}'
|
||||
echo "=== 回滚完成 ==="
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('决定镜像') {
|
||||
steps {
|
||||
script {
|
||||
def imageRegex = /^([a-zA-Z0-9.-]+)(:[0-9]+)?\/([a-zA-Z0-9._-]+)\/([a-zA-Z0-9._-]+):([a-zA-Z0-9._-]+)$/
|
||||
|
||||
if (params.CUSTOM_IMAGE?.trim()) {
|
||||
echo "检测手输镜像格式: ${params.CUSTOM_IMAGE}"
|
||||
def customImage = params.CUSTOM_IMAGE.trim()
|
||||
// 验证镜像格式
|
||||
def matcher = (customImage =~ imageRegex)
|
||||
if (!matcher.matches()) {
|
||||
error "CUSTOM_IMAGE 格式不正确,必须是 registry/namespace/repository:tag 格式\n" +
|
||||
"示例: uswccr.ccs.tencentyun.com/lessiesit/lessie-sourcing-agents:v1.0.0\n" +
|
||||
"当前输入: ${customImage}"
|
||||
}
|
||||
|
||||
echo "使用手输镜像: ${customImage}"
|
||||
env.IMAGE_FULL_NAME = customImage
|
||||
|
||||
} else {
|
||||
def img = "uswccr.ccs.tencentyun.com/${params.IMAGE_NAME}"
|
||||
echo "使用 imageTag 插件镜像: ${img}"
|
||||
env.IMAGE_FULL_NAME = img
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('查询镜像') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
echo "查询镜像: ${IMAGE_FULL_NAME}"
|
||||
|
||||
def result = sh(returnStdout: true, script: """
|
||||
/data/sh/get-image-labels-fast.sh \
|
||||
"${IMAGE_FULL_NAME}" \
|
||||
"100038894437" \
|
||||
'h8H1o6Fd!HLXn' | awk '/^{/,/^}\$/'
|
||||
""").trim()
|
||||
|
||||
if (result == "" || result == null) {
|
||||
error("查询镜像 label 失败,请检查镜像是否存在或凭证问题")
|
||||
}
|
||||
|
||||
echo "镜像 Label:\n${result}"
|
||||
env.IMAGE_LABEL = result
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('拉取yaml') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
git branch: "${params.BRANCH_NAME}",
|
||||
credentialsId: 'fly_gitlab_auth',
|
||||
url: 'http://172.24.16.20/opt/opt-config.git'
|
||||
}
|
||||
}
|
||||
stage('修改YAML') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
def oldImg = sh (
|
||||
script: """
|
||||
kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}' 2>/dev/null || echo "无(可能是首次部署)"
|
||||
""",
|
||||
returnStdout: true
|
||||
).trim()
|
||||
env.OLD_IMAGE_NAME = oldImg
|
||||
echo "--- 目前正常运行的旧镜像: ${OLD_IMAGE_NAME} ---"
|
||||
echo "--- 所选择新的镜像: ${params.IMAGE_NAME} ---"
|
||||
echo "--- 修改 Deployment YAML 中的镜像为新镜像版本 ---"
|
||||
sh """
|
||||
sed -i 's#image:.*#image: ${IMAGE_FULL_NAME}#' ${Deployment_yaml}
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('部署k8s') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
def ANNOTATION = "更新 image 为 ${params.IMAGE_NAME}"
|
||||
sh """
|
||||
echo "===Apply Deployment YAML ==="
|
||||
kubectl apply -f ${Deployment_yaml} -n ${K8s_namespace}
|
||||
|
||||
echo "=== 查看当前新使用的镜像 ==="
|
||||
kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}'
|
||||
|
||||
echo "=== 添加注解 ==="
|
||||
kubectl annotate deployment/${Deployment_name} kubernetes.io/change-cause="${ANNOTATION}" --overwrite -n ${K8s_namespace}
|
||||
|
||||
echo "=== 查看历史版本 ==="
|
||||
kubectl rollout history deployment/${Deployment_name} -n ${K8s_namespace}
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('部署情况') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
echo "=== 检测部署状态并验证新版本运行情况 ==="
|
||||
echo "--- 检查 Deployment 更新状态 ---"
|
||||
def rolloutStatus = sh(
|
||||
script: "kubectl rollout status deployment/${Deployment_name} -n ${K8s_namespace} --timeout=60s",
|
||||
returnStatus: true
|
||||
)
|
||||
|
||||
if (rolloutStatus != 0){
|
||||
echo "Deployment ${Deployment_name} 发布 **超时或失败**,开始排查..."
|
||||
sh """
|
||||
echo "--- Deployment 状态 ---"
|
||||
kubectl describe deployment ${Deployment_name} -n ${K8s_namespace} || true
|
||||
|
||||
echo '--- Pod 列表 ---'
|
||||
kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o wide || true
|
||||
|
||||
echo "--- Pod 描述 (describe) ---"
|
||||
for pod in \$(kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o jsonpath='{.items[*].metadata.name}'); do
|
||||
echo "-- Pod 描述 \$pod --"
|
||||
kubectl describe pod \$pod -n ${K8s_namespace} || true
|
||||
done
|
||||
|
||||
echo "--- 最近 200 行 Pod 日志 ---"
|
||||
for pod in \$(kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o jsonpath='{.items[*].metadata.name}'); do
|
||||
echo "-- logs \$pod --"
|
||||
kubectl logs \$pod -n ${K8s_namespace} --tail=200 || true
|
||||
done
|
||||
"""
|
||||
error("=== Deployment 发布失败,请检查以上输出定位问题 ===")
|
||||
}
|
||||
|
||||
echo "=== 检查 Pods 是否全部 Ready ==="
|
||||
sh "kubectl get pods -l 'app=${Pod_container_name},environment=${Pod_environment}' -n ${K8s_namespace} -o wide"
|
||||
|
||||
echo "=== 获取最新 Pod 名称 ==="
|
||||
NEW_POD = sh (
|
||||
script: "kubectl get pods -l 'app=${Pod_container_name},environment=${Pod_environment}' -n ${K8s_namespace} --sort-by=.metadata.creationTimestamp -o jsonpath='{.items[-1].metadata.name}'",
|
||||
returnStdout: true
|
||||
).trim()
|
||||
|
||||
echo "=== 新 Pod 启动日志(最近20行) ==="
|
||||
sh "kubectl logs ${NEW_POD} -n ${K8s_namespace} --tail=20 || true"
|
||||
|
||||
echo "部署成功:${NEW_POD} 已正常运行"
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('变更说明') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
def user = currentBuild.getBuildCauses()[0].userName ?: "SYSTEM"
|
||||
def now = sh(script: "date '+%Y-%m-%d %H:%M:%S'", returnStdout: true).trim()
|
||||
env.CHANGE_MSG = """
|
||||
jenkins执行人: ${user}
|
||||
修改时间: ${now}
|
||||
旧镜像: ${OLD_IMAGE_NAME}
|
||||
新镜像: ${IMAGE_FULL_NAME}
|
||||
部署对象: ${Deployment_name}
|
||||
镜像标签: ${env.IMAGE_LABEL}
|
||||
""".stripIndent().trim()
|
||||
echo env.CHANGE_MSG
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('提交变更') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
withCredentials([usernamePassword(credentialsId: 'fly_gitlab_auth', usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) {
|
||||
sh """
|
||||
cd ${WORKSPACE}
|
||||
git config user.name "jenkins"
|
||||
git config user.email "jenkins@local"
|
||||
|
||||
# 检查工作树是否有变化
|
||||
if ! git diff --exit-code ${Deployment_yaml} > /dev/null 2>&1; then
|
||||
echo "检测到更改,正在提交..."
|
||||
git add ${Deployment_yaml}
|
||||
git commit -m "更新镜像 \${CHANGE_MSG}"
|
||||
else
|
||||
echo "${Deployment_yaml} 没有变化, 无需commit"
|
||||
fi
|
||||
|
||||
# 检查是否需要推送(是否有新的提交)
|
||||
LOCAL=\$(git rev-parse @)
|
||||
REMOTE=\$(git rev-parse @{u} 2>/dev/null || true)
|
||||
BASE=\$(git merge-base @ @{u} 2>/dev/null || true)
|
||||
|
||||
if [ "\$LOCAL" = "\$REMOTE" ]; then
|
||||
echo "已与远程系统同步更新"
|
||||
elif [ "\$LOCAL" = "\$BASE" ]; then
|
||||
echo "需要从远程获取数据"
|
||||
git pull http://172.24.16.20/opt/opt-config.git ${params.BRANCH_NAME}
|
||||
elif [ "\$REMOTE" = "\$BASE" ]; then
|
||||
echo "将更改推送到远程服务器..."
|
||||
# 生成临时 .netrc 文件
|
||||
echo "machine 172.24.16.20 login \$GIT_USER password \$GIT_PASS" > ~/.netrc
|
||||
chmod 600 ~/.netrc
|
||||
git push http://172.24.16.20/opt/opt-config.git ${params.BRANCH_NAME}
|
||||
else
|
||||
echo "与远程模式不同,跳过推送操作"
|
||||
fi
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
post {
|
||||
success {
|
||||
echo "成功!"
|
||||
echo "=== 所选择镜像: ${params.IMAGE_NAME} ==="
|
||||
}
|
||||
failure {
|
||||
echo "有步骤失败,请检查!"
|
||||
}
|
||||
}
|
||||
}
|
||||
277
SCM/部署镜像/s5/DM_s5_lessie_ai_web.groovy
Normal file
277
SCM/部署镜像/s5/DM_s5_lessie_ai_web.groovy
Normal file
@@ -0,0 +1,277 @@
|
||||
pipeline {
|
||||
agent any
|
||||
|
||||
parameters {
|
||||
imageTag(
|
||||
name: 'IMAGE_NAME',
|
||||
description: 'sit 仓库镜像 (除非输入 CUSTOM_IMAGE, 否则使用这里的)',
|
||||
registry: 'https://uswccr.ccs.tencentyun.com',
|
||||
image: 'lessiesit/lessie-ai-web',
|
||||
credentialId: 'dxin_img_hub_auth',
|
||||
filter: '.*',
|
||||
defaultTag: '',
|
||||
verifySsl: true
|
||||
)
|
||||
string(
|
||||
name: 'CUSTOM_IMAGE',
|
||||
defaultValue: '',
|
||||
description: '手输完整镜像<registry>/<namespace>/<repo>:<tag>,例如: uswccr.ccs.tencentyun.com/lessiesit/lessie-ai-web:v0.0.1 (填充后,则忽略镜像仓库选择)'
|
||||
)
|
||||
booleanParam(
|
||||
name: 'ROLLBACK_VERSION',
|
||||
defaultValue: false,
|
||||
description: '是否快速回滚上一个版本?'
|
||||
)
|
||||
choice(
|
||||
name: 'BRANCH_NAME',
|
||||
choices: ['dxin', 'opt'],
|
||||
description: '选择资源清单Yaml的分支'
|
||||
)
|
||||
}
|
||||
environment {
|
||||
KUBECONFIG = credentials('k8s-test-config-admin') // k8s 凭证 ID, Jenkins 中配置的凭证名称
|
||||
Deployment_yaml = "${WORKSPACE}/lessie-ai/sit/s1/lessie-ai-web.yaml"
|
||||
Deployment_name = "s1-lessie-ai-web"
|
||||
K8s_namespace = "sit"
|
||||
Pod_container_name = "lessie-ai-web"
|
||||
Pod_environment = "s5"
|
||||
}
|
||||
stages {
|
||||
stage('回滚上版') {
|
||||
when { expression { return params.ROLLBACK_VERSION == true } }
|
||||
steps {
|
||||
script {
|
||||
sh """
|
||||
echo "=== 开始回滚到上一个版本 ==="
|
||||
kubectl rollout undo deployment/${Deployment_name} -n ${K8s_namespace}
|
||||
echo "=== 回滚中... ==="
|
||||
kubectl rollout status deployment/${Deployment_name} -n ${K8s_namespace} --timeout=60s
|
||||
echo "=== 查看所使用的镜像 ==="
|
||||
kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}'
|
||||
echo "=== 回滚完成 ==="
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('决定镜像') {
|
||||
steps {
|
||||
script {
|
||||
def imageRegex = /^([a-zA-Z0-9.-]+)(:[0-9]+)?\/([a-zA-Z0-9._-]+)\/([a-zA-Z0-9._-]+):([a-zA-Z0-9._-]+)$/
|
||||
|
||||
if (params.CUSTOM_IMAGE?.trim()) {
|
||||
echo "检测手输镜像格式: ${params.CUSTOM_IMAGE}"
|
||||
def customImage = params.CUSTOM_IMAGE.trim()
|
||||
// 验证镜像格式
|
||||
def matcher = (customImage =~ imageRegex)
|
||||
if (!matcher.matches()) {
|
||||
error "CUSTOM_IMAGE 格式不正确,必须是 registry/namespace/repository:tag 格式\n" +
|
||||
"示例: uswccr.ccs.tencentyun.com/lessie-ai-web:v1.0.0\n" +
|
||||
"当前输入: ${customImage}"
|
||||
}
|
||||
|
||||
echo "使用手输镜像: ${customImage}"
|
||||
env.IMAGE_FULL_NAME = customImage
|
||||
|
||||
} else {
|
||||
def img = "uswccr.ccs.tencentyun.com/${params.IMAGE_NAME}"
|
||||
echo "使用 imageTag 插件镜像: ${img}"
|
||||
env.IMAGE_FULL_NAME = img
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('查询镜像') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
echo "查询镜像: ${IMAGE_FULL_NAME}"
|
||||
|
||||
def result = sh(returnStdout: true, script: """
|
||||
/data/sh/get-image-labels-fast.sh \
|
||||
"${IMAGE_FULL_NAME}" \
|
||||
"100038894437" \
|
||||
'h8H1o6Fd!HLXn' | awk '/^{/,/^}\$/'
|
||||
""").trim()
|
||||
|
||||
if (result == "" || result == null) {
|
||||
error("查询镜像 label 失败,请检查镜像是否存在或凭证问题")
|
||||
}
|
||||
|
||||
echo "镜像 Label:\n${result}"
|
||||
env.IMAGE_LABEL = result
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('拉取yaml') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
git branch: "${params.BRANCH_NAME}",
|
||||
credentialsId: 'fly_gitlab_auth',
|
||||
url: 'http://172.24.16.20/opt/opt-config.git'
|
||||
}
|
||||
}
|
||||
stage('修改YAML') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
def oldImg = sh (
|
||||
script: """
|
||||
kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}' 2>/dev/null || echo "无(可能是首次部署)"
|
||||
""",
|
||||
returnStdout: true
|
||||
).trim()
|
||||
env.OLD_IMAGE_NAME = oldImg
|
||||
echo "--- 目前正常运行的旧镜像: ${OLD_IMAGE_NAME} ---"
|
||||
echo "--- 所选择新的镜像: ${params.IMAGE_NAME} ---"
|
||||
echo "--- 修改 Deployment YAML 中的镜像为新镜像版本 ---"
|
||||
sh """
|
||||
sed -i 's#image:.*#image: ${IMAGE_FULL_NAME}#' ${Deployment_yaml}
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('部署k8s') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
def ANNOTATION = "更新 image 为 ${params.IMAGE_NAME}"
|
||||
sh """
|
||||
echo "===Apply Deployment YAML ==="
|
||||
kubectl apply -f ${Deployment_yaml} -n ${K8s_namespace}
|
||||
|
||||
echo "=== 查看当前新使用的镜像 ==="
|
||||
kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}'
|
||||
|
||||
echo "=== 添加注解 ==="
|
||||
kubectl annotate deployment/${Deployment_name} kubernetes.io/change-cause="${ANNOTATION}" --overwrite -n ${K8s_namespace}
|
||||
|
||||
echo "=== 查看历史版本 ==="
|
||||
kubectl rollout history deployment/${Deployment_name} -n ${K8s_namespace}
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('部署情况') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
echo "=== 检测部署状态并验证新版本运行情况 ==="
|
||||
echo "--- 检查 Deployment 更新状态 ---"
|
||||
def rolloutStatus = sh(
|
||||
script: "kubectl rollout status deployment/${Deployment_name} -n ${K8s_namespace} --timeout=60s",
|
||||
returnStatus: true
|
||||
)
|
||||
|
||||
if (rolloutStatus != 0){
|
||||
echo "Deployment ${Deployment_name} 发布 **超时或失败**,开始排查..."
|
||||
sh """
|
||||
echo "--- Deployment 状态 ---"
|
||||
kubectl describe deployment ${Deployment_name} -n ${K8s_namespace} || true
|
||||
|
||||
echo '--- Pod 列表 ---'
|
||||
kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o wide || true
|
||||
|
||||
echo "--- Pod 描述 (describe) ---"
|
||||
for pod in \$(kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o jsonpath='{.items[*].metadata.name}'); do
|
||||
echo "-- Pod 描述 \$pod --"
|
||||
kubectl describe pod \$pod -n ${K8s_namespace} || true
|
||||
done
|
||||
|
||||
echo "--- 最近 200 行 Pod 日志 ---"
|
||||
for pod in \$(kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o jsonpath='{.items[*].metadata.name}'); do
|
||||
echo "-- logs \$pod --"
|
||||
kubectl logs \$pod -n ${K8s_namespace} --tail=200 || true
|
||||
done
|
||||
"""
|
||||
error("=== Deployment 发布失败,请检查以上输出定位问题 ===")
|
||||
}
|
||||
|
||||
echo "=== 检查 Pods 是否全部 Ready ==="
|
||||
sh "kubectl get pods -l 'app=${Pod_container_name},environment=${Pod_environment}' -n ${K8s_namespace} -o wide"
|
||||
|
||||
echo "=== 获取最新 Pod 名称 ==="
|
||||
NEW_POD = sh (
|
||||
script: "kubectl get pods -l 'app=${Pod_container_name},environment=${Pod_environment}' -n ${K8s_namespace} --sort-by=.metadata.creationTimestamp -o jsonpath='{.items[-1].metadata.name}'",
|
||||
returnStdout: true
|
||||
).trim()
|
||||
|
||||
echo "=== 新 Pod 启动日志(最近20行) ==="
|
||||
sh "kubectl logs ${NEW_POD} -n ${K8s_namespace} --tail=20 || true"
|
||||
|
||||
echo "部署成功:${NEW_POD} 已正常运行"
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('变更说明') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
def user = currentBuild.getBuildCauses()[0].userName ?: "SYSTEM"
|
||||
def now = sh(script: "date '+%Y-%m-%d %H:%M:%S'", returnStdout: true).trim()
|
||||
env.CHANGE_MSG = """
|
||||
jenkins执行人: ${user}
|
||||
修改时间: ${now}
|
||||
旧镜像: ${OLD_IMAGE_NAME}
|
||||
新镜像: ${IMAGE_FULL_NAME}
|
||||
部署对象: ${Deployment_name}
|
||||
镜像标签: ${env.IMAGE_LABEL}
|
||||
""".stripIndent().trim()
|
||||
echo env.CHANGE_MSG
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('提交变更') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
withCredentials([usernamePassword(credentialsId: 'fly_gitlab_auth', usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) {
|
||||
sh """
|
||||
cd ${WORKSPACE}
|
||||
git config user.name "jenkins"
|
||||
git config user.email "jenkins@local"
|
||||
|
||||
# 检查工作树是否有变化
|
||||
if ! git diff --exit-code ${Deployment_yaml} > /dev/null 2>&1; then
|
||||
echo "检测到更改,正在提交..."
|
||||
git add ${Deployment_yaml}
|
||||
git commit -m "更新镜像 \${CHANGE_MSG}"
|
||||
else
|
||||
echo "${Deployment_yaml} 没有变化, 无需commit"
|
||||
fi
|
||||
|
||||
# 检查是否需要推送(是否有新的提交)
|
||||
LOCAL=\$(git rev-parse @)
|
||||
REMOTE=\$(git rev-parse @{u} 2>/dev/null || true)
|
||||
BASE=\$(git merge-base @ @{u} 2>/dev/null || true)
|
||||
|
||||
if [ "\$LOCAL" = "\$REMOTE" ]; then
|
||||
echo "已与远程系统同步更新"
|
||||
elif [ "\$LOCAL" = "\$BASE" ]; then
|
||||
echo "需要从远程获取数据"
|
||||
git pull http://172.24.16.20/opt/opt-config.git ${params.BRANCH_NAME}
|
||||
elif [ "\$REMOTE" = "\$BASE" ]; then
|
||||
echo "将更改推送到远程服务器..."
|
||||
# 生成临时 .netrc 文件
|
||||
echo "machine 172.24.16.20 login \$GIT_USER password \$GIT_PASS" > ~/.netrc
|
||||
chmod 600 ~/.netrc
|
||||
git push http://172.24.16.20/opt/opt-config.git ${params.BRANCH_NAME}
|
||||
else
|
||||
echo "与远程模式不同,跳过推送操作"
|
||||
fi
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
post {
|
||||
success {
|
||||
echo "成功!"
|
||||
echo "=== 所选择镜像: ${params.IMAGE_NAME} ==="
|
||||
}
|
||||
failure {
|
||||
echo "有步骤失败,请检查!"
|
||||
}
|
||||
}
|
||||
}
|
||||
277
SCM/部署镜像/s5/DM_s5_lessie_go_api.groovy
Normal file
277
SCM/部署镜像/s5/DM_s5_lessie_go_api.groovy
Normal file
@@ -0,0 +1,277 @@
|
||||
pipeline {
|
||||
agent any
|
||||
|
||||
parameters {
|
||||
imageTag(
|
||||
name: 'IMAGE_NAME',
|
||||
description: 'sit 仓库镜像 (除非输入 CUSTOM_IMAGE, 否则使用这里的)',
|
||||
registry: 'https://uswccr.ccs.tencentyun.com',
|
||||
image: 'lessiesit/go_lessie-sourcing-api',
|
||||
credentialId: 'dxin_img_hub_auth',
|
||||
filter: '.*',
|
||||
defaultTag: '',
|
||||
verifySsl: true
|
||||
)
|
||||
string(
|
||||
name: 'CUSTOM_IMAGE',
|
||||
defaultValue: '',
|
||||
description: '手输完整镜像<registry>/<namespace>/<repo>:<tag>,例如: uswccr.ccs.tencentyun.com/lessiesit/go_lessie-sourcing-api:v0.0.1 (填充后,则忽略镜像仓库选择)'
|
||||
)
|
||||
booleanParam(
|
||||
name: 'ROLLBACK_VERSION',
|
||||
defaultValue: false,
|
||||
description: '是否快速回滚上一个版本?'
|
||||
)
|
||||
choice(
|
||||
name: 'BRANCH_NAME',
|
||||
choices: ['dxin', 'opt'],
|
||||
description: '选择资源清单Yaml的分支'
|
||||
)
|
||||
}
|
||||
environment {
|
||||
KUBECONFIG = credentials('k8s-test-config-admin') // k8s 凭证 ID, Jenkins 中配置的凭证名称
|
||||
Deployment_yaml = "${WORKSPACE}/lessie-ai/sit/s1/lessie-go-api.yaml"
|
||||
Deployment_name = "s1-lessie-go-api"
|
||||
K8s_namespace = "sit"
|
||||
Pod_container_name = "lessie-go-api"
|
||||
Pod_environment = "s5"
|
||||
}
|
||||
stages {
|
||||
stage('回滚上版') {
|
||||
when { expression { return params.ROLLBACK_VERSION == true } }
|
||||
steps {
|
||||
script {
|
||||
sh """
|
||||
echo "=== 开始回滚到上一个版本 ==="
|
||||
kubectl rollout undo deployment/${Deployment_name} -n ${K8s_namespace}
|
||||
echo "=== 回滚中... ==="
|
||||
kubectl rollout status deployment/${Deployment_name} -n ${K8s_namespace} --timeout=60s
|
||||
echo "=== 查看所使用的镜像 ==="
|
||||
kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}'
|
||||
echo "=== 回滚完成 ==="
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('决定镜像') {
|
||||
steps {
|
||||
script {
|
||||
def imageRegex = /^([a-zA-Z0-9.-]+)(:[0-9]+)?\/([a-zA-Z0-9._-]+)\/([a-zA-Z0-9._-]+):([a-zA-Z0-9._-]+)$/
|
||||
|
||||
if (params.CUSTOM_IMAGE?.trim()) {
|
||||
echo "检测手输镜像格式: ${params.CUSTOM_IMAGE}"
|
||||
def customImage = params.CUSTOM_IMAGE.trim()
|
||||
// 验证镜像格式
|
||||
def matcher = (customImage =~ imageRegex)
|
||||
if (!matcher.matches()) {
|
||||
error "CUSTOM_IMAGE 格式不正确,必须是 registry/namespace/repository:tag 格式\n" +
|
||||
"示例: uswccr.ccs.tencentyun.com/go_lessie-sourcing-api:v1.0.0\n" +
|
||||
"当前输入: ${customImage}"
|
||||
}
|
||||
|
||||
echo "使用手输镜像: ${customImage}"
|
||||
env.IMAGE_FULL_NAME = customImage
|
||||
|
||||
} else {
|
||||
def img = "uswccr.ccs.tencentyun.com/${params.IMAGE_NAME}"
|
||||
echo "使用 imageTag 插件镜像: ${img}"
|
||||
env.IMAGE_FULL_NAME = img
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('查询镜像') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
echo "查询镜像: ${IMAGE_FULL_NAME}"
|
||||
|
||||
def result = sh(returnStdout: true, script: """
|
||||
/data/sh/get-image-labels-fast.sh \
|
||||
"${IMAGE_FULL_NAME}" \
|
||||
"100038894437" \
|
||||
'h8H1o6Fd!HLXn' | awk '/^{/,/^}\$/'
|
||||
""").trim()
|
||||
|
||||
if (result == "" || result == null) {
|
||||
error("查询镜像 label 失败,请检查镜像是否存在或凭证问题")
|
||||
}
|
||||
|
||||
echo "镜像 Label:\n${result}"
|
||||
env.IMAGE_LABEL = result
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('拉取yaml') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
git branch: "${params.BRANCH_NAME}",
|
||||
credentialsId: 'fly_gitlab_auth',
|
||||
url: 'http://172.24.16.20/opt/opt-config.git'
|
||||
}
|
||||
}
|
||||
stage('修改YAML') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
def oldImg = sh (
|
||||
script: """
|
||||
kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}' 2>/dev/null || echo "无(可能是首次部署)"
|
||||
""",
|
||||
returnStdout: true
|
||||
).trim()
|
||||
env.OLD_IMAGE_NAME = oldImg
|
||||
echo "--- 目前正常运行的旧镜像: ${OLD_IMAGE_NAME} ---"
|
||||
echo "--- 所选择新的镜像: ${params.IMAGE_NAME} ---"
|
||||
echo "--- 修改 Deployment YAML 中的镜像为新镜像版本 ---"
|
||||
sh """
|
||||
sed -i 's#image:.*#image: ${IMAGE_FULL_NAME}#' ${Deployment_yaml}
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('部署k8s') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
def ANNOTATION = "更新 image 为 ${params.IMAGE_NAME}"
|
||||
sh """
|
||||
echo "===Apply Deployment YAML ==="
|
||||
kubectl apply -f ${Deployment_yaml} -n ${K8s_namespace}
|
||||
|
||||
echo "=== 查看当前新使用的镜像 ==="
|
||||
kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}'
|
||||
|
||||
echo "=== 添加注解 ==="
|
||||
kubectl annotate deployment/${Deployment_name} kubernetes.io/change-cause="${ANNOTATION}" --overwrite -n ${K8s_namespace}
|
||||
|
||||
echo "=== 查看历史版本 ==="
|
||||
kubectl rollout history deployment/${Deployment_name} -n ${K8s_namespace}
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('部署情况') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
echo "=== 检测部署状态并验证新版本运行情况 ==="
|
||||
echo "--- 检查 Deployment 更新状态 ---"
|
||||
def rolloutStatus = sh(
|
||||
script: "kubectl rollout status deployment/${Deployment_name} -n ${K8s_namespace} --timeout=60s",
|
||||
returnStatus: true
|
||||
)
|
||||
|
||||
if (rolloutStatus != 0){
|
||||
echo "Deployment ${Deployment_name} 发布 **超时或失败**,开始排查..."
|
||||
sh """
|
||||
echo "--- Deployment 状态 ---"
|
||||
kubectl describe deployment ${Deployment_name} -n ${K8s_namespace} || true
|
||||
|
||||
echo '--- Pod 列表 ---'
|
||||
kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o wide || true
|
||||
|
||||
echo "--- Pod 描述 (describe) ---"
|
||||
for pod in \$(kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o jsonpath='{.items[*].metadata.name}'); do
|
||||
echo "-- Pod 描述 \$pod --"
|
||||
kubectl describe pod \$pod -n ${K8s_namespace} || true
|
||||
done
|
||||
|
||||
echo "--- 最近 200 行 Pod 日志 ---"
|
||||
for pod in \$(kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o jsonpath='{.items[*].metadata.name}'); do
|
||||
echo "-- logs \$pod --"
|
||||
kubectl logs \$pod -n ${K8s_namespace} --tail=200 || true
|
||||
done
|
||||
"""
|
||||
error("=== Deployment 发布失败,请检查以上输出定位问题 ===")
|
||||
}
|
||||
|
||||
echo "=== 检查 Pods 是否全部 Ready ==="
|
||||
sh "kubectl get pods -l 'app=${Pod_container_name},environment=${Pod_environment}' -n ${K8s_namespace} -o wide"
|
||||
|
||||
echo "=== 获取最新 Pod 名称 ==="
|
||||
NEW_POD = sh (
|
||||
script: "kubectl get pods -l 'app=${Pod_container_name},environment=${Pod_environment}' -n ${K8s_namespace} --sort-by=.metadata.creationTimestamp -o jsonpath='{.items[-1].metadata.name}'",
|
||||
returnStdout: true
|
||||
).trim()
|
||||
|
||||
echo "=== 新 Pod 启动日志(最近20行) ==="
|
||||
sh "kubectl logs ${NEW_POD} -n ${K8s_namespace} --tail=20 || true"
|
||||
|
||||
echo "部署成功:${NEW_POD} 已正常运行"
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('变更说明') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
def user = currentBuild.getBuildCauses()[0].userName ?: "SYSTEM"
|
||||
def now = sh(script: "date '+%Y-%m-%d %H:%M:%S'", returnStdout: true).trim()
|
||||
env.CHANGE_MSG = """
|
||||
jenkins执行人: ${user}
|
||||
修改时间: ${now}
|
||||
旧镜像: ${OLD_IMAGE_NAME}
|
||||
新镜像: ${IMAGE_FULL_NAME}
|
||||
部署对象: ${Deployment_name}
|
||||
镜像标签: ${env.IMAGE_LABEL}
|
||||
""".stripIndent().trim()
|
||||
echo env.CHANGE_MSG
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('提交变更') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
withCredentials([usernamePassword(credentialsId: 'fly_gitlab_auth', usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) {
|
||||
sh """
|
||||
cd ${WORKSPACE}
|
||||
git config user.name "jenkins"
|
||||
git config user.email "jenkins@local"
|
||||
|
||||
# 检查工作树是否有变化
|
||||
if ! git diff --exit-code ${Deployment_yaml} > /dev/null 2>&1; then
|
||||
echo "检测到更改,正在提交..."
|
||||
git add ${Deployment_yaml}
|
||||
git commit -m "更新镜像 \${CHANGE_MSG}"
|
||||
else
|
||||
echo "${Deployment_yaml} 没有变化, 无需commit"
|
||||
fi
|
||||
|
||||
# 检查是否需要推送(是否有新的提交)
|
||||
LOCAL=\$(git rev-parse @)
|
||||
REMOTE=\$(git rev-parse @{u} 2>/dev/null || true)
|
||||
BASE=\$(git merge-base @ @{u} 2>/dev/null || true)
|
||||
|
||||
if [ "\$LOCAL" = "\$REMOTE" ]; then
|
||||
echo "已与远程系统同步更新"
|
||||
elif [ "\$LOCAL" = "\$BASE" ]; then
|
||||
echo "需要从远程获取数据"
|
||||
git pull http://172.24.16.20/opt/opt-config.git ${params.BRANCH_NAME}
|
||||
elif [ "\$REMOTE" = "\$BASE" ]; then
|
||||
echo "将更改推送到远程服务器..."
|
||||
# 生成临时 .netrc 文件
|
||||
echo "machine 172.24.16.20 login \$GIT_USER password \$GIT_PASS" > ~/.netrc
|
||||
chmod 600 ~/.netrc
|
||||
git push http://172.24.16.20/opt/opt-config.git ${params.BRANCH_NAME}
|
||||
else
|
||||
echo "与远程模式不同,跳过推送操作"
|
||||
fi
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
post {
|
||||
success {
|
||||
echo "成功!"
|
||||
echo "=== 所选择镜像: ${params.IMAGE_NAME} ==="
|
||||
}
|
||||
failure {
|
||||
echo "有步骤失败,请检查!"
|
||||
}
|
||||
}
|
||||
}
|
||||
277
SCM/部署镜像/s6/DM_s6_lessie_agent.groovy
Normal file
277
SCM/部署镜像/s6/DM_s6_lessie_agent.groovy
Normal file
@@ -0,0 +1,277 @@
|
||||
pipeline {
|
||||
agent any
|
||||
|
||||
parameters {
|
||||
imageTag(
|
||||
name: 'IMAGE_NAME',
|
||||
description: 'sit 仓库镜像 (除非输入 CUSTOM_IMAGE, 否则使用这里的)',
|
||||
registry: 'https://uswccr.ccs.tencentyun.com',
|
||||
image: 'lessiesit/lessie-sourcing-agents',
|
||||
credentialId: 'dxin_img_hub_auth',
|
||||
filter: '.*',
|
||||
defaultTag: '',
|
||||
verifySsl: true
|
||||
)
|
||||
string(
|
||||
name: 'CUSTOM_IMAGE',
|
||||
defaultValue: '',
|
||||
description: '手输完整镜像<registry>/<namespace>/<repo>:<tag>,例如: uswccr.ccs.tencentyun.com/lessiesit/lessie-sourcing-agents:v0.0.1 (填充后,则忽略镜像仓库选择)'
|
||||
)
|
||||
booleanParam(
|
||||
name: 'ROLLBACK_VERSION',
|
||||
defaultValue: false,
|
||||
description: '是否快速回滚上一个版本?'
|
||||
)
|
||||
choice(
|
||||
name: 'BRANCH_NAME',
|
||||
choices: ['dxin', 'opt'],
|
||||
description: '选择资源清单Yaml的分支'
|
||||
)
|
||||
}
|
||||
environment {
|
||||
KUBECONFIG = credentials('k8s-test-config-admin') // k8s 凭证 ID, Jenkins 中配置的凭证名称
|
||||
Deployment_yaml = "${WORKSPACE}/lessie-ai/sit/s6/lessie-agents.yaml"
|
||||
Deployment_name = "s6-lessie-agents"
|
||||
K8s_namespace = "sit"
|
||||
Pod_container_name = "lessie-agents"
|
||||
Pod_environment = "s6"
|
||||
}
|
||||
stages {
|
||||
stage('回滚上版') {
|
||||
when { expression { return params.ROLLBACK_VERSION == true } }
|
||||
steps {
|
||||
script {
|
||||
sh """
|
||||
echo "=== 开始回滚到上一个版本 ==="
|
||||
kubectl rollout undo deployment/${Deployment_name} -n ${K8s_namespace}
|
||||
echo "=== 回滚中... ==="
|
||||
kubectl rollout status deployment/${Deployment_name} -n ${K8s_namespace} --timeout=60s
|
||||
echo "=== 查看所使用的镜像 ==="
|
||||
kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}'
|
||||
echo "=== 回滚完成 ==="
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('决定镜像') {
|
||||
steps {
|
||||
script {
|
||||
def imageRegex = /^([a-zA-Z0-9.-]+)(:[0-9]+)?\/([a-zA-Z0-9._-]+)\/([a-zA-Z0-9._-]+):([a-zA-Z0-9._-]+)$/
|
||||
|
||||
if (params.CUSTOM_IMAGE?.trim()) {
|
||||
echo "检测手输镜像格式: ${params.CUSTOM_IMAGE}"
|
||||
def customImage = params.CUSTOM_IMAGE.trim()
|
||||
// 验证镜像格式
|
||||
def matcher = (customImage =~ imageRegex)
|
||||
if (!matcher.matches()) {
|
||||
error "CUSTOM_IMAGE 格式不正确,必须是 registry/namespace/repository:tag 格式\n" +
|
||||
"示例: uswccr.ccs.tencentyun.com/lessiesit/lessie-sourcing-agents:v1.0.0\n" +
|
||||
"当前输入: ${customImage}"
|
||||
}
|
||||
|
||||
echo "使用手输镜像: ${customImage}"
|
||||
env.IMAGE_FULL_NAME = customImage
|
||||
|
||||
} else {
|
||||
def img = "uswccr.ccs.tencentyun.com/${params.IMAGE_NAME}"
|
||||
echo "使用 imageTag 插件镜像: ${img}"
|
||||
env.IMAGE_FULL_NAME = img
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('查询镜像') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
echo "查询镜像: ${IMAGE_FULL_NAME}"
|
||||
|
||||
def result = sh(returnStdout: true, script: """
|
||||
/data/sh/get-image-labels-fast.sh \
|
||||
"${IMAGE_FULL_NAME}" \
|
||||
"100038894437" \
|
||||
'h8H1o6Fd!HLXn' | awk '/^{/,/^}\$/'
|
||||
""").trim()
|
||||
|
||||
if (result == "" || result == null) {
|
||||
error("查询镜像 label 失败,请检查镜像是否存在或凭证问题")
|
||||
}
|
||||
|
||||
echo "镜像 Label:\n${result}"
|
||||
env.IMAGE_LABEL = result
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('拉取yaml') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
git branch: "${params.BRANCH_NAME}",
|
||||
credentialsId: 'fly_gitlab_auth',
|
||||
url: 'http://172.24.16.20/opt/opt-config.git'
|
||||
}
|
||||
}
|
||||
stage('修改YAML') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
def oldImg = sh (
|
||||
script: """
|
||||
kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}' 2>/dev/null || echo "无(可能是首次部署)"
|
||||
""",
|
||||
returnStdout: true
|
||||
).trim()
|
||||
env.OLD_IMAGE_NAME = oldImg
|
||||
echo "--- 目前正常运行的旧镜像: ${OLD_IMAGE_NAME} ---"
|
||||
echo "--- 所选择新的镜像: ${params.IMAGE_NAME} ---"
|
||||
echo "--- 修改 Deployment YAML 中的镜像为新镜像版本 ---"
|
||||
sh """
|
||||
sed -i 's#image:.*#image: ${IMAGE_FULL_NAME}#' ${Deployment_yaml}
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('部署k8s') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
def ANNOTATION = "更新 image 为 ${params.IMAGE_NAME}"
|
||||
sh """
|
||||
echo "===Apply Deployment YAML ==="
|
||||
kubectl apply -f ${Deployment_yaml} -n ${K8s_namespace}
|
||||
|
||||
echo "=== 查看当前新使用的镜像 ==="
|
||||
kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}'
|
||||
|
||||
echo "=== 添加注解 ==="
|
||||
kubectl annotate deployment/${Deployment_name} kubernetes.io/change-cause="${ANNOTATION}" --overwrite -n ${K8s_namespace}
|
||||
|
||||
echo "=== 查看历史版本 ==="
|
||||
kubectl rollout history deployment/${Deployment_name} -n ${K8s_namespace}
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('部署情况') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
echo "=== 检测部署状态并验证新版本运行情况 ==="
|
||||
echo "--- 检查 Deployment 更新状态 ---"
|
||||
def rolloutStatus = sh(
|
||||
script: "kubectl rollout status deployment/${Deployment_name} -n ${K8s_namespace} --timeout=60s",
|
||||
returnStatus: true
|
||||
)
|
||||
|
||||
if (rolloutStatus != 0){
|
||||
echo "Deployment ${Deployment_name} 发布 **超时或失败**,开始排查..."
|
||||
sh """
|
||||
echo "--- Deployment 状态 ---"
|
||||
kubectl describe deployment ${Deployment_name} -n ${K8s_namespace} || true
|
||||
|
||||
echo '--- Pod 列表 ---'
|
||||
kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o wide || true
|
||||
|
||||
echo "--- Pod 描述 (describe) ---"
|
||||
for pod in \$(kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o jsonpath='{.items[*].metadata.name}'); do
|
||||
echo "-- Pod 描述 \$pod --"
|
||||
kubectl describe pod \$pod -n ${K8s_namespace} || true
|
||||
done
|
||||
|
||||
echo "--- 最近 200 行 Pod 日志 ---"
|
||||
for pod in \$(kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o jsonpath='{.items[*].metadata.name}'); do
|
||||
echo "-- logs \$pod --"
|
||||
kubectl logs \$pod -n ${K8s_namespace} --tail=200 || true
|
||||
done
|
||||
"""
|
||||
error("=== Deployment 发布失败,请检查以上输出定位问题 ===")
|
||||
}
|
||||
|
||||
echo "=== 检查 Pods 是否全部 Ready ==="
|
||||
sh "kubectl get pods -l 'app=${Pod_container_name},environment=${Pod_environment}' -n ${K8s_namespace} -o wide"
|
||||
|
||||
echo "=== 获取最新 Pod 名称 ==="
|
||||
NEW_POD = sh (
|
||||
script: "kubectl get pods -l 'app=${Pod_container_name},environment=${Pod_environment}' -n ${K8s_namespace} --sort-by=.metadata.creationTimestamp -o jsonpath='{.items[-1].metadata.name}'",
|
||||
returnStdout: true
|
||||
).trim()
|
||||
|
||||
echo "=== 新 Pod 启动日志(最近20行) ==="
|
||||
sh "kubectl logs ${NEW_POD} -n ${K8s_namespace} --tail=20 || true"
|
||||
|
||||
echo "部署成功:${NEW_POD} 已正常运行"
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('变更说明') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
def user = currentBuild.getBuildCauses()[0].userName ?: "SYSTEM"
|
||||
def now = sh(script: "date '+%Y-%m-%d %H:%M:%S'", returnStdout: true).trim()
|
||||
env.CHANGE_MSG = """
|
||||
jenkins执行人: ${user}
|
||||
修改时间: ${now}
|
||||
旧镜像: ${OLD_IMAGE_NAME}
|
||||
新镜像: ${IMAGE_FULL_NAME}
|
||||
部署对象: ${Deployment_name}
|
||||
镜像标签: ${env.IMAGE_LABEL}
|
||||
""".stripIndent().trim()
|
||||
echo env.CHANGE_MSG
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('提交变更') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
withCredentials([usernamePassword(credentialsId: 'fly_gitlab_auth', usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) {
|
||||
sh """
|
||||
cd ${WORKSPACE}
|
||||
git config user.name "jenkins"
|
||||
git config user.email "jenkins@local"
|
||||
|
||||
# 检查工作树是否有变化
|
||||
if ! git diff --exit-code ${Deployment_yaml} > /dev/null 2>&1; then
|
||||
echo "检测到更改,正在提交..."
|
||||
git add ${Deployment_yaml}
|
||||
git commit -m "更新镜像 \${CHANGE_MSG}"
|
||||
else
|
||||
echo "${Deployment_yaml} 没有变化, 无需commit"
|
||||
fi
|
||||
|
||||
# 检查是否需要推送(是否有新的提交)
|
||||
LOCAL=\$(git rev-parse @)
|
||||
REMOTE=\$(git rev-parse @{u} 2>/dev/null || true)
|
||||
BASE=\$(git merge-base @ @{u} 2>/dev/null || true)
|
||||
|
||||
if [ "\$LOCAL" = "\$REMOTE" ]; then
|
||||
echo "已与远程系统同步更新"
|
||||
elif [ "\$LOCAL" = "\$BASE" ]; then
|
||||
echo "需要从远程获取数据"
|
||||
git pull http://172.24.16.20/opt/opt-config.git ${params.BRANCH_NAME}
|
||||
elif [ "\$REMOTE" = "\$BASE" ]; then
|
||||
echo "将更改推送到远程服务器..."
|
||||
# 生成临时 .netrc 文件
|
||||
echo "machine 172.24.16.20 login \$GIT_USER password \$GIT_PASS" > ~/.netrc
|
||||
chmod 600 ~/.netrc
|
||||
git push http://172.24.16.20/opt/opt-config.git ${params.BRANCH_NAME}
|
||||
else
|
||||
echo "与远程模式不同,跳过推送操作"
|
||||
fi
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
post {
|
||||
success {
|
||||
echo "成功!"
|
||||
echo "=== 所选择镜像: ${params.IMAGE_NAME} ==="
|
||||
}
|
||||
failure {
|
||||
echo "有步骤失败,请检查!"
|
||||
}
|
||||
}
|
||||
}
|
||||
277
SCM/部署镜像/s6/DM_s6_lessie_ai_web.groovy
Normal file
277
SCM/部署镜像/s6/DM_s6_lessie_ai_web.groovy
Normal file
@@ -0,0 +1,277 @@
|
||||
pipeline {
|
||||
agent any
|
||||
|
||||
parameters {
|
||||
imageTag(
|
||||
name: 'IMAGE_NAME',
|
||||
description: 'sit 仓库镜像 (除非输入 CUSTOM_IMAGE, 否则使用这里的)',
|
||||
registry: 'https://uswccr.ccs.tencentyun.com',
|
||||
image: 'lessiesit/lessie-ai-web',
|
||||
credentialId: 'dxin_img_hub_auth',
|
||||
filter: '.*',
|
||||
defaultTag: '',
|
||||
verifySsl: true
|
||||
)
|
||||
string(
|
||||
name: 'CUSTOM_IMAGE',
|
||||
defaultValue: '',
|
||||
description: '手输完整镜像<registry>/<namespace>/<repo>:<tag>,例如: uswccr.ccs.tencentyun.com/lessiesit/lessie-ai-web:v0.0.1 (填充后,则忽略镜像仓库选择)'
|
||||
)
|
||||
booleanParam(
|
||||
name: 'ROLLBACK_VERSION',
|
||||
defaultValue: false,
|
||||
description: '是否快速回滚上一个版本?'
|
||||
)
|
||||
choice(
|
||||
name: 'BRANCH_NAME',
|
||||
choices: ['dxin', 'opt'],
|
||||
description: '选择资源清单Yaml的分支'
|
||||
)
|
||||
}
|
||||
environment {
|
||||
KUBECONFIG = credentials('k8s-test-config-admin') // k8s 凭证 ID, Jenkins 中配置的凭证名称
|
||||
Deployment_yaml = "${WORKSPACE}/lessie-ai/sit/s1/lessie-ai-web.yaml"
|
||||
Deployment_name = "s1-lessie-ai-web"
|
||||
K8s_namespace = "sit"
|
||||
Pod_container_name = "lessie-ai-web"
|
||||
Pod_environment = "s6"
|
||||
}
|
||||
stages {
|
||||
stage('回滚上版') {
|
||||
when { expression { return params.ROLLBACK_VERSION == true } }
|
||||
steps {
|
||||
script {
|
||||
sh """
|
||||
echo "=== 开始回滚到上一个版本 ==="
|
||||
kubectl rollout undo deployment/${Deployment_name} -n ${K8s_namespace}
|
||||
echo "=== 回滚中... ==="
|
||||
kubectl rollout status deployment/${Deployment_name} -n ${K8s_namespace} --timeout=60s
|
||||
echo "=== 查看所使用的镜像 ==="
|
||||
kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}'
|
||||
echo "=== 回滚完成 ==="
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('决定镜像') {
|
||||
steps {
|
||||
script {
|
||||
def imageRegex = /^([a-zA-Z0-9.-]+)(:[0-9]+)?\/([a-zA-Z0-9._-]+)\/([a-zA-Z0-9._-]+):([a-zA-Z0-9._-]+)$/
|
||||
|
||||
if (params.CUSTOM_IMAGE?.trim()) {
|
||||
echo "检测手输镜像格式: ${params.CUSTOM_IMAGE}"
|
||||
def customImage = params.CUSTOM_IMAGE.trim()
|
||||
// 验证镜像格式
|
||||
def matcher = (customImage =~ imageRegex)
|
||||
if (!matcher.matches()) {
|
||||
error "CUSTOM_IMAGE 格式不正确,必须是 registry/namespace/repository:tag 格式\n" +
|
||||
"示例: uswccr.ccs.tencentyun.com/lessie-ai-web:v1.0.0\n" +
|
||||
"当前输入: ${customImage}"
|
||||
}
|
||||
|
||||
echo "使用手输镜像: ${customImage}"
|
||||
env.IMAGE_FULL_NAME = customImage
|
||||
|
||||
} else {
|
||||
def img = "uswccr.ccs.tencentyun.com/${params.IMAGE_NAME}"
|
||||
echo "使用 imageTag 插件镜像: ${img}"
|
||||
env.IMAGE_FULL_NAME = img
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('查询镜像') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
echo "查询镜像: ${IMAGE_FULL_NAME}"
|
||||
|
||||
def result = sh(returnStdout: true, script: """
|
||||
/data/sh/get-image-labels-fast.sh \
|
||||
"${IMAGE_FULL_NAME}" \
|
||||
"100038894437" \
|
||||
'h8H1o6Fd!HLXn' | awk '/^{/,/^}\$/'
|
||||
""").trim()
|
||||
|
||||
if (result == "" || result == null) {
|
||||
error("查询镜像 label 失败,请检查镜像是否存在或凭证问题")
|
||||
}
|
||||
|
||||
echo "镜像 Label:\n${result}"
|
||||
env.IMAGE_LABEL = result
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('拉取yaml') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
git branch: "${params.BRANCH_NAME}",
|
||||
credentialsId: 'fly_gitlab_auth',
|
||||
url: 'http://172.24.16.20/opt/opt-config.git'
|
||||
}
|
||||
}
|
||||
stage('修改YAML') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
def oldImg = sh (
|
||||
script: """
|
||||
kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}' 2>/dev/null || echo "无(可能是首次部署)"
|
||||
""",
|
||||
returnStdout: true
|
||||
).trim()
|
||||
env.OLD_IMAGE_NAME = oldImg
|
||||
echo "--- 目前正常运行的旧镜像: ${OLD_IMAGE_NAME} ---"
|
||||
echo "--- 所选择新的镜像: ${params.IMAGE_NAME} ---"
|
||||
echo "--- 修改 Deployment YAML 中的镜像为新镜像版本 ---"
|
||||
sh """
|
||||
sed -i 's#image:.*#image: ${IMAGE_FULL_NAME}#' ${Deployment_yaml}
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('部署k8s') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
def ANNOTATION = "更新 image 为 ${params.IMAGE_NAME}"
|
||||
sh """
|
||||
echo "===Apply Deployment YAML ==="
|
||||
kubectl apply -f ${Deployment_yaml} -n ${K8s_namespace}
|
||||
|
||||
echo "=== 查看当前新使用的镜像 ==="
|
||||
kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}'
|
||||
|
||||
echo "=== 添加注解 ==="
|
||||
kubectl annotate deployment/${Deployment_name} kubernetes.io/change-cause="${ANNOTATION}" --overwrite -n ${K8s_namespace}
|
||||
|
||||
echo "=== 查看历史版本 ==="
|
||||
kubectl rollout history deployment/${Deployment_name} -n ${K8s_namespace}
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('部署情况') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
echo "=== 检测部署状态并验证新版本运行情况 ==="
|
||||
echo "--- 检查 Deployment 更新状态 ---"
|
||||
def rolloutStatus = sh(
|
||||
script: "kubectl rollout status deployment/${Deployment_name} -n ${K8s_namespace} --timeout=60s",
|
||||
returnStatus: true
|
||||
)
|
||||
|
||||
if (rolloutStatus != 0){
|
||||
echo "Deployment ${Deployment_name} 发布 **超时或失败**,开始排查..."
|
||||
sh """
|
||||
echo "--- Deployment 状态 ---"
|
||||
kubectl describe deployment ${Deployment_name} -n ${K8s_namespace} || true
|
||||
|
||||
echo '--- Pod 列表 ---'
|
||||
kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o wide || true
|
||||
|
||||
echo "--- Pod 描述 (describe) ---"
|
||||
for pod in \$(kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o jsonpath='{.items[*].metadata.name}'); do
|
||||
echo "-- Pod 描述 \$pod --"
|
||||
kubectl describe pod \$pod -n ${K8s_namespace} || true
|
||||
done
|
||||
|
||||
echo "--- 最近 200 行 Pod 日志 ---"
|
||||
for pod in \$(kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o jsonpath='{.items[*].metadata.name}'); do
|
||||
echo "-- logs \$pod --"
|
||||
kubectl logs \$pod -n ${K8s_namespace} --tail=200 || true
|
||||
done
|
||||
"""
|
||||
error("=== Deployment 发布失败,请检查以上输出定位问题 ===")
|
||||
}
|
||||
|
||||
echo "=== 检查 Pods 是否全部 Ready ==="
|
||||
sh "kubectl get pods -l 'app=${Pod_container_name},environment=${Pod_environment}' -n ${K8s_namespace} -o wide"
|
||||
|
||||
echo "=== 获取最新 Pod 名称 ==="
|
||||
NEW_POD = sh (
|
||||
script: "kubectl get pods -l 'app=${Pod_container_name},environment=${Pod_environment}' -n ${K8s_namespace} --sort-by=.metadata.creationTimestamp -o jsonpath='{.items[-1].metadata.name}'",
|
||||
returnStdout: true
|
||||
).trim()
|
||||
|
||||
echo "=== 新 Pod 启动日志(最近20行) ==="
|
||||
sh "kubectl logs ${NEW_POD} -n ${K8s_namespace} --tail=20 || true"
|
||||
|
||||
echo "部署成功:${NEW_POD} 已正常运行"
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('变更说明') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
def user = currentBuild.getBuildCauses()[0].userName ?: "SYSTEM"
|
||||
def now = sh(script: "date '+%Y-%m-%d %H:%M:%S'", returnStdout: true).trim()
|
||||
env.CHANGE_MSG = """
|
||||
jenkins执行人: ${user}
|
||||
修改时间: ${now}
|
||||
旧镜像: ${OLD_IMAGE_NAME}
|
||||
新镜像: ${IMAGE_FULL_NAME}
|
||||
部署对象: ${Deployment_name}
|
||||
镜像标签: ${env.IMAGE_LABEL}
|
||||
""".stripIndent().trim()
|
||||
echo env.CHANGE_MSG
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('提交变更') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
withCredentials([usernamePassword(credentialsId: 'fly_gitlab_auth', usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) {
|
||||
sh """
|
||||
cd ${WORKSPACE}
|
||||
git config user.name "jenkins"
|
||||
git config user.email "jenkins@local"
|
||||
|
||||
# 检查工作树是否有变化
|
||||
if ! git diff --exit-code ${Deployment_yaml} > /dev/null 2>&1; then
|
||||
echo "检测到更改,正在提交..."
|
||||
git add ${Deployment_yaml}
|
||||
git commit -m "更新镜像 \${CHANGE_MSG}"
|
||||
else
|
||||
echo "${Deployment_yaml} 没有变化, 无需commit"
|
||||
fi
|
||||
|
||||
# 检查是否需要推送(是否有新的提交)
|
||||
LOCAL=\$(git rev-parse @)
|
||||
REMOTE=\$(git rev-parse @{u} 2>/dev/null || true)
|
||||
BASE=\$(git merge-base @ @{u} 2>/dev/null || true)
|
||||
|
||||
if [ "\$LOCAL" = "\$REMOTE" ]; then
|
||||
echo "已与远程系统同步更新"
|
||||
elif [ "\$LOCAL" = "\$BASE" ]; then
|
||||
echo "需要从远程获取数据"
|
||||
git pull http://172.24.16.20/opt/opt-config.git ${params.BRANCH_NAME}
|
||||
elif [ "\$REMOTE" = "\$BASE" ]; then
|
||||
echo "将更改推送到远程服务器..."
|
||||
# 生成临时 .netrc 文件
|
||||
echo "machine 172.24.16.20 login \$GIT_USER password \$GIT_PASS" > ~/.netrc
|
||||
chmod 600 ~/.netrc
|
||||
git push http://172.24.16.20/opt/opt-config.git ${params.BRANCH_NAME}
|
||||
else
|
||||
echo "与远程模式不同,跳过推送操作"
|
||||
fi
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
post {
|
||||
success {
|
||||
echo "成功!"
|
||||
echo "=== 所选择镜像: ${params.IMAGE_NAME} ==="
|
||||
}
|
||||
failure {
|
||||
echo "有步骤失败,请检查!"
|
||||
}
|
||||
}
|
||||
}
|
||||
277
SCM/部署镜像/s6/DM_s6_lessie_go_api.groovy
Normal file
277
SCM/部署镜像/s6/DM_s6_lessie_go_api.groovy
Normal file
@@ -0,0 +1,277 @@
|
||||
pipeline {
|
||||
agent any
|
||||
|
||||
parameters {
|
||||
imageTag(
|
||||
name: 'IMAGE_NAME',
|
||||
description: 'sit 仓库镜像 (除非输入 CUSTOM_IMAGE, 否则使用这里的)',
|
||||
registry: 'https://uswccr.ccs.tencentyun.com',
|
||||
image: 'lessiesit/go_lessie-sourcing-api',
|
||||
credentialId: 'dxin_img_hub_auth',
|
||||
filter: '.*',
|
||||
defaultTag: '',
|
||||
verifySsl: true
|
||||
)
|
||||
string(
|
||||
name: 'CUSTOM_IMAGE',
|
||||
defaultValue: '',
|
||||
description: '手输完整镜像<registry>/<namespace>/<repo>:<tag>,例如: uswccr.ccs.tencentyun.com/lessiesit/go_lessie-sourcing-api:v0.0.1 (填充后,则忽略镜像仓库选择)'
|
||||
)
|
||||
booleanParam(
|
||||
name: 'ROLLBACK_VERSION',
|
||||
defaultValue: false,
|
||||
description: '是否快速回滚上一个版本?'
|
||||
)
|
||||
choice(
|
||||
name: 'BRANCH_NAME',
|
||||
choices: ['dxin', 'opt'],
|
||||
description: '选择资源清单Yaml的分支'
|
||||
)
|
||||
}
|
||||
environment {
|
||||
KUBECONFIG = credentials('k8s-test-config-admin') // k8s 凭证 ID, Jenkins 中配置的凭证名称
|
||||
Deployment_yaml = "${WORKSPACE}/lessie-ai/sit/s1/lessie-go-api.yaml"
|
||||
Deployment_name = "s1-lessie-go-api"
|
||||
K8s_namespace = "sit"
|
||||
Pod_container_name = "lessie-go-api"
|
||||
Pod_environment = "s6"
|
||||
}
|
||||
stages {
|
||||
stage('回滚上版') {
|
||||
when { expression { return params.ROLLBACK_VERSION == true } }
|
||||
steps {
|
||||
script {
|
||||
sh """
|
||||
echo "=== 开始回滚到上一个版本 ==="
|
||||
kubectl rollout undo deployment/${Deployment_name} -n ${K8s_namespace}
|
||||
echo "=== 回滚中... ==="
|
||||
kubectl rollout status deployment/${Deployment_name} -n ${K8s_namespace} --timeout=60s
|
||||
echo "=== 查看所使用的镜像 ==="
|
||||
kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}'
|
||||
echo "=== 回滚完成 ==="
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('决定镜像') {
|
||||
steps {
|
||||
script {
|
||||
def imageRegex = /^([a-zA-Z0-9.-]+)(:[0-9]+)?\/([a-zA-Z0-9._-]+)\/([a-zA-Z0-9._-]+):([a-zA-Z0-9._-]+)$/
|
||||
|
||||
if (params.CUSTOM_IMAGE?.trim()) {
|
||||
echo "检测手输镜像格式: ${params.CUSTOM_IMAGE}"
|
||||
def customImage = params.CUSTOM_IMAGE.trim()
|
||||
// 验证镜像格式
|
||||
def matcher = (customImage =~ imageRegex)
|
||||
if (!matcher.matches()) {
|
||||
error "CUSTOM_IMAGE 格式不正确,必须是 registry/namespace/repository:tag 格式\n" +
|
||||
"示例: uswccr.ccs.tencentyun.com/go_lessie-sourcing-api:v1.0.0\n" +
|
||||
"当前输入: ${customImage}"
|
||||
}
|
||||
|
||||
echo "使用手输镜像: ${customImage}"
|
||||
env.IMAGE_FULL_NAME = customImage
|
||||
|
||||
} else {
|
||||
def img = "uswccr.ccs.tencentyun.com/${params.IMAGE_NAME}"
|
||||
echo "使用 imageTag 插件镜像: ${img}"
|
||||
env.IMAGE_FULL_NAME = img
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('查询镜像') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
echo "查询镜像: ${IMAGE_FULL_NAME}"
|
||||
|
||||
def result = sh(returnStdout: true, script: """
|
||||
/data/sh/get-image-labels-fast.sh \
|
||||
"${IMAGE_FULL_NAME}" \
|
||||
"100038894437" \
|
||||
'h8H1o6Fd!HLXn' | awk '/^{/,/^}\$/'
|
||||
""").trim()
|
||||
|
||||
if (result == "" || result == null) {
|
||||
error("查询镜像 label 失败,请检查镜像是否存在或凭证问题")
|
||||
}
|
||||
|
||||
echo "镜像 Label:\n${result}"
|
||||
env.IMAGE_LABEL = result
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('拉取yaml') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
git branch: "${params.BRANCH_NAME}",
|
||||
credentialsId: 'fly_gitlab_auth',
|
||||
url: 'http://172.24.16.20/opt/opt-config.git'
|
||||
}
|
||||
}
|
||||
stage('修改YAML') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
def oldImg = sh (
|
||||
script: """
|
||||
kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}' 2>/dev/null || echo "无(可能是首次部署)"
|
||||
""",
|
||||
returnStdout: true
|
||||
).trim()
|
||||
env.OLD_IMAGE_NAME = oldImg
|
||||
echo "--- 目前正常运行的旧镜像: ${OLD_IMAGE_NAME} ---"
|
||||
echo "--- 所选择新的镜像: ${params.IMAGE_NAME} ---"
|
||||
echo "--- 修改 Deployment YAML 中的镜像为新镜像版本 ---"
|
||||
sh """
|
||||
sed -i 's#image:.*#image: ${IMAGE_FULL_NAME}#' ${Deployment_yaml}
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('部署k8s') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
def ANNOTATION = "更新 image 为 ${params.IMAGE_NAME}"
|
||||
sh """
|
||||
echo "===Apply Deployment YAML ==="
|
||||
kubectl apply -f ${Deployment_yaml} -n ${K8s_namespace}
|
||||
|
||||
echo "=== 查看当前新使用的镜像 ==="
|
||||
kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}'
|
||||
|
||||
echo "=== 添加注解 ==="
|
||||
kubectl annotate deployment/${Deployment_name} kubernetes.io/change-cause="${ANNOTATION}" --overwrite -n ${K8s_namespace}
|
||||
|
||||
echo "=== 查看历史版本 ==="
|
||||
kubectl rollout history deployment/${Deployment_name} -n ${K8s_namespace}
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('部署情况') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
echo "=== 检测部署状态并验证新版本运行情况 ==="
|
||||
echo "--- 检查 Deployment 更新状态 ---"
|
||||
def rolloutStatus = sh(
|
||||
script: "kubectl rollout status deployment/${Deployment_name} -n ${K8s_namespace} --timeout=60s",
|
||||
returnStatus: true
|
||||
)
|
||||
|
||||
if (rolloutStatus != 0){
|
||||
echo "Deployment ${Deployment_name} 发布 **超时或失败**,开始排查..."
|
||||
sh """
|
||||
echo "--- Deployment 状态 ---"
|
||||
kubectl describe deployment ${Deployment_name} -n ${K8s_namespace} || true
|
||||
|
||||
echo '--- Pod 列表 ---'
|
||||
kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o wide || true
|
||||
|
||||
echo "--- Pod 描述 (describe) ---"
|
||||
for pod in \$(kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o jsonpath='{.items[*].metadata.name}'); do
|
||||
echo "-- Pod 描述 \$pod --"
|
||||
kubectl describe pod \$pod -n ${K8s_namespace} || true
|
||||
done
|
||||
|
||||
echo "--- 最近 200 行 Pod 日志 ---"
|
||||
for pod in \$(kubectl get pods -n ${K8s_namespace} -l "app=${Pod_container_name},environment=${Pod_environment}" -o jsonpath='{.items[*].metadata.name}'); do
|
||||
echo "-- logs \$pod --"
|
||||
kubectl logs \$pod -n ${K8s_namespace} --tail=200 || true
|
||||
done
|
||||
"""
|
||||
error("=== Deployment 发布失败,请检查以上输出定位问题 ===")
|
||||
}
|
||||
|
||||
echo "=== 检查 Pods 是否全部 Ready ==="
|
||||
sh "kubectl get pods -l 'app=${Pod_container_name},environment=${Pod_environment}' -n ${K8s_namespace} -o wide"
|
||||
|
||||
echo "=== 获取最新 Pod 名称 ==="
|
||||
NEW_POD = sh (
|
||||
script: "kubectl get pods -l 'app=${Pod_container_name},environment=${Pod_environment}' -n ${K8s_namespace} --sort-by=.metadata.creationTimestamp -o jsonpath='{.items[-1].metadata.name}'",
|
||||
returnStdout: true
|
||||
).trim()
|
||||
|
||||
echo "=== 新 Pod 启动日志(最近20行) ==="
|
||||
sh "kubectl logs ${NEW_POD} -n ${K8s_namespace} --tail=20 || true"
|
||||
|
||||
echo "部署成功:${NEW_POD} 已正常运行"
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('变更说明') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
def user = currentBuild.getBuildCauses()[0].userName ?: "SYSTEM"
|
||||
def now = sh(script: "date '+%Y-%m-%d %H:%M:%S'", returnStdout: true).trim()
|
||||
env.CHANGE_MSG = """
|
||||
jenkins执行人: ${user}
|
||||
修改时间: ${now}
|
||||
旧镜像: ${OLD_IMAGE_NAME}
|
||||
新镜像: ${IMAGE_FULL_NAME}
|
||||
部署对象: ${Deployment_name}
|
||||
镜像标签: ${env.IMAGE_LABEL}
|
||||
""".stripIndent().trim()
|
||||
echo env.CHANGE_MSG
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('提交变更') {
|
||||
when { expression { return params.ROLLBACK_VERSION == false } }
|
||||
steps {
|
||||
script {
|
||||
withCredentials([usernamePassword(credentialsId: 'fly_gitlab_auth', usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) {
|
||||
sh """
|
||||
cd ${WORKSPACE}
|
||||
git config user.name "jenkins"
|
||||
git config user.email "jenkins@local"
|
||||
|
||||
# 检查工作树是否有变化
|
||||
if ! git diff --exit-code ${Deployment_yaml} > /dev/null 2>&1; then
|
||||
echo "检测到更改,正在提交..."
|
||||
git add ${Deployment_yaml}
|
||||
git commit -m "更新镜像 \${CHANGE_MSG}"
|
||||
else
|
||||
echo "${Deployment_yaml} 没有变化, 无需commit"
|
||||
fi
|
||||
|
||||
# 检查是否需要推送(是否有新的提交)
|
||||
LOCAL=\$(git rev-parse @)
|
||||
REMOTE=\$(git rev-parse @{u} 2>/dev/null || true)
|
||||
BASE=\$(git merge-base @ @{u} 2>/dev/null || true)
|
||||
|
||||
if [ "\$LOCAL" = "\$REMOTE" ]; then
|
||||
echo "已与远程系统同步更新"
|
||||
elif [ "\$LOCAL" = "\$BASE" ]; then
|
||||
echo "需要从远程获取数据"
|
||||
git pull http://172.24.16.20/opt/opt-config.git ${params.BRANCH_NAME}
|
||||
elif [ "\$REMOTE" = "\$BASE" ]; then
|
||||
echo "将更改推送到远程服务器..."
|
||||
# 生成临时 .netrc 文件
|
||||
echo "machine 172.24.16.20 login \$GIT_USER password \$GIT_PASS" > ~/.netrc
|
||||
chmod 600 ~/.netrc
|
||||
git push http://172.24.16.20/opt/opt-config.git ${params.BRANCH_NAME}
|
||||
else
|
||||
echo "与远程模式不同,跳过推送操作"
|
||||
fi
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
post {
|
||||
success {
|
||||
echo "成功!"
|
||||
echo "=== 所选择镜像: ${params.IMAGE_NAME} ==="
|
||||
}
|
||||
failure {
|
||||
echo "有步骤失败,请检查!"
|
||||
}
|
||||
}
|
||||
}
|
||||
104
k8s_yaml/elasticsearch-9.2.2/单节点/es-single.yaml
Normal file
104
k8s_yaml/elasticsearch-9.2.2/单节点/es-single.yaml
Normal file
@@ -0,0 +1,104 @@
|
||||
---
|
||||
# Headless Service(StatefulSet 必需,哪怕单节点,无头)
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: elasticsearch
|
||||
namespace: es-test
|
||||
spec:
|
||||
clusterIP: None
|
||||
selector:
|
||||
app: elasticsearch
|
||||
ports:
|
||||
- port: 9200
|
||||
name: http
|
||||
- port: 9300
|
||||
name: transport
|
||||
|
||||
---
|
||||
# 单节点 StatefulSet (有状态工作负载)
|
||||
apiVersion: apps/v1
|
||||
kind: StatefulSet
|
||||
metadata:
|
||||
name: elasticsearch
|
||||
namespace: es-test
|
||||
spec:
|
||||
serviceName: "elasticsearch"
|
||||
replicas: 1 # 👈 单节点!
|
||||
selector:
|
||||
matchLabels:
|
||||
app: elasticsearch
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: elasticsearch
|
||||
spec:
|
||||
# ✅ 关键:initContainer 修复内核参数(腾讯云默认可能不足)
|
||||
initContainers:
|
||||
- name: increase-vm-max-map
|
||||
image: busybox
|
||||
command: ["sysctl", "-w", "vm.max_map_count=262144"]
|
||||
securityContext:
|
||||
privileged: true
|
||||
|
||||
containers:
|
||||
- name: elasticsearch
|
||||
image: docker.elastic.co/elasticsearch/elasticsearch:9.2.2
|
||||
env:
|
||||
- name: discovery.type
|
||||
value: "single-node" # ⚠️ 必须!否则 ES 9 会报错退出
|
||||
- name: cluster.name
|
||||
value: "test-es-cluster"
|
||||
- name: node.name
|
||||
value: "es-test-node"
|
||||
- name: ES_JAVA_OPTS
|
||||
value: "-Xms1g -Xmx1g" # 测试环境 1G 足够
|
||||
ports:
|
||||
- containerPort: 9200
|
||||
name: http
|
||||
- containerPort: 9300
|
||||
name: transport
|
||||
volumeMounts:
|
||||
- name: data
|
||||
mountPath: /usr/share/elasticsearch/data
|
||||
|
||||
# 探针(可选,加快感知)
|
||||
readinessProbe:
|
||||
httpGet:
|
||||
path: /_cluster/health
|
||||
port: 9200
|
||||
initialDelaySeconds: 20
|
||||
periodSeconds: 10
|
||||
livenessProbe:
|
||||
httpGet:
|
||||
path: /_cluster/health
|
||||
port: 9200
|
||||
initialDelaySeconds: 60
|
||||
periodSeconds: 30
|
||||
|
||||
# ✅ ES 9 默认非 root 运行,必须设置
|
||||
securityContext:
|
||||
runAsUser: 1000
|
||||
runAsGroup: 1000
|
||||
fsGroup: 1000
|
||||
|
||||
# 腾讯云 CBS 需要 RWO,单节点没问题
|
||||
volumes:
|
||||
- name: data
|
||||
persistentVolumeClaim:
|
||||
claimName: es-data-pvc
|
||||
|
||||
---
|
||||
# 单独创建 PVC(StatefulSet 用 volumeClaimTemplates 更规范,但单节点直接 PVC 更直观)
|
||||
apiVersion: v1
|
||||
kind: PersistentVolumeClaim
|
||||
metadata:
|
||||
name: es-data-pvc
|
||||
namespace: es-test
|
||||
spec:
|
||||
accessModes:
|
||||
- ReadWriteOnce
|
||||
storageClassName: cbs # 👈 腾讯云默认存储类(如用高性能 SSD 改为 cbs-premium)
|
||||
resources:
|
||||
requests:
|
||||
storage: 20Gi # 测试够用,可调小如 10Gi
|
||||
14
k8s_yaml/elasticsearch-9.2.2/单节点/步骤.txt
Normal file
14
k8s_yaml/elasticsearch-9.2.2/单节点/步骤.txt
Normal file
@@ -0,0 +1,14 @@
|
||||
步骤 1:创建 Namespace
|
||||
kubectl create ns es-test
|
||||
|
||||
步骤 2:创建 Elasticsearch 集群
|
||||
kubectl apply -f es-cluster.yaml -n es-test
|
||||
|
||||
步骤 3:创建 Kibana
|
||||
kubectl apply -f kibana.yaml -n es-test
|
||||
|
||||
步骤 4:创建 Logstash
|
||||
kubectl apply -f logstash.yaml -n es-test
|
||||
|
||||
步骤 5:创建 Filebeat
|
||||
kubectl apply -f filebeat.yaml
|
||||
97
k8s_yaml/elasticsearch-9.2.2/高可用/es-sts.yaml
Normal file
97
k8s_yaml/elasticsearch-9.2.2/高可用/es-sts.yaml
Normal file
@@ -0,0 +1,97 @@
|
||||
# es-sts.yaml
|
||||
apiVersion: apps/v1
|
||||
kind: StatefulSet
|
||||
metadata:
|
||||
name: elasticsearch
|
||||
namespace: elastic-system
|
||||
spec:
|
||||
serviceName: "elasticsearch" # 必须与 Headless Service 名一致
|
||||
replicas: 3 # 生产环境至少3节点!
|
||||
selector:
|
||||
matchLabels:
|
||||
app: elasticsearch
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: elasticsearch
|
||||
spec:
|
||||
# ⚠️ 关键:避免多个ES Pod调度到同一Node(防单点故障)
|
||||
affinity:
|
||||
podAntiAffinity:
|
||||
requiredDuringSchedulingIgnoredDuringExecution:
|
||||
- labelSelector:
|
||||
matchExpressions:
|
||||
- key: app
|
||||
operator: In
|
||||
values: ["elasticsearch"]
|
||||
topologyKey: kubernetes.io/hostname
|
||||
|
||||
initContainers:
|
||||
# 必须:调整 OS 内核参数(ES 要求)
|
||||
- name: init-sysctl
|
||||
image: busybox:1.35
|
||||
securityContext:
|
||||
privileged: true
|
||||
command:
|
||||
- sysctl
|
||||
- -w
|
||||
- vm.max_map_count=262144
|
||||
- sysctl
|
||||
- -w
|
||||
- fs.file-max=65536
|
||||
|
||||
containers:
|
||||
- name: elasticsearch
|
||||
image: docker.elastic.co/elasticsearch/elasticsearch:9.2.2
|
||||
env:
|
||||
- name: cluster.name
|
||||
value: "k8s-es-cluster"
|
||||
- name: node.name
|
||||
valueFrom:
|
||||
fieldRef:
|
||||
fieldPath: metadata.name # → es-0, es-1, es-2
|
||||
- name: discovery.seed_hosts
|
||||
value: "elasticsearch-0.elasticsearch,elasticsearch-1.elasticsearch,elasticsearch-2.elasticsearch"
|
||||
- name: cluster.initial_master_nodes
|
||||
value: "elasticsearch-0,elasticsearch-1,elasticsearch-2"
|
||||
- name: ES_JAVA_OPTS
|
||||
value: "-Xms2g -Xmx2g"
|
||||
- name: network.host
|
||||
value: "0.0.0.0"
|
||||
ports:
|
||||
- containerPort: 9200
|
||||
name: http
|
||||
- containerPort: 9300
|
||||
name: transport
|
||||
volumeMounts:
|
||||
- name: data
|
||||
mountPath: /usr/share/elasticsearch/data
|
||||
readinessProbe:
|
||||
httpGet:
|
||||
path: /_cluster/health
|
||||
port: 9200
|
||||
initialDelaySeconds: 30
|
||||
periodSeconds: 10
|
||||
livenessProbe:
|
||||
httpGet:
|
||||
path: /_cluster/health
|
||||
port: 9200
|
||||
initialDelaySeconds: 60
|
||||
periodSeconds: 30
|
||||
|
||||
# ⚠️ 必须设置 securityContext(ES 9+ 默认以非 root 启动)
|
||||
securityContext:
|
||||
fsGroup: 1000
|
||||
runAsUser: 1000
|
||||
runAsNonRoot: true
|
||||
|
||||
# ⚠️ 关键:每个副本独享 PVC → 自动创建3个 PV(各绑定一块 CBS)
|
||||
volumeClaimTemplates:
|
||||
- metadata:
|
||||
name: data
|
||||
spec:
|
||||
accessModes: [ "ReadWriteOnce" ] # 腾讯云 CBS 支持 RWO
|
||||
storageClassName: cbs # 或 cbs-premium(高性能 SSD)
|
||||
resources:
|
||||
requests:
|
||||
storage: 100Gi # 按需调整
|
||||
16
k8s_yaml/elasticsearch-9.2.2/高可用/es-svc.yaml
Normal file
16
k8s_yaml/elasticsearch-9.2.2/高可用/es-svc.yaml
Normal file
@@ -0,0 +1,16 @@
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: elasticsearch
|
||||
namespace: elastic-system
|
||||
labels:
|
||||
app: elasticsearch
|
||||
spec:
|
||||
clusterIP: None # Headless Service!关键!
|
||||
selector:
|
||||
app: elasticsearch
|
||||
ports:
|
||||
- port: 9200
|
||||
name: http
|
||||
- port: 9300
|
||||
name: transport
|
||||
10
k8s_yaml/elasticsearch-9.2.2/高可用/步骤.md
Normal file
10
k8s_yaml/elasticsearch-9.2.2/高可用/步骤.md
Normal file
@@ -0,0 +1,10 @@
|
||||
## 步骤 1:创建 Namespace(可选但推荐)
|
||||
kubectl create ns elastic-system
|
||||
|
||||
# 步骤 2:创建 Headless Service(StatefulSet 必需)
|
||||
# es-svc.yaml
|
||||
# Headless Service 为每个 Pod 提供稳定 DNS(es-0.elasticsearch.elastic-system.svc.cluster.local)
|
||||
|
||||
|
||||
# 步骤 3:创建 StatefulSet(3 节点,3 个独立 PV)
|
||||
# es-sts.yaml
|
||||
Reference in New Issue
Block a user