|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# These should be set by Travis |
| 4 | +#TRAVIS_BUILD_NUMBER=1 |
| 5 | +#TRAVIS_BRANCH=master |
| 6 | +#TRAVIS_REPO_SLUG="RemoteTechnologiesGroup/RemoteTech" |
| 7 | +#TRAVIS_COMMIT=master |
| 8 | +#GITHUB_TOKEN="Personal access token from https://github.com/settings/applications" |
| 9 | +#TRAVIS_PULL_REQUEST=false |
| 10 | + |
| 11 | +VERSION="build-${TRAVIS_BRANCH}-${TRAVIS_BUILD_NUMBER}" |
| 12 | +FILENAME=$(echo "${VERSION}.zip" | tr '/' '_') # else it will fail on branches like chore/travis |
| 13 | + |
| 14 | +python_parse_json() { |
| 15 | + # python errors are surpressed for when the key doesn't exist |
| 16 | + cat | python -c 'import sys,json;obj=json.load(sys.stdin);print obj[sys.argv[1]];' $1 2>/dev/null |
| 17 | +} |
| 18 | + |
| 19 | +if [ -z "$GITHUB_TOKEN" ] || [ -z "$TRAVIS_REPO_SLUG" ] \ |
| 20 | + || [ -z "$TRAVIS_BUILD_NUMBER" ] || [ -z "$TRAVIS_BRANCH" ] \ |
| 21 | + || [ -z "$TRAVIS_COMMIT" ] |
| 22 | +then |
| 23 | + echo "GITHUB_TOKEN, TRAVIS_REPO_SLUG, TRAVIS_BUILD_NUMBER and TRAVIS_COMMIT must be set in order to deploy"; |
| 24 | + echo "Skipping deploy for now"; |
| 25 | + exit 0; # prevent build failing if unset |
| 26 | +fi |
| 27 | + |
| 28 | +if [ "$TRAVIS_PULL_REQUEST" != "false" ] |
| 29 | +then |
| 30 | + echo "This is a pull request build, it doesn't need to be released." |
| 31 | + exit 0; # prevent build fail |
| 32 | +fi |
| 33 | + |
| 34 | +if [[ "$TRAVIS_BRANCH" == build* ]] |
| 35 | +then |
| 36 | + echo "We're already on a 'build branch' (or tag), don't need to deploy again"; |
| 37 | + exit 0; |
| 38 | +fi |
| 39 | + |
| 40 | +echo "Build ${TRAVIS_BUILD_NUMBER} from branch ${TRAVIS_BRANCH} in ${TRAVIS_REPO_SLUG}" > GameData/build.txt |
| 41 | +echo "Built from commit ${TRAVIS_COMMIT} with tag ${BUILDTAG}" >> GameData/build.txt |
| 42 | +echo "Creating ${FILENAME}" |
| 43 | +zip -r "${FILENAME}" GameData/ |
| 44 | + |
| 45 | +echo "Attempting to create tag ${VERSION} on ${TRAVIS_REPO_SLUG}" |
| 46 | +API_JSON=$(printf '{"tag_name": "%s","target_commitish": "%s","name": "%s","body": "Automated pre-release of branch %s build %s","draft": false,"prerelease": true}' \ |
| 47 | + $VERSION $TRAVIS_COMMIT $VERSION $TRAVIS_BRANCH $TRAVIS_BUILD_NUMBER) |
| 48 | +ADDRESS=$(printf 'https://api.github.com/repos/%s/releases?access_token=%s' $TRAVIS_REPO_SLUG $GITHUB_TOKEN) |
| 49 | + |
| 50 | +REPLY=$(curl --data "$API_JSON" "$ADDRESS"); |
| 51 | +UPLOAD_ID=$(echo $REPLY | python_parse_json "id") |
| 52 | +ERRORS=$(echo $REPLY | python_parse_json "errors"); |
| 53 | + |
| 54 | +if [ -n "$ERRORS" ] || [ -z "$REPLY" ] || [ -z "$UPLOAD_ID" ] |
| 55 | +then |
| 56 | + echo "ERROR: An error occured while setting the tag"; |
| 57 | + echo $REPLY; |
| 58 | + exit 1; |
| 59 | +fi |
| 60 | + |
| 61 | +UPLOAD_URL="https://uploads.github.com/repos/${TRAVIS_REPO_SLUG}/releases/${UPLOAD_ID}/assets" |
| 62 | + |
| 63 | +echo "Uploading ${FILENAME} to GitHub repo ${UPLOAD_ID} (tag ${VERSION} on ${TRAVIS_REPO_SLUG})" |
| 64 | +REPLY=$(curl -H "Authorization: token ${GITHUB_TOKEN}" \ |
| 65 | + -H "Accept: application/vnd.github.manifold-preview" \ |
| 66 | + -H "Content-Type: application/zip" \ |
| 67 | + --data-binary @${FILENAME} \ |
| 68 | + "${UPLOAD_URL}?name=${FILENAME}") |
| 69 | + |
| 70 | +ERRORS=$(echo $REPLY | python_parse_json "errors") |
| 71 | +ASSET_ID=$(echo $REPLY | python_parse_json "id" ) |
| 72 | + |
| 73 | +if [ -n "$ERRORS" ] || [ -z "$REPLY" ] || [ -z "$ASSET_ID" ] |
| 74 | +then |
| 75 | + echo "ERROR: An error occured while uploading the file to GitHub"; |
| 76 | + echo $REPLY; |
| 77 | + exit 1; |
| 78 | +fi |
| 79 | + |
| 80 | +echo "Uploaded ${FILENAME} to ${TRAVIS_REPO_SLUG} as asset id ${ASSET_ID}" |
0 commit comments