#!/bin/bash
THIS=$(basename $0)
NOW=$(date '+%Y%m%d')
function usage() {
echo -e "$THIS 0.2\t\nAuthor: Renato Campos <[email protected]>\t"
echo -e "\n$THIS [option] [-j backoffice|taf|release|sistemico]"
echo -e "\nOptions:"
echo -e "\t-r, build rpo "
echo -e "\t-i, build image"
echo -e "\t-u, flag upddistr"
echo -e "\t-v, new version (12.1.2310). If empty, build version will be 12.1.2210"
echo -e "\n$THIS -h (shows this help)"
echo -e "If used without options, this script will return fail\n"
echo -e "Remember export DRONE_TOKEN in your environment\n"
exit -1
}
function getJobId() {
DroneAPI=$1
numberBuild=$2
curl --request GET -s \
--url "$DroneAPI/builds/$numberBuild" \
--header 'Authorization: Bearer '$DRONE_TOKEN \
--header 'Content-Type: application/json' | jq -r '. | select( .status == "running")' | jq -r .number
}
function getJobSTEP() {
DroneAPI=$1
numberBuild=$2
curl --request GET -s \
--url "$DroneAPI/builds/$numberBuild" \
--header 'Authorization: Bearer '$DRONE_TOKEN \
--header 'Content-Type: application/json' | jq -r '.stages | .[].steps | .[] | select ( .status == "running" )' | jq -r .name
}
function waitProcJob(){
DroneAPI=$1
numberBuild=$2
echo "Job $numberBuild started in $DroneAPI"
while [[ "$(getJobId $DroneAPI $numberBuild)" != "" ]]; do
echo "Job $numberBuild - STEP: "$(getJobSTEP $DroneAPI $numberBuild)" - running..."
sleep 10
done
}
function buildWithParameters(){
DroneAPI=$1
param=$2
curl --request POST -s \
--url "$DroneAPI/builds?$param" \
--header 'Authorization: Bearer '$DRONE_TOKEN \
--header 'Content-Type: application/json' | jq -r .number
}
function gerarpo() {
branch=$1
release=$2
DroneAPI="https://drone.engpro.totvs.com.br/api/repos/smarterp/pipeline-rpo"
param="branch=$branch&RELEASE=$release&BUILDIMAGE=false"
numberBuild=$(buildWithParameters $DroneAPI $param)
sleep 10
waitProcJob $DroneAPI $numberBuild
}
function geraimage() {
branch=$1
release=$2
upddistr=$3
DroneAPI="https://drone.engpro.totvs.com.br/api/repos/smarterp/pipeline-imagem"
param="branch=$branch&RELEASE=$release&UPDDISTR=$upddistr&GERA_IMAGEM=true&GERA_CHART=true"
numberBuild=$(buildWithParameters $DroneAPI $param)
sleep 10
waitProcJob $DroneAPI $numberBuild
}
# check whether user had supplied -h or --help . If yes display usage
if [[ ( $# == "--help") || $# == "-h" ]] ;then
usage
fi
if [ $# -le 1 ]; then
usage
fi
if [ $# -ge 2 ]; then
rpo="false"
image="false"
upddistr="false"
JOB=""
VERSION="12.1.2210"
while getopts ": hriu j:" opt; do
case $opt in
"h") usage;;
"r") rpo="true" ;;
"i") image="true" ;;
"u") upddistr="true" ;;
"v") VERSION="12.1.2310" ;;
"j") JOB=${OPTARG} # defina $ JOB para o valor especificado.
;;
:)
echo "Erro: - $ {OPTARG} requer um argumento."
usage
;;
*)
usage
;;
esac
done
if [ "$rpo" == "true" ]; then
echo "Starting CI SmartAPI - RPO: $JOB - $VERSION"
gerarpo $JOB $VERSION
fi
if [ "$image" == "true" ]; then
if [ "$upddistr" == "true" ]; then
echo "Starting CI SmartBuild - IMAGE: $JOB - with UPDDISTR"
else
echo "Starting CI SmartBuild - IMAGE: $JOB - without UPDDISTR"
fi
geraimage $JOB $VERSION $upddistr
fi
echo "SmartERP-CI terminate. See file /tmp/ci-smarterp-xxxx.txt"
fi
exit 0 |