|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# 默认使用名为artifacts的token, 权限较小 |
| 4 | +# @see https://xn.transcodegroup.cn:8590/profile.html?item=accessTokens |
| 5 | +TOKEN="${TEAMCITY_TOKEN:-eyJ0eXAiOiAiVENWMiJ9.TS1Fb3BjZlR5US02UHdkam90dUNYaUgwaWl3.OWY2MDBiYjktZTQxMC00NTM5LTg4MGYtODkwY2Q5NTUwYTk0}" |
| 6 | +HOST="https://xn.transcodegroup.cn:8590" |
| 7 | + |
| 8 | +print_help() { |
| 9 | + bin_name=$(basename "$0") |
| 10 | + echo "下载 TeamCity 构建存档" |
| 11 | + echo |
| 12 | + echo "Usage: $bin_name --build=<build> [--tag=<tag>]" |
| 13 | + echo |
| 14 | + echo "--build: 构建的名称, 例如: MaintainVbenAdmin_Release, 必填" |
| 15 | + echo "--tag: 标签名, 若不填, 则下载最后一次的构建" |
| 16 | + echo |
| 17 | + echo "示例:" |
| 18 | + echo |
| 19 | + echo "1. 下载maintain-vben-admin/Release中v1.14.0版本的存档" |
| 20 | + echo "$bin_name --build=MaintainVbenAdmin_Release --tag=v1.14.0" |
| 21 | +} |
| 22 | + |
| 23 | +while [ $# -gt 0 ]; do |
| 24 | + case "$1" in |
| 25 | + --tag=*) |
| 26 | + TAG="${1#*=}" |
| 27 | + ;; |
| 28 | + --build=*) |
| 29 | + BUILD="${1#*=}" |
| 30 | + ;; |
| 31 | + *) |
| 32 | + print_help |
| 33 | + exit 1 |
| 34 | + ;; |
| 35 | + esac |
| 36 | + shift |
| 37 | +done |
| 38 | + |
| 39 | +if [ -z "$BUILD" ]; then |
| 40 | + print_help |
| 41 | + exit 1 |
| 42 | +fi |
| 43 | + |
| 44 | +if [ -n "$TAG" ]; then |
| 45 | + # locator的value包含`/`时, 需要进行base64编码 |
| 46 | + # @see https://www.jetbrains.com/help/teamcity/rest/locators.html#e20589c5 |
| 47 | + encoded_tag="$(printf '%s' "tags/$TAG" | base64)" |
| 48 | + branch_locator="branch:(\$base64:$encoded_tag)" |
| 49 | +else |
| 50 | + branch_locator="branch:(policy:ALL_BRANCHES)" |
| 51 | +fi |
| 52 | +build_locator="buildType:$BUILD,status:SUCCESS,state:finished,$branch_locator" |
| 53 | + |
| 54 | +# @see https://www.jetbrains.com/help/teamcity/rest/manage-finished-builds.html#Get+Build+Artifacts |
| 55 | +download_artifacts() { |
| 56 | + _output="${BUILD}-${TAG:-latest}.zip" |
| 57 | + if ! curl \ |
| 58 | + --fail \ |
| 59 | + --header "Authorization: Bearer $TOKEN" \ |
| 60 | + --output "$_output" \ |
| 61 | + "${HOST}/app/rest/builds/${build_locator}/artifacts/archived/"; then |
| 62 | + echo "下载存档失败!" |
| 63 | + exit 1 |
| 64 | + fi |
| 65 | +} |
| 66 | + |
| 67 | +# @see https://www.jetbrains.com/help/teamcity/rest/get-build-details.html |
| 68 | +print_build_info() { |
| 69 | + curl \ |
| 70 | + --header "Authorization: Bearer $TOKEN" \ |
| 71 | + "${HOST}/app/rest/builds/${build_locator}" |
| 72 | +} |
| 73 | + |
| 74 | +# print_build_info |
| 75 | +download_artifacts |
0 commit comments