github.com/web-platform-tests/wpt.fyi@v0.0.0-20240530210107-70cf978996f1/util/deploy-comment.sh (about) 1 #!/bin/bash 2 3 # Helper script for posting a GitHub comment pointing to the deployed environment, 4 # from GitHub Actions. Also see deploy.sh 5 6 usage() { 7 USAGE='Usage: deploy-comment.sh [-e environment-name] [deployed url] 8 -e : Environment name (e.g. webapp) 9 -h : Show (this) help information 10 deployed url: The URL of the deployed environment, e.g. "https://branch-name-dot-wptdashboard-staging.appspot.com"' 11 echo "${USAGE}" 12 } 13 14 STAGING_URL=${@: -1} 15 ENVIRONMENT="staging" 16 while getopts ':e:' flag; do 17 case "${flag}" in 18 e) ENVIRONMENT="${OPTARG}" ;; 19 :) echo "Option -$OPTARG requires an argument." && exit 1;; 20 h|*) usage && exit 1;; 21 esac 22 done 23 24 UTIL_DIR="$(dirname "${BASH_SOURCE[0]}")" 25 source "${UTIL_DIR}/logging.sh" 26 27 if [[ -z "${STAGING_URL}" ]]; 28 then fatal "Deployed URL is required"; 29 else debug "Deployed URL: ${STAGING_URL}"; 30 fi 31 if [[ -z "${GITHUB_TOKEN}" ]]; 32 then fatal "GitHub Token is required"; 33 else debug "GitHub token detected."; 34 fi 35 # TODO: rewrite to GitHub Actions equivalent: 36 if [[ -z "${TRAVIS_REPO_SLUG}" ]]; 37 then fatal "Travis Repo slug (user/repo) is required"; 38 else debug "Travis Repo slug: ${TRAVIS_REPO_SLUG}"; 39 fi 40 if [[ -z "${TRAVIS_PULL_REQUEST_BRANCH}" ]]; 41 then fatal "Travis pull request branch is required"; 42 else debug "Travis pull request branch: ${TRAVIS_PULL_REQUEST_BRANCH}"; 43 fi 44 45 set -e 46 set -o pipefail 47 48 info "Posting deployed environment to GitHub..." 49 POST_URL="https://api.github.com/repos/${TRAVIS_REPO_SLUG}/deployments" 50 debug "${POST_URL}" 51 52 # By default, all commit statuses need to be "success" to deploy. 53 # But travis itself will execute this script, so we don't block on anything. 54 POST_BODY="{ 55 \"ref\": \"${TRAVIS_PULL_REQUEST_BRANCH}\", 56 \"task\": \"deploy\", 57 \"auto_merge\": false, 58 \"environment\": \"${ENVIRONMENT}\", 59 \"transient_environment\": true, 60 \"required_contexts\": [] 61 }" 62 debug "POST body: ${POST_BODY}" 63 64 debug "Copying output to ${TEMP_FILE:=$(mktemp)}" 65 curl -H "Authorization: token ${GITHUB_TOKEN}" \ 66 -H "Accept: application/vnd.github.ant-man-preview+json" \ 67 -X "POST" \ 68 -d "${POST_BODY}" \ 69 -s \ 70 "${POST_URL}" \ 71 | tee "${TEMP_FILE}" 72 if [[ "${EXIT_CODE:=${PIPESTATUS[0]}}" != "0" ]]; then exit ${EXIT_CODE}; fi 73 74 DEPLOYMENT_ID=$(jq .id ${TEMP_FILE}) 75 if [[ "${DEPLOYMENT_ID}" == "null" ]] 76 then 77 fatal "Something went wrong creating the deployment" 78 fi 79 80 debug "Created deployment ${DEPLOYMENT_ID}" 81 82 debug "Setting status to deployed" 83 POST_BODY="{ 84 \"state\": \"success\", 85 \"environment_url\": \"${STAGING_URL}\", 86 \"auto_inactive\": true 87 }" 88 curl -H "Authorization: token ${GITHUB_TOKEN}" \ 89 -H "Accept: application/vnd.github.ant-man-preview+json" \ 90 -X "POST" \ 91 -d "${POST_BODY}" \ 92 -s \ 93 "${POST_URL}/${DEPLOYMENT_ID}/statuses"