Skip to content

Commit

Permalink
Fix edge-case with empty env fix for yq (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
highb authored Aug 14, 2023
1 parent 1011624 commit d9c0d11
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ jobs:
- uses: actions/checkout@v3
- name: Run ShellCheck
uses: ludeeus/[email protected]
with:
check_together: 'yes'
19 changes: 11 additions & 8 deletions branch-and-stage.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#!/bin/bash

# Output all commands
# set -x

# Show line numbers
# export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
source "${GITHUB_ACTION_PATH}/util.sh"

# Base changes off the branch being deployed to
set +e
Expand All @@ -31,14 +27,21 @@ git rm -rf --ignore-unmatch '*'
rm -rf base/ env/
# Ensure that untracked files are cleaned up
git clean -fd
echo "Post-staging cleanup status:"
git status
if is_debug; then
echo "Post-staging cleanup status:"
git status
fi

# If there are yaml files in RENDER_DIR (set by kustomize-build.sh), copy them
# to staging and commit, otherwise, output that there are no files in the
# rendered env.
if [[ -n $(find "${RENDER_DIR?}" -name '*.y*ml') ]]; then
FOUND_YAML=$(find "${RENDER_DIR?}" -name '*.y*ml')
if [[ -n "${FOUND_YAML}" ]]; then
echo "Moving built k8s manifests into staging area..."
if is_debug; then
echo "[DEBUG] YAML files found in ${RENDER_DIR?}:"
echo "[DEBUG] ${FOUND_YAML}"
fi
cp "${RENDER_DIR?}"/*.y*ml .
git add --all -fv ./*.y*ml
else
Expand Down
2 changes: 2 additions & 0 deletions commit.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash

source "${GITHUB_ACTION_PATH}/util.sh"

if [ -z "${GIT_COMMIT_MESSAGE}" ]; then
GIT_COMMIT_MESSAGE="${GIT_HEAD_COMMIT_MESSAGE}"
fi
Expand Down
17 changes: 15 additions & 2 deletions diff.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
#!/bin/bash

source "${GITHUB_ACTION_PATH}/util.sh"

DIFF_BRANCH_HEAD_SHA="$(git show-ref --hash "origin/${DIFF_BRANCH}")"
echo "DIFF_BRANCH_HEAD_SHA=${DIFF_BRANCH_HEAD_SHA}" >> "${GITHUB_ENV}"
DIFF_BRANCH_HEAD_SHORT_SHA="$(git show-ref --hash=6 "origin/${DIFF_BRANCH}")"
echo "DIFF_BRANCH_HEAD_SHORT_SHA=${DIFF_BRANCH_HEAD_SHORT_SHA}" >> "${GITHUB_ENV}"
DIFF_BRANCH_HEAD_SHA_URL="$DEPLOY_REPO_URL/commit/$DIFF_BRANCH_HEAD_SHA"
echo "DIFF_BRANCH_HEAD_SHA_URL=${DIFF_BRANCH_HEAD_SHA_URL}" >> "${GITHUB_ENV}"

if is_debug; then
echo "[debug] DIFF_BRANCH_HEAD_SHA=${DIFF_BRANCH_HEAD_SHA}"
echo "[debug] DIFF_BRANCH_HEAD_SHORT_SHA=${DIFF_BRANCH_HEAD_SHORT_SHA}"
echo "[debug] DIFF_BRANCH_HEAD_SHA_URL=${DIFF_BRANCH_HEAD_SHA_URL}"
fi

if ! git diff --quiet "origin/${DIFF_BRANCH}" --; then
# Fail on non-zero exit
set -e

git diff "origin/${DIFF_BRANCH}" -- > git-diff
echo "git diff origin/${DIFF_BRANCH}:"
cat git-diff
if is_debug; then
echo "[debug] git-diff:"
cat git-diff
fi
# Set random delimiter https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings
EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)
# shellcheck disable=SC2129
Expand All @@ -24,7 +35,9 @@ if ! git diff --quiet "origin/${DIFF_BRANCH}" --; then
echo "$EOF" >> "${GITHUB_OUTPUT}"
bytes="$(wc -c < git-diff | tr -d ' \n')"
echo
echo "Bytes: ${bytes}"
if is_debug; then
echo "[debug] git-diff bytes: ${bytes}"
fi
echo "diff-bytes=${bytes}" >> "${GITHUB_OUTPUT}"
rm git-diff
else
Expand Down
31 changes: 29 additions & 2 deletions kustomize-build.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
#!/bin/bash

source "${GITHUB_ACTION_PATH}/util.sh"

# Fail on non-zero exit
set -e

# Generate dir in /tmp for kustomize to store the render
RENDER_DIR=$(mktemp -d)
RENDER_FILE="${RENDER_DIR}/all.yaml"

if is_debug; then
echo "[debug] RENDER_DIR=${RENDER_DIR}"
echo "[debug] RENDER_FILE=${RENDER_FILE}"
echo "[debug] ls ${RENDER_DIR} pre-kustomize:"
ls -la "${RENDER_DIR}"
fi

# Automatically add meta annotations at build-time
pushd "${ENV_DIR}" || exit 1
kustomize edit add annotation env-branch:"${ENV_BRANCH}"
Expand All @@ -18,9 +27,27 @@ popd || exit 1

kustomize build --enable-helm "${ENV_DIR}" > "${RENDER_FILE}"

if is_debug; then
echo "[debug] ls ${RENDER_DIR} post-kustomize"
ls -la "${RENDER_DIR}"
fi


pushd "${RENDER_DIR}" || exit 1
# Invalid GitHub artifact path name characters: Double quote ", Colon :, Less than <, Greater than >, Vertical bar |, Asterisk *, Question mark ?
yq -s '.kind + "-" + (.apiVersion | sub("/", "_")) + "-" + (.metadata.name | sub("[:<>|*?/\\]", "_")) + ".yaml"' < "${RENDER_FILE}"
# If the render file is not empty
if [[ -s "${RENDER_FILE}" ]]; then
# Split the rendered file into individual files for each resource
# Invalid GitHub artifact path name characters: Double quote ", Colon :, Less than <, Greater than >, Vertical bar |, Asterisk *, Question mark ?
yq -s '.kind + "-" + (.apiVersion | sub("/", "_")) + "-" + (.metadata.name | sub("[:<>|*?/\\]", "_")) + ".yaml"' < "${RENDER_FILE}"

if is_debug; then
echo "[debug] ls ${RENDER_DIR} post-yq"
ls -la "${RENDER_DIR}"
fi
else
echo "[WARN] ${RENDER_FILE} is empty"
fi
# Always cleanup the render file
rm "${RENDER_FILE}"
popd || exit 1

Expand Down
2 changes: 2 additions & 0 deletions setup-shared-env.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash

source "${GITHUB_ACTION_PATH}/util.sh"

# Fail on non-zero exit
set -e

Expand Down
19 changes: 19 additions & 0 deletions util.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

function is_debug() {
if [[ "$RUNNER_DEBUG" == "1" ]]; then
return 0
else
return 1
fi
}

# Output all commands
if is_debug; then
set -x
fi

# Show line numbers
if is_debug; then
export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
fi

0 comments on commit d9c0d11

Please sign in to comment.