更改构建镜像流水线为v2
This commit is contained in:
@@ -40,7 +40,7 @@ pipeline {
|
||||
steps {
|
||||
git branch: "${params.Code_branch}",
|
||||
credentialsId: 'fly_gitlab_auth',
|
||||
url: 'http://106.53.194.199/root/fly_moon_admin.git'
|
||||
url: 'http://172.24.16.20/root/fly_moon_admin.git'
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
327
SCM/构建镜像/build_image_flymoon_admin_v2.groovy
Normal file
327
SCM/构建镜像/build_image_flymoon_admin_v2.groovy
Normal file
@@ -0,0 +1,327 @@
|
||||
// --- 辅助函数:深拷贝对象以确保可序列化 ---
|
||||
def deepCopyForSerialization(obj) {
|
||||
if (obj instanceof Map) {
|
||||
// 创建新的 LinkedHashMap,递归拷贝值
|
||||
return obj.collectEntries { k, v -> [(k): deepCopyForSerialization(v)] }
|
||||
} else if (obj instanceof List) {
|
||||
// 创建新的 ArrayList,递归拷贝元素
|
||||
return obj.collect { item -> deepCopyForSerialization(item) }
|
||||
} else if (obj instanceof String || obj instanceof Number || obj instanceof Boolean || obj == null) {
|
||||
return obj
|
||||
} else {
|
||||
return obj.toString()
|
||||
}
|
||||
}
|
||||
// --- 结束辅助函数 ---
|
||||
|
||||
pipeline {
|
||||
agent any
|
||||
tools{
|
||||
maven 'mvn3.8.8'
|
||||
jdk 'jdk21'
|
||||
}
|
||||
parameters {
|
||||
gitParameter(
|
||||
branchFilter: 'origin/(.*)',
|
||||
defaultValue: 'dxin',
|
||||
name: 'Code_branch',
|
||||
type: 'PT_BRANCH_TAG',
|
||||
selectedValue: 'DEFAULT',
|
||||
sortMode: 'NONE',
|
||||
description: '选择代码分支: ',
|
||||
quickFilterEnabled: true,
|
||||
tagFilter: '*',
|
||||
listSize: "1"
|
||||
)
|
||||
choice(
|
||||
name: 'NAME_SPACES',
|
||||
choices: ['sit', 'test', 'prod'],
|
||||
description: '选择存放镜像的仓库命名空间:'
|
||||
)
|
||||
string(
|
||||
name: 'CUSTOM_TAG',
|
||||
defaultValue: '',
|
||||
description: '可选:自定义镜像 Tag (字母、数字、点、下划线、短横线), 留空则自动生成 “ v+构建次数_分支名_短哈希_构建时间 ”'
|
||||
)
|
||||
}
|
||||
environment {
|
||||
REGISTRY = "uswccr.ccs.tencentyun.com" // 镜像仓库地址
|
||||
NAMESPACE = "lessie${params.NAME_SPACES}" // 命名空间根据choices的选择拼接
|
||||
IMAGE_NAME = "flymoon-admin" // 镜像名(固定前缀)
|
||||
CREDENTIALS_ID = "dxin_img_hub_auth" // 容器仓库凭证ID
|
||||
}
|
||||
|
||||
stages {
|
||||
stage('拉取代码') {
|
||||
steps {
|
||||
git branch: "${params.Code_branch}",
|
||||
credentialsId: 'fly_gitlab_auth',
|
||||
url: 'http://172.24.16.20/root/fly_moon_admin.git'
|
||||
}
|
||||
}
|
||||
|
||||
stage('获取信息') {
|
||||
steps {
|
||||
script {
|
||||
// 获取分支名
|
||||
env.Code_branch = "${params.Code_branch}"
|
||||
// 获取最近一次提交的哈希值(短格式,前8位)
|
||||
env.GIT_COMMIT_SHORT = sh(script: 'git rev-parse --short HEAD',returnStdout: true).trim()
|
||||
// 获取最近一次提交的哈希值(全格式)
|
||||
env.GIT_COMMIT_LONG = sh(script: 'git rev-parse HEAD', returnStdout: true).trim()
|
||||
// 获取最近一次提交的作者
|
||||
env.GIT_AUTHOR = sh(script: 'git log -1 --pretty=format:%an',returnStdout: true).trim()
|
||||
// 获取最近一次提交的时间(格式化)
|
||||
env.GIT_COMMIT_TIME = sh(
|
||||
script: 'git log -1 --pretty=format:%ct | xargs -I {} date -d @{} +%Y%m%d-%H%M%S',
|
||||
returnStdout: true
|
||||
).trim()
|
||||
// 获取最近一次提交的备注信息(转义特殊字符,避免构建失败)
|
||||
env.GIT_COMMIT_MSG = sh(script: 'git log -1 --pretty=format:%s | sed -e \'s/"/\\"/g\'', returnStdout: true).trim()
|
||||
|
||||
// Jenkins构建次数
|
||||
def buildNumber = env.BUILD_NUMBER // Jenkins内置变量,直接获取当前Job的构建序号
|
||||
// 当前分支名(处理/为-,如feature/docker_1015 → feature-docker_1015)
|
||||
def branchName = sh(script: 'git rev-parse --abbrev-ref HEAD', returnStdout: true).trim()
|
||||
def formattedBranch = branchName.replace('/', '-').replace('_', '-') // 替换分支名中的/和_为-
|
||||
// 构建时间(格式:202510181215,年-月-日-时-分,无分隔符)
|
||||
def buildTime = sh(script: 'date +%Y%m%d%H%M', returnStdout: true).trim()
|
||||
def defaultTag = "v${buildNumber}_${formattedBranch}_${GIT_COMMIT_SHORT}_${buildTime}"
|
||||
|
||||
def customTag = params.CUSTOM_TAG?.trim()
|
||||
def tagPattern = ~/^[a-zA-Z0-9._-]+$/
|
||||
|
||||
// 判断最终Tag
|
||||
if (customTag && customTag ==~ tagPattern) {
|
||||
echo "✅ 使用自定义镜像 Tag: ${customTag}"
|
||||
env.IMAGE_TAG = customTag
|
||||
} else if (customTag) {
|
||||
echo "⚠️ 自定义 Tag '${customTag}' 不符合规范,将使用默认生成的 Tag: ${defaultTag}"
|
||||
|
||||
def confirmed = true
|
||||
timeout(time: 1, unit: 'MINUTES') {
|
||||
try {
|
||||
input(
|
||||
message: """⚠️ Tag 命名不规范:
|
||||
${customTag}
|
||||
|
||||
将使用自动生成的 Tag:
|
||||
${defaultTag}
|
||||
|
||||
是否继续构建?""",
|
||||
ok: '确认'
|
||||
)
|
||||
} catch (err) {
|
||||
// 用户点击“取消”或中断
|
||||
echo "🚫 用户取消构建"
|
||||
confirmed = false
|
||||
}
|
||||
}
|
||||
|
||||
if (confirmed) {
|
||||
echo "✅ 用户确认使用自动生成的 Tag:${defaultTag}"
|
||||
env.IMAGE_TAG = defaultTag
|
||||
} else {
|
||||
error("流水线已终止。")
|
||||
}
|
||||
} else {
|
||||
env.IMAGE_TAG = defaultTag
|
||||
echo "未输入自定义 Tag, 使用自动生成规则: ${env.IMAGE_TAG}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage('登录容器') {
|
||||
steps {
|
||||
withCredentials([usernamePassword(
|
||||
credentialsId: env.CREDENTIALS_ID,
|
||||
usernameVariable: 'REGISTRY_USER',
|
||||
passwordVariable: 'REGISTRY_PWD'
|
||||
)]) {
|
||||
sh '''
|
||||
echo "$REGISTRY_PWD" | docker login ${REGISTRY} -u ${REGISTRY_USER} --password-stdin
|
||||
'''
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage('构建镜像') {
|
||||
steps {
|
||||
script {
|
||||
// 构建镜像,添加标签信息
|
||||
sh """
|
||||
docker build -t ${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG} \
|
||||
--label "git-branch='${Code_branch}'" \
|
||||
--label "git-commit='${GIT_COMMIT_SHORT}'" \
|
||||
--label "git-author='${GIT_AUTHOR}'" \
|
||||
--label "git-message='${GIT_COMMIT_MSG}'" \
|
||||
--label "build-time='${GIT_COMMIT_TIME}'" \
|
||||
.
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage('推送镜像') {
|
||||
steps {
|
||||
script {
|
||||
sh "docker push ${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG}"
|
||||
echo "推送镜像成功:${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
post {
|
||||
always {
|
||||
script {
|
||||
def keepCount = 2
|
||||
echo "开始清理本地旧镜像,仅保留最近 ${keepCount} 个构建版本"
|
||||
def imagePrefix = "${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}"
|
||||
|
||||
// 获取所有镜像(按创建时间排序,越新的越前)
|
||||
// 格式:Repository:Tag ImageID CreatedAt
|
||||
def allImagesRaw = sh(script: "docker images ${imagePrefix} --format '{{.Repository}}:{{.Tag}} {{.ID}} {{.CreatedAt}}' | sort -rk3", returnStdout: true).trim()
|
||||
if (!allImagesRaw) {
|
||||
echo "未找到任何镜像,无需清理"
|
||||
return
|
||||
}
|
||||
|
||||
def allImages = allImagesRaw.split('\n')
|
||||
if (allImages.size() <= keepCount) {
|
||||
echo "当前镜像数未超过 ${keepCount} 个,无需清理"
|
||||
return
|
||||
}
|
||||
|
||||
def oldImages = allImages.drop(keepCount)
|
||||
echo "发现 ${oldImages.size()} 个旧镜像需要清理"
|
||||
oldImages.each { line ->
|
||||
echo " ${line}"
|
||||
}
|
||||
|
||||
oldImages.each { line ->
|
||||
def parts = line.split(' ')
|
||||
def imageTag = parts[0]
|
||||
def imageId = parts.size() > 1 ? parts[1] : ""
|
||||
|
||||
// 对于标签为<none>的无效镜像,使用镜像ID删除
|
||||
if (imageTag.contains("<none>") && imageId) {
|
||||
echo "删除无效镜像: ${imageId}"
|
||||
sh(returnStatus: true, script: "docker rmi -f ${imageId} || true")
|
||||
} else if (imageId) {
|
||||
// 对于有标签的有效镜像,优先使用镜像ID删除
|
||||
echo "删除旧镜像: ${imageTag} (${imageId})"
|
||||
sh(returnStatus: true, script: "docker rmi -f ${imageId} || true")
|
||||
} else {
|
||||
// 兜底方案,使用标签删除
|
||||
echo "删除旧镜像: ${imageTag}"
|
||||
sh(returnStatus: true, script: "docker rmi -f ${imageTag} || true")
|
||||
}
|
||||
}
|
||||
|
||||
echo "清理完成,当前镜像状态:"
|
||||
sh """
|
||||
docker images ${imagePrefix} --format 'table {{.Repository}}\\t{{.Tag}}\\t{{.CreatedAt}}\\t{{.Size}}'
|
||||
"""
|
||||
|
||||
sh "docker logout ${REGISTRY}"
|
||||
echo "容器仓库已登出,本地凭证已清理"
|
||||
}
|
||||
}
|
||||
success {
|
||||
script {
|
||||
// 1. 准备元数据 (转换所有环境变量为 String)
|
||||
def metadataDir = '/var/lib/jenkins/metadata'
|
||||
def metadataFileRelativePath = "${env.NAMESPACE}-${env.IMAGE_NAME}.json" // 相对于 metadataDir 的文件名
|
||||
def fullMetadataPath = "${metadataDir}/${metadataFileRelativePath}"
|
||||
|
||||
// --- 转换为 String ---
|
||||
def registry = env.REGISTRY as String
|
||||
def namespace = env.NAMESPACE as String
|
||||
def imageName = env.IMAGE_NAME as String
|
||||
def imageTag = env.IMAGE_TAG as String
|
||||
def codeBranch = params.Code_branch as String // 使用 params,因为 Code_branch 是参数
|
||||
def gitCommit = env.GIT_COMMIT_LONG as String
|
||||
def gitAuthor = env.GIT_AUTHOR as String
|
||||
def gitCommitMsg = env.GIT_COMMIT_MSG as String
|
||||
def gitCommitTime = env.GIT_COMMIT_TIME as String
|
||||
def buildNumber = env.BUILD_NUMBER as String
|
||||
// --- 转换为 String ---
|
||||
|
||||
// 2. 准备新数据
|
||||
def newImageData = [
|
||||
image_tag: imageTag, // 使用转换后的变量
|
||||
full_image_name: "${registry}/${namespace}/${imageName}:${imageTag}", // 使用转换后的变量
|
||||
labels: [
|
||||
"git-branch": codeBranch,
|
||||
"git-commit": gitCommit,
|
||||
"git-author": gitAuthor,
|
||||
"git-message": gitCommitMsg,
|
||||
"build-time": gitCommitTime
|
||||
],
|
||||
build_job_number: buildNumber,
|
||||
build_time: new Date().format('yyyy-MM-dd HH:mm:ss') // Jenkins 构建完成时间
|
||||
]
|
||||
|
||||
// 2. 读取现有数据(如果文件存在)
|
||||
def existingDataList = []
|
||||
try {
|
||||
// 使用 readJSON 步骤读取文件内容 (readJSON 会自动处理 LazyMap 问题)
|
||||
def rawExistingData = readJSON file: fullMetadataPath, default: [] // 如果文件不存在,则返回空列表 []
|
||||
|
||||
// --- ✅ 修复:深拷贝 rawExistingData (修正内联代码) ---
|
||||
if (rawExistingData instanceof List) {
|
||||
existingDataList = rawExistingData.collect { item ->
|
||||
if (item instanceof Map) {
|
||||
// 递归深拷贝 Map (使用辅助函数)
|
||||
return deepCopyForSerialization(item)
|
||||
} else {
|
||||
return item
|
||||
}
|
||||
}
|
||||
} else {
|
||||
echo "警告: 元数据文件 ${fullMetadataPath} 格式不正确(非 List 类型),将被覆盖。"
|
||||
existingDataList = []
|
||||
}
|
||||
// --- 结束修复 ---
|
||||
|
||||
} catch (Exception e) {
|
||||
// readJSON 在文件不存在时通常会返回 default 值,但如果文件存在但格式错误,会抛出异常
|
||||
echo "警告: 读取元数据文件 ${fullMetadataPath} 失败或格式错误: ${e.getMessage()},将被覆盖。"
|
||||
// 确保目录存在
|
||||
sh "mkdir -p ${metadataDir}"
|
||||
existingDataList = [] // 重置为新列表
|
||||
}
|
||||
|
||||
// 3. 将新数据添加到列表开头(最新的在前)
|
||||
existingDataList.add(0, newImageData)
|
||||
|
||||
// 4. 限制列表大小为 20
|
||||
if (existingDataList.size() > 20) {
|
||||
existingDataList = existingDataList.take(20)
|
||||
}
|
||||
|
||||
// 5. 使用 writeJSON 步骤写入文件 (writeJSON 会自动处理 Map 的序列化)
|
||||
writeJSON file: fullMetadataPath, json: existingDataList, pretty: 2 // pretty: 2 表示格式化 JSON (2 个空格缩进)
|
||||
|
||||
echo "镜像元数据已存储到: ${fullMetadataPath}"
|
||||
|
||||
// 输出构建结果
|
||||
echo """
|
||||
镜像地址:${registry}/${namespace}/${imageName}:${imageTag}
|
||||
对应代码提交哈希:${gitCommit}
|
||||
对应代码分支:${codeBranch}
|
||||
代码提交者:${gitAuthor}
|
||||
提交备注:${gitCommitMsg}
|
||||
""".stripIndent()
|
||||
}
|
||||
}
|
||||
failure {
|
||||
// 输出构建结果
|
||||
echo "有步骤失败,请检查!"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
330
SCM/构建镜像/build_image_flymoon_admin_web_v2.groovy
Normal file
330
SCM/构建镜像/build_image_flymoon_admin_web_v2.groovy
Normal file
@@ -0,0 +1,330 @@
|
||||
|
||||
// --- 辅助函数:深拷贝对象以确保可序列化 ---
|
||||
def deepCopyForSerialization(obj) {
|
||||
if (obj instanceof Map) {
|
||||
// 创建新的 LinkedHashMap,递归拷贝值
|
||||
return obj.collectEntries { k, v -> [(k): deepCopyForSerialization(v)] }
|
||||
} else if (obj instanceof List) {
|
||||
// 创建新的 ArrayList,递归拷贝元素
|
||||
return obj.collect { item -> deepCopyForSerialization(item) }
|
||||
} else if (obj instanceof String || obj instanceof Number || obj instanceof Boolean || obj == null) {
|
||||
return obj
|
||||
} else {
|
||||
return obj.toString()
|
||||
}
|
||||
}
|
||||
// --- 结束辅助函数 ---
|
||||
|
||||
pipeline {
|
||||
agent any
|
||||
|
||||
parameters {
|
||||
gitParameter(
|
||||
branchFilter: 'origin/(.*)',
|
||||
defaultValue: 'dxin',
|
||||
name: 'Code_branch',
|
||||
type: 'PT_BRANCH_TAG',
|
||||
selectedValue: 'DEFAULT',
|
||||
sortMode: 'NONE',
|
||||
description: '选择代码分支: ',
|
||||
quickFilterEnabled: true,
|
||||
tagFilter: '*',
|
||||
listSize: "1"
|
||||
)
|
||||
choice(
|
||||
name: 'NAME_SPACES',
|
||||
choices: ['sit', 'test', 'prod'],
|
||||
description: '选择存放镜像的仓库命名空间:'
|
||||
)
|
||||
choice(
|
||||
name: 'BUILD_ENV',
|
||||
choices: ['sit', 'test', 'prod'],
|
||||
description: '选择构建的环境配置, 默认为 pnpm build:im 构建'
|
||||
)
|
||||
string(
|
||||
name: 'CUSTOM_TAG',
|
||||
defaultValue: '',
|
||||
description: '可选:自定义镜像 Tag (字母、数字、点、下划线、短横线), 如 v0.0.1, 留空则自动生成 “ v+构建次数_分支名_短哈希_构建时间 ”'
|
||||
)
|
||||
}
|
||||
environment {
|
||||
REGISTRY = "uswccr.ccs.tencentyun.com" // 镜像仓库地址
|
||||
NAMESPACE = "lessie${params.NAME_SPACES}" // 命名空间根据choices的选择拼接
|
||||
IMAGE_NAME = "flymoon-admin-web" // 镜像名(固定前缀)
|
||||
CREDENTIALS_ID = "dxin_img_hub_auth" // 容器仓库凭证ID
|
||||
}
|
||||
|
||||
stages {
|
||||
stage('拉取代码') {
|
||||
steps {
|
||||
git branch: "${params.Code_branch}",
|
||||
credentialsId: 'fly_gitlab_auth',
|
||||
url: 'http://172.24.16.20/root/fly_moon_web.git'
|
||||
}
|
||||
}
|
||||
|
||||
stage('获取信息') {
|
||||
steps {
|
||||
script {
|
||||
// 获取分支名
|
||||
env.Code_branch = "${params.Code_branch}"
|
||||
// 获取最近一次提交的哈希值(短格式,前8位)
|
||||
env.GIT_COMMIT_SHORT = sh(script: 'git rev-parse --short HEAD',returnStdout: true).trim()
|
||||
// 获取最近一次提交的哈希值(全格式)
|
||||
env.GIT_COMMIT_LONG = sh(script: 'git rev-parse HEAD', returnStdout: true).trim()
|
||||
// 获取最近一次提交的作者
|
||||
env.GIT_AUTHOR = sh(script: 'git log -1 --pretty=format:%an',returnStdout: true).trim()
|
||||
// 获取最近一次提交的时间(格式化)
|
||||
env.GIT_COMMIT_TIME = sh(
|
||||
script: 'git log -1 --pretty=format:%ct | xargs -I {} date -d @{} +%Y%m%d-%H%M%S',
|
||||
returnStdout: true
|
||||
).trim()
|
||||
// 获取最近一次提交的备注信息(转义特殊字符,避免构建失败)
|
||||
env.GIT_COMMIT_MSG = sh(script: 'git log -1 --pretty=format:%s | sed -e \'s/"/\\"/g\'', returnStdout: true).trim()
|
||||
|
||||
// Jenkins构建次数
|
||||
def buildNumber = env.BUILD_NUMBER // Jenkins内置变量,直接获取当前Job的构建序号
|
||||
// 当前分支名(处理/为-,如feature/docker_1015 → feature-docker_1015)
|
||||
def branchName = sh(script: 'git rev-parse --abbrev-ref HEAD', returnStdout: true).trim()
|
||||
def formattedBranch = branchName.replace('/', '-').replace('_', '-') // 替换分支名中的/和_为-
|
||||
// 构建时间(格式:202510181215,年-月-日-时-分,无分隔符)
|
||||
def buildTime = sh(script: 'date +%Y%m%d%H%M', returnStdout: true).trim()
|
||||
def defaultTag = "v${buildNumber}_${formattedBranch}_${GIT_COMMIT_SHORT}_${buildTime}"
|
||||
|
||||
def customTag = params.CUSTOM_TAG?.trim()
|
||||
def tagPattern = ~/^[a-zA-Z0-9._-]+$/
|
||||
|
||||
// 判断最终Tag
|
||||
if (customTag && customTag ==~ tagPattern) {
|
||||
echo "✅ 使用自定义镜像 Tag: ${customTag}"
|
||||
env.IMAGE_TAG = customTag
|
||||
} else if (customTag) {
|
||||
echo "⚠️ 自定义 Tag '${customTag}' 不符合规范,将使用默认生成的 Tag: ${defaultTag}"
|
||||
|
||||
def confirmed = true
|
||||
timeout(time: 1, unit: 'MINUTES') {
|
||||
try {
|
||||
input(
|
||||
message: """⚠️ Tag 命名不规范:
|
||||
${customTag}
|
||||
|
||||
将使用自动生成的 Tag:
|
||||
${defaultTag}
|
||||
|
||||
是否继续构建?""",
|
||||
ok: '确认'
|
||||
)
|
||||
} catch (err) {
|
||||
// 用户点击“取消”或中断
|
||||
echo "🚫 用户取消构建"
|
||||
confirmed = false
|
||||
}
|
||||
}
|
||||
|
||||
if (confirmed) {
|
||||
echo "✅ 用户确认使用自动生成的 Tag:${defaultTag}"
|
||||
env.IMAGE_TAG = defaultTag
|
||||
} else {
|
||||
error("流水线已终止。")
|
||||
}
|
||||
} else {
|
||||
env.IMAGE_TAG = defaultTag
|
||||
echo "未输入自定义 Tag, 使用自动生成规则: ${env.IMAGE_TAG}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage('登录容器') {
|
||||
steps {
|
||||
withCredentials([usernamePassword(
|
||||
credentialsId: env.CREDENTIALS_ID,
|
||||
usernameVariable: 'REGISTRY_USER',
|
||||
passwordVariable: 'REGISTRY_PWD'
|
||||
)]) {
|
||||
sh '''
|
||||
echo "$REGISTRY_PWD" | docker login ${REGISTRY} -u ${REGISTRY_USER} --password-stdin
|
||||
'''
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage('构建镜像') {
|
||||
steps {
|
||||
script {
|
||||
// 构建镜像,添加标签信息
|
||||
sh """
|
||||
docker build \
|
||||
--build-arg BUILD_ENV=${params.BUILD_ENV} \
|
||||
-t ${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG} \
|
||||
--label "git-branch='${Code_branch}'" \
|
||||
--label "git-commit='${GIT_COMMIT_SHORT}'" \
|
||||
--label "git-author='${GIT_AUTHOR}'" \
|
||||
--label "git-message='${GIT_COMMIT_MSG}'" \
|
||||
--label "build-time='${GIT_COMMIT_TIME}'" \
|
||||
.
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage('推送镜像') {
|
||||
steps {
|
||||
script {
|
||||
sh "docker push ${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG}"
|
||||
echo "推送镜像成功:${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
post {
|
||||
always {
|
||||
script {
|
||||
def keepCount = 3
|
||||
echo "开始清理本地旧镜像,仅保留最近 ${keepCount} 个构建版本"
|
||||
def imagePrefix = "${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}"
|
||||
|
||||
// 获取所有镜像(按创建时间排序,越新的越前)
|
||||
// 格式:Repository:Tag ImageID CreatedAt
|
||||
def allImagesRaw = sh(script: "docker images ${imagePrefix} --format '{{.Repository}}:{{.Tag}} {{.ID}} {{.CreatedAt}}' | sort -rk3", returnStdout: true).trim()
|
||||
if (!allImagesRaw) {
|
||||
echo "未找到任何镜像,无需清理"
|
||||
return
|
||||
}
|
||||
|
||||
def allImages = allImagesRaw.split('\n')
|
||||
if (allImages.size() <= keepCount) {
|
||||
echo "当前镜像数未超过 ${keepCount} 个,无需清理"
|
||||
return
|
||||
}
|
||||
|
||||
def oldImages = allImages.drop(keepCount)
|
||||
echo "发现 ${oldImages.size()} 个旧镜像需要清理"
|
||||
oldImages.each { line ->
|
||||
echo " ${line}"
|
||||
}
|
||||
|
||||
oldImages.each { line ->
|
||||
def parts = line.split(' ')
|
||||
def imageTag = parts[0]
|
||||
def imageId = parts.size() > 1 ? parts[1] : ""
|
||||
|
||||
// 对于标签为<none>的无效镜像,使用镜像ID删除
|
||||
if (imageTag.contains("<none>") && imageId) {
|
||||
echo "删除无效镜像: ${imageId}"
|
||||
sh(returnStatus: true, script: "docker rmi -f ${imageId} || true")
|
||||
} else if (imageId) {
|
||||
// 对于有标签的有效镜像,优先使用镜像ID删除
|
||||
echo "删除旧镜像: ${imageTag} (${imageId})"
|
||||
sh(returnStatus: true, script: "docker rmi -f ${imageId} || true")
|
||||
} else {
|
||||
// 兜底方案,使用标签删除
|
||||
echo "删除旧镜像: ${imageTag}"
|
||||
sh(returnStatus: true, script: "docker rmi -f ${imageTag} || true")
|
||||
}
|
||||
}
|
||||
|
||||
echo "清理完成,当前镜像状态:"
|
||||
sh """
|
||||
docker images ${imagePrefix} --format 'table {{.Repository}}\\t{{.Tag}}\\t{{.CreatedAt}}\\t{{.Size}}'
|
||||
"""
|
||||
|
||||
sh "docker logout ${REGISTRY}"
|
||||
echo "容器仓库已登出,本地凭证已清理"
|
||||
}
|
||||
}
|
||||
success {
|
||||
script {
|
||||
// 1. 准备元数据 (转换所有环境变量为 String)
|
||||
def metadataDir = '/var/lib/jenkins/metadata'
|
||||
def metadataFileRelativePath = "${env.NAMESPACE}-${env.IMAGE_NAME}.json" // 相对于 metadataDir 的文件名
|
||||
def fullMetadataPath = "${metadataDir}/${metadataFileRelativePath}"
|
||||
|
||||
// --- 转换为 String ---
|
||||
def registry = env.REGISTRY as String
|
||||
def namespace = env.NAMESPACE as String
|
||||
def imageName = env.IMAGE_NAME as String
|
||||
def imageTag = env.IMAGE_TAG as String
|
||||
def codeBranch = params.Code_branch as String // 使用 params,因为 Code_branch 是参数
|
||||
def gitCommit = env.GIT_COMMIT_LONG as String
|
||||
def gitAuthor = env.GIT_AUTHOR as String
|
||||
def gitCommitMsg = env.GIT_COMMIT_MSG as String
|
||||
def gitCommitTime = env.GIT_COMMIT_TIME as String
|
||||
def buildNumber = env.BUILD_NUMBER as String
|
||||
// --- 转换为 String ---
|
||||
|
||||
// 2. 准备新数据
|
||||
def newImageData = [
|
||||
image_tag: imageTag, // 使用转换后的变量
|
||||
full_image_name: "${registry}/${namespace}/${imageName}:${imageTag}", // 使用转换后的变量
|
||||
labels: [
|
||||
"git-branch": codeBranch,
|
||||
"git-commit": gitCommit,
|
||||
"git-author": gitAuthor,
|
||||
"git-message": gitCommitMsg,
|
||||
"build-time": gitCommitTime
|
||||
],
|
||||
build_job_number: buildNumber,
|
||||
build_time: new Date().format('yyyy-MM-dd HH:mm:ss') // Jenkins 构建完成时间
|
||||
]
|
||||
|
||||
// 2. 读取现有数据(如果文件存在)
|
||||
def existingDataList = []
|
||||
try {
|
||||
// 使用 readJSON 步骤读取文件内容 (readJSON 会自动处理 LazyMap 问题)
|
||||
def rawExistingData = readJSON file: fullMetadataPath, default: [] // 如果文件不存在,则返回空列表 []
|
||||
|
||||
// --- ✅ 修复:深拷贝 rawExistingData (修正内联代码) ---
|
||||
if (rawExistingData instanceof List) {
|
||||
existingDataList = rawExistingData.collect { item ->
|
||||
if (item instanceof Map) {
|
||||
// 递归深拷贝 Map (使用辅助函数)
|
||||
return deepCopyForSerialization(item)
|
||||
} else {
|
||||
return item
|
||||
}
|
||||
}
|
||||
} else {
|
||||
echo "警告: 元数据文件 ${fullMetadataPath} 格式不正确(非 List 类型),将被覆盖。"
|
||||
existingDataList = []
|
||||
}
|
||||
// --- 结束修复 ---
|
||||
|
||||
} catch (Exception e) {
|
||||
// readJSON 在文件不存在时通常会返回 default 值,但如果文件存在但格式错误,会抛出异常
|
||||
echo "警告: 读取元数据文件 ${fullMetadataPath} 失败或格式错误: ${e.getMessage()},将被覆盖。"
|
||||
// 确保目录存在
|
||||
sh "mkdir -p ${metadataDir}"
|
||||
existingDataList = [] // 重置为新列表
|
||||
}
|
||||
|
||||
// 3. 将新数据添加到列表开头(最新的在前)
|
||||
existingDataList.add(0, newImageData)
|
||||
|
||||
// 4. 限制列表大小为 20
|
||||
if (existingDataList.size() > 20) {
|
||||
existingDataList = existingDataList.take(20)
|
||||
}
|
||||
|
||||
// 5. 使用 writeJSON 步骤写入文件 (writeJSON 会自动处理 Map 的序列化)
|
||||
writeJSON file: fullMetadataPath, json: existingDataList, pretty: 2 // pretty: 2 表示格式化 JSON (2 个空格缩进)
|
||||
|
||||
echo "镜像元数据已存储到: ${fullMetadataPath}"
|
||||
|
||||
// 输出构建结果
|
||||
echo """
|
||||
镜像地址:${registry}/${namespace}/${imageName}:${imageTag}
|
||||
对应代码提交哈希:${gitCommit}
|
||||
对应代码分支:${codeBranch}
|
||||
代码提交者:${gitAuthor}
|
||||
提交备注:${gitCommitMsg}
|
||||
""".stripIndent()
|
||||
}
|
||||
}
|
||||
failure {
|
||||
// 输出构建结果
|
||||
echo "部署有错误,请检查!"
|
||||
}
|
||||
}
|
||||
}
|
||||
322
SCM/构建镜像/build_image_flymoon_agent_v2.groovy
Normal file
322
SCM/构建镜像/build_image_flymoon_agent_v2.groovy
Normal file
@@ -0,0 +1,322 @@
|
||||
// --- 辅助函数:深拷贝对象以确保可序列化 ---
|
||||
def deepCopyForSerialization(obj) {
|
||||
if (obj instanceof Map) {
|
||||
// 创建新的 LinkedHashMap,递归拷贝值
|
||||
return obj.collectEntries { k, v -> [(k): deepCopyForSerialization(v)] }
|
||||
} else if (obj instanceof List) {
|
||||
// 创建新的 ArrayList,递归拷贝元素
|
||||
return obj.collect { item -> deepCopyForSerialization(item) }
|
||||
} else if (obj instanceof String || obj instanceof Number || obj instanceof Boolean || obj == null) {
|
||||
return obj
|
||||
} else {
|
||||
return obj.toString()
|
||||
}
|
||||
}
|
||||
// --- 结束辅助函数 ---
|
||||
|
||||
pipeline {
|
||||
agent any
|
||||
tools{
|
||||
maven 'mvn3.8.8'
|
||||
jdk 'jdk21'
|
||||
}
|
||||
parameters {
|
||||
gitParameter(
|
||||
branchFilter: 'origin/(.*)',
|
||||
defaultValue: 'dxin',
|
||||
name: 'Code_branch',
|
||||
type: 'PT_BRANCH_TAG',
|
||||
selectedValue: 'DEFAULT',
|
||||
sortMode: 'NONE',
|
||||
description: '选择代码分支: ',
|
||||
quickFilterEnabled: true,
|
||||
tagFilter: '*',
|
||||
listSize: "1"
|
||||
)
|
||||
choice(
|
||||
name: 'NAME_SPACES',
|
||||
choices: ['sit', 'test', 'prod'],
|
||||
description: '选择存放镜像的仓库命名空间:'
|
||||
)
|
||||
string(
|
||||
name: 'CUSTOM_TAG',
|
||||
defaultValue: '',
|
||||
description: '可选:自定义镜像 Tag (字母、数字、点、下划线、短横线), 留空则自动生成 “ v+构建次数_分支名_短哈希_构建时间 ”'
|
||||
)
|
||||
}
|
||||
environment {
|
||||
REGISTRY = "uswccr.ccs.tencentyun.com" // 镜像仓库地址
|
||||
NAMESPACE = "lessie${params.NAME_SPACES}" // 命名空间根据choices的选择拼接
|
||||
IMAGE_NAME = "flymoon-agent" // 镜像名(固定前缀)
|
||||
CREDENTIALS_ID = "dxin_img_hub_auth" // 容器仓库凭证ID
|
||||
}
|
||||
|
||||
stages {
|
||||
stage('拉取代码') {
|
||||
steps {
|
||||
git branch: "${params.Code_branch}",
|
||||
credentialsId: 'fly_gitlab_auth',
|
||||
url: 'http://172.24.16.20/root/fly_moon_agent.git'
|
||||
}
|
||||
}
|
||||
stage('获取信息') {
|
||||
steps {
|
||||
script {
|
||||
// 获取分支名
|
||||
env.Code_branch = "${params.Code_branch}"
|
||||
// 获取最近一次提交的哈希值(短格式,前8位)
|
||||
env.GIT_COMMIT_SHORT = sh(script: 'git rev-parse --short HEAD',returnStdout: true).trim()
|
||||
// 获取最近一次提交的哈希值(全格式)
|
||||
env.GIT_COMMIT_LONG = sh(script: 'git rev-parse HEAD', returnStdout: true).trim()
|
||||
// 获取最近一次提交的作者
|
||||
env.GIT_AUTHOR = sh(script: 'git log -1 --pretty=format:%an',returnStdout: true).trim()
|
||||
// 获取最近一次提交的时间(格式化)
|
||||
env.GIT_COMMIT_TIME = sh(
|
||||
script: 'git log -1 --pretty=format:%ct | xargs -I {} date -d @{} +%Y%m%d-%H%M%S',
|
||||
returnStdout: true
|
||||
).trim()
|
||||
// 获取最近一次提交的备注信息(转义特殊字符,避免构建失败)
|
||||
env.GIT_COMMIT_MSG = sh(script: 'git log -1 --pretty=format:%s | sed -e \'s/"/\\"/g\'', returnStdout: true).trim()
|
||||
|
||||
// Jenkins构建次数
|
||||
def buildNumber = env.BUILD_NUMBER // Jenkins内置变量,直接获取当前Job的构建序号
|
||||
// 当前分支名(处理/为-,如feature/docker_1015 → feature-docker_1015)
|
||||
def branchName = sh(script: 'git rev-parse --abbrev-ref HEAD', returnStdout: true).trim()
|
||||
def formattedBranch = branchName.replace('/', '-').replace('_', '-') // 替换分支名中的/和_为-
|
||||
// 构建时间(格式:202510181215,年-月-日-时-分,无分隔符)
|
||||
def buildTime = sh(script: 'date +%Y%m%d%H%M', returnStdout: true).trim()
|
||||
def defaultTag = "v${buildNumber}_${formattedBranch}_${GIT_COMMIT_SHORT}_${buildTime}"
|
||||
|
||||
def customTag = params.CUSTOM_TAG?.trim()
|
||||
def tagPattern = ~/^[a-zA-Z0-9._-]+$/
|
||||
|
||||
// 判断最终Tag
|
||||
if (customTag && customTag ==~ tagPattern) {
|
||||
echo "✅ 使用自定义镜像 Tag: ${customTag}"
|
||||
env.IMAGE_TAG = customTag
|
||||
} else if (customTag) {
|
||||
echo "⚠️ 自定义 Tag '${customTag}' 不符合规范,将使用默认生成的 Tag: ${defaultTag}"
|
||||
|
||||
def confirmed = true
|
||||
timeout(time: 1, unit: 'MINUTES') {
|
||||
try {
|
||||
input(
|
||||
message: """⚠️ Tag 命名不规范:
|
||||
${customTag}
|
||||
|
||||
将使用自动生成的 Tag:
|
||||
${defaultTag}
|
||||
|
||||
是否继续构建?""",
|
||||
ok: '确认'
|
||||
)
|
||||
} catch (err) {
|
||||
// 用户点击“取消”或中断
|
||||
echo "🚫 用户取消构建"
|
||||
confirmed = false
|
||||
}
|
||||
}
|
||||
|
||||
if (confirmed) {
|
||||
echo "✅ 用户确认使用自动生成的 Tag:${defaultTag}"
|
||||
env.IMAGE_TAG = defaultTag
|
||||
} else {
|
||||
error("流水线已终止。")
|
||||
}
|
||||
} else {
|
||||
env.IMAGE_TAG = defaultTag
|
||||
echo "未输入自定义 Tag, 使用自动生成规则: ${env.IMAGE_TAG}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('登录仓库') {
|
||||
steps {
|
||||
withCredentials([usernamePassword(
|
||||
credentialsId: env.CREDENTIALS_ID,
|
||||
usernameVariable: 'REGISTRY_USER',
|
||||
passwordVariable: 'REGISTRY_PWD'
|
||||
)]) {
|
||||
sh '''
|
||||
echo "$REGISTRY_PWD" | docker login ${REGISTRY} -u ${REGISTRY_USER} --password-stdin
|
||||
'''
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('构建镜像') {
|
||||
steps {
|
||||
script {
|
||||
// 构建镜像,添加标签信息
|
||||
sh """
|
||||
docker build -t ${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG} \
|
||||
--label "git-branch='${Code_branch}'" \
|
||||
--label "git-commit='${GIT_COMMIT_SHORT}'" \
|
||||
--label "git-author='${GIT_AUTHOR}'" \
|
||||
--label "git-message='${GIT_COMMIT_MSG}'" \
|
||||
--label "build-time='${GIT_COMMIT_TIME}'" \
|
||||
.
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('推送镜像') {
|
||||
steps {
|
||||
script {
|
||||
// 推送镜像(带唯一 Tag)
|
||||
sh "docker push ${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG}"
|
||||
echo "推送镜像成功:${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
post {
|
||||
always {
|
||||
script {
|
||||
def keepCount = 3
|
||||
echo "开始清理本地旧镜像,仅保留最近 ${keepCount} 个构建版本"
|
||||
def imagePrefix = "${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}"
|
||||
|
||||
// 获取所有镜像(按创建时间排序,越新的越前)
|
||||
// 格式:Repository:Tag ImageID CreatedAt
|
||||
def allImagesRaw = sh(script: "docker images ${imagePrefix} --format '{{.Repository}}:{{.Tag}} {{.ID}} {{.CreatedAt}}' | sort -rk3", returnStdout: true).trim()
|
||||
if (!allImagesRaw) {
|
||||
echo "未找到任何镜像,无需清理"
|
||||
return
|
||||
}
|
||||
|
||||
def allImages = allImagesRaw.split('\n')
|
||||
if (allImages.size() <= keepCount) {
|
||||
echo "当前镜像数未超过 ${keepCount} 个,无需清理"
|
||||
return
|
||||
}
|
||||
|
||||
def oldImages = allImages.drop(keepCount)
|
||||
echo "发现 ${oldImages.size()} 个旧镜像需要清理"
|
||||
oldImages.each { line ->
|
||||
echo " ${line}"
|
||||
}
|
||||
|
||||
oldImages.each { line ->
|
||||
def parts = line.split(' ')
|
||||
def imageTag = parts[0]
|
||||
def imageId = parts.size() > 1 ? parts[1] : ""
|
||||
|
||||
// 对于标签为<none>的无效镜像,使用镜像ID删除
|
||||
if (imageTag.contains("<none>") && imageId) {
|
||||
echo "删除无效镜像: ${imageId}"
|
||||
sh(returnStatus: true, script: "docker rmi -f ${imageId} || true")
|
||||
} else if (imageId) {
|
||||
// 对于有标签的有效镜像,优先使用镜像ID删除
|
||||
echo "删除旧镜像: ${imageTag} (${imageId})"
|
||||
sh(returnStatus: true, script: "docker rmi -f ${imageId} || true")
|
||||
} else {
|
||||
// 兜底方案,使用标签删除
|
||||
echo "删除旧镜像: ${imageTag}"
|
||||
sh(returnStatus: true, script: "docker rmi -f ${imageTag} || true")
|
||||
}
|
||||
}
|
||||
|
||||
echo "清理完成,当前镜像状态:"
|
||||
sh """
|
||||
docker images ${imagePrefix} --format 'table {{.Repository}}\\t{{.Tag}}\\t{{.CreatedAt}}\\t{{.Size}}'
|
||||
"""
|
||||
|
||||
sh "docker logout ${REGISTRY}"
|
||||
echo "容器仓库已登出,本地凭证已清理"
|
||||
}
|
||||
}
|
||||
success {
|
||||
script {
|
||||
// 1. 准备元数据 (转换所有环境变量为 String)
|
||||
def metadataDir = '/var/lib/jenkins/metadata'
|
||||
def metadataFileRelativePath = "${env.NAMESPACE}-${env.IMAGE_NAME}.json" // 相对于 metadataDir 的文件名
|
||||
def fullMetadataPath = "${metadataDir}/${metadataFileRelativePath}"
|
||||
|
||||
// --- 转换为 String ---
|
||||
def registry = env.REGISTRY as String
|
||||
def namespace = env.NAMESPACE as String
|
||||
def imageName = env.IMAGE_NAME as String
|
||||
def imageTag = env.IMAGE_TAG as String
|
||||
def codeBranch = params.Code_branch as String // 使用 params,因为 Code_branch 是参数
|
||||
def gitCommit = env.GIT_COMMIT_LONG as String
|
||||
def gitAuthor = env.GIT_AUTHOR as String
|
||||
def gitCommitMsg = env.GIT_COMMIT_MSG as String
|
||||
def gitCommitTime = env.GIT_COMMIT_TIME as String
|
||||
def buildNumber = env.BUILD_NUMBER as String
|
||||
// --- 转换为 String ---
|
||||
|
||||
// 2. 准备新数据
|
||||
def newImageData = [
|
||||
image_tag: imageTag, // 使用转换后的变量
|
||||
full_image_name: "${registry}/${namespace}/${imageName}:${imageTag}", // 使用转换后的变量
|
||||
labels: [
|
||||
"git-branch": codeBranch,
|
||||
"git-commit": gitCommit,
|
||||
"git-author": gitAuthor,
|
||||
"git-message": gitCommitMsg,
|
||||
"build-time": gitCommitTime
|
||||
],
|
||||
build_job_number: buildNumber,
|
||||
build_time: new Date().format('yyyy-MM-dd HH:mm:ss') // Jenkins 构建完成时间
|
||||
]
|
||||
|
||||
// 2. 读取现有数据(如果文件存在)
|
||||
def existingDataList = []
|
||||
try {
|
||||
// 使用 readJSON 步骤读取文件内容 (readJSON 会自动处理 LazyMap 问题)
|
||||
def rawExistingData = readJSON file: fullMetadataPath, default: [] // 如果文件不存在,则返回空列表 []
|
||||
|
||||
// --- ✅ 修复:深拷贝 rawExistingData (修正内联代码) ---
|
||||
if (rawExistingData instanceof List) {
|
||||
existingDataList = rawExistingData.collect { item ->
|
||||
if (item instanceof Map) {
|
||||
// 递归深拷贝 Map (使用辅助函数)
|
||||
return deepCopyForSerialization(item)
|
||||
} else {
|
||||
return item
|
||||
}
|
||||
}
|
||||
} else {
|
||||
echo "警告: 元数据文件 ${fullMetadataPath} 格式不正确(非 List 类型),将被覆盖。"
|
||||
existingDataList = []
|
||||
}
|
||||
// --- 结束修复 ---
|
||||
|
||||
} catch (Exception e) {
|
||||
// readJSON 在文件不存在时通常会返回 default 值,但如果文件存在但格式错误,会抛出异常
|
||||
echo "警告: 读取元数据文件 ${fullMetadataPath} 失败或格式错误: ${e.getMessage()},将被覆盖。"
|
||||
// 确保目录存在
|
||||
sh "mkdir -p ${metadataDir}"
|
||||
existingDataList = [] // 重置为新列表
|
||||
}
|
||||
|
||||
// 3. 将新数据添加到列表开头(最新的在前)
|
||||
existingDataList.add(0, newImageData)
|
||||
|
||||
// 4. 限制列表大小为 20
|
||||
if (existingDataList.size() > 20) {
|
||||
existingDataList = existingDataList.take(20)
|
||||
}
|
||||
|
||||
// 5. 使用 writeJSON 步骤写入文件 (writeJSON 会自动处理 Map 的序列化)
|
||||
writeJSON file: fullMetadataPath, json: existingDataList, pretty: 2 // pretty: 2 表示格式化 JSON (2 个空格缩进)
|
||||
|
||||
echo "镜像元数据已存储到: ${fullMetadataPath}"
|
||||
|
||||
// 输出构建结果
|
||||
echo """
|
||||
镜像地址:${registry}/${namespace}/${imageName}:${imageTag}
|
||||
对应代码提交哈希:${gitCommit}
|
||||
对应代码分支:${codeBranch}
|
||||
代码提交者:${gitAuthor}
|
||||
提交备注:${gitCommitMsg}
|
||||
""".stripIndent()
|
||||
}
|
||||
}
|
||||
failure {
|
||||
// 输出构建结果
|
||||
echo "有步骤出错!"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -131,7 +131,7 @@ pipeline {
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('登录容器仓库') {
|
||||
stage('登录仓库') {
|
||||
steps {
|
||||
withCredentials([usernamePassword(
|
||||
credentialsId: env.CREDENTIALS_ID,
|
||||
@@ -144,7 +144,7 @@ pipeline {
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('构建容器镜像') {
|
||||
stage('构建镜像') {
|
||||
steps {
|
||||
script {
|
||||
// 构建镜像,添加标签信息
|
||||
@@ -162,7 +162,7 @@ pipeline {
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('推送镜像到仓库') {
|
||||
stage('推送镜像') {
|
||||
steps {
|
||||
script {
|
||||
sh "docker push ${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG}"
|
||||
@@ -175,7 +175,7 @@ pipeline {
|
||||
post {
|
||||
always {
|
||||
script {
|
||||
def keepCount = 2
|
||||
def keepCount = 3
|
||||
echo "开始清理本地旧镜像,仅保留最近 ${keepCount} 个构建版本"
|
||||
def imagePrefix = "${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}"
|
||||
|
||||
|
||||
321
SCM/构建镜像/build_image_flymoon_payment_v2.groovy
Normal file
321
SCM/构建镜像/build_image_flymoon_payment_v2.groovy
Normal file
@@ -0,0 +1,321 @@
|
||||
// --- 辅助函数:深拷贝对象以确保可序列化 ---
|
||||
def deepCopyForSerialization(obj) {
|
||||
if (obj instanceof Map) {
|
||||
// 创建新的 LinkedHashMap,递归拷贝值
|
||||
return obj.collectEntries { k, v -> [(k): deepCopyForSerialization(v)] }
|
||||
} else if (obj instanceof List) {
|
||||
// 创建新的 ArrayList,递归拷贝元素
|
||||
return obj.collect { item -> deepCopyForSerialization(item) }
|
||||
} else if (obj instanceof String || obj instanceof Number || obj instanceof Boolean || obj == null) {
|
||||
return obj
|
||||
} else {
|
||||
return obj.toString()
|
||||
}
|
||||
}
|
||||
// --- 结束辅助函数 ---
|
||||
|
||||
pipeline {
|
||||
agent any
|
||||
tools{
|
||||
maven 'mvn3.8.8'
|
||||
jdk 'jdk21'
|
||||
}
|
||||
parameters {
|
||||
gitParameter(
|
||||
branchFilter: 'origin/(.*)',
|
||||
defaultValue: 'dxin',
|
||||
name: 'Code_branch',
|
||||
type: 'PT_BRANCH_TAG',
|
||||
selectedValue: 'DEFAULT',
|
||||
sortMode: 'NONE',
|
||||
description: '选择代码分支: ',
|
||||
quickFilterEnabled: true,
|
||||
tagFilter: '*',
|
||||
listSize: "1"
|
||||
)
|
||||
choice(
|
||||
name: 'NAME_SPACES',
|
||||
choices: ['sit', 'test', 'prod'],
|
||||
description: '选择存放镜像的仓库命名空间:'
|
||||
)
|
||||
string(
|
||||
name: 'CUSTOM_TAG',
|
||||
defaultValue: '',
|
||||
description: '可选:自定义镜像 Tag (字母、数字、点、下划线、短横线), 留空则自动生成 “ v+构建次数_分支名_短哈希_构建时间 ”'
|
||||
)
|
||||
}
|
||||
environment {
|
||||
REGISTRY = "uswccr.ccs.tencentyun.com" // 镜像仓库地址
|
||||
NAMESPACE = "lessie${params.NAME_SPACES}" // 命名空间根据choices的选择拼接
|
||||
IMAGE_NAME = "flymoon-payment" // 镜像名(固定前缀)
|
||||
CREDENTIALS_ID = "dxin_img_hub_auth" // 容器仓库凭证ID
|
||||
}
|
||||
|
||||
stages {
|
||||
stage('拉取代码') {
|
||||
steps {
|
||||
git branch: "${params.Code_branch}",
|
||||
credentialsId: 'fly_gitlab_auth',
|
||||
url: 'http://172.24.16.20/root/fly_moon_payment.git'
|
||||
}
|
||||
}
|
||||
stage('获取信息') {
|
||||
steps {
|
||||
script {
|
||||
// 获取分支名
|
||||
env.Code_branch = "${params.Code_branch}"
|
||||
// 获取最近一次提交的哈希值(短格式,前8位)
|
||||
env.GIT_COMMIT_SHORT = sh(script: 'git rev-parse --short HEAD',returnStdout: true).trim()
|
||||
// 获取最近一次提交的哈希值(全格式)
|
||||
env.GIT_COMMIT_LONG = sh(script: 'git rev-parse HEAD', returnStdout: true).trim()
|
||||
// 获取最近一次提交的作者
|
||||
env.GIT_AUTHOR = sh(script: 'git log -1 --pretty=format:%an',returnStdout: true).trim()
|
||||
// 获取最近一次提交的时间(格式化)
|
||||
env.GIT_COMMIT_TIME = sh(
|
||||
script: 'git log -1 --pretty=format:%ct | xargs -I {} date -d @{} +%Y%m%d-%H%M%S',
|
||||
returnStdout: true
|
||||
).trim()
|
||||
// 获取最近一次提交的备注信息(转义特殊字符,避免构建失败)
|
||||
env.GIT_COMMIT_MSG = sh(script: 'git log -1 --pretty=format:%s | sed -e \'s/"/\\"/g\'', returnStdout: true).trim()
|
||||
|
||||
// Jenkins构建次数
|
||||
def buildNumber = env.BUILD_NUMBER // Jenkins内置变量,直接获取当前Job的构建序号
|
||||
// 当前分支名(处理/为-,如feature/docker_1015 → feature-docker_1015)
|
||||
def branchName = sh(script: 'git rev-parse --abbrev-ref HEAD', returnStdout: true).trim()
|
||||
def formattedBranch = branchName.replace('/', '-').replace('_', '-') // 替换分支名中的/和_为-
|
||||
// 构建时间(格式:202510181215,年-月-日-时-分,无分隔符)
|
||||
def buildTime = sh(script: 'date +%Y%m%d%H%M', returnStdout: true).trim()
|
||||
def defaultTag = "v${buildNumber}_${formattedBranch}_${GIT_COMMIT_SHORT}_${buildTime}"
|
||||
|
||||
def customTag = params.CUSTOM_TAG?.trim()
|
||||
def tagPattern = ~/^[a-zA-Z0-9._-]+$/
|
||||
|
||||
// 最终Tag
|
||||
if (customTag && customTag ==~ tagPattern) {
|
||||
echo "✅ 使用自定义镜像 Tag: ${customTag}"
|
||||
env.IMAGE_TAG = customTag
|
||||
} else if (customTag) {
|
||||
echo "⚠️ 自定义 Tag '${customTag}' 不符合规范,将使用默认生成的 Tag: ${defaultTag}"
|
||||
|
||||
def confirmed = true
|
||||
timeout(time: 1, unit: 'MINUTES') {
|
||||
try {
|
||||
input(
|
||||
message: """⚠️ Tag 命名不规范:
|
||||
${customTag}
|
||||
|
||||
将使用自动生成的 Tag:
|
||||
${defaultTag}
|
||||
|
||||
是否继续构建?""",
|
||||
ok: '确认'
|
||||
)
|
||||
} catch (err) {
|
||||
// 用户点击“取消”或中断
|
||||
echo "🚫 用户取消构建"
|
||||
confirmed = false
|
||||
}
|
||||
}
|
||||
|
||||
if (confirmed) {
|
||||
echo "✅ 用户确认使用自动生成的 Tag:${defaultTag}"
|
||||
env.IMAGE_TAG = defaultTag
|
||||
} else {
|
||||
error("流水线已终止。")
|
||||
}
|
||||
} else {
|
||||
env.IMAGE_TAG = defaultTag
|
||||
echo "未输入自定义 Tag, 使用自动生成规则: ${env.IMAGE_TAG}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('登录仓库') {
|
||||
steps {
|
||||
withCredentials([usernamePassword(
|
||||
credentialsId: env.CREDENTIALS_ID,
|
||||
usernameVariable: 'REGISTRY_USER',
|
||||
passwordVariable: 'REGISTRY_PWD'
|
||||
)]) {
|
||||
sh '''
|
||||
echo "$REGISTRY_PWD" | docker login ${REGISTRY} -u ${REGISTRY_USER} --password-stdin
|
||||
'''
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('构建镜像') {
|
||||
steps {
|
||||
script {
|
||||
// 构建镜像,添加标签信息
|
||||
sh """
|
||||
docker build -t ${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG} \
|
||||
--label "git-branch='${Code_branch}'" \
|
||||
--label "git-commit='${GIT_COMMIT_SHORT}'" \
|
||||
--label "git-author='${GIT_AUTHOR}'" \
|
||||
--label "git-message='${GIT_COMMIT_MSG}'" \
|
||||
--label "build-time='${GIT_COMMIT_TIME}'" \
|
||||
.
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('推送镜像到仓库') {
|
||||
steps {
|
||||
script {
|
||||
sh "docker push ${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG}"
|
||||
echo "推送镜像成功:${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
post {
|
||||
always {
|
||||
script {
|
||||
def keepCount = 2
|
||||
echo "开始清理本地旧镜像,仅保留最近 ${keepCount} 个构建版本"
|
||||
def imagePrefix = "${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}"
|
||||
|
||||
// 获取所有镜像(按创建时间排序,越新的越前)
|
||||
// 格式:Repository:Tag ImageID CreatedAt
|
||||
def allImagesRaw = sh(script: "docker images ${imagePrefix} --format '{{.Repository}}:{{.Tag}} {{.ID}} {{.CreatedAt}}' | sort -rk3", returnStdout: true).trim()
|
||||
if (!allImagesRaw) {
|
||||
echo "未找到任何镜像,无需清理"
|
||||
return
|
||||
}
|
||||
|
||||
def allImages = allImagesRaw.split('\n')
|
||||
if (allImages.size() <= keepCount) {
|
||||
echo "当前镜像数未超过 ${keepCount} 个,无需清理"
|
||||
return
|
||||
}
|
||||
|
||||
def oldImages = allImages.drop(keepCount)
|
||||
echo "发现 ${oldImages.size()} 个旧镜像需要清理"
|
||||
oldImages.each { line ->
|
||||
echo " ${line}"
|
||||
}
|
||||
|
||||
oldImages.each { line ->
|
||||
def parts = line.split(' ')
|
||||
def imageTag = parts[0]
|
||||
def imageId = parts.size() > 1 ? parts[1] : ""
|
||||
|
||||
// 对于标签为<none>的无效镜像,使用镜像ID删除
|
||||
if (imageTag.contains("<none>") && imageId) {
|
||||
echo "删除无效镜像: ${imageId}"
|
||||
sh(returnStatus: true, script: "docker rmi -f ${imageId} || true")
|
||||
} else if (imageId) {
|
||||
// 对于有标签的有效镜像,优先使用镜像ID删除
|
||||
echo "删除旧镜像: ${imageTag} (${imageId})"
|
||||
sh(returnStatus: true, script: "docker rmi -f ${imageId} || true")
|
||||
} else {
|
||||
// 兜底方案,使用标签删除
|
||||
echo "删除旧镜像: ${imageTag}"
|
||||
sh(returnStatus: true, script: "docker rmi -f ${imageTag} || true")
|
||||
}
|
||||
}
|
||||
|
||||
echo "清理完成,当前镜像状态:"
|
||||
sh """
|
||||
docker images ${imagePrefix} --format 'table {{.Repository}}\\t{{.Tag}}\\t{{.CreatedAt}}\\t{{.Size}}'
|
||||
"""
|
||||
|
||||
sh "docker logout ${REGISTRY}"
|
||||
echo "容器仓库已登出,本地凭证已清理"
|
||||
}
|
||||
}
|
||||
success {
|
||||
script {
|
||||
// 1. 准备元数据 (转换所有环境变量为 String)
|
||||
def metadataDir = '/var/lib/jenkins/metadata'
|
||||
def metadataFileRelativePath = "${env.NAMESPACE}-${env.IMAGE_NAME}.json" // 相对于 metadataDir 的文件名
|
||||
def fullMetadataPath = "${metadataDir}/${metadataFileRelativePath}"
|
||||
|
||||
// --- 转换为 String ---
|
||||
def registry = env.REGISTRY as String
|
||||
def namespace = env.NAMESPACE as String
|
||||
def imageName = env.IMAGE_NAME as String
|
||||
def imageTag = env.IMAGE_TAG as String
|
||||
def codeBranch = params.Code_branch as String // 使用 params,因为 Code_branch 是参数
|
||||
def gitCommit = env.GIT_COMMIT_LONG as String
|
||||
def gitAuthor = env.GIT_AUTHOR as String
|
||||
def gitCommitMsg = env.GIT_COMMIT_MSG as String
|
||||
def gitCommitTime = env.GIT_COMMIT_TIME as String
|
||||
def buildNumber = env.BUILD_NUMBER as String
|
||||
// --- 转换为 String ---
|
||||
|
||||
// 2. 准备新数据
|
||||
def newImageData = [
|
||||
image_tag: imageTag, // 使用转换后的变量
|
||||
full_image_name: "${registry}/${namespace}/${imageName}:${imageTag}", // 使用转换后的变量
|
||||
labels: [
|
||||
"git-branch": codeBranch,
|
||||
"git-commit": gitCommit,
|
||||
"git-author": gitAuthor,
|
||||
"git-message": gitCommitMsg,
|
||||
"build-time": gitCommitTime
|
||||
],
|
||||
build_job_number: buildNumber,
|
||||
build_time: new Date().format('yyyy-MM-dd HH:mm:ss') // Jenkins 构建完成时间
|
||||
]
|
||||
|
||||
// 2. 读取现有数据(如果文件存在)
|
||||
def existingDataList = []
|
||||
try {
|
||||
// 使用 readJSON 步骤读取文件内容 (readJSON 会自动处理 LazyMap 问题)
|
||||
def rawExistingData = readJSON file: fullMetadataPath, default: [] // 如果文件不存在,则返回空列表 []
|
||||
|
||||
// --- ✅ 修复:深拷贝 rawExistingData (修正内联代码) ---
|
||||
if (rawExistingData instanceof List) {
|
||||
existingDataList = rawExistingData.collect { item ->
|
||||
if (item instanceof Map) {
|
||||
// 递归深拷贝 Map (使用辅助函数)
|
||||
return deepCopyForSerialization(item)
|
||||
} else {
|
||||
return item
|
||||
}
|
||||
}
|
||||
} else {
|
||||
echo "警告: 元数据文件 ${fullMetadataPath} 格式不正确(非 List 类型),将被覆盖。"
|
||||
existingDataList = []
|
||||
}
|
||||
// --- 结束修复 ---
|
||||
|
||||
} catch (Exception e) {
|
||||
// readJSON 在文件不存在时通常会返回 default 值,但如果文件存在但格式错误,会抛出异常
|
||||
echo "警告: 读取元数据文件 ${fullMetadataPath} 失败或格式错误: ${e.getMessage()},将被覆盖。"
|
||||
// 确保目录存在
|
||||
sh "mkdir -p ${metadataDir}"
|
||||
existingDataList = [] // 重置为新列表
|
||||
}
|
||||
|
||||
// 3. 将新数据添加到列表开头(最新的在前)
|
||||
existingDataList.add(0, newImageData)
|
||||
|
||||
// 4. 限制列表大小为 20
|
||||
if (existingDataList.size() > 20) {
|
||||
existingDataList = existingDataList.take(20)
|
||||
}
|
||||
|
||||
// 5. 使用 writeJSON 步骤写入文件 (writeJSON 会自动处理 Map 的序列化)
|
||||
writeJSON file: fullMetadataPath, json: existingDataList, pretty: 2 // pretty: 2 表示格式化 JSON (2 个空格缩进)
|
||||
|
||||
echo "镜像元数据已存储到: ${fullMetadataPath}"
|
||||
|
||||
// 输出构建结果
|
||||
echo """
|
||||
镜像地址:${registry}/${namespace}/${imageName}:${imageTag}
|
||||
对应代码提交哈希:${gitCommit}
|
||||
对应代码分支:${codeBranch}
|
||||
代码提交者:${gitAuthor}
|
||||
提交备注:${gitCommitMsg}
|
||||
""".stripIndent()
|
||||
}
|
||||
}
|
||||
failure {
|
||||
// 输出构建结果
|
||||
echo "有片段失败!"
|
||||
}
|
||||
}
|
||||
}
|
||||
309
SCM/构建镜像/build_image_lessie_agents_v2.groovy
Normal file
309
SCM/构建镜像/build_image_lessie_agents_v2.groovy
Normal file
@@ -0,0 +1,309 @@
|
||||
// --- 辅助函数:深拷贝对象以确保可序列化 ---
|
||||
def deepCopyForSerialization(obj) {
|
||||
if (obj instanceof Map) {
|
||||
// 创建新的 LinkedHashMap,递归拷贝值
|
||||
return obj.collectEntries { k, v -> [(k): deepCopyForSerialization(v)] }
|
||||
} else if (obj instanceof List) {
|
||||
// 创建新的 ArrayList,递归拷贝元素
|
||||
return obj.collect { item -> deepCopyForSerialization(item) }
|
||||
} else if (obj instanceof String || obj instanceof Number || obj instanceof Boolean || obj == null) {
|
||||
return obj
|
||||
} else {
|
||||
return obj.toString()
|
||||
}
|
||||
}
|
||||
// --- 结束辅助函数 ---
|
||||
|
||||
pipeline {
|
||||
agent any
|
||||
parameters {
|
||||
gitParameter(
|
||||
branchFilter: 'origin/(.*)',
|
||||
defaultValue: 'dxin',
|
||||
name: 'Code_branch',
|
||||
type: 'PT_BRANCH_TAG',
|
||||
selectedValue: 'DEFAULT',
|
||||
sortMode: 'NONE',
|
||||
description: '选择代码分支:',
|
||||
quickFilterEnabled: true,
|
||||
tagFilter: '*',
|
||||
listSize: "1"
|
||||
)
|
||||
choice(
|
||||
name: 'NAME_SPACES',
|
||||
choices: ['sit', 'test', 'prod'],
|
||||
description: '选择存放镜像的仓库命名空间:'
|
||||
)
|
||||
string(
|
||||
name: 'CUSTOM_TAG',
|
||||
defaultValue: '',
|
||||
description: '可选:自定义镜像 Tag (字母、数字、点、下划线、短横线), 留空则自动生成 “ v+构建次数_分支名_短哈希_构建时间 ”'
|
||||
)
|
||||
}
|
||||
environment {
|
||||
REGISTRY = "uswccr.ccs.tencentyun.com" // 镜像仓库地址
|
||||
NAMESPACE = "lessie${params.NAME_SPACES}" // 命名空间
|
||||
IMAGE_NAME = "lessie-sourcing-agents" // 镜像名(固定前缀)
|
||||
CREDENTIALS_ID = "dxin_img_hub_auth" // 容器仓库凭证ID
|
||||
}
|
||||
|
||||
stages {
|
||||
stage('拉取代码') {
|
||||
steps {
|
||||
// 拉取指定分支代码(通过参数 params.Code_branch 动态指定)
|
||||
git branch: "${params.Code_branch}",
|
||||
credentialsId: 'fly_gitlab_auth',
|
||||
url: 'http://172.24.16.20/python/lessie-sourcing-agents.git'
|
||||
}
|
||||
}
|
||||
|
||||
stage('获取信息') {
|
||||
steps {
|
||||
script {
|
||||
env.Code_branch = "${params.Code_branch}"
|
||||
env.GIT_COMMIT_SHORT = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
|
||||
env.GIT_COMMIT_LONG = sh(script: 'git rev-parse HEAD', returnStdout: true).trim()
|
||||
env.GIT_AUTHOR = sh(script: 'git log -1 --pretty=format:%an', returnStdout: true).trim()
|
||||
env.GIT_COMMIT_TIME = sh(script: 'git log -1 --pretty=format:%ct | xargs -I {} date -d @{} +%Y%m%d-%H%M%S', returnStdout: true).trim()
|
||||
env.GIT_COMMIT_MSG = sh(script: 'git log -1 --pretty=format:%s | sed -e \'s/"/\\"/g\'', returnStdout: true).trim()
|
||||
|
||||
def buildNumber = env.BUILD_NUMBER
|
||||
def branchName = sh(script: 'git rev-parse --abbrev-ref HEAD', returnStdout: true).trim()
|
||||
def formattedBranch = branchName.replace('/', '-').replace('_', '-')
|
||||
def buildTime = sh(script: 'date +%Y%m%d%H%M', returnStdout: true).trim()
|
||||
def defaultTag = "v${buildNumber}_${formattedBranch}_${GIT_COMMIT_SHORT}_${buildTime}"
|
||||
|
||||
def customTag = params.CUSTOM_TAG?.trim()
|
||||
def tagPattern = ~/^[a-zA-Z0-9._-]+$/
|
||||
|
||||
if (customTag && customTag ==~ tagPattern) {
|
||||
echo "✅ 使用自定义镜像 Tag: ${customTag}"
|
||||
env.IMAGE_TAG = customTag
|
||||
} else if (customTag) {
|
||||
echo "⚠️ 自定义 Tag '${customTag}' 不符合规范,将使用默认生成的 Tag: ${defaultTag}"
|
||||
|
||||
def confirmed = true
|
||||
timeout(time: 1, unit: 'MINUTES') {
|
||||
try {
|
||||
input(
|
||||
message: """⚠️ Tag 命名不规范:
|
||||
${customTag}
|
||||
|
||||
将使用自动生成的 Tag:
|
||||
${defaultTag}
|
||||
|
||||
是否继续构建?""",
|
||||
ok: '确认'
|
||||
)
|
||||
} catch (err) {
|
||||
// 用户点击“取消”或中断
|
||||
echo "🚫 用户取消构建"
|
||||
confirmed = false
|
||||
}
|
||||
}
|
||||
|
||||
if (confirmed) {
|
||||
echo "✅ 用户确认使用自动生成的 Tag:${defaultTag}"
|
||||
env.IMAGE_TAG = defaultTag
|
||||
} else {
|
||||
error("流水线已终止。")
|
||||
}
|
||||
} else {
|
||||
env.IMAGE_TAG = defaultTag
|
||||
echo "未输入自定义 Tag, 使用自动生成规则: ${env.IMAGE_TAG}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage('登录仓库') {
|
||||
steps {
|
||||
withCredentials([usernamePassword(
|
||||
credentialsId: env.CREDENTIALS_ID,
|
||||
usernameVariable: 'REGISTRY_USER',
|
||||
passwordVariable: 'REGISTRY_PWD'
|
||||
)]) {
|
||||
sh '''
|
||||
echo "$REGISTRY_PWD" | docker login ${REGISTRY} -u ${REGISTRY_USER} --password-stdin
|
||||
'''
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage('构建镜像') {
|
||||
steps {
|
||||
script {
|
||||
// 构建镜像,添加标签信息
|
||||
sh """
|
||||
docker build -t ${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG} \
|
||||
--label "git-branch='${Code_branch}'" \
|
||||
--label "git-commit='${GIT_COMMIT_SHORT}'" \
|
||||
--label "git-author='${GIT_AUTHOR}'" \
|
||||
--label "git-message='${GIT_COMMIT_MSG}'" \
|
||||
--label "build-time='${GIT_COMMIT_TIME}'" \
|
||||
.
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage('推送镜像') {
|
||||
steps {
|
||||
script {
|
||||
sh "docker push ${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG}"
|
||||
echo "推送镜像成功:${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
post {
|
||||
always {
|
||||
script {
|
||||
def keepCount = 3
|
||||
echo "开始清理本地旧镜像,仅保留最近 ${keepCount} 个构建版本"
|
||||
def imagePrefix = "${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}"
|
||||
|
||||
// 获取所有镜像(按创建时间排序,越新的越前)
|
||||
// 格式:Repository:Tag ImageID CreatedAt
|
||||
def allImagesRaw = sh(script: "docker images ${imagePrefix} --format '{{.Repository}}:{{.Tag}} {{.ID}} {{.CreatedAt}}' | sort -rk3", returnStdout: true).trim()
|
||||
if (!allImagesRaw) {
|
||||
echo "未找到任何镜像,无需清理"
|
||||
return
|
||||
}
|
||||
|
||||
def allImages = allImagesRaw.split('\n')
|
||||
if (allImages.size() <= keepCount) {
|
||||
echo "当前镜像数未超过 ${keepCount} 个,无需清理"
|
||||
return
|
||||
}
|
||||
|
||||
def oldImages = allImages.drop(keepCount)
|
||||
echo "发现 ${oldImages.size()} 个旧镜像需要清理"
|
||||
oldImages.each { line ->
|
||||
echo " ${line}"
|
||||
}
|
||||
|
||||
oldImages.each { line ->
|
||||
def parts = line.split(' ')
|
||||
def imageTag = parts[0]
|
||||
def imageId = parts.size() > 1 ? parts[1] : ""
|
||||
|
||||
// 对于标签为<none>的无效镜像,使用镜像ID删除
|
||||
if (imageTag.contains("<none>") && imageId) {
|
||||
echo "删除无效镜像: ${imageId}"
|
||||
sh(returnStatus: true, script: "docker rmi -f ${imageId} || true")
|
||||
} else if (imageId) {
|
||||
// 对于有标签的有效镜像,优先使用镜像ID删除
|
||||
echo "删除旧镜像: ${imageTag} (${imageId})"
|
||||
sh(returnStatus: true, script: "docker rmi -f ${imageId} || true")
|
||||
} else {
|
||||
// 兜底方案,使用标签删除
|
||||
echo "删除旧镜像: ${imageTag}"
|
||||
sh(returnStatus: true, script: "docker rmi -f ${imageTag} || true")
|
||||
}
|
||||
}
|
||||
|
||||
echo "清理完成,当前镜像状态:"
|
||||
sh """
|
||||
docker images ${imagePrefix} --format 'table {{.Repository}}\\t{{.Tag}}\\t{{.CreatedAt}}\\t{{.Size}}'
|
||||
"""
|
||||
|
||||
sh "docker logout ${REGISTRY}"
|
||||
echo "容器仓库已登出,本地凭证已清理"
|
||||
}
|
||||
}
|
||||
success {
|
||||
script {
|
||||
// 1. 准备元数据 (转换所有环境变量为 String)
|
||||
def metadataDir = '/var/lib/jenkins/metadata'
|
||||
def metadataFileRelativePath = "${env.NAMESPACE}-${env.IMAGE_NAME}.json" // 相对于 metadataDir 的文件名
|
||||
def fullMetadataPath = "${metadataDir}/${metadataFileRelativePath}"
|
||||
|
||||
// --- 转换为 String ---
|
||||
def registry = env.REGISTRY as String
|
||||
def namespace = env.NAMESPACE as String
|
||||
def imageName = env.IMAGE_NAME as String
|
||||
def imageTag = env.IMAGE_TAG as String
|
||||
def codeBranch = params.Code_branch as String // 使用 params,因为 Code_branch 是参数
|
||||
def gitCommit = env.GIT_COMMIT_LONG as String
|
||||
def gitAuthor = env.GIT_AUTHOR as String
|
||||
def gitCommitMsg = env.GIT_COMMIT_MSG as String
|
||||
def gitCommitTime = env.GIT_COMMIT_TIME as String
|
||||
def buildNumber = env.BUILD_NUMBER as String
|
||||
// --- 转换为 String ---
|
||||
|
||||
// 2. 准备新数据
|
||||
def newImageData = [
|
||||
image_tag: imageTag, // 使用转换后的变量
|
||||
full_image_name: "${registry}/${namespace}/${imageName}:${imageTag}", // 使用转换后的变量
|
||||
labels: [
|
||||
"git-branch": codeBranch,
|
||||
"git-commit": gitCommit,
|
||||
"git-author": gitAuthor,
|
||||
"git-message": gitCommitMsg,
|
||||
"build-time": gitCommitTime
|
||||
],
|
||||
build_job_number: buildNumber,
|
||||
build_time: new Date().format('yyyy-MM-dd HH:mm:ss') // Jenkins 构建完成时间
|
||||
]
|
||||
|
||||
// 2. 读取现有数据(如果文件存在)
|
||||
def existingDataList = []
|
||||
try {
|
||||
// 使用 readJSON 步骤读取文件内容 (readJSON 会自动处理 LazyMap 问题)
|
||||
def rawExistingData = readJSON file: fullMetadataPath, default: [] // 如果文件不存在,则返回空列表 []
|
||||
|
||||
// --- ✅ 修复:深拷贝 rawExistingData (修正内联代码) ---
|
||||
if (rawExistingData instanceof List) {
|
||||
existingDataList = rawExistingData.collect { item ->
|
||||
if (item instanceof Map) {
|
||||
// 递归深拷贝 Map (使用辅助函数)
|
||||
return deepCopyForSerialization(item)
|
||||
} else {
|
||||
return item
|
||||
}
|
||||
}
|
||||
} else {
|
||||
echo "警告: 元数据文件 ${fullMetadataPath} 格式不正确(非 List 类型),将被覆盖。"
|
||||
existingDataList = []
|
||||
}
|
||||
// --- 结束修复 ---
|
||||
|
||||
} catch (Exception e) {
|
||||
// readJSON 在文件不存在时通常会返回 default 值,但如果文件存在但格式错误,会抛出异常
|
||||
echo "警告: 读取元数据文件 ${fullMetadataPath} 失败或格式错误: ${e.getMessage()},将被覆盖。"
|
||||
// 确保目录存在
|
||||
sh "mkdir -p ${metadataDir}"
|
||||
existingDataList = [] // 重置为新列表
|
||||
}
|
||||
|
||||
// 3. 将新数据添加到列表开头(最新的在前)
|
||||
existingDataList.add(0, newImageData)
|
||||
|
||||
// 4. 限制列表大小为 20
|
||||
if (existingDataList.size() > 20) {
|
||||
existingDataList = existingDataList.take(20)
|
||||
}
|
||||
|
||||
// 5. 使用 writeJSON 步骤写入文件 (writeJSON 会自动处理 Map 的序列化)
|
||||
writeJSON file: fullMetadataPath, json: existingDataList, pretty: 2 // pretty: 2 表示格式化 JSON (2 个空格缩进)
|
||||
|
||||
echo "镜像元数据已存储到: ${fullMetadataPath}"
|
||||
|
||||
// 输出构建结果
|
||||
echo """
|
||||
镜像地址:${registry}/${namespace}/${imageName}:${imageTag}
|
||||
对应代码提交哈希:${gitCommit}
|
||||
对应代码分支:${codeBranch}
|
||||
代码提交者:${gitAuthor}
|
||||
提交备注:${gitCommitMsg}
|
||||
""".stripIndent()
|
||||
}
|
||||
}
|
||||
failure {
|
||||
// 输出构建结果
|
||||
echo "有失败步骤!"
|
||||
}
|
||||
}
|
||||
}
|
||||
345
SCM/构建镜像/build_image_lessie_ai_web_v2.groovy
Normal file
345
SCM/构建镜像/build_image_lessie_ai_web_v2.groovy
Normal file
@@ -0,0 +1,345 @@
|
||||
// --- 辅助函数:深拷贝对象以确保可序列化 ---
|
||||
def deepCopyForSerialization(obj) {
|
||||
if (obj instanceof Map) {
|
||||
// 创建新的 LinkedHashMap,递归拷贝值
|
||||
return obj.collectEntries { k, v -> [(k): deepCopyForSerialization(v)] }
|
||||
} else if (obj instanceof List) {
|
||||
// 创建新的 ArrayList,递归拷贝元素
|
||||
return obj.collect { item -> deepCopyForSerialization(item) }
|
||||
} else if (obj instanceof String || obj instanceof Number || obj instanceof Boolean || obj == null) {
|
||||
return obj
|
||||
} else {
|
||||
return obj.toString()
|
||||
}
|
||||
}
|
||||
// --- 结束辅助函数 ---
|
||||
|
||||
|
||||
pipeline {
|
||||
agent any
|
||||
|
||||
parameters {
|
||||
gitParameter(
|
||||
branchFilter: 'origin/(.*)',
|
||||
defaultValue: 'dxin',
|
||||
name: 'Code_branch',
|
||||
type: 'PT_BRANCH_TAG',
|
||||
selectedValue: 'DEFAULT',
|
||||
sortMode: 'NONE',
|
||||
description: '选择代码分支: ',
|
||||
quickFilterEnabled: true,
|
||||
tagFilter: '*',
|
||||
listSize: "1"
|
||||
)
|
||||
choice(
|
||||
name: 'NAME_SPACES',
|
||||
choices: ['sit', 'test', 'prod'],
|
||||
description: '选择存放镜像的仓库命名空间:'
|
||||
)
|
||||
choice(
|
||||
name: 'BUILD_ENV',
|
||||
choices: ['im', 's2', 'prod'],
|
||||
description: '选择构建的环境配置, 默认为 pnpm build:im 构建'
|
||||
)
|
||||
string(
|
||||
name: 'CUSTOM_TAG',
|
||||
defaultValue: '',
|
||||
description: '可选:自定义镜像 Tag (字母、数字、点、下划线、短横线), 如 v0.0.1, 留空则自动生成 “ v+构建次数_分支名_短哈希_构建时间 ”'
|
||||
)
|
||||
}
|
||||
environment {
|
||||
REGISTRY = "uswccr.ccs.tencentyun.com" // 镜像仓库地址
|
||||
NAMESPACE = "lessie${params.NAME_SPACES}" // 命名空间根据choices的选择拼接
|
||||
IMAGE_NAME = "lessie-ai-web" // 镜像名(固定前缀)
|
||||
CREDENTIALS_ID = "dxin_img_hub_auth" // 容器仓库凭证ID
|
||||
}
|
||||
|
||||
stages {
|
||||
stage('拉取代码') {
|
||||
steps {
|
||||
git branch: "${params.Code_branch}",
|
||||
credentialsId: 'fly_gitlab_auth',
|
||||
url: 'http://172.24.16.20/web/jennie.git'
|
||||
}
|
||||
}
|
||||
|
||||
stage('获取信息') {
|
||||
steps {
|
||||
script {
|
||||
// 获取分支名
|
||||
env.Code_branch = "${params.Code_branch}"
|
||||
// 获取最近一次提交的哈希值(短格式,前8位)
|
||||
env.GIT_COMMIT_SHORT = sh(script: 'git rev-parse --short HEAD',returnStdout: true).trim()
|
||||
// 获取最近一次提交的哈希值(全格式)
|
||||
env.GIT_COMMIT_LONG = sh(script: 'git rev-parse HEAD', returnStdout: true).trim()
|
||||
// 获取最近一次提交的作者
|
||||
env.GIT_AUTHOR = sh(script: 'git log -1 --pretty=format:%an',returnStdout: true).trim()
|
||||
// 获取最近一次提交的时间(格式化)
|
||||
env.GIT_COMMIT_TIME = sh(
|
||||
script: 'git log -1 --pretty=format:%ct | xargs -I {} date -d @{} +%Y%m%d-%H%M%S',
|
||||
returnStdout: true
|
||||
).trim()
|
||||
// 获取最近一次提交的备注信息(转义特殊字符,避免构建失败)
|
||||
env.GIT_COMMIT_MSG = sh(script: 'git log -1 --pretty=format:%s | sed -e \'s/"/\\"/g\'', returnStdout: true).trim()
|
||||
|
||||
// Jenkins构建次数
|
||||
def buildNumber = env.BUILD_NUMBER // Jenkins内置变量,直接获取当前Job的构建序号
|
||||
// 当前分支名(处理/为-,如feature/docker_1015 → feature-docker_1015)
|
||||
def branchName = sh(script: 'git rev-parse --abbrev-ref HEAD', returnStdout: true).trim()
|
||||
def formattedBranch = branchName.replace('/', '-').replace('_', '-') // 替换分支名中的/和_为-
|
||||
// 构建时间(格式:202510181215,年-月-日-时-分,无分隔符)
|
||||
def buildTime = sh(script: 'date +%Y%m%d%H%M', returnStdout: true).trim()
|
||||
def defaultTag = "v${buildNumber}_${formattedBranch}_${GIT_COMMIT_SHORT}_${buildTime}"
|
||||
|
||||
def customTag = params.CUSTOM_TAG?.trim()
|
||||
def tagPattern = ~/^[a-zA-Z0-9._-]+$/
|
||||
|
||||
// 判断最终Tag
|
||||
if (customTag && customTag ==~ tagPattern) {
|
||||
echo "✅ 使用自定义镜像 Tag: ${customTag}"
|
||||
env.IMAGE_TAG = customTag
|
||||
} else if (customTag) {
|
||||
echo "⚠️ 自定义 Tag '${customTag}' 不符合规范,将使用默认生成的 Tag: ${defaultTag}"
|
||||
|
||||
def confirmed = true
|
||||
timeout(time: 1, unit: 'MINUTES') {
|
||||
try {
|
||||
input(
|
||||
message: """⚠️ Tag 命名不规范:
|
||||
${customTag}
|
||||
|
||||
将使用自动生成的 Tag:
|
||||
${defaultTag}
|
||||
|
||||
是否继续构建?""",
|
||||
ok: '确认'
|
||||
)
|
||||
} catch (err) {
|
||||
// 用户点击“取消”或中断
|
||||
echo "🚫 用户取消构建"
|
||||
confirmed = false
|
||||
}
|
||||
}
|
||||
|
||||
if (confirmed) {
|
||||
echo "✅ 用户确认使用自动生成的 Tag:${defaultTag}"
|
||||
env.IMAGE_TAG = defaultTag
|
||||
} else {
|
||||
error("流水线已终止。")
|
||||
}
|
||||
} else {
|
||||
env.IMAGE_TAG = defaultTag
|
||||
echo "未输入自定义 Tag, 使用自动生成规则: ${env.IMAGE_TAG}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage('pnpm i&b') {
|
||||
steps {
|
||||
script {
|
||||
def buildEnv = params.BUILD_ENV // 获取参数
|
||||
sh """
|
||||
export PATH="/data/nvm/versions/node/v20.15.0/bin:$PATH"
|
||||
echo "开始安装依赖包"
|
||||
cd ${WORKSPACE}/ && rm -rf node_modules && pnpm install
|
||||
echo "开始构建"
|
||||
pnpm build:${buildEnv}
|
||||
mv dist/main/index.html dist/
|
||||
chmod -R 755 dist/
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage('登录容器') {
|
||||
steps {
|
||||
withCredentials([usernamePassword(
|
||||
credentialsId: env.CREDENTIALS_ID,
|
||||
usernameVariable: 'REGISTRY_USER',
|
||||
passwordVariable: 'REGISTRY_PWD'
|
||||
)]) {
|
||||
sh '''
|
||||
echo "$REGISTRY_PWD" | docker login ${REGISTRY} -u ${REGISTRY_USER} --password-stdin
|
||||
'''
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage('构建容器') {
|
||||
steps {
|
||||
script {
|
||||
// 构建镜像,添加标签信息
|
||||
sh """
|
||||
docker build -t ${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG} \
|
||||
--label "git-branch='${Code_branch}'" \
|
||||
--label "git-commit='${GIT_COMMIT_SHORT}'" \
|
||||
--label "git-author='${GIT_AUTHOR}'" \
|
||||
--label "git-message='${GIT_COMMIT_MSG}'" \
|
||||
--label "build-time='${GIT_COMMIT_TIME}'" \
|
||||
.
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage('推送镜像') {
|
||||
steps {
|
||||
script {
|
||||
sh "docker push ${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG}"
|
||||
echo "推送镜像成功:${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
post {
|
||||
always {
|
||||
script {
|
||||
def keepCount = 3
|
||||
echo "开始清理本地旧镜像,仅保留最近 ${keepCount} 个构建版本"
|
||||
def imagePrefix = "${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}"
|
||||
|
||||
// 获取所有镜像(按创建时间排序,越新的越前)
|
||||
// 格式:Repository:Tag ImageID CreatedAt
|
||||
def allImagesRaw = sh(script: "docker images ${imagePrefix} --format '{{.Repository}}:{{.Tag}} {{.ID}} {{.CreatedAt}}' | sort -rk3", returnStdout: true).trim()
|
||||
if (!allImagesRaw) {
|
||||
echo "未找到任何镜像,无需清理"
|
||||
return
|
||||
}
|
||||
|
||||
def allImages = allImagesRaw.split('\n')
|
||||
if (allImages.size() <= keepCount) {
|
||||
echo "当前镜像数未超过 ${keepCount} 个,无需清理"
|
||||
return
|
||||
}
|
||||
|
||||
def oldImages = allImages.drop(keepCount)
|
||||
echo "发现 ${oldImages.size()} 个旧镜像需要清理"
|
||||
oldImages.each { line ->
|
||||
echo " ${line}"
|
||||
}
|
||||
|
||||
oldImages.each { line ->
|
||||
def parts = line.split(' ')
|
||||
def imageTag = parts[0]
|
||||
def imageId = parts.size() > 1 ? parts[1] : ""
|
||||
|
||||
// 对于标签为<none>的无效镜像,使用镜像ID删除
|
||||
if (imageTag.contains("<none>") && imageId) {
|
||||
echo "删除无效镜像: ${imageId}"
|
||||
sh(returnStatus: true, script: "docker rmi -f ${imageId} || true")
|
||||
} else if (imageId) {
|
||||
// 对于有标签的有效镜像,优先使用镜像ID删除
|
||||
echo "删除旧镜像: ${imageTag} (${imageId})"
|
||||
sh(returnStatus: true, script: "docker rmi -f ${imageId} || true")
|
||||
} else {
|
||||
// 兜底方案,使用标签删除
|
||||
echo "删除旧镜像: ${imageTag}"
|
||||
sh(returnStatus: true, script: "docker rmi -f ${imageTag} || true")
|
||||
}
|
||||
}
|
||||
|
||||
echo "清理完成,当前镜像状态:"
|
||||
sh """
|
||||
docker images ${imagePrefix} --format 'table {{.Repository}}\\t{{.Tag}}\\t{{.CreatedAt}}\\t{{.Size}}'
|
||||
"""
|
||||
|
||||
sh "docker logout ${REGISTRY}"
|
||||
echo "容器仓库已登出,本地凭证已清理"
|
||||
}
|
||||
}
|
||||
success {
|
||||
script {
|
||||
// 1. 准备元数据 (转换所有环境变量为 String)
|
||||
def metadataDir = '/var/lib/jenkins/metadata'
|
||||
def metadataFileRelativePath = "${env.NAMESPACE}-${env.IMAGE_NAME}.json" // 相对于 metadataDir 的文件名
|
||||
def fullMetadataPath = "${metadataDir}/${metadataFileRelativePath}"
|
||||
|
||||
// --- 转换为 String ---
|
||||
def registry = env.REGISTRY as String
|
||||
def namespace = env.NAMESPACE as String
|
||||
def imageName = env.IMAGE_NAME as String
|
||||
def imageTag = env.IMAGE_TAG as String
|
||||
def codeBranch = params.Code_branch as String // 使用 params,因为 Code_branch 是参数
|
||||
def gitCommit = env.GIT_COMMIT_LONG as String
|
||||
def gitAuthor = env.GIT_AUTHOR as String
|
||||
def gitCommitMsg = env.GIT_COMMIT_MSG as String
|
||||
def gitCommitTime = env.GIT_COMMIT_TIME as String
|
||||
def buildNumber = env.BUILD_NUMBER as String
|
||||
// --- 转换为 String ---
|
||||
|
||||
// 2. 准备新数据
|
||||
def newImageData = [
|
||||
image_tag: imageTag, // 使用转换后的变量
|
||||
full_image_name: "${registry}/${namespace}/${imageName}:${imageTag}", // 使用转换后的变量
|
||||
labels: [
|
||||
"git-branch": codeBranch,
|
||||
"git-commit": gitCommit,
|
||||
"git-author": gitAuthor,
|
||||
"git-message": gitCommitMsg,
|
||||
"build-time": gitCommitTime
|
||||
],
|
||||
build_job_number: buildNumber,
|
||||
build_time: new Date().format('yyyy-MM-dd HH:mm:ss') // Jenkins 构建完成时间
|
||||
]
|
||||
|
||||
// 2. 读取现有数据(如果文件存在)
|
||||
def existingDataList = []
|
||||
try {
|
||||
// 使用 readJSON 步骤读取文件内容 (readJSON 会自动处理 LazyMap 问题)
|
||||
def rawExistingData = readJSON file: fullMetadataPath, default: [] // 如果文件不存在,则返回空列表 []
|
||||
|
||||
// --- ✅ 修复:深拷贝 rawExistingData (修正内联代码) ---
|
||||
if (rawExistingData instanceof List) {
|
||||
existingDataList = rawExistingData.collect { item ->
|
||||
if (item instanceof Map) {
|
||||
// 递归深拷贝 Map (使用辅助函数)
|
||||
return deepCopyForSerialization(item)
|
||||
} else {
|
||||
return item
|
||||
}
|
||||
}
|
||||
} else {
|
||||
echo "警告: 元数据文件 ${fullMetadataPath} 格式不正确(非 List 类型),将被覆盖。"
|
||||
existingDataList = []
|
||||
}
|
||||
// --- 结束修复 ---
|
||||
|
||||
} catch (Exception e) {
|
||||
// readJSON 在文件不存在时通常会返回 default 值,但如果文件存在但格式错误,会抛出异常
|
||||
echo "警告: 读取元数据文件 ${fullMetadataPath} 失败或格式错误: ${e.getMessage()},将被覆盖。"
|
||||
// 确保目录存在
|
||||
sh "mkdir -p ${metadataDir}"
|
||||
existingDataList = [] // 重置为新列表
|
||||
}
|
||||
|
||||
// 3. 将新数据添加到列表开头(最新的在前)
|
||||
existingDataList.add(0, newImageData)
|
||||
|
||||
// 4. 限制列表大小为 20
|
||||
if (existingDataList.size() > 20) {
|
||||
existingDataList = existingDataList.take(20)
|
||||
}
|
||||
|
||||
// 5. 使用 writeJSON 步骤写入文件 (writeJSON 会自动处理 Map 的序列化)
|
||||
writeJSON file: fullMetadataPath, json: existingDataList, pretty: 2 // pretty: 2 表示格式化 JSON (2 个空格缩进)
|
||||
|
||||
echo "镜像元数据已存储到: ${fullMetadataPath}"
|
||||
|
||||
// 输出构建结果
|
||||
echo """
|
||||
镜像地址:${registry}/${namespace}/${imageName}:${imageTag}
|
||||
对应代码提交哈希:${gitCommit}
|
||||
对应代码分支:${codeBranch}
|
||||
代码提交者:${gitAuthor}
|
||||
提交备注:${gitCommitMsg}
|
||||
""".stripIndent()
|
||||
}
|
||||
}
|
||||
failure {
|
||||
// 输出构建结果
|
||||
echo "部署有错误,请检查!"
|
||||
}
|
||||
}
|
||||
}
|
||||
331
SCM/构建镜像/build_image_lessie_go_api_v2.groovy
Normal file
331
SCM/构建镜像/build_image_lessie_go_api_v2.groovy
Normal file
@@ -0,0 +1,331 @@
|
||||
// --- 辅助函数:深拷贝对象以确保可序列化 ---
|
||||
def deepCopyForSerialization(obj) {
|
||||
if (obj instanceof Map) {
|
||||
// 创建新的 LinkedHashMap,递归拷贝值
|
||||
return obj.collectEntries { k, v -> [(k): deepCopyForSerialization(v)] }
|
||||
} else if (obj instanceof List) {
|
||||
// 创建新的 ArrayList,递归拷贝元素
|
||||
return obj.collect { item -> deepCopyForSerialization(item) }
|
||||
} else if (obj instanceof String || obj instanceof Number || obj instanceof Boolean || obj == null) {
|
||||
return obj
|
||||
} else {
|
||||
return obj.toString()
|
||||
}
|
||||
}
|
||||
// --- 结束辅助函数 ---
|
||||
|
||||
pipeline {
|
||||
agent any
|
||||
tools{
|
||||
go 'go1.24.0'
|
||||
}
|
||||
parameters {
|
||||
gitParameter(
|
||||
branchFilter: 'origin/(.*)',
|
||||
defaultValue: 'dxin',
|
||||
name: 'Code_branch',
|
||||
type: 'PT_BRANCH_TAG',
|
||||
selectedValue: 'DEFAULT',
|
||||
sortMode: 'NONE',
|
||||
description: '选择代码分支: ',
|
||||
quickFilterEnabled: true,
|
||||
tagFilter: '*',
|
||||
listSize: "1"
|
||||
)
|
||||
choice(
|
||||
name: 'NAME_SPACES',
|
||||
choices: ['sit', 'test', 'prod'],
|
||||
description: '选择存放镜像的仓库命名空间:'
|
||||
)
|
||||
string(
|
||||
name: 'CUSTOM_TAG',
|
||||
defaultValue: '',
|
||||
description: '可选:自定义镜像 Tag (字母、数字、点、下划线、短横线), 留空则自动生成 “ v+构建次数_分支名_短哈希_构建时间 ”'
|
||||
)
|
||||
}
|
||||
environment {
|
||||
REGISTRY = "uswccr.ccs.tencentyun.com" // 镜像仓库地址
|
||||
NAMESPACE = "lessie${params.NAME_SPACES}" // 命名空间根据choices的选择拼接(这个镜像仓库的命名空间)
|
||||
IMAGE_NAME = "go_lessie-sourcing-api" // 镜像名(固定前缀,这里是指构建后的镜像名)
|
||||
CREDENTIALS_ID = "dxin_img_hub_auth" // 容器仓库凭证ID
|
||||
}
|
||||
stages {
|
||||
stage('Checkout代码') {
|
||||
steps {
|
||||
git branch: "${params.Code_branch}",
|
||||
credentialsId: 'fly_gitlab_auth',
|
||||
url: 'http://172.24.16.20/go/lessie-sourcing-api.git'
|
||||
}
|
||||
}
|
||||
stage('获取信息') {
|
||||
steps {
|
||||
script {
|
||||
// 获取分支名
|
||||
env.Code_branch = "${params.Code_branch}"
|
||||
// 获取最近一次提交的哈希值(短格式,前8位)
|
||||
env.GIT_COMMIT_SHORT = sh(script: 'git rev-parse --short HEAD',returnStdout: true).trim()
|
||||
// 获取最近一次提交的哈希值(全格式)
|
||||
env.GIT_COMMIT_LONG = sh(script: 'git rev-parse HEAD', returnStdout: true).trim()
|
||||
// 获取最近一次提交的作者
|
||||
env.GIT_AUTHOR = sh(script: 'git log -1 --pretty=format:%an',returnStdout: true).trim()
|
||||
// 获取最近一次提交的时间(格式化)
|
||||
env.GIT_COMMIT_TIME = sh(
|
||||
script: 'git log -1 --pretty=format:%ct | xargs -I {} date -d @{} +%Y%m%d-%H%M%S',
|
||||
returnStdout: true
|
||||
).trim()
|
||||
// 获取最近一次提交的备注信息(转义特殊字符,避免构建失败)
|
||||
env.GIT_COMMIT_MSG = sh(script: 'git log -1 --pretty=format:%s | sed -e \'s/"/\\"/g\'', returnStdout: true).trim()
|
||||
|
||||
// Jenkins构建次数
|
||||
def buildNumber = env.BUILD_NUMBER // Jenkins内置变量,直接获取当前Job的构建序号
|
||||
// 当前分支名(处理/为-,如feature/docker_1015 → feature-docker_1015)
|
||||
def branchName = sh(script: 'git rev-parse --abbrev-ref HEAD', returnStdout: true).trim()
|
||||
def formattedBranch = branchName.replace('/', '-').replace('_', '-') // 替换分支名中的/和_为-
|
||||
// 构建时间(格式:202510181215,年-月-日-时-分,无分隔符)
|
||||
def buildTime = sh(script: 'date +%Y%m%d%H%M', returnStdout: true).trim()
|
||||
def defaultTag = "v${buildNumber}_${formattedBranch}_${GIT_COMMIT_SHORT}_${buildTime}"
|
||||
|
||||
def customTag = params.CUSTOM_TAG?.trim()
|
||||
def tagPattern = ~/^[a-zA-Z0-9._-]+$/
|
||||
|
||||
if (customTag && customTag ==~ tagPattern) {
|
||||
echo "✅ 使用自定义镜像 Tag: ${customTag}"
|
||||
env.IMAGE_TAG = customTag
|
||||
} else if (customTag) {
|
||||
echo "⚠️ 自定义 Tag '${customTag}' 不符合规范,将使用默认生成的 Tag: ${defaultTag}"
|
||||
|
||||
def confirmed = true
|
||||
timeout(time: 1, unit: 'MINUTES') {
|
||||
try {
|
||||
input(
|
||||
message: """⚠️ Tag 命名不规范:
|
||||
${customTag}
|
||||
|
||||
将使用自动生成的 Tag:
|
||||
${defaultTag}
|
||||
|
||||
是否继续构建?""",
|
||||
ok: '确认'
|
||||
)
|
||||
} catch (err) {
|
||||
// 用户点击“取消”或中断
|
||||
echo "🚫 用户取消构建"
|
||||
confirmed = false
|
||||
}
|
||||
}
|
||||
|
||||
if (confirmed) {
|
||||
echo "✅ 用户确认使用自动生成的 Tag:${defaultTag}"
|
||||
env.IMAGE_TAG = defaultTag
|
||||
} else {
|
||||
error("流水线已终止。")
|
||||
}
|
||||
} else {
|
||||
env.IMAGE_TAG = defaultTag
|
||||
echo "未输入自定义 Tag, 使用自动生成规则: ${env.IMAGE_TAG}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('依赖&构建') {
|
||||
steps {
|
||||
sh """
|
||||
cd ${WORKSPACE}/
|
||||
export GOVCS="git.deeplink.media:git,*:git"
|
||||
echo "Jenkins 当前使用的 Go 路径:\$(which go)"
|
||||
echo "Jenkins 当前使用的 Go 版本:\$(go version)"
|
||||
echo "拉依赖"
|
||||
go mod tidy -v -x
|
||||
make build-linux
|
||||
chmod +x ./build/lessie-sourcing-api
|
||||
"""
|
||||
}
|
||||
}
|
||||
stage('登录仓库') {
|
||||
steps {
|
||||
withCredentials([usernamePassword(
|
||||
credentialsId: env.CREDENTIALS_ID,
|
||||
usernameVariable: 'REGISTRY_USER',
|
||||
passwordVariable: 'REGISTRY_PWD'
|
||||
)]) {
|
||||
sh '''
|
||||
echo "$REGISTRY_PWD" | docker login ${REGISTRY} -u ${REGISTRY_USER} --password-stdin
|
||||
'''
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('构建镜像') {
|
||||
steps {
|
||||
script {
|
||||
// 构建镜像,添加标签信息
|
||||
sh """
|
||||
docker build -t ${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG} \
|
||||
--label "git-branch='${Code_branch}'" \
|
||||
--label "git-commit='${GIT_COMMIT_SHORT}'" \
|
||||
--label "git-author='${GIT_AUTHOR}'" \
|
||||
--label "git-message='${GIT_COMMIT_MSG}'" \
|
||||
--label "build-time='${GIT_COMMIT_TIME}'" \
|
||||
.
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('推送镜像到仓库') {
|
||||
steps {
|
||||
script {
|
||||
sh "docker push ${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG}"
|
||||
echo "推送镜像成功:${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
post {
|
||||
always {
|
||||
script {
|
||||
def keepCount = 3
|
||||
echo "开始清理本地旧镜像,仅保留最近 ${keepCount} 个构建版本"
|
||||
def imagePrefix = "${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}"
|
||||
|
||||
// 获取所有镜像(按创建时间排序,越新的越前)
|
||||
// 格式:Repository:Tag ImageID CreatedAt
|
||||
def allImagesRaw = sh(script: "docker images ${imagePrefix} --format '{{.Repository}}:{{.Tag}} {{.ID}} {{.CreatedAt}}' | sort -rk3", returnStdout: true).trim()
|
||||
if (!allImagesRaw) {
|
||||
echo "未找到任何镜像,无需清理"
|
||||
return
|
||||
}
|
||||
|
||||
def allImages = allImagesRaw.split('\n')
|
||||
if (allImages.size() <= keepCount) {
|
||||
echo "当前镜像数未超过 ${keepCount} 个,无需清理"
|
||||
return
|
||||
}
|
||||
|
||||
def oldImages = allImages.drop(keepCount)
|
||||
echo "发现 ${oldImages.size()} 个旧镜像需要清理"
|
||||
oldImages.each { line ->
|
||||
echo " ${line}"
|
||||
}
|
||||
|
||||
oldImages.each { line ->
|
||||
def parts = line.split(' ')
|
||||
def imageTag = parts[0]
|
||||
def imageId = parts.size() > 1 ? parts[1] : ""
|
||||
|
||||
// 对于标签为<none>的无效镜像,使用镜像ID删除
|
||||
if (imageTag.contains("<none>") && imageId) {
|
||||
echo "删除无效镜像: ${imageId}"
|
||||
sh(returnStatus: true, script: "docker rmi -f ${imageId} || true")
|
||||
} else if (imageId) {
|
||||
// 对于有标签的有效镜像,优先使用镜像ID删除
|
||||
echo "删除旧镜像: ${imageTag} (${imageId})"
|
||||
sh(returnStatus: true, script: "docker rmi -f ${imageId} || true")
|
||||
} else {
|
||||
// 兜底方案,使用标签删除
|
||||
echo "删除旧镜像: ${imageTag}"
|
||||
sh(returnStatus: true, script: "docker rmi -f ${imageTag} || true")
|
||||
}
|
||||
}
|
||||
|
||||
echo "清理完成,当前镜像状态:"
|
||||
sh """
|
||||
docker images ${imagePrefix} --format 'table {{.Repository}}\\t{{.Tag}}\\t{{.CreatedAt}}\\t{{.Size}}'
|
||||
"""
|
||||
|
||||
sh "docker logout ${REGISTRY}"
|
||||
echo "容器仓库已登出,本地凭证已清理"
|
||||
}
|
||||
}
|
||||
success {
|
||||
script {
|
||||
// 1. 准备元数据 (转换所有环境变量为 String)
|
||||
def metadataDir = '/var/lib/jenkins/metadata'
|
||||
def metadataFileRelativePath = "${env.NAMESPACE}-${env.IMAGE_NAME}.json" // 相对于 metadataDir 的文件名
|
||||
def fullMetadataPath = "${metadataDir}/${metadataFileRelativePath}"
|
||||
|
||||
// --- 转换为 String ---
|
||||
def registry = env.REGISTRY as String
|
||||
def namespace = env.NAMESPACE as String
|
||||
def imageName = env.IMAGE_NAME as String
|
||||
def imageTag = env.IMAGE_TAG as String
|
||||
def codeBranch = params.Code_branch as String // 使用 params,因为 Code_branch 是参数
|
||||
def gitCommit = env.GIT_COMMIT_LONG as String
|
||||
def gitAuthor = env.GIT_AUTHOR as String
|
||||
def gitCommitMsg = env.GIT_COMMIT_MSG as String
|
||||
def gitCommitTime = env.GIT_COMMIT_TIME as String
|
||||
def buildNumber = env.BUILD_NUMBER as String
|
||||
// --- 转换为 String ---
|
||||
|
||||
// 2. 准备新数据
|
||||
def newImageData = [
|
||||
image_tag: imageTag, // 使用转换后的变量
|
||||
full_image_name: "${registry}/${namespace}/${imageName}:${imageTag}", // 使用转换后的变量
|
||||
labels: [
|
||||
"git-branch": codeBranch,
|
||||
"git-commit": gitCommit,
|
||||
"git-author": gitAuthor,
|
||||
"git-message": gitCommitMsg,
|
||||
"build-time": gitCommitTime
|
||||
],
|
||||
build_job_number: buildNumber,
|
||||
build_time: new Date().format('yyyy-MM-dd HH:mm:ss') // Jenkins 构建完成时间
|
||||
]
|
||||
|
||||
// 2. 读取现有数据(如果文件存在)
|
||||
def existingDataList = []
|
||||
try {
|
||||
// 使用 readJSON 步骤读取文件内容 (readJSON 会自动处理 LazyMap 问题)
|
||||
def rawExistingData = readJSON file: fullMetadataPath, default: [] // 如果文件不存在,则返回空列表 []
|
||||
|
||||
// --- ✅ 修复:深拷贝 rawExistingData (修正内联代码) ---
|
||||
if (rawExistingData instanceof List) {
|
||||
existingDataList = rawExistingData.collect { item ->
|
||||
if (item instanceof Map) {
|
||||
// 递归深拷贝 Map (使用辅助函数)
|
||||
return deepCopyForSerialization(item)
|
||||
} else {
|
||||
return item
|
||||
}
|
||||
}
|
||||
} else {
|
||||
echo "警告: 元数据文件 ${fullMetadataPath} 格式不正确(非 List 类型),将被覆盖。"
|
||||
existingDataList = []
|
||||
}
|
||||
// --- 结束修复 ---
|
||||
|
||||
} catch (Exception e) {
|
||||
// readJSON 在文件不存在时通常会返回 default 值,但如果文件存在但格式错误,会抛出异常
|
||||
echo "警告: 读取元数据文件 ${fullMetadataPath} 失败或格式错误: ${e.getMessage()},将被覆盖。"
|
||||
// 确保目录存在
|
||||
sh "mkdir -p ${metadataDir}"
|
||||
existingDataList = [] // 重置为新列表
|
||||
}
|
||||
|
||||
// 3. 将新数据添加到列表开头(最新的在前)
|
||||
existingDataList.add(0, newImageData)
|
||||
|
||||
// 4. 限制列表大小为 20
|
||||
if (existingDataList.size() > 20) {
|
||||
existingDataList = existingDataList.take(20)
|
||||
}
|
||||
|
||||
// 5. 使用 writeJSON 步骤写入文件 (writeJSON 会自动处理 Map 的序列化)
|
||||
writeJSON file: fullMetadataPath, json: existingDataList, pretty: 2 // pretty: 2 表示格式化 JSON (2 个空格缩进)
|
||||
|
||||
echo "镜像元数据已存储到: ${fullMetadataPath}"
|
||||
|
||||
// 输出构建结果
|
||||
echo """
|
||||
镜像地址:${registry}/${namespace}/${imageName}:${imageTag}
|
||||
对应代码提交哈希:${gitCommit}
|
||||
对应代码分支:${codeBranch}
|
||||
代码提交者:${gitAuthor}
|
||||
提交备注:${gitCommitMsg}
|
||||
""".stripIndent()
|
||||
}
|
||||
}
|
||||
failure {
|
||||
// 输出构建结果
|
||||
echo "部署有错误,请检查!"
|
||||
}
|
||||
}
|
||||
}
|
||||
322
SCM/构建镜像/build_image_lessie_official_web_v2.groovy
Normal file
322
SCM/构建镜像/build_image_lessie_official_web_v2.groovy
Normal file
@@ -0,0 +1,322 @@
|
||||
// --- 辅助函数:深拷贝对象以确保可序列化 ---
|
||||
def deepCopyForSerialization(obj) {
|
||||
if (obj instanceof Map) {
|
||||
// 创建新的 LinkedHashMap,递归拷贝值
|
||||
return obj.collectEntries { k, v -> [(k): deepCopyForSerialization(v)] }
|
||||
} else if (obj instanceof List) {
|
||||
// 创建新的 ArrayList,递归拷贝元素
|
||||
return obj.collect { item -> deepCopyForSerialization(item) }
|
||||
} else if (obj instanceof String || obj instanceof Number || obj instanceof Boolean || obj == null) {
|
||||
return obj
|
||||
} else {
|
||||
return obj.toString()
|
||||
}
|
||||
}
|
||||
// --- 结束辅助函数 ---
|
||||
|
||||
pipeline {
|
||||
agent any
|
||||
|
||||
parameters {
|
||||
gitParameter(
|
||||
branchFilter: 'origin/(.*)',
|
||||
defaultValue: 'dxin',
|
||||
name: 'Code_branch',
|
||||
type: 'PT_BRANCH_TAG',
|
||||
selectedValue: 'DEFAULT',
|
||||
sortMode: 'NONE',
|
||||
description: '选择代码分支: ',
|
||||
quickFilterEnabled: true,
|
||||
tagFilter: '*',
|
||||
listSize: "1"
|
||||
)
|
||||
choice(
|
||||
name: 'NAME_SPACES',
|
||||
choices: ['sit', 'test', 'prod'],
|
||||
description: '选择存放镜像的仓库命名空间:'
|
||||
)
|
||||
string(
|
||||
name: 'CUSTOM_TAG',
|
||||
defaultValue: '',
|
||||
description: '可选:自定义镜像 Tag (字母、数字、点、下划线、短横线), 如 v0.0.1, 留空则自动生成 “ v+构建次数_分支名_短哈希_构建时间 ”'
|
||||
)
|
||||
}
|
||||
environment {
|
||||
REGISTRY = "uswccr.ccs.tencentyun.com" // 镜像仓库地址
|
||||
NAMESPACE = "lessie${params.NAME_SPACES}" // 命名空间根据choices的选择拼接
|
||||
IMAGE_NAME = "lessie-official-web" // 镜像名(固定前缀)
|
||||
CREDENTIALS_ID = "dxin_img_hub_auth" // 容器仓库凭证ID
|
||||
}
|
||||
|
||||
stages {
|
||||
stage('拉取代码') {
|
||||
steps {
|
||||
git branch: "${params.Code_branch}",
|
||||
credentialsId: 'fly_gitlab_auth',
|
||||
url: 'http://172.24.16.20/web/scalelink-frontend.git'
|
||||
}
|
||||
}
|
||||
|
||||
stage('获取信息') {
|
||||
steps {
|
||||
script {
|
||||
// 获取分支名
|
||||
env.Code_branch = "${params.Code_branch}"
|
||||
// 获取最近一次提交的哈希值(短格式,前8位)
|
||||
env.GIT_COMMIT_SHORT = sh(script: 'git rev-parse --short HEAD',returnStdout: true).trim()
|
||||
// 获取最近一次提交的哈希值(全格式)
|
||||
env.GIT_COMMIT_LONG = sh(script: 'git rev-parse HEAD', returnStdout: true).trim()
|
||||
// 获取最近一次提交的作者
|
||||
env.GIT_AUTHOR = sh(script: 'git log -1 --pretty=format:%an',returnStdout: true).trim()
|
||||
// 获取最近一次提交的时间(格式化)
|
||||
env.GIT_COMMIT_TIME = sh(
|
||||
script: 'git log -1 --pretty=format:%ct | xargs -I {} date -d @{} +%Y%m%d-%H%M%S',
|
||||
returnStdout: true
|
||||
).trim()
|
||||
// 获取最近一次提交的备注信息(转义特殊字符,避免构建失败)
|
||||
env.GIT_COMMIT_MSG = sh(script: 'git log -1 --pretty=format:%s | sed -e \'s/"/\\"/g\'', returnStdout: true).trim()
|
||||
|
||||
// Jenkins构建次数
|
||||
def buildNumber = env.BUILD_NUMBER // Jenkins内置变量,直接获取当前Job的构建序号
|
||||
// 当前分支名(处理/为-,如feature/docker_1015 → feature-docker_1015)
|
||||
def branchName = sh(script: 'git rev-parse --abbrev-ref HEAD', returnStdout: true).trim()
|
||||
def formattedBranch = branchName.replace('/', '-').replace('_', '-') // 替换分支名中的/和_为-
|
||||
// 构建时间(格式:202510181215,年-月-日-时-分,无分隔符)
|
||||
def buildTime = sh(script: 'date +%Y%m%d%H%M', returnStdout: true).trim()
|
||||
def defaultTag = "v${buildNumber}_${formattedBranch}_${GIT_COMMIT_SHORT}_${buildTime}"
|
||||
|
||||
def customTag = params.CUSTOM_TAG?.trim()
|
||||
def tagPattern = ~/^[a-zA-Z0-9._-]+$/
|
||||
|
||||
// 判断最终Tag
|
||||
if (customTag && customTag ==~ tagPattern) {
|
||||
echo "✅ 使用自定义镜像 Tag: ${customTag}"
|
||||
env.IMAGE_TAG = customTag
|
||||
} else if (customTag) {
|
||||
echo "⚠️ 自定义 Tag '${customTag}' 不符合规范,将使用默认生成的 Tag: ${defaultTag}"
|
||||
|
||||
def confirmed = true
|
||||
timeout(time: 1, unit: 'MINUTES') {
|
||||
try {
|
||||
input(
|
||||
message: """⚠️ Tag 命名不规范:
|
||||
${customTag}
|
||||
|
||||
将使用自动生成的 Tag:
|
||||
${defaultTag}
|
||||
|
||||
是否继续构建?""",
|
||||
ok: '确认'
|
||||
)
|
||||
} catch (err) {
|
||||
// 用户点击“取消”或中断
|
||||
echo "🚫 用户取消构建"
|
||||
confirmed = false
|
||||
}
|
||||
}
|
||||
|
||||
if (confirmed) {
|
||||
echo "✅ 用户确认使用自动生成的 Tag:${defaultTag}"
|
||||
env.IMAGE_TAG = defaultTag
|
||||
} else {
|
||||
error("流水线已终止。")
|
||||
}
|
||||
} else {
|
||||
env.IMAGE_TAG = defaultTag
|
||||
echo "未输入自定义 Tag, 使用自动生成规则: ${env.IMAGE_TAG}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage('登录仓库') {
|
||||
steps {
|
||||
withCredentials([usernamePassword(
|
||||
credentialsId: env.CREDENTIALS_ID,
|
||||
usernameVariable: 'REGISTRY_USER',
|
||||
passwordVariable: 'REGISTRY_PWD'
|
||||
)]) {
|
||||
sh '''
|
||||
echo "$REGISTRY_PWD" | docker login ${REGISTRY} -u ${REGISTRY_USER} --password-stdin
|
||||
'''
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage('构建镜像') {
|
||||
steps {
|
||||
script {
|
||||
// 构建镜像,添加标签信息
|
||||
sh """
|
||||
docker build -t ${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG} \
|
||||
--label "git-branch='${Code_branch}'" \
|
||||
--label "git-commit='${GIT_COMMIT_SHORT}'" \
|
||||
--label "git-author='${GIT_AUTHOR}'" \
|
||||
--label "git-message='${GIT_COMMIT_MSG}'" \
|
||||
--label "build-time='${GIT_COMMIT_TIME}'" \
|
||||
.
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage('推送仓库') {
|
||||
steps {
|
||||
script {
|
||||
sh "docker push ${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG}"
|
||||
echo "推送镜像成功:${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
post {
|
||||
always {
|
||||
script {
|
||||
def keepCount = 3
|
||||
echo "开始清理本地旧镜像,仅保留最近 ${keepCount} 个构建版本"
|
||||
def imagePrefix = "${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}"
|
||||
|
||||
// 获取所有镜像(按创建时间排序,越新的越前)
|
||||
// 格式:Repository:Tag ImageID CreatedAt
|
||||
def allImagesRaw = sh(script: "docker images ${imagePrefix} --format '{{.Repository}}:{{.Tag}} {{.ID}} {{.CreatedAt}}' | sort -rk3", returnStdout: true).trim()
|
||||
if (!allImagesRaw) {
|
||||
echo "未找到任何镜像,无需清理"
|
||||
return
|
||||
}
|
||||
|
||||
def allImages = allImagesRaw.split('\n')
|
||||
if (allImages.size() <= keepCount) {
|
||||
echo "当前镜像数未超过 ${keepCount} 个,无需清理"
|
||||
return
|
||||
}
|
||||
|
||||
def oldImages = allImages.drop(keepCount)
|
||||
echo "发现 ${oldImages.size()} 个旧镜像需要清理"
|
||||
oldImages.each { line ->
|
||||
echo " ${line}"
|
||||
}
|
||||
|
||||
oldImages.each { line ->
|
||||
def parts = line.split(' ')
|
||||
def imageTag = parts[0]
|
||||
def imageId = parts.size() > 1 ? parts[1] : ""
|
||||
|
||||
// 对于标签为<none>的无效镜像,使用镜像ID删除
|
||||
if (imageTag.contains("<none>") && imageId) {
|
||||
echo "删除无效镜像: ${imageId}"
|
||||
sh(returnStatus: true, script: "docker rmi -f ${imageId} || true")
|
||||
} else if (imageId) {
|
||||
// 对于有标签的有效镜像,优先使用镜像ID删除
|
||||
echo "删除旧镜像: ${imageTag} (${imageId})"
|
||||
sh(returnStatus: true, script: "docker rmi -f ${imageId} || true")
|
||||
} else {
|
||||
// 兜底方案,使用标签删除
|
||||
echo "删除旧镜像: ${imageTag}"
|
||||
sh(returnStatus: true, script: "docker rmi -f ${imageTag} || true")
|
||||
}
|
||||
}
|
||||
|
||||
echo "清理完成,当前镜像状态:"
|
||||
sh """
|
||||
docker images ${imagePrefix} --format 'table {{.Repository}}\\t{{.Tag}}\\t{{.CreatedAt}}\\t{{.Size}}'
|
||||
"""
|
||||
|
||||
sh "docker logout ${REGISTRY}"
|
||||
echo "容器仓库已登出,本地凭证已清理"
|
||||
}
|
||||
}
|
||||
success {
|
||||
script {
|
||||
// 1. 准备元数据 (转换所有环境变量为 String)
|
||||
def metadataDir = '/var/lib/jenkins/metadata'
|
||||
def metadataFileRelativePath = "${env.NAMESPACE}-${env.IMAGE_NAME}.json" // 相对于 metadataDir 的文件名
|
||||
def fullMetadataPath = "${metadataDir}/${metadataFileRelativePath}"
|
||||
|
||||
// --- 转换为 String ---
|
||||
def registry = env.REGISTRY as String
|
||||
def namespace = env.NAMESPACE as String
|
||||
def imageName = env.IMAGE_NAME as String
|
||||
def imageTag = env.IMAGE_TAG as String
|
||||
def codeBranch = params.Code_branch as String // 使用 params,因为 Code_branch 是参数
|
||||
def gitCommit = env.GIT_COMMIT_LONG as String
|
||||
def gitAuthor = env.GIT_AUTHOR as String
|
||||
def gitCommitMsg = env.GIT_COMMIT_MSG as String
|
||||
def gitCommitTime = env.GIT_COMMIT_TIME as String
|
||||
def buildNumber = env.BUILD_NUMBER as String
|
||||
// --- 转换为 String ---
|
||||
|
||||
// 2. 准备新数据
|
||||
def newImageData = [
|
||||
image_tag: imageTag, // 使用转换后的变量
|
||||
full_image_name: "${registry}/${namespace}/${imageName}:${imageTag}", // 使用转换后的变量
|
||||
labels: [
|
||||
"git-branch": codeBranch,
|
||||
"git-commit": gitCommit,
|
||||
"git-author": gitAuthor,
|
||||
"git-message": gitCommitMsg,
|
||||
"build-time": gitCommitTime
|
||||
],
|
||||
build_job_number: buildNumber,
|
||||
build_time: new Date().format('yyyy-MM-dd HH:mm:ss') // Jenkins 构建完成时间
|
||||
]
|
||||
|
||||
// 2. 读取现有数据(如果文件存在)
|
||||
def existingDataList = []
|
||||
try {
|
||||
// 使用 readJSON 步骤读取文件内容 (readJSON 会自动处理 LazyMap 问题)
|
||||
def rawExistingData = readJSON file: fullMetadataPath, default: [] // 如果文件不存在,则返回空列表 []
|
||||
|
||||
// --- ✅ 修复:深拷贝 rawExistingData (修正内联代码) ---
|
||||
if (rawExistingData instanceof List) {
|
||||
existingDataList = rawExistingData.collect { item ->
|
||||
if (item instanceof Map) {
|
||||
// 递归深拷贝 Map (使用辅助函数)
|
||||
return deepCopyForSerialization(item)
|
||||
} else {
|
||||
return item
|
||||
}
|
||||
}
|
||||
} else {
|
||||
echo "警告: 元数据文件 ${fullMetadataPath} 格式不正确(非 List 类型),将被覆盖。"
|
||||
existingDataList = []
|
||||
}
|
||||
// --- 结束修复 ---
|
||||
|
||||
} catch (Exception e) {
|
||||
// readJSON 在文件不存在时通常会返回 default 值,但如果文件存在但格式错误,会抛出异常
|
||||
echo "警告: 读取元数据文件 ${fullMetadataPath} 失败或格式错误: ${e.getMessage()},将被覆盖。"
|
||||
// 确保目录存在
|
||||
sh "mkdir -p ${metadataDir}"
|
||||
existingDataList = [] // 重置为新列表
|
||||
}
|
||||
|
||||
// 3. 将新数据添加到列表开头(最新的在前)
|
||||
existingDataList.add(0, newImageData)
|
||||
|
||||
// 4. 限制列表大小为 20
|
||||
if (existingDataList.size() > 20) {
|
||||
existingDataList = existingDataList.take(20)
|
||||
}
|
||||
|
||||
// 5. 使用 writeJSON 步骤写入文件 (writeJSON 会自动处理 Map 的序列化)
|
||||
writeJSON file: fullMetadataPath, json: existingDataList, pretty: 2 // pretty: 2 表示格式化 JSON (2 个空格缩进)
|
||||
|
||||
echo "镜像元数据已存储到: ${fullMetadataPath}"
|
||||
|
||||
// 输出构建结果
|
||||
echo """
|
||||
镜像地址:${registry}/${namespace}/${imageName}:${imageTag}
|
||||
对应代码提交哈希:${gitCommit}
|
||||
对应代码分支:${codeBranch}
|
||||
代码提交者:${gitAuthor}
|
||||
提交备注:${gitCommitMsg}
|
||||
""".stripIndent()
|
||||
}
|
||||
}
|
||||
failure {
|
||||
// 输出构建结果
|
||||
echo "部署有错误,请检查!"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -34,6 +34,7 @@ pipeline {
|
||||
Deployment_name = "s1-lessie-agents"
|
||||
K8s_namespace = "sit"
|
||||
Pod_container_name = "lessie-agents"
|
||||
Pod_environment = "s1"
|
||||
}
|
||||
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-ai-web"
|
||||
K8s_namespace = "sit"
|
||||
Pod_container_name = "lessie-ai-web"
|
||||
Pod_environment = "s1"
|
||||
}
|
||||
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 = "s1"
|
||||
}
|
||||
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/部署镜像/s2/DM_s2_lessie_agent.groovy
Normal file
277
SCM/部署镜像/s2/DM_s2_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/s2/lessie-agents.yaml"
|
||||
Deployment_name = "s2-lessie-agents"
|
||||
K8s_namespace = "sit"
|
||||
Pod_container_name = "lessie-agents"
|
||||
Pod_environment = "s2"
|
||||
}
|
||||
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 "有步骤失败,请检查!"
|
||||
}
|
||||
}
|
||||
}
|
||||
276
SCM/部署镜像/s2/DM_s2_lessie_ai_web.groovy
Normal file
276
SCM/部署镜像/s2/DM_s2_lessie_ai_web.groovy
Normal file
@@ -0,0 +1,276 @@
|
||||
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"
|
||||
}
|
||||
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} -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
|
||||
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
|
||||
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} -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}'",
|
||||
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 "有步骤失败,请检查!"
|
||||
}
|
||||
}
|
||||
}
|
||||
276
SCM/部署镜像/s2/DM_s2_lessie_go_api.groovy
Normal file
276
SCM/部署镜像/s2/DM_s2_lessie_go_api.groovy
Normal file
@@ -0,0 +1,276 @@
|
||||
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"
|
||||
}
|
||||
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} -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
|
||||
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
|
||||
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} -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}'",
|
||||
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 "有步骤失败,请检查!"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user