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

Use environment variables instead of docker args #56

Merged
merged 1 commit into from
Jul 21, 2021
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
34 changes: 17 additions & 17 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,23 @@ outputs:
runs:
using: docker
image: "docker://quay.io/terraform-docs/gh-actions:edge"
args:
- ${{ inputs.working-dir }}
- ${{ inputs.atlantis-file }}
- ${{ inputs.find-dir }}
- ${{ inputs.output-format }}
- ${{ inputs.output-method }}
- ${{ inputs.output-file }}
- ${{ inputs.template }}
- ${{ inputs.args }}
- ${{ inputs.indention }}
- ${{ inputs.git-push }}
- ${{ inputs.git-commit-message }}
- ${{ inputs.config-file }}
- ${{ inputs.fail-on-diff }}
- ${{ inputs.git-push-sign-off }}
- ${{ inputs.git-push-user-name }}
- ${{ inputs.git-push-user-email }}
env:
INPUT_WORKING_DIR: ${{ inputs.working-dir }}
INPUT_ATLANTIS_FILE: ${{ inputs.atlantis-file }}
INPUT_FIND_DIR: ${{ inputs.find-dir }}
INPUT_OUTPUT_FORMAT: ${{ inputs.output-format }}
INPUT_OUTPUT_METHOD: ${{ inputs.output-method }}
INPUT_OUTPUT_FILE: ${{ inputs.output-file }}
INPUT_TEMPLATE: ${{ inputs.template }}
INPUT_ARGS: ${{ inputs.args }}
INPUT_INDENTION: ${{ inputs.indention }}
INPUT_GIT_PUSH: ${{ inputs.git-push }}
INPUT_GIT_COMMIT_MESSAGE: ${{ inputs.git-commit-message }}
INPUT_CONFIG_FILE: ${{ inputs.config-file }}
INPUT_FAIL_ON_DIFF: ${{ inputs.fail-on-diff }}
INPUT_GIT_PUSH_SIGN_OFF: ${{ inputs.git-push-sign-off }}
INPUT_GIT_PUSH_USER_NAME: ${{ inputs.git-push-user-name }}
INPUT_GIT_PUSH_USER_EMAIL: ${{ inputs.git-push-user-email }}

branding:
icon: file-text
Expand Down
92 changes: 37 additions & 55 deletions src/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,49 +16,31 @@

set -e

# Ensure all variables are present
WORKING_DIR="${1}"
ATLANTIS_FILE="${2}"
FIND_DIR="${3}"
OUTPUT_FORMAT="${4}"
OUTPUT_METHOD="${5}"
OUTPUT_FILE="${6}"
TEMPLATE="${7}"
ARGS="${8}"
INDENTION="${9}"
GIT_PUSH="${10}"
GIT_COMMIT_MESSAGE="${11}"
CONFIG_FILE="${12}"
FAIL_ON_DIFF="${13}"
GIT_PUSH_SIGN_OFF="${14}"
GIT_PUSH_USER_NAME="${15}"
GIT_PUSH_USER_EMAIL="${16}"

if [ "${CONFIG_FILE}" = "disabled" ]; then
case "$OUTPUT_FORMAT" in
if [ "${INPUT_CONFIG_FILE}" = "disabled" ]; then
case "$INPUT_OUTPUT_FORMAT" in
"asciidoc" | "asciidoc table" | "asciidoc document")
ARGS="--indent ${INDENTION} ${ARGS}"
INPUT_ARGS="--indent ${INPUT_INDENTION} ${INPUT_ARGS}"
;;

"markdown" | "markdown table" | "markdown document")
ARGS="--indent ${INDENTION} ${ARGS}"
INPUT_ARGS="--indent ${INPUT_INDENTION} ${INPUT_ARGS}"
;;
esac

if [ -z "${TEMPLATE}" ]; then
TEMPLATE=$(printf '# Usage\n\n<!--- BEGIN_TF_DOCS --->\n<!--- END_TF_DOCS --->\n')
if [ -z "${INPUT_TEMPLATE}" ]; then
INPUT_TEMPLATE=$(printf '# Usage\n\n<!--- BEGIN_TF_DOCS --->\n<!--- END_TF_DOCS --->\n')
fi
fi

