adding script to download artefacts

This commit is contained in:
AleaJactaEst 2018-04-05 22:21:55 +02:00
parent f661343e0a
commit 07e05605f6

View file

@ -0,0 +1,98 @@
#!/bin/bash
# Script to download artefact
#
# Copyright (C) 2017 AleaJactaEst
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# define project & branch
usage()
{
cat << EOF
usage:$0 url path project branch [branch]
url : define url gitlab (ex.: "https://git.khaganat.net")
path : define path you can access on your project (Ex.: "khaganat")
project : project name (ex.: "mmorpg_khanat/khanat-opennel-code")
branch : define branch (ex. "develop")
you can define other branch (script check 1st, 2nd, ... to find branch with success build)
EOF
exit 2
}
if [[ -z "$1" ]]
then
echo "*** ERROR : missing 1st argument (url root 'https://yoursite')" >&2
usage
fi
if [[ -z "$2" ]]
then
echo "*** ERROR : missing 2nd argument (path project)" >&2
usage
fi
if [[ -z "$3" ]]
then
echo "*** ERROR : missing 3th argument (project - define in url)" >&2
usage
fi
if [[ -z "$4" ]]
then
echo "*** ERROR : missing 4rd argument (branch)" >&2
usage
fi
# urlroot like "https://git.khaganat.net"
urlroot=$1
shift
# urlpath like "khaganat"
urlpath=$1
shift
# project like "mmorpg_khanat/khanat-opennel-code"
project=$1
shift
declare tempfile="/tmp/download_artefacts.$$"
# Get list build
wget -q -O $tempfile.json "$urlroot/$urlpath/"$project"/pipelines.json?scope=all&page=1"
# GET LAST ID
lastid=0
while [[ $lastid -eq 0 ]]
do
# get param branch (like "develop")
branch=$1
shift
echo "Search artefact on $branch"
lastid=$(jq -r '.pipelines[] | select (.details.status.group | contains("success")) | select ( .path | contains("'$project'")) | select ( .ref.name == "'$branch'") | .id as $ident | $ident ' $tempfile.json | awk 'BEGIN{ID=0;}{if($1 > ID) ID=$1;}END{print ID;}')
done
echo "Get artefacts from $branch #$lastid"
# Get end URL artefact for specific ID
jq -r '.pipelines[] | select ( .id == '$lastid' ) | .details.artifacts[] as $detail | [ {artifacts_path: $detail.path} ] | (.[0] | keys_unsorted) as $keys | ([$keys] + map([.[ $keys[] ]])) [1] | @tsv' $tempfile.json > $tempfile.tsv
# Download artefact
while read -r line
do
wget -q --content-disposition "$urlroot$line"
done < $tempfile.tsv
rm $tempfile.*
echo "END"