How to purge all the queued jobs in the Bamboo
プラットフォームについて: Data Center - この記事は、Data Center プラットフォームのアトラシアン製品に適用されます。
このナレッジベース記事は製品の Data Center バージョン用に作成されています。Data Center 固有ではない機能の Data Center ナレッジベースは、製品のサーバー バージョンでも動作する可能性はありますが、テストは行われていません。サーバー*製品のサポートは 2024 年 2 月 15 日に終了しました。サーバー製品を利用している場合は、アトラシアンのサーバー製品のサポート終了のお知らせページにて移行オプションをご確認ください。
*Fisheye および Crucible は除く
本記事で説明している手順は、現時点でのものとなります。そのため、一部のお客様で特定の状況下で動作したという報告がありますが、正式にサポートされているわけではなく、お客様の特定のシナリオで動作することを保証するものではありません。
本番環境での実施の前に一通り非本番環境で検証し、成功しなかった場合にはサポートされている代替案にフォール バックしてください。
要約
The article outlines the process for purging all queued jobs in Bamboo. This procedure is particularly useful if a misconfiguration in plan triggers has caused numerous jobs to be queued up, thereby keeping the agents occupied.
環境
- The solution was tested on Bamboo 9.6.1, but it will be applicable to other supported versions as well.
- Python 3.12.4
- Bash 3.2.57(1)
ソリューション
This task can be accomplished using Bamboo APIs along with scripting.
Step 1: Fetch the list of builds scheduled for execution and waiting in the build queue using api-latest-queue-get
Step 2: Iterate through the list of builds fetched in the above step and stop the builds one by one using api-latest-queue-projectkey-buildkey-buildnumber-delete
Below are example Python and Shell scripts developed for this purpose. Depending on system capabilities and preferences, one of these scripts can be used to achieve the task.
Before executing the script, substitute the below placeholders with actual values:
- <bamboo-base-url> - Bamboo Server Base URL
- <admin_username> - Admin username
- <admin_password> - Admin password
PYTHON SCRIPT:
import requests
import json
headers = {
"Accept": "application/json"
}
base_url = "<bamboo-base-url>"
username = "<admin_username>"
password = "<admin_password>"
response = requests.get(f"{base_url}/rest/api/latest/queue?expand=queuedBuilds", auth=(username, password),headers=headers)
if response.status_code == 200:
try:
data = response.json()
queued_builds = data['queuedBuilds']['queuedBuild']
for build in queued_builds:
build_number = build['buildResultKey']
# Make a DELETE request based on the buildNumber
delete_url = f'{base_url}/rest/api/latest/queue/{build_number}'
response = requests.delete(delete_url,auth=(username, password))
if response.status_code == 204:
print(f"Build {build_number} deleted successfully.")
else:
print(f"Failed to delete build {build_number}.")
except ValueError:
print("Response is not in JSON format.")
else:
print(f"Failed to fetch queued builds. Status code: {response.status_code}")
出力サンプル
Build TP-TES0-JOB1-45 deleted successfully.
Build TP-TES1-JOB1-45 deleted successfully.
Build TP-TES2-JOB1-43 deleted successfully.
Build TP-TES-JOB1-58 deleted successfully.
SHELL SCRIPT:
base_url="<bamboo-base-url>"
username="<admin_username>"
password="<admin_password>"
buildKeys=$(curl --user ${username}:${password} --request GET --url "${base_url}"'/rest/api/latest/queue?expand=queuedBuilds' --header 'Accept: application/json'|jq -c -r '.queuedBuilds.queuedBuild.[].buildResultKey')
for buildKey in ${buildKeys[@]}; do
response=$(curl --write-out %{http_code} --silent --output /dev/null --user ${username}:${password} --request DELETE --url "${base_url}"'/rest/api/latest/queue/'"${buildKey}")
if [[ "$response" -eq 204 ]] ; then
echo "Build '"${buildKey}"' deleted successfully"
else
echo "Build '"${buildKey}"' deletion failed"
fi
done
出力サンプル
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1103 0 1103 0 0 19883 0 --:--:-- --:--:-- --:--:-- 20054
Build 'TP-TES0-JOB1-46' deleted successfully
Build 'TP-TES-JOB1-59' deleted successfully
Build 'TP-TES1-JOB1-46' deleted successfully
Build 'TP-TES2-JOB1-44' deleted successfully