...
This commit is contained in:
+47
-39
@@ -1,47 +1,55 @@
|
||||
name: "Terraform/OpenTofu PR Commenter"
|
||||
description: "Posts PR comments for Terraform or OpenTofu commands (init/plan/fmt/validate), compatible with Gitea and GitHub."
|
||||
author: "Trez"
|
||||
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
|
||||
|
||||
description: "Posts Terraform or OpenTofu command results as PR comments for GitHub/Gitea."
|
||||
author: "Charish Patel"
|
||||
runs:
|
||||
using: "composite"
|
||||
steps:
|
||||
- name: Run Terraform/OpenTofu PR Commenter
|
||||
shell: bash
|
||||
run: |
|
||||
bash "${GITHUB_ACTION_PATH}/tf-pr-comment.sh" \
|
||||
"${{ inputs.commenter_type }}" \
|
||||
"${{ inputs.commenter_input }}" \
|
||||
"${{ inputs.commenter_exitcode }}" \
|
||||
"${{ inputs.working_directory }}"
|
||||
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 }}
|
||||
PR_NUMBER: ${{ env.PR_NUMBER }}
|
||||
PR_URL: ${{ env.PR_URL }}
|
||||
PR_COMMENTS_URL: ${{ env.PR_COMMENTS_URL }}
|
||||
PR_COMMENT_ID: ${{ env.PR_COMMENT_ID }}
|
||||
PR_COMMENT_URI: ${{ env.PR_COMMENT_URI }}
|
||||
run: bash "${GITHUB_ACTION_PATH}/tf-pr-comment.sh"
|
||||
|
||||
inputs:
|
||||
commenter_type:
|
||||
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
|
||||
set -euo pipefail
|
||||
|
||||
# === Inputs ===========================================================
|
||||
COMMENTER_TYPE="${1:-}"
|
||||
COMMENTER_INPUT="${2:-}"
|
||||
COMMENTER_EXITCODE="${3:-0}"
|
||||
WORKING_DIR="${4:-}"
|
||||
# === Inputs via ENV ===================================================
|
||||
COMMENTER_TYPE="${COMMENTER_TYPE:-}"
|
||||
COMMENTER_INPUT="${COMMENTER_INPUT:-}"
|
||||
COMMENTER_EXITCODE="${COMMENTER_EXITCODE:-0}"
|
||||
WORKING_DIR="${WORKING_DIR:-}"
|
||||
|
||||
# If WORKING_DIR is numeric (likely passed exitcode by mistake), reset it
|
||||
if [[ "$WORKING_DIR" =~ ^[0-9]+$ ]]; then
|
||||
echo "⚠️ Detected numeric working_dir ('$WORKING_DIR'); treating as unset."
|
||||
WORKING_DIR=""
|
||||
PR_COMMENTS_URL="${PR_COMMENTS_URL:-}"
|
||||
PR_COMMENT_ID="${PR_COMMENT_ID:-}"
|
||||
PR_COMMENT_URI="${PR_COMMENT_URI:-}"
|
||||
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
|
||||
|
||||
# === Move into working directory if specified ========================
|
||||
if [[ -n "$WORKING_DIR" ]]; then
|
||||
echo "🔹 Changing to working directory: $WORKING_DIR"
|
||||
cd "${GITHUB_WORKSPACE}/${WORKING_DIR}" || {
|
||||
TARGET_DIR="${GITHUB_WORKSPACE}/${WORKING_DIR}"
|
||||
if [[ ! -d "$TARGET_DIR" ]]; then
|
||||
echo "❌ Error: Cannot change to working directory '${WORKING_DIR}'"
|
||||
exit 1
|
||||
}
|
||||
fi
|
||||
echo "🔹 Changing to working directory: $WORKING_DIR"
|
||||
cd "$TARGET_DIR"
|
||||
else
|
||||
cd "${GITHUB_WORKSPACE:-.}"
|
||||
fi
|
||||
|
||||
# === Environment Debug Info ==========================================
|
||||
echo "📁 Current working directory: $(pwd)"
|
||||
echo "📦 Repository root: ${GITHUB_WORKSPACE:-N/A}"
|
||||
|
||||
# === Setup PATH for OpenTofu =========================================
|
||||
export PATH="${PATH}:/home/runner/.opentofu/bin"
|
||||
|
||||
# === Detect tofu / terraform command =================================
|
||||
if command -v tofu >/dev/null 2>&1; then
|
||||
# === Detect IaC command ==============================================
|
||||
if command -v tofu &>/dev/null; then
|
||||
TOFU_CMD="tofu"
|
||||
elif command -v terraform >/dev/null 2>&1; then
|
||||
elif command -v terraform &>/dev/null; then
|
||||
TOFU_CMD="terraform"
|
||||
else
|
||||
echo "❌ Neither 'tofu' nor 'terraform' found in PATH."
|
||||
echo "PATH: $PATH"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "🧰 Using IaC command: $TOFU_CMD"
|
||||
echo "🧰 Using IaC command: $TOFU_CMD ($(command -v $TOFU_CMD))"
|
||||
|
||||
# === Validation ======================================================
|
||||
if [[ -z "${COMMENTER_TYPE}" ]]; then
|
||||
echo "❌ commenter_type input is required."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# === Prepare Comment Body ============================================
|
||||
# === Prepare comment body ===========================================
|
||||
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."
|
||||
else
|
||||
EXIT_STATUS_MSG="❌ ${COMMENTER_TYPE^} failed with exit code ${COMMENTER_EXITCODE}."
|
||||
fi
|
||||
|
||||
# Format comment body
|
||||
COMMENT_BODY="### ${COMMENTER_TYPE^} Results
|
||||
\`\`\`
|
||||
${COMMENTER_INPUT}
|
||||
@@ -66,15 +68,12 @@ ${EXIT_STATUS_MSG}
|
||||
"
|
||||
|
||||
# === 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."
|
||||
exit 1
|
||||
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})"
|
||||
curl -sS -X PATCH \
|
||||
-H "Authorization: token ${GITHUB_TOKEN}" \
|
||||
|
||||
Reference in New Issue
Block a user