angular-cn/aio/scripts/deploy-preview.sh
Georgios Kalpakas d263595c63 ci(aio): do not fail when re-deploying preview for the same PR/SHA
Previously, when trying to upload the build artifacts for a PR/SHA that was
already successfully deployed (e.g. when re-running a Travis job), the preview
server would return a 403 and the build would fail.

Since we have other mechanisms to verify that the PR author is trusted and the
artifacts do indeed come from the specified PR and since the new artifacts
should be the same with the already deployed ones (same SHA), there is no reason
to fail the build. The preview server will reject the request with a special
HTTP status code (409 - Conflict), which the `deploy-preview` script will
recognize and exit with 0.
2017-04-13 11:26:21 +01:00

34 lines
904 B
Bash
Executable File

#!/usr/bin/env bash
# WARNING: NGBUILDS_IO_KEY should NOT be printed.
set +x -eu -o pipefail
INPUT_DIR=dist/
OUTPUT_FILE=/tmp/snapshot.tar.gz
AIO_BUILDS_HOST=https://ngbuilds.io
UPLOAD_URL=$AIO_BUILDS_HOST/create-build/$TRAVIS_PULL_REQUEST/$TRAVIS_PULL_REQUEST_SHA
cd "`dirname $0`/.."
yarn run build
tar --create --gzip --directory "$INPUT_DIR" --file "$OUTPUT_FILE" .
exec 3>&1
httpCode=$(
curl --include --location --request POST --silent --write-out "\nHTTP_CODE: %{http_code}\n" \
--header "Authorization: Token $NGBUILDS_IO_KEY" --data-binary "@$OUTPUT_FILE" "$UPLOAD_URL" \
| sed 's/\r\n/\n/' \
| tee /dev/fd/3 \
| tail -1 \
| sed 's/HTTP_CODE: //'
)
# Exit with an error if the request failed.
# (Ignore 409 failures, which mean trying to re-deploy for the same PR/SHA.)
if [ $httpCode -lt 200 ] || ([ $httpCode -ge 400 ] && [ $httpCode -ne 409 ]); then
exit 1
fi
cd -