1
0
James Ketrenos ef3e113f0d Updated to latest xe-solutions
Signed-off-by: James Ketrenos <james.p.ketrenos@intel.com>
2020-02-05 21:57:45 -08:00

226 lines
4.7 KiB
Bash
Executable File

#!/bin/bash
# 1. List project triggers, looking for 'sys_osgc CI trigger'
# 2. If not found, create the 'sys_osgc CI trigger'
# 3. Invoke the trigger
#
# The trigger will pass in the following variables (declared from
# the environment or from the SOLUTION file)
#
YES=
QUIET=
PROJECT=$(basename $(pwd))
while getopts ydqp: opt; do
case "${opt}" in
q)
QUIET=1
;;
p)
PROJECT=${OPTARG}
;;
y)
YES=1
;;
[?])
>&2 echo -e "\nInvalid parameter: ${opt}\n"
usage >&2
exit 1
;;
esac
done
shift $(( OPTIND-1 ))
TRIGGER_VARS=(
OS_DISTRO
OS_RELEASE
PACKAGE_STREAM
PACKAGE_REPOSITORY
BRANCH
BUILD
REF
)
# Create a copy of the current values for each trigger
# variable so we can show which ones were modified by the
# SOLUTION import.
for variable in ${TRIGGER_VARS[@]}; do
def=def_${variable}
export ${def}="${!variable}"
done
# Bring in the variables from SOLUTION file, supporting
# nested substitution
[[ -e SOLUTION ]] && . SOLUTION
VARS=()
[[ -e SOLUTION ]] && VARS=($(sed -nE "s,(^[^#][^=]*).*$,\1,pg" SOLUTION))
for var in ${VARS[@]}; do
export ${var}
done
# The following constants are used so we can run trigger.sh
# against a known build
BUILD=${BUILD:-7517585}
BRANCH=${BRANCH:-releases_19.4}
REF=${REF:-master}
PROJECT="vtt/sws/osgc/solutions/${PROJECT}"
URI_PROJECT=$(echo ${PROJECT} | sed -E 's,/,%2F,g')
eval $(grep ^SYS_OSGC_TOKEN SECRETS)
[[ "$SYS_OSGC_TOKEN" == "" ]] && {
>&2 echo "SECRETS needs to contain a GitLab token for SYS_OSGC_TOKEN."
exit -1
}
GITLAB="https://gitlab.devtools.intel.com"
(( ! QUIET )) && echo "Looking for CI trigger on ${PROJECT}"
RESULTS=$(curl --noproxy '*' -s -X GET \
--header "PRIVATE-TOKEN: ${SYS_OSGC_TOKEN}" \
--header "Content-Type: application/json" \
${GITLAB}/api/v4/projects/${URI_PROJECT}/triggers)
TOKEN=$(echo ${RESULTS} | sed -E 's#([[{},])#\1\n#g' | {
STATE=0
MATCH=
TOKEN=
# Parse the JSON looking for the trigger token and description of
# 'sys_osgc CI Trigger'
while read line; do
case ${STATE} in
0)
[[ ${line} =~ ^[[:space:]]*\{ ]] && STATE=1
;;
1)
[[ "${MATCH}" != "sys_osgc CI trigger" ]] &&
MATCH=$(echo ${line} |
sed -nE 's,^.*"description":\s*"([^"]+)".*$,\1,p')
[[ "${TOKEN}" == "" ]] &&
TOKEN=$(echo ${line} |
sed -nE 's,^.*"token":\s*"([^"]+)".*$,\1,p')
[[ "${TOKEN}" != "" ]] && [[ "${MATCH}" == "sys_osgc CI trigger" ]] && break
[[ "${line}" =~ .*\s*} ]] && {
MATCH=
TOKEN=
STATE=0
}
;;
esac
done
[[ "${TOKEN}" == "" ]] && echo -n ""
echo -n "${TOKEN}"
})
[[ "${TOKEN}" == "" ]] && {
(( ! QUIET )) && echo "'sys_osgc CI trigger' does not exist on ${PROJECT}."
(( YES )) || while read -n1 -r -p "Create new trigger [y|N]? "; do
case $REPLY in
y|Y)
echo ""
break
;;
n|N|"")
echo " Exiting."
exit 1
;;
*)
echo ""
;;
esac
done
RESULTS=$(curl --noproxy '*' -s -X POST \
--header "PRIVATE-TOKEN: ${SYS_OSGC_TOKEN}" \
-F 'description="sys_osgc CI trigger"' \
${GITLAB}/api/v4/projects/${URI_PROJECT}/triggers)
TOKEN=$(echo ${RESULTS} | sed -nE 's,.*"token":\s*"([^"]*).*,\1,p')
[[ "${TOKEN}" == "" ]] && {
>&2 echo "Unable to create trigger"
>&2 echo "${RESULTS}"
exit 1
}
(( ! QUIET )) && echo "Created token: ${TOKEN}"
}
(( ! QUIET )) && echo "Using token: ${TOKEN}"
function post {
CMD="curl --noproxy '*' -s -X POST -F token=${TOKEN} -F ref=${REF} "
for variable in ${TRIGGER_VARS[@]}; do
CMD+="-F variables[${variable}]=${!variable} "
done
CMD+="${GITLAB}/api/v4/projects/${URI_PROJECT}/trigger/pipeline"
RESULTS=$(${CMD})
PIPELINE=$(echo ${RESULTS} | sed -nE 's,^.*details_path":"([^"]*)".*,\1,p')
[[ "${PIPELINE}" == "" ]] && {
>&2 echo "Unable to find pipeline details URL when running:"
>&2 echo "${CMD}"
>&2 echo ${RESULTS}
return 1
}
(( ! QUIET )) && echo -e "Pipeline URL:\n\n ${GITLAB}${PIPELINE}\n"
(( QUIET )) && echo "$(basename ${PIPELINE})"
true
}
(( ! QUIET )) && cat << EOF
Triggering pipeline on project:
${PROJECT}
With the following variables:
$(
has_override=0
for variable in ${TRIGGER_VARS[@]}; do
def=def_${variable}
modified=' '
if [[ "${!def}" != "" ]]; then
if [[ "${!def}" != "${!variable}" ]]; then
modified='*'
has_override=1
fi
fi
echo " ${modified} ${variable}=${!variable}"
done
(( has_override )) && {
echo -e " ^\n │\n └── Value set in ENV but modified by SOLUTION file."
}
)
EOF
(( YES )) || while read -n1 -r -p "Is this correct [y|N]? "; do
case $REPLY in
y|Y)
echo ""
break
;;
n|N|"")
echo " Exiting."
exit 1
;;
*)
echo ""
;;
esac
done
post