From 83c9f7ce054e1d05bc5965e2a671f7c0d1a24bd5 Mon Sep 17 00:00:00 2001 From: leonmin <1334137558@qq.com> Date: Thu, 6 Feb 2025 07:27:04 +0800 Subject: [PATCH] =?UTF-8?q?feature:=20=E4=B8=8A=E4=BC=A0=E9=A1=B9=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- resources/notify/feishu/post.json | 18 ++++++++++++++++ vars/buildWeixin.groovy | 34 +++++++++++++++++++++++++++++++ vars/notityFeishu.groovy | 15 ++++++++++++++ vars/previewWeixin.groovy | 27 ++++++++++++++++++++++++ vars/pullGitCode.groovy | 19 +++++++++++++++++ vars/renderTemplate.groovy | 5 +++++ 6 files changed, 118 insertions(+) create mode 100644 resources/notify/feishu/post.json create mode 100644 vars/buildWeixin.groovy create mode 100644 vars/notityFeishu.groovy create mode 100644 vars/previewWeixin.groovy create mode 100644 vars/pullGitCode.groovy create mode 100644 vars/renderTemplate.groovy diff --git a/resources/notify/feishu/post.json b/resources/notify/feishu/post.json new file mode 100644 index 0000000..d300d57 --- /dev/null +++ b/resources/notify/feishu/post.json @@ -0,0 +1,18 @@ +{ + \"msg_type\": \"post\", + \"content\": { + \"post\": { + \"zh_cn\": { + \"title\": \"$title\", + \"content\": [ + [ + { + \"tag\": \"text\", + \"text\": \"$message\" + } + ] + ] + } + } + } +} \ No newline at end of file diff --git a/vars/buildWeixin.groovy b/vars/buildWeixin.groovy new file mode 100644 index 0000000..449674f --- /dev/null +++ b/vars/buildWeixin.groovy @@ -0,0 +1,34 @@ +def call (Map config = [:]) { + echo "--> 构建开始" + def privateKey = config.privateKey // 小程序秘钥(仅第一次构建或修改秘钥时提供) + def appId = config.appId // 小程序appId + + def privatePath = "wx.${appId}.key" + // 1. 检查appId + if (!appId) { + currentBuild.description = """ +

未找到appId, 请输入appId

+ """ + error "--> 未找到appId" + } + // 2. 检查小程序授权秘钥(用于miniprogram-ci鉴权) + // 2.1 将用户传入的秘钥写入工作目录 + if (privateKey) { + writeFile encoding: "utf-8", file: privatePath, text: privateKey + echo "--> 成功写入${appId}授权信息" + } + // 2.2 检查工作目录是否有秘钥 + if (!fileExists(privatePath)) { + currentBuild.description = """ +

未找到${appId}授权信息, 请输入授权信息

+ """ + error "--> 未找到${appId}授权信息" + } + // 3. 编译项目 + sh """ + rm -rf node_modules + npm install --registry=https://registry.npmmirror.com + npm run build:mp-weixin + """ + echo "--> 构建成功" +} \ No newline at end of file diff --git a/vars/notityFeishu.groovy b/vars/notityFeishu.groovy new file mode 100644 index 0000000..61a5bb6 --- /dev/null +++ b/vars/notityFeishu.groovy @@ -0,0 +1,15 @@ +// 飞书消息推送, 向指定的飞书群发送消息 +def call (Map config = [:]) { + def webhook = config.webhook + def title = config.title + def context = config.context + def rawBody = libraryResource 'notify/feishu/post.json' + def binding = [ + title: "${title}", + message: "${context}" + ] + def render = renderTemplate(rawBody,binding) + sh """ + curl -X POST -H \"Content-Type: application/json\" --data \"${render}\" $webhook + """ +} \ No newline at end of file diff --git a/vars/previewWeixin.groovy b/vars/previewWeixin.groovy new file mode 100644 index 0000000..d2535fb --- /dev/null +++ b/vars/previewWeixin.groovy @@ -0,0 +1,27 @@ +def call (Map config = [:]) { + echo "--> 预览开始" + def appId = config.appId // 小程序appId + def page = config.page // 小程序预览页面路径 + def pageQuery = config.pageQuery // 小程序预览页面路径启动参数 + def robot = config.robot // ci机器人(1-30)处理预览缓存问题 + def branch = config.branch // 项目构建分支 + def deployEnv = config.deployEnv // 项目构建环境 + + def workspace = pwd() // 工作区地址 + def privatePath = "${workspace}/wx.${appId}.key" // 小程序秘钥地址 + def projectPath = "${workspace}/dist/build/mp-weixin" // 项目地址 + def artifactPath = "output/preview_${env.BUILD_NUMBER}.png" // 预览图片归档地址, 用于预览 + def outPath = "${workspace}/${artifactPath}" // 预览图片生成地址 + // 预览逻辑 + sh """ + mkdir -p output + npm install miniprogram-ci@2.0.10 --save-dev --registry=https://registry.npmmirror.com + node ./.ci/preview.js APPID=${appId} PROJECT_PATH=${projectPath} PRIVATE_KEY_PATH=${privatePath} PAGE=${page} PAGE_QUERY=${pageQuery} OUTPUT_PATH=${outPath} ROBOT=${robot} + """ + archiveArtifacts artifacts: artifactPath, fingerprint: true + def previewPath = "https://yourciwebsite.com/job/${env.JOB_NAME}/${env.BUILD_NUMBER}/artifact/${artifactPath}" + currentBuild.description = """ +

扫描我测试

二维码

分支: ${branch},环境: ${deployEnv},机器人: ${robot}

+ """ + echo "--> 预览成功" +} \ No newline at end of file diff --git a/vars/pullGitCode.groovy b/vars/pullGitCode.groovy new file mode 100644 index 0000000..464510c --- /dev/null +++ b/vars/pullGitCode.groovy @@ -0,0 +1,19 @@ +// 拉取git仓库指定分支代码 +def call (Map config = [:]) { + echo "--> 拉取代码开始" + def branch = config.branch + def url = config.url + def credentialId = config.credentialId + checkout([ + $class: 'GitSCM', + branches: [[name: "*/${branch}"]], + doGenerateSubmoduleConfigurations: false, + extensions: [], + submoduleCfg: [], + userRemoteConfigs: [[ + url: "${url}", + credentialsId: "${credentialId}" + ]] + ]) + echo "--> 拉取代码成功" +} \ No newline at end of file diff --git a/vars/renderTemplate.groovy b/vars/renderTemplate.groovy new file mode 100644 index 0000000..a3a8d38 --- /dev/null +++ b/vars/renderTemplate.groovy @@ -0,0 +1,5 @@ +def call(input, binding) { + def engine = new groovy.text.GStringTemplateEngine() + def template = engine.createTemplate(input).make(binding) + return template.toString() +} \ No newline at end of file