From 9675a5f00e4ad95e4482fe728469afda6d64470e Mon Sep 17 00:00:00 2001 From: Chris Werner Rau Date: Fri, 23 Aug 2024 15:57:11 +0200 Subject: [PATCH] chore(ci): improve scripts inlining the `&&` or `||` is more performant checking for existance of files or directories is safer than string comparing --- .github/scripts/enforce-trusted-registries.sh | 4 ++-- .github/scripts/extract-artifacthub-images.sh | 6 +++--- .github/scripts/sync-codeowners.sh | 4 ++-- .github/scripts/templateHelmChartRecursivelyToFolder.sh | 4 ++-- .github/workflows/update-artifacthub-images.yaml | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/.github/scripts/enforce-trusted-registries.sh b/.github/scripts/enforce-trusted-registries.sh index 9f31604d2..7d296240a 100755 --- a/.github/scripts/enforce-trusted-registries.sh +++ b/.github/scripts/enforce-trusted-registries.sh @@ -39,12 +39,12 @@ function enforceTrustedImages() { fi } -if [[ "$#" == 1 ]] && [[ -d "$1" ]]; then +if [[ "$#" == 1 && -d "$1" ]]; then enforceTrustedImages "$1" else result=0 for chart in charts/*; do - [[ "$chart" == "charts/*" ]] && continue + [[ -d "$chart" ]] || continue if ! enforceTrustedImages "$chart"; then result=1 diff --git a/.github/scripts/extract-artifacthub-images.sh b/.github/scripts/extract-artifacthub-images.sh index 66d943c7f..df5145b3d 100755 --- a/.github/scripts/extract-artifacthub-images.sh +++ b/.github/scripts/extract-artifacthub-images.sh @@ -13,7 +13,7 @@ function getImages() { local chart="$1" local tmpDir tmpDir="$(mktemp -d -p "$TMP_DIR")" - if [[ -v 2 ]] && [[ -n "$2" ]]; then + if [[ -v 2 && -n "$2" ]]; then cp -r "$2/artifacthub-values" "$tmpDir/helmRelease" else "$(dirname "$0")/templateLocalHelmChart" -1 "$chart" >"$tmpDir/helmRelease.yaml" @@ -77,14 +77,14 @@ if [[ "$#" -ge 1 ]]; then echo "There is no 'artifacthub-values.yaml' in 'charts/$1/ci', exiting" >&2 exit 1 fi - if [[ -v 2 ]] && ! [[ -d "$2/artifacthub-values" ]]; then + if [[ -v 2 && ! -d "$2/artifacthub-values" ]]; then echo "Missing artifacthub-values directory '$2', exiting" >&2 exit 1 fi updateChartYaml "$1" "${2:-}" else for chart in charts/*; do - [[ "$chart" == "charts/*" ]] && continue + [[ -d "$chart" ]] || continue [[ -f "$chart/ci/artifacthub-values.yaml" ]] || continue if yq -e '.type == "library"' "$chart/Chart.yaml" >/dev/null; then diff --git a/.github/scripts/sync-codeowners.sh b/.github/scripts/sync-codeowners.sh index 54f511211..16e34c49c 100755 --- a/.github/scripts/sync-codeowners.sh +++ b/.github/scripts/sync-codeowners.sh @@ -5,8 +5,8 @@ echo "* @teutonet/k8s" -for DIR in ./charts/*; do - [[ "$DIR" = "./charts/*" ]] && continue +for DIR in charts/*; do + [[ -f "$DIR/Chart.yaml" ]] || continue FILE="$DIR/Chart.yaml" DIR="${DIR//\./}" MAINTAINERS="$(yq e '.maintainers.[].name' "$FILE" | sed 's/^/@/' | sort --ignore-case | tr '\r\n' ' ')" diff --git a/.github/scripts/templateHelmChartRecursivelyToFolder.sh b/.github/scripts/templateHelmChartRecursivelyToFolder.sh index 9b36a67e4..32d87181a 100755 --- a/.github/scripts/templateHelmChartRecursivelyToFolder.sh +++ b/.github/scripts/templateHelmChartRecursivelyToFolder.sh @@ -11,10 +11,10 @@ targetDir=${2?You need to provide the target directory} if yq -e '.type == "library"' "$chart/Chart.yaml" >/dev/null; then echo "Skipping library chart '$chart'" >&2 - [[ -v GITHUB_OUTPUT ]] && [[ -f "$GITHUB_OUTPUT" ]] && echo "skipped=true" | tee -a "$GITHUB_OUTPUT" + [[ -v GITHUB_OUTPUT && -f "$GITHUB_OUTPUT" ]] && echo "skipped=true" | tee -a "$GITHUB_OUTPUT" exit 0 else - [[ -v GITHUB_OUTPUT ]] && [[ -f "$GITHUB_OUTPUT" ]] && echo "skipped=false" | tee -a "$GITHUB_OUTPUT" + [[ -v GITHUB_OUTPUT && -f "$GITHUB_OUTPUT" ]] && echo "skipped=false" | tee -a "$GITHUB_OUTPUT" fi [[ ! -d "$targetDir" ]] && mkdir -p "$targetDir" diff --git a/.github/workflows/update-artifacthub-images.yaml b/.github/workflows/update-artifacthub-images.yaml index bd07cd788..42531e39b 100644 --- a/.github/workflows/update-artifacthub-images.yaml +++ b/.github/workflows/update-artifacthub-images.yaml @@ -21,7 +21,7 @@ jobs: ( echo -n charts= for chart in charts/*; do - if [[ "$chart" != "charts/*" ]] && [[ -f "$chart/ci/artifacthub-values.yaml" ]]; then + if [[ "$chart" != "charts/*" && -f "$chart/ci/artifacthub-values.yaml" ]]; then echo "$chart" fi done | cut -d / -f 2 | jq -c -Rn '[inputs]'