From 8ebb1d8e361c09f2dccdded7913f57ee4726ba63 Mon Sep 17 00:00:00 2001 From: Khosrow Moossavi Date: Wed, 21 Jul 2021 16:45:48 -0400 Subject: [PATCH] Use environment variables instead of docker args Signed-off-by: Khosrow Moossavi --- action.yml | 34 +++++++-------- src/docker-entrypoint.sh | 92 ++++++++++++++++------------------------ 2 files changed, 54 insertions(+), 72 deletions(-) diff --git a/action.yml b/action.yml index 7afb1a3..8a597a7 100644 --- a/action.yml +++ b/action.yml @@ -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 diff --git a/src/docker-entrypoint.sh b/src/docker-entrypoint.sh index f69af3f..a3c0e1a 100755 --- a/src/docker-entrypoint.sh +++ b/src/docker-entrypoint.sh @@ -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\n\n') + if [ -z "${INPUT_TEMPLATE}" ]; then + INPUT_TEMPLATE=$(printf '# Usage\n\n\n\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 @@ -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 } @@ -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 @@ -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 '// {p; r /tmp/tf_doc.md' -e ':a; n; // {p; b}; ba}; p' "${working_dir}/${OUTPUT_FILE}" - git_add "${working_dir}/${OUTPUT_FILE}" + sed -i -ne '// {p; r /tmp/tf_doc.md' -e ':a; n; // {p; b}; ba}; p' "${working_dir}/${INPUT_OUTPUT_FILE}" + git_add "${working_dir}/${INPUT_OUTPUT_FILE}" rm -f /tmp/tf_doc.md ;; esac @@ -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