|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -e -o pipefail |
| 4 | + |
| 5 | +type curl >/dev/null 2>&1 || { |
| 6 | + echo >&2 'ERROR: Script requires "curl"' |
| 7 | + exit 1 |
| 8 | +} |
| 9 | +type sha256sum >/dev/null 2>&1 || { |
| 10 | + echo >&2 'ERROR: Script requires "sha256sum"' |
| 11 | + exit 1 |
| 12 | +} |
| 13 | +type tar >/dev/null 2>&1 || { |
| 14 | + echo >&2 'ERROR: Script requires "tar"' |
| 15 | + exit 1 |
| 16 | +} |
| 17 | + |
| 18 | +USAGE="$0: Download the go-coverage-report binary from GitHub. |
| 19 | +
|
| 20 | +This script is meant to be used as part of a GitHub action and makes use of Workflow commands as |
| 21 | +described in https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions |
| 22 | +
|
| 23 | +Usage: |
| 24 | + $0 version [sha256sum] |
| 25 | +
|
| 26 | +Example: |
| 27 | + $0 Linux X64 |
| 28 | +
|
| 29 | +You can use the following environment variables to configure the script: |
| 30 | +- RUNNER_OS: The operating system of the runner (default: Linux) |
| 31 | +- RUNNER_ARCH: The architecture of the runner (default: X64) |
| 32 | +- DRY_RUN: Do not actually move the binary to /usr/bin (default: false) |
| 33 | +" |
| 34 | + |
| 35 | +if [[ $# == 0 ]]; then |
| 36 | + echo -e "Error: script requires at least one argument\n" |
| 37 | + echo "$USAGE" |
| 38 | + exit 1 |
| 39 | +fi |
| 40 | + |
| 41 | +VERSION=$1 |
| 42 | +RUNNER_OS=${RUNNER_OS:-Linux} |
| 43 | +RUNNER_ARCH=${RUNNER_ARCH:-X64} |
| 44 | + |
| 45 | +if [[ -z ${VERSION+x} ]]; then |
| 46 | + echo "Missing version argument" |
| 47 | + exit 1 |
| 48 | +fi |
| 49 | + |
| 50 | +if [[ $# == 2 ]]; then |
| 51 | + SHA256SUM=$2 |
| 52 | +fi |
| 53 | + |
| 54 | +sudo_or_dry_run="sudo" |
| 55 | +if [[ "$DRY_RUN" == "true" ]]; then |
| 56 | + sudo_or_dry_run='echo [DRY-RUN]: sudo' |
| 57 | +fi |
| 58 | + |
| 59 | +start_group() { |
| 60 | + echo "::group::$*" |
| 61 | + { |
| 62 | + set -x |
| 63 | + return |
| 64 | + } 2>/dev/null |
| 65 | +} |
| 66 | + |
| 67 | +end_group() { |
| 68 | + { |
| 69 | + set +x |
| 70 | + return |
| 71 | + } 2>/dev/null |
| 72 | + echo "::endgroup::" |
| 73 | +} |
| 74 | + |
| 75 | +if [[ $VERSION == "local" ]]; then |
| 76 | + start_group "Installing go-coverage-report from local source" |
| 77 | + go install -v ./cmd/go-coverage-report |
| 78 | + end_group |
| 79 | + exit 0 |
| 80 | +fi |
| 81 | + |
| 82 | +if [[ ${#VERSION} == 40 ]]; then |
| 83 | + start_group "Installing go-coverage-report from remote source" |
| 84 | + go install -v "github.com/teamniteo/go-coverage-report@$VERSION" |
| 85 | + end_group |
| 86 | + exit 0 |
| 87 | +fi |
| 88 | + |
| 89 | +start_group "Determining runner architecture" |
| 90 | +if [ "$RUNNER_ARCH" = "ARM64" ]; then |
| 91 | + ARCH="arm64" |
| 92 | +elif [ "$RUNNER_ARCH" = "ARM" ]; then |
| 93 | + ARCH="arm" |
| 94 | +elif [ "$RUNNER_ARCH" = "X86" ]; then |
| 95 | + ARCH="386" |
| 96 | +elif [ "$RUNNER_ARCH" = "X64" ]; then |
| 97 | + ARCH="amd64" |
| 98 | +else |
| 99 | + ARCH="amd64" |
| 100 | +fi |
| 101 | +end_group |
| 102 | + |
| 103 | +start_group "Downloading tar archive from GitHub" |
| 104 | +mkdir -p .github/outputs |
| 105 | +OS=$(echo "$RUNNER_OS" | tr '[:upper:]' '[:lower:]') |
| 106 | +FILENAME="go-coverage-report-${VERSION}-${OS}-${ARCH}.tar.gz" |
| 107 | +URL="https://github.com/teamniteo/go-coverage-report/releases/download/${VERSION}/${FILENAME}" |
| 108 | +curl --fail --location "$URL" --output ".github/outputs/$FILENAME" |
| 109 | +end_group |
| 110 | + |
| 111 | +if ! [[ "$SHA256SUM" ]]; then |
| 112 | + start_group "Checking checksum using checksums.txt file from GitHub release" |
| 113 | + URL="https://github.com/teamniteo/go-coverage-report/releases/download/${VERSION}/checksums.txt" |
| 114 | + cd .github/outputs |
| 115 | + curl -fsSL "$URL" | sha256sum --check --ignore-missing |
| 116 | + cd - |
| 117 | + end_group |
| 118 | +else |
| 119 | + start_group "Checking checksum using provided SHA256 hash" |
| 120 | + echo "Actual sha256:" |
| 121 | + sha256sum ".github/outputs/$FILENAME" |
| 122 | + echo "Checking checksum" |
| 123 | + echo "$SHA256SUM .github/outputs/$FILENAME" | sha256sum -c |
| 124 | + end_group |
| 125 | +fi |
| 126 | + |
| 127 | +start_group "Decompressing tar archive" |
| 128 | +tar -xzf ".github/outputs/$FILENAME" -C .github/outputs/ go-coverage-report |
| 129 | +rm ".github/outputs/$FILENAME" |
| 130 | +end_group |
| 131 | + |
| 132 | +start_group "Move binary to /usr/bin" |
| 133 | +$sudo_or_dry_run mv .github/outputs/go-coverage-report /usr/bin |
| 134 | +end_group |
0 commit comments