git_setup() {
if [ -n "${GIT_PUSH_USER_NAME}" ]; then
git config --global user.name "${GIT_PUSH_USER_NAME}"
if [ -n "${INPUT_GIT_PUSH_USER_NAME}" ]; then
git config --global user.name "${INPUT_GIT_PUSH_USER_NAME}"
else
git config --global user.name github-actions[bot]
fi

if [ -n "${GIT_PUSH_USER_EMAIL}" ]; then
git config --global user.email "${GIT_PUSH_USER_EMAIL}"
if [ -n "${INPUT_GIT_PUSH_USER_EMAIL}" ]; then
git config --global user.email "${INPUT_GIT_PUSH_USER_EMAIL}"
else
git config --global user.email github-actions[bot]@users.noreply.github.com
fi
Expand Down Expand Up @@ -92,10 +74,10 @@ git_commit() {
else
local signoff
signoff=""
if [ "${GIT_PUSH_SIGN_OFF}" = "true" ]; then
if [ "${INPUT_GIT_PUSH_SIGN_OFF}" = "true" ]; then
signoff="-s"
fi
git commit ${signoff} -m "${GIT_COMMIT_MESSAGE}"
git commit ${signoff} -m "${INPUT_GIT_COMMIT_MESSAGE}"
fi
}

Expand All @@ -110,19 +92,19 @@ update_doc() {
set +e

# shellcheck disable=SC2086
if [ -n "${CONFIG_FILE}" ] && [ "${CONFIG_FILE}" != "disabled" ]; then
echo "::debug file=entrypoint.sh,line=80 command=terraform-docs --config ${CONFIG_FILE} ${ARGS} ${working_dir}"
if [ -n "${INPUT_CONFIG_FILE}" ] && [ "${INPUT_CONFIG_FILE}" != "disabled" ]; then
echo "::debug file=entrypoint.sh,line=80 command=terraform-docs --config ${INPUT_CONFIG_FILE} ${INPUT_ARGS} ${working_dir}"
local config_file
if [ -f "${CONFIG_FILE}" ]; then
config_file="${CONFIG_FILE}"
if [ -f "${INPUT_CONFIG_FILE}" ]; then
config_file="${INPUT_CONFIG_FILE}"
else
config_file="${working_dir}/${CONFIG_FILE}"
config_file="${working_dir}/${INPUT_CONFIG_FILE}"
fi
terraform-docs --config ${config_file} ${ARGS} ${working_dir} >/tmp/tf_generated
terraform-docs --config ${config_file} ${INPUT_ARGS} ${working_dir} >/tmp/tf_generated
success=$?
else
echo "::debug file=entrypoint.sh,line=84 command=terraform-docs ${OUTPUT_FORMAT} ${ARGS} ${working_dir}"
terraform-docs ${OUTPUT_FORMAT} ${ARGS} ${working_dir} >/tmp/tf_generated
echo "::debug file=entrypoint.sh,line=84 command=terraform-docs ${INPUT_OUTPUT_FORMAT} ${INPUT_ARGS} ${working_dir}"
terraform-docs ${INPUT_OUTPUT_FORMAT} ${INPUT_ARGS} ${working_dir} >/tmp/tf_generated
success=$?
fi

Expand All @@ -137,36 +119,36 @@ update_doc() {
generated=$(cat /tmp/tf_generated)
rm -f /tmp/tf_generated

case "${OUTPUT_METHOD}" in
case "${INPUT_OUTPUT_METHOD}" in
print)
echo "${generated}"
;;

replace | inject)
# Create file if it doesn't exist
if [ "${OUTPUT_METHOD}" = "replace" ]; then
echo "${TEMPLATE}" >"${working_dir}/${OUTPUT_FILE}"
if [ "${INPUT_OUTPUT_METHOD}" = "replace" ]; then
echo "${INPUT_TEMPLATE}" >"${working_dir}/${INPUT_OUTPUT_FILE}"
else
if [ ! -f "${working_dir}/${OUTPUT_FILE}" ]; then
echo "${TEMPLATE}" >"${working_dir}/${OUTPUT_FILE}"
if [ ! -f "${working_dir}/${INPUT_OUTPUT_FILE}" ]; then
echo "${INPUT_TEMPLATE}" >"${working_dir}/${INPUT_OUTPUT_FILE}"
fi
fi

local has_delimiter
has_delimiter=$(grep -c -E '(BEGIN|END)_TF_DOCS' "${working_dir}/${OUTPUT_FILE}")
has_delimiter=$(grep -c -E '(BEGIN|END)_TF_DOCS' "${working_dir}/${INPUT_OUTPUT_FILE}")
echo "::debug file=entrypoint.sh,line=115 has_delimiter=${has_delimiter}"

# Verify it has BEGIN and END markers
if [ "${has_delimiter}" -ne 2 ]; then
echo "::error file=entrypoint.sh,line=119::Output file ${working_dir}/${OUTPUT_FILE} does not contain BEGIN_TF_DOCS and END_TF_DOCS"
echo "::error file=entrypoint.sh,line=119::Output file ${working_dir}/${INPUT_OUTPUT_FILE} does not contain BEGIN_TF_DOCS and END_TF_DOCS"
exit 1
fi

# Output generated markdown to temporary file with a trailing newline and then replace the block
echo "${generated}" >/tmp/tf_doc.md
echo "" >>/tmp/tf_doc.md
sed -i -ne '/<!--- BEGIN_TF_DOCS --->/ {p; r /tmp/tf_doc.md' -e ':a; n; /<!--- END_TF_DOCS --->/ {p; b}; ba}; p' "${working_dir}/${OUTPUT_FILE}"
git_add "${working_dir}/${OUTPUT_FILE}"
sed -i -ne '/<!--- BEGIN_TF_DOCS --->/ {p; r /tmp/tf_doc.md' -e ':a; n; /<!--- END_TF_DOCS --->/ {p; b}; ba}; p' "${working_dir}/${INPUT_OUTPUT_FILE}"
git_add "${working_dir}/${INPUT_OUTPUT_FILE}"
rm -f /tmp/tf_doc.md
;;
esac
Expand All @@ -177,32 +159,32 @@ cd "${GITHUB_WORKSPACE}"

git_setup

if [ -f "${GITHUB_WORKSPACE}/${ATLANTIS_FILE}" ]; then
if [ -f "${GITHUB_WORKSPACE}/${INPUT_ATLANTIS_FILE}" ]; then
# Parse an atlantis yaml file
while read -r line; do
project_dir=${line//- /}
update_doc "${project_dir}"
done < <(yq e '.projects[].dir' "${GITHUB_WORKSPACE}/${ATLANTIS_FILE}")
elif [ -n "${FIND_DIR}" ] && [ "${FIND_DIR}" != "disabled" ]; then
done < <(yq e '.projects[].dir' "${GITHUB_WORKSPACE}/${INPUT_ATLANTIS_FILE}")
elif [ -n "${INPUT_FIND_DIR}" ] && [ "${INPUT_FIND_DIR}" != "disabled" ]; then
# Find all tf
while read -r project_dir; do
update_doc "${project_dir}"
done < <(find "${FIND_DIR}" -name '*.tf' -exec dirname {} \; | uniq)
done < <(find "${INPUT_FIND_DIR}" -name '*.tf' -exec dirname {} \; | uniq)
else
# Split WORKING_DIR by commas
for project_dir in ${WORKING_DIR//,/ }; do
# Split INPUT_WORKING_DIR by commas
for project_dir in ${INPUT_WORKING_DIR//,/ }; do
update_doc "${project_dir}"
done
fi

if [ "${GIT_PUSH}" = "true" ]; then
if [ "${INPUT_GIT_PUSH}" = "true" ]; then
git_commit
git push
else
set +e
num_changed=$(git_status)
set -e
if [ "${FAIL_ON_DIFF}" = "true" ] && [ "${num_changed}" -ne 0 ]; then
if [ "${INPUT_FAIL_ON_DIFF}" = "true" ] && [ "${num_changed}" -ne 0 ]; then
echo "::error file=entrypoint.sh,line=169::Uncommitted change(s) has been found!"
exit 1
fi
Expand Down