diff --git a/.github/workflows/install-frsca.yaml b/.github/workflows/install-frsca.yaml index 90af0858..184ef538 100644 --- a/.github/workflows/install-frsca.yaml +++ b/.github/workflows/install-frsca.yaml @@ -55,27 +55,7 @@ jobs: REGISTRY: "registry.registry" run: | make registry-proxy >/dev/null & - # wait for PipelineRun to be created - WAIT_CNT=0 - RETRIES=0 - while [ -z $(tkn pr ls -o jsonpath='{.items[?(@.metadata.generateName == "example-buildpacks-")].metadata.name}') ]; do - if [ $WAIT_CNT -eq 0 ]; then - if [ $RETRIES -lt 2 ]; then - RETRIES=$(expr $RETRIES + 1) - make example-buildpacks - else - exit 1 - fi - fi - if [ $WAIT_CNT -gt 15 ]; then - echo "Retrying example-buildpacks-" - WAIT_CNT=0 - else - WAIT_CNT=$(expr $WAIT_CNT + 1) - echo "Waiting for PipelineRun example-buildpacks-" - sleep 1 - fi - done + ./platform/wait-for-pipelinerun.sh -m example-buildpacks -n example-buildpacks- # tail PipelineRun logs tkn pr logs --last -f if [ "$(tkn pr describe --last -o jsonpath='{.status.conditions[?(@.type == "Succeeded")].status}')" != "True" ]; then @@ -109,27 +89,7 @@ jobs: REGISTRY: "registry.registry" run: | make registry-proxy >/dev/null & - # wait for PipelineRun to be created - WAIT_CNT=0 - RETRIES=0 - while [ -z $(tkn pr ls -o jsonpath='{.items[?(@.metadata.generateName == "example-sample-pipeline-")].metadata.name}') ]; do - if [ $WAIT_CNT -eq 0 ]; then - if [ $RETRIES -lt 2 ]; then - RETRIES=$(expr $RETRIES + 1) - make example-sample-pipeline - else - exit 1 - fi - fi - if [ $WAIT_CNT -gt 15 ]; then - echo "Retrying example-sample-pipeline-" - WAIT_CNT=0 - else - WAIT_CNT=$(expr $WAIT_CNT + 1) - echo "Waiting for PipelineRun example-sample-pipeline-" - sleep 1 - fi - done + ./platform/wait-for-pipelinerun.sh -m example-sample-pipeline -n example-sample-pipeline- # tail PipelineRun logs tkn pr logs --last -f if [ "$(tkn pr describe --last -o jsonpath='{.status.conditions[?(@.type == "Succeeded")].status}')" != "True" ]; then diff --git a/platform/wait-for-pipelinerun.sh b/platform/wait-for-pipelinerun.sh new file mode 100755 index 00000000..b612db88 --- /dev/null +++ b/platform/wait-for-pipelinerun.sh @@ -0,0 +1,81 @@ +#!/usr/bin/env bash +set -euo pipefail + +# ANSI colors +C_RED='\033[31m' +C_GREEN='\033[32m' +C_YELLOW='\033[33m' +C_CYAN='\033[36m' +C_RESET_ALL='\033[0m' + +function _log() { + echo -e "$1$2${C_RESET_ALL}" +} + +function info() { + _log "${C_GREEN}" "$1" +} + +function progress() { + _log "${C_CYAN}" "$1" +} + +function warn() { + _log "${C_YELLOW}" "$1" +} + +function error() { + _log "${C_RED}" "$1" +} + +# read CLI args +opt_m="" +opt_n="" +opt_h=0 + +while getopts 'm:n:h' option; do + case $option in + m) opt_m="$OPTARG";; + n) opt_n="$OPTARG";; + h) opt_h=1;; + *);; # ignore + esac +done +shift "$(( OPTIND - 1 ))" +if [ $# -gt 0 ] || [ -z "${opt_m}" ] || [ -z "${opt_n}" ] || [ "${opt_h}" = 1 ]; then + warn "Usage: $0 [opts]" + warn " -h: this help message" + warn " -m cmd: make command to create a PipelineRun (required)" + warn " -n name: generated name prefix of PipelineRun (required)" + exit 1 +fi + +info "Waiting for PipelineRun creation: ${opt_m} ${opt_n}" + +function tkn_pr() { + tkn pr ls -o jsonpath='{.items[?(@.metadata.generateName == "'"$1"'")].metadata.name}' +} + +WAIT_CNT=0 +RETRIES=0 +while [ -z "$(tkn_pr "${opt_n}")" ]; do + if [ "$WAIT_CNT" -eq 0 ]; then + if [ "$RETRIES" -lt 2 ]; then + RETRIES=$((RETRIES + 1)) + make "${opt_m}" + else + error "Failed to create PipelineRun: ${opt_m} ${opt_n}" + exit 1 + fi + fi + if [ "$WAIT_CNT" -gt 15 ]; then + warn "Retrying ${opt_n}" + WAIT_CNT=0 + else + WAIT_CNT=$((WAIT_CNT + 1)) + progress "Waiting for PipelineRun ${opt_n}" + sleep 1 + fi +done + +info "PipelineRun created: ${opt_m} ${opt_n}"