travis-ci: record and skip successfully built trees
[git/git.git] / ci / run-windows-build.sh
CommitLineData
029aeeed
LS
1#!/usr/bin/env bash
2#
5621760f 3# Script to trigger the Git for Windows build and test run.
029aeeed
LS
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
657343a6
LS
9. ${0%/*}/lib-travisci.sh
10
029aeeed
LS
11test $# -ne 2 && echo "Unexpected number of parameters" && exit 1
12test -z "$GFW_CI_TOKEN" && echo "GFW_CI_TOKEN not defined" && exit
13
14BRANCH=$1
15COMMIT=$2
16
17gfwci () {
18 local CURL_ERROR_CODE HTTP_CODE
016d66f5
LS
19 CONTENT_FILE=$(mktemp -t "git-windows-ci-XXXXXX")
20 while test -z $HTTP_CODE
21 do
029aeeed
LS
22 HTTP_CODE=$(curl \
23 -H "Authentication: Bearer $GFW_CI_TOKEN" \
24 --silent --retry 5 --write-out '%{HTTP_CODE}' \
016d66f5 25 --output >(sed "$(printf '1s/^\xef\xbb\xbf//')" >$CONTENT_FILE) \
029aeeed
LS
26 "https://git-for-windows-ci.azurewebsites.net/api/TestNow?$1" \
27 )
28 CURL_ERROR_CODE=$?
016d66f5
LS
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
36 if test $HTTP_CODE -eq 502 ||
37 test $HTTP_CODE -eq 503 ||
38 grep "502 - Web server received an invalid response" $CONTENT_FILE >/dev/null
39 then
40 sleep 10
41 HTTP_CODE=
42 fi
43 done
44 cat $CONTENT_FILE
45 rm $CONTENT_FILE
029aeeed
LS
46 if test $CURL_ERROR_CODE -ne 0
47 then
48 return $CURL_ERROR_CODE
49 fi
50 if test "$HTTP_CODE" -ge 400 && test "$HTTP_CODE" -lt 600
51 then
52 return 127
53 fi
54}
55
56# Trigger build job
57BUILD_ID=$(gfwci "action=trigger&branch=$BRANCH&commit=$COMMIT&skipTests=false")
58if test $? -ne 0
59then
60 echo "Unable to trigger Visual Studio Team Services Build"
61 echo "$BUILD_ID"
62 exit 1
63fi
64
65# Check if the $BUILD_ID contains a number
66case $BUILD_ID in
67''|*[!0-9]*) echo "Unexpected build number: $BUILD_ID" && exit 1
68esac
69
70echo "Visual Studio Team Services Build #${BUILD_ID}"
71
a8b8b6b8
SG
72# Tracing execued commands would produce too much noise in the waiting
73# loop below.
74set +x
75
029aeeed
LS
76# Wait until build job finished
77STATUS=
78RESULT=
79while true
80do
81 LAST_STATUS=$STATUS
82 STATUS=$(gfwci "action=status&buildId=$BUILD_ID")
c4b49683 83 test "$STATUS" = "$LAST_STATUS" || printf "\nStatus: %s " "$STATUS"
029aeeed
LS
84 printf "."
85
86 case "$STATUS" in
87 inProgress|postponed|notStarted) sleep 10 ;; # continue
88 "completed: succeeded") RESULT="success"; break;; # success
6fa68ff2
LS
89 "completed: failed") break;; # failure
90 *) echo "Unhandled status: $STATUS"; break;; # unknown
029aeeed
LS
91 esac
92done
93
94# Print log
95echo ""
96echo ""
a8b8b6b8 97set -x
029aeeed
LS
98gfwci "action=log&buildId=$BUILD_ID" | cut -c 30-
99
100# Set exit code for TravisCI
101test "$RESULT" = "success"
9cc2c76f
SG
102
103save_good_tree