Created
October 22, 2014 12:01
-
-
Save tazjin/141b5ebffc0b6e3dd621 to your computer and use it in GitHub Desktop.
Download Buildbox artifacts
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
| #!/bin/bash | |
| # This script will download all Buildbox artifacts for a certain build, matching | |
| # a certain query string. | |
| # | |
| # The script is called as: | |
| # | |
| # buildbox-query ACCOUNT PROJECT BRANCH API_KEY AGENT_TOKEN QUERY_STRING | |
| # | |
| # With API_KEY being a key to the normal Buildbox API and AGENT_TOKEN being an | |
| # access token for a Buildbox agent. | |
| # We need both at the moment because the Buildbox API does not expose the download | |
| # URLs for artifacts, so we need to speak to the agent API. | |
| # A Github issue exists for this: https://github.com/buildbox/feedback/issues/69 | |
| # | |
| # The script will then fetch the URLs for all artifacts of the latest build and | |
| # download them. The artifacts can be restricted with a search query, but the | |
| # default is '*' (all) | |
| set -e | |
| # Ignoring * expansion because we need to pass it verbatim in strings. | |
| readonly GLOBIGNORE="*" | |
| function error() { | |
| local MSG="${1}" | |
| echo "${MSG}" | |
| exit 1 | |
| } | |
| function check_dependencies() { | |
| command -v jq > /dev/null 2> /dev/null || \ | |
| error "Please install jq" | |
| command -v curl > /dev/null 2> /dev/null || \ | |
| error "Please install curl" | |
| } | |
| function get_latest_build_id() { | |
| if [ ! "$#" -eq 4 ]; then | |
| error "get_latest_build_id() takes arguments ACCOUNT PROJECT BRANCH API_KEY" | |
| fi | |
| local ACCOUNT="${1}" | |
| local PROJECT="${2}" | |
| local BRANCH="${3}" | |
| local API_KEY="${4}" | |
| local API_URL="https://api.buildbox.io/v1/accounts/${ACCOUNT}/projects/${PROJECT}/builds?branch=${BRANCH}&per_page=1&api_key=${API_KEY}" | |
| curl -s "${API_URL}" | jq -r ".[0].id" | |
| } | |
| function get_artifacts() { | |
| if [ ! "$#" -eq 3 ]; then | |
| error "get_artifact_url() takes arguments AGENT_TOKEN BUILD_ID QUERY_STRING" | |
| fi | |
| local AGENT_TOKEN="${1}" | |
| local BUILD_ID="${2}" | |
| local QUERY_STRING="${3}" | |
| local QUERY_URL="https://agent.buildbox.io/v2/builds/${BUILD_ID}/artifacts/search?query=${QUERY_STRING}" | |
| curl -sH "Authorization: Token ${AGENT_TOKEN}" "${QUERY_URL}" |\ | |
| jq -r . | |
| } | |
| function download_artifacts() { | |
| local ARTIFACTS="$@" | |
| for artifact in $(echo $ARTIFACTS | jq -c -r '.[]'); do | |
| local URL=$(echo $artifact | jq -r '.url') | |
| echo "Downloading ${URL}..." | |
| curl -sO "${URL}" | |
| done | |
| } | |
| function main() { | |
| check_dependencies | |
| local ACCOUNT="${1}" | |
| local PROJECT="${2}" | |
| local BRANCH="${3}" | |
| local API_KEY="${4}" | |
| local AGENT_TOKEN="${5}" | |
| local QUERY_STRING=${6:-'*'} | |
| # Query for build ID | |
| local BUILD_ID=$(get_latest_build_id "${ACCOUNT}" "${PROJECT}" "${BRANCH}" "${API_KEY}") | |
| echo "Fetching artifacts for build ${BUILD_ID}" | |
| # Query for artifacts | |
| local ARTIFACTS=$(get_artifacts ${AGENT_TOKEN} ${BUILD_ID} ${QUERY_STRING}) | |
| local ARTIFACT_COUNT=$(echo $ARTIFACTS | jq 'length') | |
| if [ "${ARTIFACT_COUNT}" -eq 0 ]; then | |
| error "Found no artifacts for this build. Exiting..." | |
| fi | |
| echo "Found ${ARTIFACT_COUNT} artifacts." | |
| download_artifacts "${ARTIFACTS}" | |
| } | |
| if [ "$#" -lt 5 ] || [ "$#" -gt 6 ]; then | |
| error "Call this script with arguments ACCOUNT PROJECT BRANCH API_KEY AGENT_TOKEN [QUERY_STRING]" | |
| fi | |
| main "${1}" "${2}" "${3}" "${4}" "${5}" "${6}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment