|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -o errexit |
| 4 | +set -o nounset |
| 5 | +set -o pipefail |
| 6 | + |
| 7 | +EVENT="workflow_dispatch" |
| 8 | +INPUTS="" |
| 9 | +PARSED_INPUTS=() |
| 10 | +PROFILE="" |
| 11 | +VERBOSE=false |
| 12 | +WORKFLOW="" |
| 13 | + |
| 14 | +# Script metadata. Don't modify this. |
| 15 | +MYDIR=$(cd -- "$(dirname "$0")" >/dev/null 2>&1; pwd -P) |
| 16 | +cd "${MYDIR}" |
| 17 | +MYNAME=$(basename "$0") |
| 18 | + |
| 19 | +# Configure colorful messages. |
| 20 | +GRAY=$(tput setaf 248) |
| 21 | +GREEN=$(tput setaf 2) |
| 22 | +RED=$(tput setaf 202) |
| 23 | +RESET=$(tput sgr0) |
| 24 | + |
| 25 | +# Output formatting. |
| 26 | +function success() { echo -e "${GREEN}${1}${RESET}"; } |
| 27 | +function info() { echo -e "${GRAY}${1}${RESET}"; } |
| 28 | +function error() { echo -e "${RED}ERROR: ${1}${RESET}" >&2; } |
| 29 | +function fail() { echo -e "\n${RED}ERROR: ${1}${RESET}" >&2; exit 1; } |
| 30 | +function argerr() { echo -e "\n${RED}ERROR: ${1}${RESET}" >&2; show_help; } |
| 31 | + |
| 32 | +# Helper functions. |
| 33 | +function file_missing() { if [[ -f "${1}" ]]; then return 1; else return 0; fi } |
| 34 | + |
| 35 | +function show_help() { |
| 36 | + echo -e "\nUsage: ${MYNAME} --workflow [workflow-name.yaml] --profile [aws-profile-name]\n" |
| 37 | + echo -e " Flags Description Req Default" |
| 38 | + echo -e " ------------- ---------------------------- --- -------------------" |
| 39 | + echo -e " -e --event Github event to simulate workflow_dispatch" |
| 40 | + echo -e " -i --inputs CSV list of k/v pairs * ${GRAY}null${RESET}" |
| 41 | + echo -e " -p --profile AWS profile to use for auth Y ${GRAY}null${RESET}" |
| 42 | + echo -e " -v --verbose Enable verbose mode (set -xv) ${GRAY}disabled${RESET}" |
| 43 | + echo -e " -w --workflow Path/file of workflow to run Y ${GRAY}null${RESET}" |
| 44 | + echo -e " -h --help Show this message\n" |
| 45 | + echo -e " * --inputs requirements are determined by the workflow.\n" |
| 46 | + echo -e "See https://nektosact.com/ for more info about 'act'.\n" |
| 47 | + exit |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +function parse_args() { |
| 52 | + # Parse the args. |
| 53 | + while [[ $# -gt 0 ]]; do |
| 54 | + case $1 in |
| 55 | + -e|--event) shift |
| 56 | + if (( $# < 1 )); then argerr "--event requires an event type [push|pull_request|workflow_dispatch]" |
| 57 | + else EVENT="${1}"; fi |
| 58 | + shift;; |
| 59 | + -i|--inputs) shift |
| 60 | + if (( $# < 1 )); then argerr "--inputs requires a csv list of input args" |
| 61 | + else INPUTS="${1}"; fi |
| 62 | + shift;; |
| 63 | + -p|--profile) shift |
| 64 | + if (( $# < 1 )); then argerr "--profile requires an AWS profile name" |
| 65 | + else PROFILE="${1}"; fi |
| 66 | + shift;; |
| 67 | + -w|--workflow) shift |
| 68 | + if (( $# < 1 )); then argerr "--workflow requires a path to a workflow file" |
| 69 | + else WORKFLOW="${1}"; fi |
| 70 | + shift;; |
| 71 | + -v|--verbose) VERBOSE=true; shift;; |
| 72 | + -h|--help) show_help;; |
| 73 | + *) argerr "Unknown argument '$1'";; |
| 74 | + esac |
| 75 | + done |
| 76 | + |
| 77 | + # Enable verbose mode if requested. |
| 78 | + if [ "${VERBOSE}" = true ]; then set -xv; fi |
| 79 | + |
| 80 | + # Make sure --workflow is passed. |
| 81 | + if [[ -z "${WORKFLOW}" ]]; then argerr "--workflow is required"; fi |
| 82 | + # Make sure --workflow is passed. |
| 83 | + if [[ -z "${PROFILE}" ]]; then argerr "--profile is required"; fi |
| 84 | + # Make sure workflow file exists |
| 85 | + if file_missing "${WORKFLOW}"; then fail "Workflow '${WORKFLOW}' does not exist"; fi |
| 86 | +} |
| 87 | + |
| 88 | + |
| 89 | +function validate_prereqs() { |
| 90 | + local prereqs="act gh" |
| 91 | + for p in ${prereqs}; do |
| 92 | + if ! command -v "${p}" &>/dev/null; then |
| 93 | + error "${p} not found. Install it and try again." |
| 94 | + if [[ "${p}" == "gh" ]]; then |
| 95 | + info "\nAfter installing gh, login:" |
| 96 | + info " gh auth login" |
| 97 | + info "\nMake sure you can get a token:" |
| 98 | + info " gh auth token" |
| 99 | + fi |
| 100 | + exit 1 |
| 101 | + fi |
| 102 | + done |
| 103 | +} |
| 104 | + |
| 105 | + |
| 106 | +function parse_inputs() { |
| 107 | + if [[ -n "${INPUTS}" ]]; then |
| 108 | + IFS="," |
| 109 | + for i in ${INPUTS}; do |
| 110 | + PARSED_INPUTS+=(--input "${i}") |
| 111 | + done |
| 112 | + fi |
| 113 | +} |
| 114 | + |
| 115 | + |
| 116 | +function main() { |
| 117 | + parse_args "$@" |
| 118 | + validate_prereqs |
| 119 | + parse_inputs |
| 120 | + |
| 121 | + act "${EVENT}" \ |
| 122 | + --secret GITHUB_TOKEN="$(gh auth token)" \ |
| 123 | + --secret-file .env \ |
| 124 | + --workflows "${WORKFLOW}" \ |
| 125 | + --env-file <(aws configure export-credentials --profile "${PROFILE}" --format env) \ |
| 126 | + "${PARSED_INPUTS[@]}" |
| 127 | +} |
| 128 | + |
| 129 | + |
| 130 | +main "$@" |
0 commit comments