Created
June 15, 2016 07:41
-
-
Save Voronenko/703d25f4a3a468a212888b452a14227a to your computer and use it in GitHub Desktop.
gitlab_get_last_artifact.sh
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 | |
# credits https://gitlab.com/morph027/gitlab-ci-helpers/blob/master/get-last-successful-build-artifact.sh | |
set -e | |
# don't forget to send ref | |
# BASE_URL=https://zzz.githost.io/ | |
# PRIVATE_TOKEN=aaaaaaaa | |
# STAGE=build | |
# PROJECT=5 | |
# REF=develop | |
# OUT_FILE=artifacts.zip | |
check_vars() { | |
for VAR in ${VARS[@]} | |
do | |
if [ -z ${!VAR} ]; then echo "\$${VAR} is missing"; exit 1; fi | |
done | |
} | |
check_files() { | |
for REQ in ${REQS[@]} | |
do | |
which ${REQ} >/dev/null | |
if [ ! $? -eq 0 ]; then | |
echo "requirement ${REQ} is missing" | |
exit 1 | |
fi | |
done | |
} | |
download_latest() { | |
[ -z $OUT_FILE ] && OUT_FILE="artifacts.zip" | |
echo curl -s -o ${OUT_FILE} -H "PRIVATE-TOKEN: ${PRIVATE_TOKEN}" "${BASE_URL}/api/v3/projects/${PROJECT}/builds/${LAST_SUCCESSFUL_BUILD}/artifacts" | |
curl -s -o ${OUT_FILE} -H "PRIVATE-TOKEN: ${PRIVATE_TOKEN}" "${BASE_URL}/api/v3/projects/${PROJECT}/builds/${LAST_SUCCESSFUL_BUILD}/artifacts" | |
} | |
# check requirements | |
REQS=( curl jq ) | |
check_files | |
# set vars in project settings | |
# grab project id with: | |
# curl -s -H "PRIVATE-TOKEN: $PRIVATE-TOKEN" "${BASEURL}/api/v3/projects" | jq . | |
VARS=( BASE_URL PRIVATE_TOKEN PROJECT STAGE REF) | |
check_vars | |
echo curl -s -H "PRIVATE-TOKEN: ${PRIVATE_TOKEN}" "${BASE_URL}/api/v3/projects/${PROJECT}/builds?per_page=${PER_PAGE:-50}" | |
# fetch last successful build | |
LAST_SUCCESSFUL_BUILD=$(curl -s -H "PRIVATE-TOKEN: ${PRIVATE_TOKEN}" "${BASE_URL}/api/v3/projects/${PROJECT}/builds?per_page=${PER_PAGE:-50}" | jq -c '.[] | select(.status=="success") | select(.stage=="'${STAGE}'") | select(.ref=="'${REF:-master}'") | {id}' | head -n1 | grep -oE '[0-9]+') | |
echo Located: $LAST_SUCCESSFUL_BUILD , downloading: | |
# download | |
download_latest |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment