181 lines
9.9 KiB
Plaintext
181 lines
9.9 KiB
Plaintext
// ========= 使用 properties 定义参数 (Scripted Pipeline 部分) =========
|
||
properties([
|
||
parameters([
|
||
// --- 1. 动态下拉框 (移除 filterable) ---
|
||
[$class: 'org.biouno.unochoice.DynamicReferenceParameter',
|
||
name: 'IMAGE_TAG_WITH_LABELS', // 参数名,用于被其他参数引用
|
||
description: '选择镜像 (tag - Labels)',
|
||
choiceType: 'PT_SINGLE_SELECT', // 单选
|
||
// filterable: true, // ❌ 移除:2.8.8 可能不支持
|
||
script: [
|
||
$class: 'org.biouno.unochoice.model.GroovyScript',
|
||
// ✅ 修复:使用 GroovyScriptSource
|
||
script: [
|
||
$class: 'org.biouno.unochoice.model.GroovyScriptSource',
|
||
script: '''
|
||
import groovy.json.JsonSlurper
|
||
|
||
def choices = []
|
||
def metadataDir = '/var/lib/jenkins/metadata'
|
||
// 注意:这里的文件名需要与构建 Job 存储的文件名一致
|
||
// 假设你的命名空间是 lessiesit
|
||
def metadataFileRelativePath = "lessiesit-flymoon-email.json" // <--- 修正:根据实际命名空间和镜像名调整
|
||
def fullMetadataPath = "${metadataDir}/${metadataFileRelativePath}"
|
||
|
||
try {
|
||
// 使用 readFile 步骤读取文件内容(作为字符串)
|
||
def existingDataString = readFile file: fullMetadataPath, encoding: 'UTF-8'
|
||
if (existingDataString) {
|
||
def jsonSlurper = new groovy.json.JsonSlurper()
|
||
def existingDataList = jsonSlurper.parseText(existingDataString)
|
||
|
||
if (existingDataList instanceof List) {
|
||
existingDataList.each { imageData ->
|
||
def tag = imageData.image_tag
|
||
def labels = imageData.labels
|
||
def branch = labels?.get('git-branch') ?: 'N/A'
|
||
def commit = labels?.get('git-commit') ?: 'N/A'
|
||
def author = labels?.get('git-author') ?: 'N/A'
|
||
def message = labels?.get('git-message') ?: 'N/A'
|
||
def time = labels?.get('build-time') ?: 'N/A'
|
||
|
||
// 格式化显示文本: "tag - commit: message (time) - branch"
|
||
def displayText = "${tag} - ${commit}: ${message} (${time}) - ${branch}"
|
||
// 将 image_tag 作为选项的值
|
||
def value = tag
|
||
|
||
// Active Choices 期望返回 ["displayText [value]", ...] 格式的 List
|
||
choices << "${displayText} [${value}]"
|
||
}
|
||
} else {
|
||
choices = ["(元数据文件格式错误: 非 List 类型)"]
|
||
}
|
||
} else {
|
||
choices = ["(元数据文件为空)"]
|
||
}
|
||
} catch (java.io.FileNotFoundException e) {
|
||
// 文件不存在
|
||
choices = ["(未找到元数据文件: ${fullMetadataPath})"]
|
||
} catch (Exception e) {
|
||
choices = ["❌ 加载镜像列表失败: ${e.message}"]
|
||
}
|
||
|
||
return choices
|
||
'''
|
||
],
|
||
fallbackScript: [
|
||
$class: 'org.biouno.unochoice.model.GroovyScriptSource',
|
||
script: '''
|
||
return ['⚠️ 脚本执行异常']
|
||
'''
|
||
]
|
||
],
|
||
omitValueField: true // 不显示输入框,只显示下拉
|
||
],
|
||
|
||
// --- 2. 监听下拉框变化,显示 Labels ---
|
||
[$class: 'org.biouno.unochoice.ReactiveReferenceParameter',
|
||
name: 'IMAGE_LABELS_PREVIEW', // 参数名,用于显示
|
||
description: '镜像 Labels 预览', // 页面上会显示这个描述
|
||
script: [
|
||
$class: 'org.biouno.unochoice.model.GroovyScript',
|
||
// ✅ 修复:使用 GroovyScriptSource
|
||
script: [
|
||
$class: 'org.biouno.unochoice.model.GroovyScriptSource',
|
||
script: '''
|
||
import groovy.json.JsonSlurper
|
||
|
||
// 获取被引用的参数值
|
||
def selectedTag = params.IMAGE_TAG_WITH_LABELS ?: '' // 使用下拉框的参数名
|
||
|
||
if (!selectedTag) {
|
||
return '<div style="color: gray; font-style: italic;">(请选择镜像)</div>'
|
||
}
|
||
|
||
def metadataDir = '/var/lib/jenkins/metadata'
|
||
// 注意:这里的文件名需要与构建 Job 存储的文件名一致
|
||
// 假设你的命名空间是 lessiesit
|
||
def metadataFileRelativePath = "lessiesit-flymoon-email.json" // <--- 修正:根据实际命名空间和镜像名调整
|
||
def fullMetadataPath = "${metadataDir}/${metadataFileRelativePath}"
|
||
|
||
try {
|
||
def existingDataString = readFile file: fullMetadataPath, encoding: 'UTF-8'
|
||
if (existingDataString) {
|
||
def jsonSlurper = new groovy.json.JsonSlurper()
|
||
def existingDataList = jsonSlurper.parseText(existingDataString)
|
||
|
||
if (existingDataList instanceof List) {
|
||
// 查找匹配的镜像数据
|
||
def imageData = existingDataList.find { it.image_tag == selectedTag }
|
||
|
||
if (imageData) {
|
||
def labels = imageData.labels
|
||
def branch = labels?.get('git-branch') ?: 'N/A'
|
||
def commit = labels?.get('git-commit') ?: 'N/A'
|
||
def author = labels?.get('git-author') ?: 'N/A'
|
||
def message = labels?.get('git-message') ?: 'N/A'
|
||
def time = labels?.get('build-time') ?: 'N/A'
|
||
|
||
// 格式化输出
|
||
def output = "<div style='font-family: monospace; font-size: 12px; padding: 5px; border: 1px solid #ccc; background-color: #f9f9f9;'><strong>🏷️ Labels for ${selectedTag}:</strong><br/>"
|
||
output += "<strong>git-branch:</strong> ${branch}<br/>"
|
||
output += "<strong>git-commit:</strong> ${commit}<br/>"
|
||
output += "<strong>git-author:</strong> ${author}<br/>"
|
||
output += "<strong>git-message:</strong> ${message}<br/>"
|
||
output += "<strong>build-time:</strong> ${time}<br/>"
|
||
output += "</div>"
|
||
|
||
return output
|
||
} else {
|
||
return "<div style='color: orange;'>⚠️ 未找到标签为 '${selectedTag}' 的镜像信息</div>"
|
||
}
|
||
} else {
|
||
return "<div style='color: red;'>❌ 元数据文件格式错误: 非 List 类型</div>"
|
||
}
|
||
} else {
|
||
return "<div style='color: red;'>❌ 元数据文件为空</div>"
|
||
}
|
||
} catch (java.io.FileNotFoundException e) {
|
||
return "<div style='color: red;'>❌ 未找到元数据文件: ${fullMetadataPath}</div>"
|
||
} catch (Exception e) {
|
||
return "<div style='color: red;'>❌ 查询镜像信息失败: ${e.message}</div>"
|
||
}
|
||
'''
|
||
],
|
||
fallbackScript: [
|
||
$class: 'org.biouno.unochoice.model.GroovyScriptSource',
|
||
script: '''
|
||
return '<div style="color: red;">⚠️ 查询脚本执行异常</div>'
|
||
'''
|
||
]
|
||
],
|
||
choiceType: 'ET_FORMATTED_HTML', // 返回 HTML
|
||
omitValueField: true, // 不显示输入框,只显示返回的 HTML
|
||
referencedParameters: 'IMAGE_TAG_WITH_LABELS' // 监听的参数
|
||
]
|
||
// --- 结束添加 ---
|
||
])
|
||
])
|
||
|
||
// ========= Declarative Pipeline 主体 =========
|
||
pipeline {
|
||
agent any
|
||
// 注意:parameters 块已移至 properties 中
|
||
stages {
|
||
stage('部署') {
|
||
steps {
|
||
script {
|
||
// --- ✅ 修正:构建完整镜像名 ---
|
||
// 从参数 IMAGE_TAG_WITH_LABELS 获取 tag
|
||
def tag = params.IMAGE_TAG_WITH_LABELS // 使用下拉框的参数名
|
||
// 拼接完整镜像名
|
||
def fullImageName = "uswccr.ccs.tencentyun.com/lessiesit/flymoon-email:${tag}" // 修正:命名空间
|
||
|
||
echo "开始部署镜像: ${fullImageName}"
|
||
// 部署命令(如kubectl、docker run等)
|
||
// sh "kubectl set image deployment/your-app app=${fullImageName}"
|
||
}
|
||
}
|
||
}
|
||
}
|
||
} |