Skip to content

Commit c0be705

Browse files
committed
fix: robust output and tuned auth sanitization
1 parent ae44114 commit c0be705

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

action.yml

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -326,10 +326,13 @@ runs:
326326
echo "Using Workload Identity Federation; unsetting conflicting credentials."
327327
unset GEMINI_API_KEY
328328
unset GOOGLE_API_KEY
329-
# Aggressively unset credential file pointers to prevent CLI from picking them up
330-
unset GOOGLE_APPLICATION_CREDENTIALS
329+
# Aggressively unset credential file pointers to prevent CLI from picking them up and hanging.
330+
# We unset CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE and GOOGLE_GHA_CREDS_PATH as they are known to cause issues.
331+
# We keep GOOGLE_APPLICATION_CREDENTIALS for now to see if it's needed for MCP tools.
331332
unset CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE
332333
unset GOOGLE_GHA_CREDS_PATH
334+
# If it still hangs, we may need to unset GOOGLE_APPLICATION_CREDENTIALS too.
335+
# unset GOOGLE_APPLICATION_CREDENTIALS
333336
elif [[ "${GOOGLE_GENAI_USE_VERTEXAI:-false}" == "true" && -n "${GOOGLE_API_KEY:-}" ]]; then
334337
echo "Using Vertex AI API Key; unsetting conflicting Gemini API key."
335338
unset GEMINI_API_KEY
@@ -376,12 +379,18 @@ runs:
376379
if jq -e . "${TEMP_STDOUT}" >/dev/null 2>&1; then
377380
RESPONSE=$(jq -r '.response // ""' "${TEMP_STDOUT}")
378381
fi
379-
if jq -e . "${TEMP_STDERR}" >/dev/null 2>&1; then
380-
ERROR_JSON=$(jq -c '.error // empty' "${TEMP_STDERR}")
382+
383+
# Stderr might contain non-JSON (like stack traces), so we try to extract the last valid JSON object
384+
if grep -q "{" "${TEMP_STDERR}"; then
385+
# Extract the last curly-braced block from stderr
386+
ERROR_CANDIDATE=$(tac "${TEMP_STDERR}" | awk '/^}/{p=1} p; /^{/{if(p)exit}' | tac)
387+
if [[ -n "${ERROR_CANDIDATE}" ]] && jq -e . <<< "${ERROR_CANDIDATE}" >/dev/null 2>&1; then
388+
ERROR_JSON=$(jq -c '.error // empty' <<< "${ERROR_CANDIDATE}")
389+
fi
381390
fi
382391
383-
if { [[ -s "${TEMP_STDERR}" ]] && ! jq -e . "${TEMP_STDERR}" >/dev/null 2>&1; }; then
384-
echo "::warning::Gemini CLI stderr was not valid JSON"
392+
if { [[ -s "${TEMP_STDERR}" ]] && [[ -z "${ERROR_JSON}" ]]; }; then
393+
echo "::warning::Gemini CLI stderr contains data but no valid JSON error object was extracted"
385394
fi
386395
387396
if { [[ -s "${TEMP_STDOUT}" ]] && ! jq -e . "${TEMP_STDOUT}" >/dev/null 2>&1; }; then
@@ -391,22 +400,26 @@ runs:
391400
392401
# Set the captured response as a step output, supporting multiline
393402
echo "Finished Gemini CLI execution."
394-
echo "gemini_response<<EOF" >> "${GITHUB_OUTPUT}"
403+
404+
# Use a more unique delimiter to avoid collisions
405+
EOF_DELIMITER="gh_gemini_out_$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 8 | head -n 1)"
406+
407+
echo "gemini_response<<${EOF_DELIMITER}" >> "${GITHUB_OUTPUT}"
395408
if [[ -n "${RESPONSE}" ]]; then
396409
echo "${RESPONSE}" >> "${GITHUB_OUTPUT}"
397-
else
410+
elif [[ -s "${TEMP_STDOUT}" ]]; then
398411
cat "${TEMP_STDOUT}" >> "${GITHUB_OUTPUT}"
399412
fi
400-
echo "EOF" >> "${GITHUB_OUTPUT}"
413+
echo "${EOF_DELIMITER}" >> "${GITHUB_OUTPUT}"
401414
402415
# Set the captured errors as a step output, supporting multiline
403-
echo "gemini_errors<<EOF" >> "${GITHUB_OUTPUT}"
416+
echo "gemini_errors<<${EOF_DELIMITER}" >> "${GITHUB_OUTPUT}"
404417
if [[ -n "${ERROR_JSON}" ]]; then
405418
echo "${ERROR_JSON}" >> "${GITHUB_OUTPUT}"
406-
else
419+
elif [[ -s "${TEMP_STDERR}" ]]; then
407420
cat "${TEMP_STDERR}" >> "${GITHUB_OUTPUT}"
408421
fi
409-
echo "EOF" >> "${GITHUB_OUTPUT}"
422+
echo "${EOF_DELIMITER}" >> "${GITHUB_OUTPUT}"
410423
411424
# Generate Job Summary
412425
if [[ -n "${GITHUB_STEP_SUMMARY:-}" ]]; then

0 commit comments

Comments
 (0)