1#!/usr/bin/env bash 2# 3# Script to trigger the Git for Windows build and test run. 4# Set the $GFW_CI_TOKEN as environment variable. 5# Pass the branch (only branches on https://github.com/git/git are 6# supported) and a commit hash. 7# 8 9. ${0%/*}/lib-travisci.sh 10 11test$#-ne2&&echo"Unexpected number of parameters"&&exit1 12test -z"$GFW_CI_TOKEN"&&echo"GFW_CI_TOKEN not defined"&&exit 13 14BRANCH=$1 15COMMIT=$2 16 17gfwci () { 18local CURL_ERROR_CODE HTTP_CODE 19 CONTENT_FILE=$(mktemp -t "git-windows-ci-XXXXXX") 20whiletest -z$HTTP_CODE 21do 22 HTTP_CODE=$(curl \ 23-H"Authentication: Bearer$GFW_CI_TOKEN" \ 24--silent --retry5--write-out'%{HTTP_CODE}' \ 25--output>(sed"$(printf '1s/^\xef\xbb\xbf//')">$CONTENT_FILE) \ 26"https://git-for-windows-ci.azurewebsites.net/api/TestNow?$1" \ 27) 28 CURL_ERROR_CODE=$? 29# The GfW CI web app sometimes returns HTTP errors of 30# "502 bad gateway" or "503 service unavailable". 31# We also need to check the HTTP content because the GfW web 32# app seems to pass through (error) results from other Azure 33# calls with HTTP code 200. 34# Wait a little and retry if we detect this error. More info: 35# https://docs.microsoft.com/en-in/azure/app-service-web/app-service-web-troubleshoot-http-502-http-503 36iftest$HTTP_CODE-eq502|| 37test$HTTP_CODE-eq503|| 38grep"502 - Web server received an invalid response"$CONTENT_FILE>/dev/null 39then 40sleep10 41 HTTP_CODE= 42fi 43done 44cat$CONTENT_FILE 45rm$CONTENT_FILE 46iftest$CURL_ERROR_CODE-ne0 47then 48return$CURL_ERROR_CODE 49fi 50iftest"$HTTP_CODE"-ge400&&test"$HTTP_CODE"-lt600 51then 52return127 53fi 54} 55 56# Trigger build job 57BUILD_ID=$(gfwci "action=trigger&branch=$BRANCH&commit=$COMMIT&skipTests=false") 58iftest $? -ne0 59then 60echo"Unable to trigger Visual Studio Team Services Build" 61echo"$BUILD_ID" 62exit1 63fi 64 65# Check if the $BUILD_ID contains a number 66case$BUILD_IDin 67''|*[!0-9]*)echo"Unexpected build number:$BUILD_ID"&&exit1 68esac 69 70echo"Visual Studio Team Services Build #${BUILD_ID}" 71 72# Wait until build job finished 73STATUS= 74RESULT= 75while true 76do 77 LAST_STATUS=$STATUS 78 STATUS=$(gfwci "action=status&buildId=$BUILD_ID") 79test"$STATUS"="$LAST_STATUS"||printf"\nStatus: %s ""$STATUS" 80printf"." 81 82case"$STATUS"in 83 inProgress|postponed|notStarted)sleep10;;# continue 84"completed: succeeded") RESULT="success";break;;# success 85"completed: failed")break;;# failure 86*)echo"Unhandled status:$STATUS";break;;# unknown 87esac 88done 89 90# Print log 91echo"" 92echo"" 93gfwci "action=log&buildId=$BUILD_ID"| cut -c30- 94 95# Set exit code for TravisCI 96test"$RESULT"="success"