|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Run this in publish step after all version information have been updated. |
| 4 | +set -euo pipefail |
| 5 | + |
| 6 | +CIRCLECI_CLI_HOST="https://circleci.com" |
| 7 | + |
| 8 | +install_circleci() ( |
| 9 | + # Install the CircleCI CLI tool. |
| 10 | + # https://github.com/CircleCI-Public/circleci-cli |
| 11 | + # Dependencies: curl, cut |
| 12 | + # The version to install and the binary location can be passed in via VERSION and DESTDIR respectively. |
| 13 | + |
| 14 | + # GitHub's URL for the latest release, will redirect. |
| 15 | + local GITHUB_BASE_URL="https://github.com/CircleCI-Public/circleci-cli" |
| 16 | + local LATEST_URL="${GITHUB_BASE_URL}/releases/latest/" |
| 17 | + local DESTDIR="${DESTDIR:-/usr/local/bin}" |
| 18 | + local VERSION=$(curl -sLI -o /dev/null -w '%{url_effective}' "$LATEST_URL" | cut -d "v" -f 2) |
| 19 | + |
| 20 | + # Run the script in a temporary directory that we know is empty. |
| 21 | + local SCRATCH=$(mktemp -d || mktemp -d -t 'tmp') |
| 22 | + cd "$SCRATCH" |
| 23 | + |
| 24 | + error() ( |
| 25 | + echo "An error occured installing the tool." |
| 26 | + echo "The contents of the directory $SCRATCH have been left in place to help to debug the issue." |
| 27 | + ) |
| 28 | + |
| 29 | + trap error ERR |
| 30 | + |
| 31 | + case "$(uname)" in |
| 32 | + Linux) |
| 33 | + OS='linux' |
| 34 | + ;; |
| 35 | + Darwin) |
| 36 | + OS='darwin' |
| 37 | + ;; |
| 38 | + *) |
| 39 | + echo "This operating system is not supported." |
| 40 | + exit 1 |
| 41 | + ;; |
| 42 | + esac |
| 43 | + |
| 44 | + local RELEASE_URL="${GITHUB_BASE_URL}/releases/download/v${VERSION}/circleci-cli_${VERSION}_${OS}_amd64.tar.gz" |
| 45 | + |
| 46 | + # Download & unpack the release tarball. |
| 47 | + curl -sL --retry 3 "${RELEASE_URL}" | tar zx --strip 1 |
| 48 | + install circleci "$DESTDIR" |
| 49 | + command -v circleci |
| 50 | + |
| 51 | + # Delete the working directory when the install was successful. |
| 52 | + rm -r "$SCRATCH" |
| 53 | +) |
| 54 | + |
| 55 | +validate_circleci_orb_config() ( |
| 56 | + circleci orb validate build/package/circleci/orb.yml || (echo "Unable to validate orb"; exit 1) |
| 57 | +) |
| 58 | + |
| 59 | +publish_circleci() ( |
| 60 | + install_circleci |
| 61 | + validate_circleci_orb_config |
| 62 | + |
| 63 | + if circleci orb list | grep launchdarkly/ld-find-code-refs@$LD_RELEASE_VERSION; then |
| 64 | + echo "Version exists; skipping publishing CircleCI Orb" |
| 65 | + else |
| 66 | + echo "Live run: will publish orb to production circleci repo." |
| 67 | + circleci orb publish build/package/circleci/orb.yml launchdarkly/ld-find-code-refs@$LD_RELEASE_VERSION --token $CIRCLECI_CLI_TOKEN --host $CIRCLECI_CLI_HOST |
| 68 | + fi |
| 69 | +) |
| 70 | + |
| 71 | +dry_run_circleci() ( |
| 72 | + install_circleci |
| 73 | + validate_circleci_orb_config |
| 74 | + |
| 75 | + echo "Dry run: will not publish orb to production. Will publish to circleci dev repo instead." |
| 76 | + circleci orb publish build/package/circleci/orb.yml launchdarkly/ld-find-code-refs@dev:$LD_RELEASE_VERSION --token $CIRCLECI_CLI_TOKEN --host $CIRCLECI_CLI_HOST |
| 77 | +) |
0 commit comments