diff --git a/Jenkinsfile b/Jenkinsfile index 23e1984..3114201 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,108 +1,31 @@ -// BuildFlow buildflow-web CI/CD - clean version -// ①拉取代码 -> ②编译打包 -> 推送Harbor -> ③部署 + 飞书通知 - -def feishuNotify = { String result, String failedStageName = "" -> - def dur = currentBuild.durationString.replace(" and counting", "") - def commit = "" - try { commit = sh(script: "git log -1 --pretty=format:%s", returnStdout: true).trim() } catch(ignored) {} - def isOk = (result == "SUCCESS") - def color = isOk ? "green" : "red" - def statusText = isOk ? "成功" : "失败" - def title = "CI/CD " + statusText + " " + env.JOB_NAME + " #" + env.BUILD_NUMBER - def fields = [ - [is_short: true, text: [tag: "lark_md", content: "**项目**\n" + env.JOB_NAME]], - [is_short: true, text: [tag: "lark_md", content: "**耗时**\n" + dur]], - [is_short: true, text: [tag: "lark_md", content: "**提交**\n" + commit.take(50)]], - [is_short: true, text: [tag: "lark_md", content: "**状态**\n" + statusText]] - ] - def elements = [[tag: "div", fields: fields], [tag: "hr"]] - if (isOk) { - elements.add([tag: "div", text: [tag: "lark_md", content: "镜像地址\n`" + env.IMAGE + ":" + env.TAG + "`"]]) - } else if (failedStageName) { - elements.add([tag: "div", text: [tag: "lark_md", content: "失败阶段\n" + failedStageName]]) - } - def burl = env.BUILD_URL - if (burl == null) { burl = "http://115.191.51.246:8080/job/" + env.JOB_NAME.replace("/", "/job/") + "/" } - elements.add([tag: "action", actions: [[tag: "button", text: [tag: "plain_text", content: "查看构建"], url: burl, type: isOk ? "primary" : "danger"]]]) - def card = [msg_type: "interactive", card: [header: [title: [tag: "plain_text", content: title], template: color], elements: elements]] - def ts = String.valueOf(System.currentTimeMillis() / 1000L as long) - def stringToSign = ts + "\n" + "Qp6SwLIkgRJEtV9UsOf6mg" - def mac = javax.crypto.Mac.getInstance("HmacSHA256") - mac.init(new javax.crypto.spec.SecretKeySpec(stringToSign.getBytes("UTF-8"), "HmacSHA256")) - def sign = mac.doFinal().encodeBase64().toString() - card.timestamp = ts - card.sign = sign - def jsonStr = groovy.json.JsonOutput.toJson(card) - writeFile file: "/tmp/feishu_msg.json", text: jsonStr - sh 'curl -sS -X POST https://open.feishu.cn/open-apis/bot/v2/hook/921d9874-de0a-4eca-9ca6-66bf91bd2fbc -H "Content-Type: application/json" -d @/tmp/feishu_msg.json || true' - sh 'rm -f /tmp/feishu_msg.json' -} - pipeline { - agent any - environment { - HARBOR = "43.134.235.58:58080" - IMAGE = "${HARBOR}/buildflow/buildflow-web" - TAG = "${env.BUILD_NUMBER}" + agent any + options { timestamps() } + environment { + IMG = "127.0.0.1/middleware/buildflow-web" + } + stages { + stage("Build image") { + steps { + sh "docker build -t ${IMG}:latest -t ${IMG}:${BUILD_NUMBER} ." + } } - options { - timeout(time: 30, unit: "MINUTES") - buildDiscarder(logRotator(numToKeepStr: "10")) + stage("Push to Harbor") { + steps { + withCredentials([usernamePassword(credentialsId: "harbor-admin", usernameVariable: "HU", passwordVariable: "HP")]) { + sh 'echo "$HP" | docker login 127.0.0.1 -u "$HU" --password-stdin' + sh "docker push ${IMG}:latest && docker push ${IMG}:${BUILD_NUMBER}" + } + } } - stages { - stage("① 拉取代码") { - steps { - checkout scm - sh "git log --oneline -3" - } - } - stage("② 编译打包") { - steps { - sh "docker build -t ${IMAGE}:${TAG} -t ${IMAGE}:latest ." - sh "docker images | grep ${IMAGE} | head -2" - } - } - stage("推送 Harbor") { - steps { - withCredentials([usernamePassword(credentialsId: "harbor-credentials", usernameVariable: "HUSER", passwordVariable: "HPASS")]) { - sh ''' - echo $HPASS | docker login $HARBOR -u $HUSER --password-stdin - docker push $IMAGE:$TAG - docker push $IMAGE:latest - docker logout $HARBOR - ''' - } - } - } - stage("③ 部署") { - steps { - script { - try { - node("deploy-agent") { - sh ''' - docker pull $IMAGE:latest - docker rm -f buildflow-web 2>/dev/null || true - docker run -d --name buildflow-web --restart unless-stopped -p 5175:80 $IMAGE:latest - ''' - } - echo "部署完成" - } catch (e) { - echo "部署节点暂不可用,跳过自动部署。镜像已推送 Harbor。" - } - } - } - } + stage("Deploy") { + steps { + sh "docker compose -f /home/xiangning/deploy/buildflow/docker-compose.yml pull buildflow-web" + sh "docker compose -f /home/xiangning/deploy/buildflow/docker-compose.yml up -d --no-deps buildflow-web" + } } - post { - success { - script { feishuNotify("SUCCESS") } - } - failure { - script { feishuNotify("FAILURE", "构建失败,请查看日志") } - } - always { - sh "docker rmi ${IMAGE}:${TAG} ${IMAGE}:latest 2>/dev/null || true" - deleteDir() - } - } -} \ No newline at end of file + } + post { + always { sh "docker image prune -f || true" } + } +}