Created
March 23, 2021 15:45
-
-
Save timstott/e3f4401197e3f8b51290c8751c6f8088 to your computer and use it in GitHub Desktop.
Fetch/Download specific artifact from CircleCI build
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -euo pipefail | |
log_debug() { | |
local blue="\e[34m" | |
local default="\e[39m" | |
# ISO8601 timestamp + ms | |
local timestamp=$(date +%FT%T.%3NZ) | |
echo -e "${blue}${timestamp} [debug] $1${default}" 1>&2 | |
} | |
circleci() { | |
curl -s -H "Circle-Token: $CIRCLE_TOKEN" \ | |
"https://circleci.com/api/v1.1/project/github/$CIRCLE_PROJECT$1" | jq . | |
} | |
fetch_build_artifacts() { | |
BUILD_NUM="$1" | |
log_debug "Fetching build: $BUILD_NUM" | |
artifacts=$(circleci "/$BUILD_NUM/artifacts") | |
OUTPUT_DIR="factory_bot_debugger/$BUILD_NUM" | |
if [ -d "$OUTPUT_DIR" ]; then | |
echo "Artifacts already saved!" | |
exit 0 | |
fi | |
mkdir -p "$OUTPUT_DIR" | |
files=$( | |
echo "$artifacts" \ | |
| jq '[ .[] | select( .path == "factory_bot_debugger.json" ) ]' | |
) | |
files_count=$(echo "$files" | jq -r '. | length') | |
log_debug "Artifacts count: $files_count" | |
((files_count-=1)) | |
for index in $(seq 0 $files_count); do | |
node_index="$(echo "$files" | jq -r ".[$index].node_index")" | |
url="$(echo "$files" | jq -r ".[$index].url")" | |
log_debug "Fetching: $url" | |
curl -s -L -H "Circle-Token: $CIRCLE_TOKEN" "$url" > "$OUTPUT_DIR/${node_index}.json" | |
done | |
} | |
export -f fetch_build_artifacts log_debug circleci | |
build() { | |
fetch_build_artifacts "$1" | |
} | |
recent() { | |
circleci '?limit=10&filter=successful' | jq '.[] | .build_num' \ | |
| xargs -d '\n' -I {} bash -c 'fetch_build_artifacts "$@"' _ {} | |
} | |
command="$1" | |
shift | |
usage=$(cat <<-EOF | |
Usage: $0 build <number>|recent | |
build <number> | |
Fetch artifacts for the specifiec build number | |
recent | |
Fetch artifacts for the most recent successful builds | |
environment variables | |
- CIRCLE_TOKEN | |
Your CircleCI access token | |
Can be created at https://app.circleci.com/settings/user/tokens | |
- CIRCLE_PROJECT | |
The '<owner>/<repo>'. Example with the project | |
https://github.com/NixOS/nixpkgs it would be | |
CIRCLECI_PROJECT='NixOS/nixpkgs' | |
EOF | |
) | |
case "$command" in | |
build) build "$@" ;; | |
recent) recent "$@" ;; | |
*) echo "$usage" >&2; exit 1 ;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment