Created
August 12, 2025 13:47
-
-
Save withakay/794513ca8cc1c08cb8d9cb0bb185bb06 to your computer and use it in GitHub Desktop.
gha-output.sh - Bash script with helper methods for writing output in GitHub Actions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # GitHub Actions output and summary helpers | |
| # Version: 2.0.0 | |
| # Source common functions if not already loaded | |
| if [[ -z "${SCRIPT_DIR:-}" ]]; then | |
| SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | |
| fi | |
| # Source common.sh if functions not available | |
| if ! declare -f log_debug >/dev/null; then | |
| source "${SCRIPT_DIR}/common.sh" | |
| fi | |
| # Check if running in GitHub Actions | |
| is_github_actions() { | |
| [[ "${GITHUB_ACTIONS:-false}" == "true" ]] | |
| } | |
| # Set output - works in both GitHub Actions and CLI | |
| set_output() { | |
| local name="$1" | |
| local value="$2" | |
| if is_github_actions && [[ -n "${GITHUB_OUTPUT:-}" ]]; then | |
| echo "${name}=${value}" >> "$GITHUB_OUTPUT" | |
| log_debug "Set output: ${name}=${value}" | |
| else | |
| echo "${name}=${value}" | |
| fi | |
| } | |
| # Add to summary - works in both GitHub Actions and CLI | |
| add_summary() { | |
| local content="$1" | |
| if is_github_actions && [[ -n "${GITHUB_STEP_SUMMARY:-}" ]]; then | |
| echo "$content" >> "$GITHUB_STEP_SUMMARY" | |
| else | |
| echo "$content" | |
| fi | |
| } | |
| # Add a section to the summary with title | |
| add_summary_section() { | |
| local title="$1" | |
| shift | |
| local content="$*" | |
| add_summary "### $title" | |
| add_summary "" | |
| if [[ -n "$content" ]]; then | |
| add_summary "$content" | |
| add_summary "" | |
| fi | |
| } | |
| # Exit with error | |
| exit_with_error() { | |
| local message="$1" | |
| local code="${2:-1}" | |
| log_error "$message" | |
| if is_github_actions; then | |
| add_summary "❌ **Error**: $message" | |
| fi | |
| exit "$code" | |
| } | |
| # Export functions | |
| export -f is_github_actions set_output add_summary add_summary_section exit_with_error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment