Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle empty env properly #27

Merged
merged 5 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions branch-and-stage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ rm -rf base/ env/
git clean -fd
echo "Post-staging cleanup status:"
git status
echo "Moving built k8s-manifests into staging area..."
cp /tmp/*.y*ml .
git add --all -fv ./*.y*ml

# 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
echo "Moving built k8s manifests into staging area..."
cp "${RENDER_DIR?}"/*.y*ml .
git add --all -fv ./*.y*ml
else
echo "No k8s manifests were built, staging area will be empty."
# git add the removed files; hopefully no yaml pollution
git add --all -fv .
fi
8 changes: 5 additions & 3 deletions commit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ fi
# Multi-line output
# Reference: 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)
echo "commit-message<<$EOF" >> "${GITHUB_OUTPUT}"
echo "${GIT_COMMIT_MESSAGE}" >> "${GITHUB_OUTPUT}"
echo "$EOF" >> "${GITHUB_OUTPUT}"
{
echo "commit-message<<$EOF";
echo "${GIT_COMMIT_MESSAGE}";
echo "$EOF";
} >> "${GITHUB_OUTPUT}"

set +e
if ! git diff --quiet "origin/${DIFF_BRANCH}" ; then
Expand Down
15 changes: 11 additions & 4 deletions kustomize-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
# 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"

# Automatically add meta annotations at build-time
pushd "${ENV_DIR}" || exit 1
kustomize edit add annotation env-branch:"${ENV_BRANCH}"
Expand All @@ -12,13 +16,16 @@ kustomize edit add annotation deployment-repo-url:"${DEPLOY_REPO_URL}"
kustomize edit add buildmetadata originAnnotations,managedByLabel
popd || exit 1

kustomize build --enable-helm "${ENV_DIR}" > /tmp/all.yaml
kustomize build --enable-helm "${ENV_DIR}" > "${RENDER_FILE}"

pushd /tmp || exit 1
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"' < all.yaml
rm all.yaml
yq -s '.kind + "-" + (.apiVersion | sub("/", "_")) + "-" + (.metadata.name | sub("[:<>|*?/\\]", "_")) + ".yaml"' < "${RENDER_FILE}"
rm "${RENDER_FILE}"
popd || exit 1

# Must reset to clear build-time annotations
git reset --hard

# Export RENDER_DIR for downstream steps using new GH action output syntax
echo "RENDER_DIR=${RENDER_DIR}" >> "${GITHUB_ENV}"