1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-22 05:33:10 +02:00

introduce function for notifying github

This commit is contained in:
Marcus Holl 2019-03-01 13:27:33 +01:00
parent e69a690614
commit 31a10bc522

View File

@ -1,5 +1,29 @@
#!/bin/bash #!/bin/bash
function fail() {
local message="$1"
local returnCode=${2:-1}
echo "[ERROR] ${message}" >&2
exit ${returnCode}
}
function notify() {
local state=${1}
local description=${2}
local hash=${3}
echo "[INFO] Notifying about state \"${state}\" for commit \"${hash}\"."
curl -X POST \
--fail \
--silent \
--output /dev/null \
--data "{\"state\": \"${state}\", \"target_url\": \"${TRAVIS_BUILD_WEB_URL}\", \"description\": \"${description}\", \"context\": \"integration-tests\"}" \
--user "${INTEGRATION_TEST_VOTING_USER}:${INTEGRATION_TEST_VOTING_TOKEN}" \
"https://api.github.com/repos/SAP/jenkins-library/statuses/${hash}" || fail "Cannot send notification. curl return code: $?"
}
# #
# In case the build is performed for a pull request TRAVIS_COMMIT is a merge # In case the build is performed for a pull request TRAVIS_COMMIT is a merge
# commit between the base branch and the PR branch HEAD. That commit is actually built. # commit between the base branch and the PR branch HEAD. That commit is actually built.
@ -14,19 +38,20 @@
COMMIT_HASH_FOR_STATUS_NOTIFICATIONS="${TRAVIS_PULL_REQUEST_SHA}" COMMIT_HASH_FOR_STATUS_NOTIFICATIONS="${TRAVIS_PULL_REQUEST_SHA}"
[ -z "${COMMIT_HASH_FOR_STATUS_NOTIFICATIONS}" ] && COMMIT_HASH_FOR_STATUS_NOTIFICATIONS="${TRAVIS_COMMIT}" [ -z "${COMMIT_HASH_FOR_STATUS_NOTIFICATIONS}" ] && COMMIT_HASH_FOR_STATUS_NOTIFICATIONS="${TRAVIS_COMMIT}"
curl -X POST \ notify "pending" "Integration tests in progress." "${COMMIT_HASH_FOR_STATUS_NOTIFICATIONS}"
--data "{\"state\": \"pending\", \"target_url\": \"${TRAVIS_BUILD_WEB_URL}\", \"description\": \"Integration tests pending.\", \"context\": \"integration-tests\"}" \
--user "${INTEGRATION_TEST_VOTING_USER}:${INTEGRATION_TEST_VOTING_TOKEN}" \
"https://api.github.com/repos/SAP/jenkins-library/statuses/${COMMIT_HASH_FOR_STATUS_NOTIFICATIONS}"
WORKSPACES_ROOT=workspaces WORKSPACES_ROOT=workspaces
[ -e "${WORKSPACES_ROOT}" ] && rm -rf ${WORKSPACES_ROOT} [ -e "${WORKSPACES_ROOT}" ] && rm -rf ${WORKSPACES_ROOT}
TEST_CASES=$(find testCases -name '*.yml') TEST_CASES=$(find testCases -name '*.yml')
# This auxiliar thread is needed in order to produce some output while the
# test are running. Otherwise the job will be canceled after 10 minutes without
# output.
while true; do sleep 10; echo "[INFO] Integration tests still running."; done & while true; do sleep 10; echo "[INFO] Integration tests still running."; done &
notificationThreadPid=$! notificationThreadPid=$!
declare -a processes
i=0 i=0
for f in ${TEST_CASES} for f in ${TEST_CASES}
do do
@ -35,7 +60,7 @@ do
echo "[INFO] Running test case \"${testCase}\" in area \"${area}\"." echo "[INFO] Running test case \"${testCase}\" in area \"${area}\"."
TEST_CASE_ROOT="${WORKSPACES_ROOT}/${area}/${testCase}" TEST_CASE_ROOT="${WORKSPACES_ROOT}/${area}/${testCase}"
[ -e "${TEST_CASE_ROOT}" ] && rm -rf "${TEST_CASE_ROOT}" [ -e "${TEST_CASE_ROOT}" ] && rm -rf "${TEST_CASE_ROOT}"
mkdir -p "${TEST_CASE_ROOT}" mkdir -p "${TEST_CASE_ROOT}" || fail "Cannot create test case root director for test case \"${testCase}\"." 1
source ./runTest.sh "${testCase}" "${TEST_CASE_ROOT}" &> "${TEST_CASE_ROOT}/log.txt" & source ./runTest.sh "${testCase}" "${TEST_CASE_ROOT}" &> "${TEST_CASE_ROOT}/log.txt" &
pid=$! pid=$!
processes[$i]="${testCase}:${pid}" processes[$i]="${testCase}:${pid}"
@ -43,7 +68,7 @@ do
let i=i+1 let i=i+1
done done
[ "${i}" == 0 ] && { echo "No tests has been executed."; exit 1; } [ "${i}" == 0 ] && fail "No tests has been executed." 1
# #
# wait for the test cases and cat the log # wait for the test cases and cat the log
@ -63,7 +88,6 @@ kill -PIPE "${notificationThreadPid}" &>/dev/null
for p in "${processes[@]}" for p in "${processes[@]}"
do do
testCase=${p%:*} testCase=${p%:*}
processId=${p#*:}
echo "[INFO] === START === Logs for test case \"${testCase}\" ===." echo "[INFO] === START === Logs for test case \"${testCase}\" ===."
cat "${TEST_CASE_ROOT}/log.txt" cat "${TEST_CASE_ROOT}/log.txt"
echo "[INFO] === END === Logs for test case \"${testCase}\" ===." echo "[INFO] === END === Logs for test case \"${testCase}\" ===."
@ -84,30 +108,21 @@ do
status="FAILURE" status="FAILURE"
failure="true" failure="true"
fi fi
printf "[INFO] %-30s: %s\n" "${testCase}" ${status} printf "[INFO] %-30s: %s\n" "${testCase}" "${status}"
done done
STATUS_DESCRIPTION="The integration tests failed." STATUS_DESCRIPTION="The integration tests failed."
STATUS_STATE="failure" STATUS_STATE="failure"
if [ "${failure}" == "true" ] if [ "${failure}" == "false" ]
then then
echo "[WARNING] There are test failures. Check earlier log for details."
else
STATUS_DESCRIPTION="The integration tests succeeded." STATUS_DESCRIPTION="The integration tests succeeded."
STATUS_STATE="success" STATUS_STATE="success"
fi fi
notify "${STATUS_STATE}" "${STATUS_DESCRIPTION}" "${COMMIT_HASH_FOR_STATUS_NOTIFICATIONS}"
[ "${failure}" != "false" ] && fail "Integration tests failed." 1
echo "[INFO] Integration tests succeeded." echo "[INFO] Integration tests succeeded."
curl -X POST \
--data "{\"state\": \"${STATUS_STATE}\", \"target_url\": \"${TRAVIS_BUILD_WEB_URL}\", \"description\": \"${STATUS_DESCRIPTION}\", \"context\": \"integration-tests\"}" \
--user "${INTEGRATION_TEST_VOTING_USER}:${INTEGRATION_TEST_VOTING_TOKEN}" \
"https://api.github.com/repos/SAP/jenkins-library/statuses/${COMMIT_HASH_FOR_STATUS_NOTIFICATIONS}"
if [ "${failure}" == "true" ]
then
exit 1
fi
exit 0 exit 0