Skip to content

Commit d9c0d11

Browse files
authored
Fix edge-case with empty env fix for yq (kustomize-everything#28)
1 parent 1011624 commit d9c0d11

File tree

7 files changed

+80
-12
lines changed

7 files changed

+80
-12
lines changed

Diff for: .github/workflows/static-analysis.yml

+2
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ jobs:
1414
- uses: actions/checkout@v3
1515
- name: Run ShellCheck
1616
uses: ludeeus/[email protected]
17+
with:
18+
check_together: 'yes'

Diff for: branch-and-stage.sh

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
#!/bin/bash
22

3-
# Output all commands
4-
# set -x
5-
6-
# Show line numbers
7-
# export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
3+
source "${GITHUB_ACTION_PATH}/util.sh"
84

95
# Base changes off the branch being deployed to
106
set +e
@@ -31,14 +27,21 @@ git rm -rf --ignore-unmatch '*'
3127
rm -rf base/ env/
3228
# Ensure that untracked files are cleaned up
3329
git clean -fd
34-
echo "Post-staging cleanup status:"
35-
git status
30+
if is_debug; then
31+
echo "Post-staging cleanup status:"
32+
git status
33+
fi
3634

3735
# If there are yaml files in RENDER_DIR (set by kustomize-build.sh), copy them
3836
# to staging and commit, otherwise, output that there are no files in the
3937
# rendered env.
40-
if [[ -n $(find "${RENDER_DIR?}" -name '*.y*ml') ]]; then
38+
FOUND_YAML=$(find "${RENDER_DIR?}" -name '*.y*ml')
39+
if [[ -n "${FOUND_YAML}" ]]; then
4140
echo "Moving built k8s manifests into staging area..."
41+
if is_debug; then
42+
echo "[DEBUG] YAML files found in ${RENDER_DIR?}:"
43+
echo "[DEBUG] ${FOUND_YAML}"
44+
fi
4245
cp "${RENDER_DIR?}"/*.y*ml .
4346
git add --all -fv ./*.y*ml
4447
else

Diff for: commit.sh

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/bin/bash
22

3+
source "${GITHUB_ACTION_PATH}/util.sh"
4+
35
if [ -z "${GIT_COMMIT_MESSAGE}" ]; then
46
GIT_COMMIT_MESSAGE="${GIT_HEAD_COMMIT_MESSAGE}"
57
fi

Diff for: diff.sh

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
11
#!/bin/bash
22

3+
source "${GITHUB_ACTION_PATH}/util.sh"
4+
35
DIFF_BRANCH_HEAD_SHA="$(git show-ref --hash "origin/${DIFF_BRANCH}")"
46
echo "DIFF_BRANCH_HEAD_SHA=${DIFF_BRANCH_HEAD_SHA}" >> "${GITHUB_ENV}"
57
DIFF_BRANCH_HEAD_SHORT_SHA="$(git show-ref --hash=6 "origin/${DIFF_BRANCH}")"
68
echo "DIFF_BRANCH_HEAD_SHORT_SHA=${DIFF_BRANCH_HEAD_SHORT_SHA}" >> "${GITHUB_ENV}"
79
DIFF_BRANCH_HEAD_SHA_URL="$DEPLOY_REPO_URL/commit/$DIFF_BRANCH_HEAD_SHA"
810
echo "DIFF_BRANCH_HEAD_SHA_URL=${DIFF_BRANCH_HEAD_SHA_URL}" >> "${GITHUB_ENV}"
911

12+
if is_debug; then
13+
echo "[debug] DIFF_BRANCH_HEAD_SHA=${DIFF_BRANCH_HEAD_SHA}"
14+
echo "[debug] DIFF_BRANCH_HEAD_SHORT_SHA=${DIFF_BRANCH_HEAD_SHORT_SHA}"
15+
echo "[debug] DIFF_BRANCH_HEAD_SHA_URL=${DIFF_BRANCH_HEAD_SHA_URL}"
16+
fi
17+
1018
if ! git diff --quiet "origin/${DIFF_BRANCH}" --; then
1119
# Fail on non-zero exit
1220
set -e
1321

1422
git diff "origin/${DIFF_BRANCH}" -- > git-diff
1523
echo "git diff origin/${DIFF_BRANCH}:"
16-
cat git-diff
24+
if is_debug; then
25+
echo "[debug] git-diff:"
26+
cat git-diff
27+
fi
1728
# Set random delimiter https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings
1829
EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)
1930
# shellcheck disable=SC2129
@@ -24,7 +35,9 @@ if ! git diff --quiet "origin/${DIFF_BRANCH}" --; then
2435
echo "$EOF" >> "${GITHUB_OUTPUT}"
2536
bytes="$(wc -c < git-diff | tr -d ' \n')"
2637
echo
27-
echo "Bytes: ${bytes}"
38+
if is_debug; then
39+
echo "[debug] git-diff bytes: ${bytes}"
40+
fi
2841
echo "diff-bytes=${bytes}" >> "${GITHUB_OUTPUT}"
2942
rm git-diff
3043
else

Diff for: kustomize-build.sh

+29-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
#!/bin/bash
22

3+
source "${GITHUB_ACTION_PATH}/util.sh"
4+
35
# Fail on non-zero exit
46
set -e
57

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

12+
if is_debug; then
13+
echo "[debug] RENDER_DIR=${RENDER_DIR}"
14+
echo "[debug] RENDER_FILE=${RENDER_FILE}"
15+
echo "[debug] ls ${RENDER_DIR} pre-kustomize:"
16+
ls -la "${RENDER_DIR}"
17+
fi
18+
1019
# Automatically add meta annotations at build-time
1120
pushd "${ENV_DIR}" || exit 1
1221
kustomize edit add annotation env-branch:"${ENV_BRANCH}"
@@ -18,9 +27,27 @@ popd || exit 1
1827

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

30+
if is_debug; then
31+
echo "[debug] ls ${RENDER_DIR} post-kustomize"
32+
ls -la "${RENDER_DIR}"
33+
fi
34+
35+
2136
pushd "${RENDER_DIR}" || exit 1
22-
# Invalid GitHub artifact path name characters: Double quote ", Colon :, Less than <, Greater than >, Vertical bar |, Asterisk *, Question mark ?
23-
yq -s '.kind + "-" + (.apiVersion | sub("/", "_")) + "-" + (.metadata.name | sub("[:<>|*?/\\]", "_")) + ".yaml"' < "${RENDER_FILE}"
37+
# If the render file is not empty
38+
if [[ -s "${RENDER_FILE}" ]]; then
39+
# Split the rendered file into individual files for each resource
40+
# Invalid GitHub artifact path name characters: Double quote ", Colon :, Less than <, Greater than >, Vertical bar |, Asterisk *, Question mark ?
41+
yq -s '.kind + "-" + (.apiVersion | sub("/", "_")) + "-" + (.metadata.name | sub("[:<>|*?/\\]", "_")) + ".yaml"' < "${RENDER_FILE}"
42+
43+
if is_debug; then
44+
echo "[debug] ls ${RENDER_DIR} post-yq"
45+
ls -la "${RENDER_DIR}"
46+
fi
47+
else
48+
echo "[WARN] ${RENDER_FILE} is empty"
49+
fi
50+
# Always cleanup the render file
2451
rm "${RENDER_FILE}"
2552
popd || exit 1
2653

Diff for: setup-shared-env.sh

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/bin/bash
22

3+
source "${GITHUB_ACTION_PATH}/util.sh"
4+
35
# Fail on non-zero exit
46
set -e
57

Diff for: util.sh

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
3+
function is_debug() {
4+
if [[ "$RUNNER_DEBUG" == "1" ]]; then
5+
return 0
6+
else
7+
return 1
8+
fi
9+
}
10+
11+
# Output all commands
12+
if is_debug; then
13+
set -x
14+
fi
15+
16+
# Show line numbers
17+
if is_debug; then
18+
export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
19+
fi

0 commit comments

Comments
 (0)