Sharing Byteplus CDN Purging Script
The reason
We are coorperating with Byteplus CDN recently and using it to serve an react website, which using bamboo to deploy.
Afate bamboo agent copy the new react build to Byteplus TOS storage, will have to purge CDN cache to make new web version take effect.
What I did
Since I can not make sure every bamboo agent have the Byteplus SDK prepared, I put a script so all the bamboo just call the script to make sure SDK installed and purge targe website with parameter
I also put it under github for easy calling
Here is bamboo script example
curl -sL "https://raw.githubusercontent.com/CalvinLeung21/bamboo-script/refs/heads/main/purge_bp_cdn/purge_bp_cdn_prep.sh" | bash
curl -sL "https://raw.githubusercontent.com/CalvinLeung21/bamboo-script/refs/heads/main/purge_bp_cdn/purge_bp_cdn.py" | python3 - "${bamboo.purge_ak}" "${bamboo.purge_sk}" "https://www.exmaple.com/"
purge_ak and purge_sk is bamboo global variable so it can be called in different deployment project.
when purge success, will see response like below in bamboo log
{'ResponseMetadata': {'RequestId': '20260930000310BFC205243D73AC5C7740', 'Action': 'SubmitRefreshTask', 'Version': '2021-03-01', 'Service': 'CDN', 'Region': 'ap-singapore-1'}, 'Result': {'TaskID': 'refresh_dir_c5fd8b9fd9a240ba80614c2de72262c3'}}
referrenced document link
purge_bp_cdn_prep.sh, to prepare SDK
# Ensure pip is installed
if ! python3 -m pip -V &>/dev/null; then
python3 -m ensurepip --upgrade || {
echo "pip is not available and cannot be bootstrapped. Please install it manually."
exit 1
}
fi
# Install byteplus_sdk if not available
if ! python3 -c "import byteplus_sdk" &>/dev/null; then
opts=$( [ "$(id -u)" -eq 0 ] && echo "" || echo "--user" )
python3 -m pip install $opts --upgrade wheel byteplus-sdk
fi
purge_bp_cdn.sh, can purge up to 4 domain.
# -*- coding: utf-8 -*-
import sys
import os
sys.path.append(os.path.dirname(os.path.realpath(__file__)) + "/../../../")
from byteplus_sdk.cdn.service import CDNService
if __name__ == '__main__':
if len(sys.argv) < 4:
print("Usage: python purge_bp_cdn.py <ak> <sk> <url1> [url2] [url3] [url4]")
sys.exit(1)
ak = sys.argv[1]
sk = sys.argv[2]
urls = sys.argv[3:]
if len(urls) > 4:
print("Error: Maximum 4 URLs can be submitted at once.")
print("Received:", len(urls))
sys.exit(1)
urls_string = "\n".join(urls)
svc = CDNService()
svc.set_ak(ak)
svc.set_sk(sk)
body = {
"Type": "dir",
"Prefix": True,
"Urls": urls_string
}
resp = svc.submit_refresh_task(body)
print(resp)

