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