Mark Ku's Blog

目的

在開發各種前端框架時,總會有些靜態檔案, 過去手動複製檔案到CDN太花時間了,所以花了些時間在Jenkins 寫個腳本,在建置過程中,將靜態檔案自動複製到CDN,並順便清除Cloudflare cache。

首先,在建立 "Live - Copy Next JS public folder to CDN assets" Jenkins Job 的過程中,勾選 This project is parameterized,加入 String Parameter,在樣在執行 Jenkins 時可以帶入選擇性參數

image
image

接著撰寫 pipeline,專案資料夾下的public 檔案,複製檔案到 Content server

properties([pipelineTriggers([githubPush()])])

pipeline {
    agent any
 
    tools {nodejs "Node Core"}
    
    options {
        buildDiscarder logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '20', daysToKeepStr: '', numToKeepStr: '20')
     
        disableConcurrentBuilds()    
    }

    stages {
        stage("GitHub Pull") {
            steps {
                script {
                

                def branchName = CdnEnv
                if (CdnEnv == "staging") {
                    branchName = "staging-master"
                } else if (CdnEnv == "production") {
                    branchName = "master"
                }
				
                echo "CdnEnv value: ${env.CdnEnv}"
                echo "branchName value: ${branchName}"

                git branch: branchName, url: "https://[email protected]/iBuypowerUS/iBuypower.NextJS.git"
                }
                
            }
        }

    stage("Copy assets to content server ") {
    steps { 	            
            	powershell(script: """
              	robocopy \"${WORKSPACE}\\public\" \"\\\\192.168.0.20\\Content\\\\${CdnEnv}\\assets\" /E /MIR /MT:100 \r\n
              	 
              	""", returnStatus: true)
        }
    }    
 
    }
}

接著,在建立 "Live - Clear Cloudflare cache" Jenkins Job 的過程中,勾選 This project is parameterized,加入 String Parameter,在樣在執行 Jenkins 時可以帶入選擇性參數

image
image

撰寫 Clear Cloudflare cache 的cache 的 pipeline

pipeline {
    agent any 

    environment {
        CLOUDFLARE_EMAIL = '[email protected]'
        CLOUDFLARE_API_KEY = 'xxxxxxx'
        CLOUDFLARE_ZONE_ID = 'xxxxxxxxxxxxxxxxxxxxxxxxx'
    }

    stages {
        stage('Purge Cloudflare Cache') {
            steps {
                script {
                    // PowerShell script to purge Cloudflare cache
                    powershell """
                        \$headers = @{
                            "X-Auth-Email" = "${env.CLOUDFLARE_EMAIL}"
                            "X-Auth-Key" = "${env.CLOUDFLARE_API_KEY}"
                            "Content-Type" = "application/json"
                        }

                        \$body = @{
                            prefixes = @("${PrefixUrl}")
                        } | ConvertTo-Json

                        \$response = Invoke-RestMethod -Uri "https://api.cloudflare.com/client/v4/zones/${env.CLOUDFLARE_ZONE_ID}/purge_cache" -Method POST -Headers \$headers -Body \$body
                        
                        Write-Host prefix: \"${PrefixUrl}\" - response: \$response
                    """
                }
            }
        }
    }
}

最後,別的任務就可以,觸發這個Job

	stages {
	    stage('Copy Next JS public folder to CDN assets') {
            steps {
                script {
                    build job:  'Live - Copy Next JS public folder to CDN assets', parameters: [string(name: 'CdnEnv', value: "$cdnEnv")]
                }
            }
        }
		
		stage('Clear Cloudflare cache') {
            steps {
                script {
                build job: 'Live - Clear Cloudflare cache' , parameters: [string(name: 'PrefixUrl', value: "content.ibuypower.com/$cdnEnv/assets")]
                }
            }
        }
		
		// Original process... ...
       
	}

P.S. 如果是 Windows 權限不足

Jenkins在windows的安裝後,使用命令或者某些插件的時候容易出現權限錯誤的情況,此時因為使用的是jenkins賬戶,不是administrator管理員權限的賬戶,我們可以按Ctrl+windows鍵,輸入services.msc 在服務中右鍵選中並點擊jenkins服務,查看屬性對話框,切換登錄標簽,選中此賬戶登錄,里邊填寫administrator或者有權限的賬戶,密碼就是登錄密碼,此時重啟jenkins服務即可 image

作者

Mark Ku

擁有 10+ 年經驗的資深軟體工程師,現為 AI 應用 Builder,專注於大型平台架構與簡化複雜系統設計,從電商系統到訂閱與收費平台,結合 AI Agent、AI 整合與自動化開發,打造高效率且可持續演進的產品技術基礎。閱讀更多

覺得這篇有幫助?

作者做的免費工具、每日 Podcast 與電子報,都在這裡。

Mark Ku · 本文採用 CC BY 4.0 授權,轉載請註明作者並附上原文連結。

留言

訂閱電子報

訂閱後即時收到新文章通知,不錯過任何技術分享。

提交即表示同意接收電子報,隨時可

熱門文章

View all
Mark Ku
··604

Oracle Cloud 永久免費方案 Linux 主機及固定 IP :0 元打造雲端解決方案

Oracle Cloud 永久免費方案 Linux 主機及固定 IP :0 元打造雲端解決方案
Mark Ku
··471

告別 Postman 收費陷阱!開源 Git 原生 API 測試神器 Bruno 實戰指南

告別 Postman 收費陷阱!開源 Git 原生 API 測試神器 Bruno 實戰指南
Mark Ku
··329

一款免費開源類似於 Notion 類知識庫系統 — Outline Wiki 佈署與備份全攻略

一款免費開源類似於 Notion 類知識庫系統 — Outline Wiki 佈署與備份全攻略
Mark Ku
··240

打造高效 API 管理平台:從 0 開始部署 Kong Gateway - Part 1

打造高效 API 管理平台:從 0 開始部署 Kong Gateway - Part 1
Mark Ku
··235

訓練自己的 AI 語音:硬體門檻、開源模型比較與 LoRA 微調

訓練自己的 AI 語音:硬體門檻、開源模型比較與 LoRA 微調
Mark Ku
··216

在 Ubuntu 上設置 Samba 來共享資料夾,讓 Windows 11 用戶可以存取

在 Ubuntu 上設置 Samba 來共享資料夾,讓 Windows 11 用戶可以存取