...
This commit is contained in:
+47
-39
@@ -1,47 +1,55 @@
|
|||||||
name: "Terraform/OpenTofu PR Commenter"
|
name: "Terraform/OpenTofu PR Commenter"
|
||||||
description: "Posts PR comments for Terraform or OpenTofu commands (init/plan/fmt/validate), compatible with Gitea and GitHub."
|
description: "Posts Terraform or OpenTofu command results as PR comments for GitHub/Gitea."
|
||||||
author: "Trez"
|
author: "Charish Patel"
|
||||||
branding:
|
|
||||||
icon: "message-square"
|
|
||||||
color: "purple"
|
|
||||||
|
|
||||||
inputs:
|
|
||||||
commenter_type:
|
|
||||||
description: "Type of command (init, fmt, validate, plan)"
|
|
||||||
required: true
|
|
||||||
|
|
||||||
commenter_input:
|
|
||||||
description: "Output from the command (e.g. plan output)"
|
|
||||||
required: true
|
|
||||||
|
|
||||||
commenter_exitcode:
|
|
||||||
description: "Exit code of the command"
|
|
||||||
required: true
|
|
||||||
default: "0"
|
|
||||||
|
|
||||||
working_directory:
|
|
||||||
description: "Optional working directory (relative to repo root)"
|
|
||||||
required: false
|
|
||||||
|
|
||||||
github_token:
|
|
||||||
description: "Token used for posting the comment (Gitea or GitHub)"
|
|
||||||
required: true
|
|
||||||
|
|
||||||
runs:
|
runs:
|
||||||
using: "composite"
|
using: "composite"
|
||||||
steps:
|
steps:
|
||||||
- name: Run Terraform/OpenTofu PR Commenter
|
- name: Run Terraform/OpenTofu PR Commenter
|
||||||
shell: bash
|
shell: bash
|
||||||
run: |
|
|
||||||
bash "${GITHUB_ACTION_PATH}/tf-pr-comment.sh" \
|
|
||||||
"${{ inputs.commenter_type }}" \
|
|
||||||
"${{ inputs.commenter_input }}" \
|
|
||||||
"${{ inputs.commenter_exitcode }}" \
|
|
||||||
"${{ inputs.working_directory }}"
|
|
||||||
env:
|
env:
|
||||||
|
# Inputs
|
||||||
|
COMMENTER_TYPE: ${{ inputs.commenter_type }}
|
||||||
|
COMMENTER_INPUT: ${{ inputs.commenter_input }}
|
||||||
|
COMMENTER_EXITCODE: ${{ inputs.commenter_exitcode }}
|
||||||
|
WORKING_DIR: ${{ inputs.working_directory }}
|
||||||
|
|
||||||
|
# PR & auth
|
||||||
|
PR_COMMENTS_URL: ${{ inputs.pr_comments_url }}
|
||||||
|
PR_COMMENT_ID: ${{ inputs.pr_comment_id }}
|
||||||
|
PR_COMMENT_URI: ${{ inputs.pr_comment_uri }}
|
||||||
GITHUB_TOKEN: ${{ inputs.github_token }}
|
GITHUB_TOKEN: ${{ inputs.github_token }}
|
||||||
PR_NUMBER: ${{ env.PR_NUMBER }}
|
run: bash "${GITHUB_ACTION_PATH}/tf-pr-comment.sh"
|
||||||
PR_URL: ${{ env.PR_URL }}
|
|
||||||
PR_COMMENTS_URL: ${{ env.PR_COMMENTS_URL }}
|
inputs:
|
||||||
PR_COMMENT_ID: ${{ env.PR_COMMENT_ID }}
|
commenter_type:
|
||||||
PR_COMMENT_URI: ${{ env.PR_COMMENT_URI }}
|
description: "Type of command: init, fmt, validate, plan"
|
||||||
|
required: true
|
||||||
|
|
||||||
|
commenter_input:
|
||||||
|
description: "Output from the command (multi-line allowed)"
|
||||||
|
required: true
|
||||||
|
|
||||||
|
commenter_exitcode:
|
||||||
|
description: "Exit code from the command"
|
||||||
|
required: true
|
||||||
|
default: "0"
|
||||||
|
|
||||||
|
working_directory:
|
||||||
|
description: "Optional working directory relative to repo root"
|
||||||
|
required: false
|
||||||
|
|
||||||
|
pr_comments_url:
|
||||||
|
description: "PR comments URL (GitHub or Gitea)"
|
||||||
|
required: true
|
||||||
|
|
||||||
|
pr_comment_id:
|
||||||
|
description: "Existing PR comment ID if updating"
|
||||||
|
required: false
|
||||||
|
|
||||||
|
pr_comment_uri:
|
||||||
|
description: "PR comment URI if updating"
|
||||||
|
required: false
|
||||||
|
|
||||||
|
github_token:
|
||||||
|
description: "GitHub/Gitea token for authentication"
|
||||||
|
required: true
|
||||||
|
|||||||
+33
-34
@@ -1,63 +1,65 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
# === Inputs ===========================================================
|
# === Inputs via ENV ===================================================
|
||||||
COMMENTER_TYPE="${1:-}"
|
COMMENTER_TYPE="${COMMENTER_TYPE:-}"
|
||||||
COMMENTER_INPUT="${2:-}"
|
COMMENTER_INPUT="${COMMENTER_INPUT:-}"
|
||||||
COMMENTER_EXITCODE="${3:-0}"
|
COMMENTER_EXITCODE="${COMMENTER_EXITCODE:-0}"
|
||||||
WORKING_DIR="${4:-}"
|
WORKING_DIR="${WORKING_DIR:-}"
|
||||||
|
|
||||||
# If WORKING_DIR is numeric (likely passed exitcode by mistake), reset it
|
PR_COMMENTS_URL="${PR_COMMENTS_URL:-}"
|
||||||
if [[ "$WORKING_DIR" =~ ^[0-9]+$ ]]; then
|
PR_COMMENT_ID="${PR_COMMENT_ID:-}"
|
||||||
echo "⚠️ Detected numeric working_dir ('$WORKING_DIR'); treating as unset."
|
PR_COMMENT_URI="${PR_COMMENT_URI:-}"
|
||||||
WORKING_DIR=""
|
GITHUB_TOKEN="${GITHUB_TOKEN:-}"
|
||||||
|
|
||||||
|
# === Validate inputs =================================================
|
||||||
|
if [[ -z "$COMMENTER_TYPE" ]]; then
|
||||||
|
echo "❌ commenter_type is required."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ -z "$GITHUB_TOKEN" ]]; then
|
||||||
|
echo "❌ GITHUB_TOKEN is required to post PR comments."
|
||||||
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# === Move into working directory if specified ========================
|
# === Move into working directory if specified ========================
|
||||||
if [[ -n "$WORKING_DIR" ]]; then
|
if [[ -n "$WORKING_DIR" ]]; then
|
||||||
echo "🔹 Changing to working directory: $WORKING_DIR"
|
TARGET_DIR="${GITHUB_WORKSPACE}/${WORKING_DIR}"
|
||||||
cd "${GITHUB_WORKSPACE}/${WORKING_DIR}" || {
|
if [[ ! -d "$TARGET_DIR" ]]; then
|
||||||
echo "❌ Error: Cannot change to working directory '${WORKING_DIR}'"
|
echo "❌ Error: Cannot change to working directory '${WORKING_DIR}'"
|
||||||
exit 1
|
exit 1
|
||||||
}
|
fi
|
||||||
|
echo "🔹 Changing to working directory: $WORKING_DIR"
|
||||||
|
cd "$TARGET_DIR"
|
||||||
else
|
else
|
||||||
cd "${GITHUB_WORKSPACE:-.}"
|
cd "${GITHUB_WORKSPACE:-.}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# === Environment Debug Info ==========================================
|
|
||||||
echo "📁 Current working directory: $(pwd)"
|
echo "📁 Current working directory: $(pwd)"
|
||||||
echo "📦 Repository root: ${GITHUB_WORKSPACE:-N/A}"
|
echo "📦 Repository root: ${GITHUB_WORKSPACE:-N/A}"
|
||||||
|
|
||||||
# === Setup PATH for OpenTofu =========================================
|
# === Detect IaC command ==============================================
|
||||||
export PATH="${PATH}:/home/runner/.opentofu/bin"
|
if command -v tofu &>/dev/null; then
|
||||||
|
|
||||||
# === Detect tofu / terraform command =================================
|
|
||||||
if command -v tofu >/dev/null 2>&1; then
|
|
||||||
TOFU_CMD="tofu"
|
TOFU_CMD="tofu"
|
||||||
elif command -v terraform >/dev/null 2>&1; then
|
elif command -v terraform &>/dev/null; then
|
||||||
TOFU_CMD="terraform"
|
TOFU_CMD="terraform"
|
||||||
else
|
else
|
||||||
echo "❌ Neither 'tofu' nor 'terraform' found in PATH."
|
echo "❌ Neither 'tofu' nor 'terraform' found in PATH."
|
||||||
|
echo "PATH: $PATH"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "🧰 Using IaC command: $TOFU_CMD"
|
echo "🧰 Using IaC command: $TOFU_CMD ($(command -v $TOFU_CMD))"
|
||||||
|
|
||||||
# === Validation ======================================================
|
# === Prepare comment body ===========================================
|
||||||
if [[ -z "${COMMENTER_TYPE}" ]]; then
|
|
||||||
echo "❌ commenter_type input is required."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# === Prepare Comment Body ============================================
|
|
||||||
EXIT_STATUS_MSG=""
|
EXIT_STATUS_MSG=""
|
||||||
if [[ "$COMMENTER_EXITCODE" -eq 0 ]]; then
|
if [[ "$COMMENTER_EXITCODE" =~ ^[0-9]+$ && "$COMMENTER_EXITCODE" -eq 0 ]]; then
|
||||||
EXIT_STATUS_MSG="✅ ${COMMENTER_TYPE^} succeeded."
|
EXIT_STATUS_MSG="✅ ${COMMENTER_TYPE^} succeeded."
|
||||||
else
|
else
|
||||||
EXIT_STATUS_MSG="❌ ${COMMENTER_TYPE^} failed with exit code ${COMMENTER_EXITCODE}."
|
EXIT_STATUS_MSG="❌ ${COMMENTER_TYPE^} failed with exit code ${COMMENTER_EXITCODE}."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Format comment body
|
|
||||||
COMMENT_BODY="### ${COMMENTER_TYPE^} Results
|
COMMENT_BODY="### ${COMMENTER_TYPE^} Results
|
||||||
\`\`\`
|
\`\`\`
|
||||||
${COMMENTER_INPUT}
|
${COMMENTER_INPUT}
|
||||||
@@ -66,15 +68,12 @@ ${EXIT_STATUS_MSG}
|
|||||||
"
|
"
|
||||||
|
|
||||||
# === Post Comment ====================================================
|
# === Post Comment ====================================================
|
||||||
echo "📝 Posting PR comment to: ${PR_COMMENTS_URL:-N/A}"
|
if [[ -z "$PR_COMMENTS_URL" ]]; then
|
||||||
|
|
||||||
if [[ -z "${PR_COMMENTS_URL:-}" ]]; then
|
|
||||||
echo "❌ Missing PR_COMMENTS_URL environment variable."
|
echo "❌ Missing PR_COMMENTS_URL environment variable."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Update comment if comment_id exists, otherwise create a new one
|
if [[ -n "$PR_COMMENT_ID" && -n "$PR_COMMENT_URI" ]]; then
|
||||||
if [[ -n "${PR_COMMENT_ID:-}" && -n "${PR_COMMENT_URI:-}" ]]; then
|
|
||||||
echo "🌀 Updating existing comment (ID: ${PR_COMMENT_ID})"
|
echo "🌀 Updating existing comment (ID: ${PR_COMMENT_ID})"
|
||||||
curl -sS -X PATCH \
|
curl -sS -X PATCH \
|
||||||
-H "Authorization: token ${GITHUB_TOKEN}" \
|
-H "Authorization: token ${GITHUB_TOKEN}" \
|
||||||
|
|||||||
Reference in New Issue
Block a user