|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | +# file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 6 | +# |
| 7 | +# Copyright Oxide Computer Company |
| 8 | + |
| 9 | +set -o errexit |
| 10 | +set -o pipefail |
| 11 | +set -o xtrace |
| 12 | + |
| 13 | +# We use `--netrc` in these calls in order to use the GitHub token Buildomat |
| 14 | +# provides us. This is not strictly necessary for all APIs we use, _except_ the |
| 15 | +# one to download artifacts. But using it for all calls keeps us from hitting |
| 16 | +# rate limits! |
| 17 | + |
| 18 | +API_BASE="https://api.github.com/repos/$GITHUB_REPOSITORY" |
| 19 | + |
| 20 | +# Buildomat creates at most one check suite per commit per repository, but |
| 21 | +# GitHub Actions will generally make several. We need to choose which run we |
| 22 | +# care about, ideally picking the one most closely related to this Buildomat |
| 23 | +# check suite. We first look for the most recently-created "push" run, but if |
| 24 | +# none are found we fall back to the most recently-created "pull_request" run. |
| 25 | +# |
| 26 | +# We check 10 times with 30 second pauses in between; if we don't have a check |
| 27 | +# run within about five minutes it'll probably never show up. |
| 28 | +for attempt in {1..10}; do |
| 29 | + runs=$(curl -sSfL --netrc "$API_BASE/actions/runs?head_sha=$GITHUB_SHA" \ |
| 30 | + | jq -r --arg name "$1" ' |
| 31 | + .workflow_runs |
| 32 | + | sort_by(.created_at) | reverse |
| 33 | + | .[] | select(.name == $name) |
| 34 | + | {id: .id, event: .event} |
| 35 | + ') |
| 36 | + for event in push pull_request; do |
| 37 | + run_id=$(jq -r --arg event "$event" 'select(.event == $event) | .id' <<<"$runs" | head -n 1) |
| 38 | + [[ -n "$run_id" ]] && break 2 |
| 39 | + done |
| 40 | + sleep 30 |
| 41 | +done |
| 42 | +if [[ -z "$run_id" ]]; then |
| 43 | + echo >&2 "no check run found" |
| 44 | + exit 1 |
| 45 | +fi |
| 46 | + |
| 47 | +# Wait for the run to complete. |
| 48 | +until [[ $(curl -sSfL --netrc "$API_BASE/actions/runs/$run_id" | jq -r .status) == completed ]]; do |
| 49 | + sleep 60 |
| 50 | +done |
| 51 | + |
| 52 | +# Get information about artifacts and download them. |
| 53 | +artifacts=$(curl -sSfL --netrc "$API_BASE/actions/runs/$run_id/artifacts" \ |
| 54 | + | jq -r '.artifacts[] | {id: .id, name: .name}') |
| 55 | +for artifact_id in $(jq -r '.id' <<<"$artifacts"); do |
| 56 | + artifact_name=$(jq -r --argjson id "$artifact_id" 'select(.id == $id) | .name' <<<"$artifacts") |
| 57 | + # Artifact names are not allowed to contain special filesystem characters: |
| 58 | + # https://github.com/actions/upload-artifact/issues/22 |
| 59 | + curl -sSfL --netrc -o "$artifact_name.zip" \ |
| 60 | + "$API_BASE/actions/artifacts/$artifact_id/zip" |
| 61 | +done |
0 commit comments