Skip to content

Commit af23e99

Browse files
committed
fix: resolve hang by prioritizing authentication and unsetting conflicting credentials
1 parent c0be705 commit af23e99

File tree

1 file changed

+5
-53
lines changed

1 file changed

+5
-53
lines changed

action.yml

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ runs:
107107
id: 'validate_inputs'
108108
shell: 'bash'
109109
run: |-
110-
set -exuo pipefail
110+
set -euo pipefail
111111
112112
# Emit a clear warning in three places without failing the step
113113
warn() {
@@ -237,9 +237,8 @@ runs:
237237
GOOGLE_API_KEY: '${{ inputs.google_api_key }}'
238238
shell: 'bash'
239239
run: |-
240-
set -exuo pipefail
240+
set -euo pipefail
241241
mkdir -p ~/.gemini
242-
echo "Starting Gemini CLI installation..."
243242
244243
VERSION_INPUT="${GEMINI_CLI_VERSION:-latest}"
245244
@@ -269,14 +268,11 @@ runs:
269268
270269
# Sanitize authentication environment variables to avoid conflicts when installing extensions.
271270
if [[ -n "${GOOGLE_CLOUD_ACCESS_TOKEN:-}" ]]; then
272-
echo "Using Workload Identity Federation; unsetting conflicting API keys."
273271
unset GEMINI_API_KEY
274272
unset GOOGLE_API_KEY
275273
elif [[ "${GOOGLE_GENAI_USE_VERTEXAI:-false}" == "true" && -n "${GOOGLE_API_KEY:-}" ]]; then
276-
echo "Using Vertex AI API Key; unsetting conflicting Gemini API key."
277274
unset GEMINI_API_KEY
278275
elif [[ -n "${GEMINI_API_KEY:-}" ]]; then
279-
echo "Using Gemini API Key; ensuring Vertex AI and Code Assist are disabled."
280276
export GOOGLE_GENAI_USE_VERTEXAI="false"
281277
export GOOGLE_GENAI_USE_GCA="false"
282278
unset GOOGLE_API_KEY
@@ -298,12 +294,7 @@ runs:
298294
id: 'gemini_run'
299295
shell: 'bash'
300296
run: |-
301-
set -exuo pipefail
302-
echo "Starting Gemini CLI execution..."
303-
304-
echo "--- Environment variables ---"
305-
env | sort | grep -vE "TOKEN|KEY|PASS|SECRET|GHA_CREDS" || true
306-
echo "--- End environment variables ---"
297+
set -euo pipefail
307298
308299
# Create a temporary directory for storing the output, and ensure it's
309300
# cleaned up later
@@ -323,21 +314,14 @@ runs:
323314
# 2. Vertex AI API Key (use_vertex_ai with google_api_key)
324315
# 3. Gemini API Key (gemini_api_key)
325316
if [[ -n "${GOOGLE_CLOUD_ACCESS_TOKEN:-}" ]]; then
326-
echo "Using Workload Identity Federation; unsetting conflicting credentials."
327317
unset GEMINI_API_KEY
328318
unset GOOGLE_API_KEY
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.
319+
# Unset credential file pointers that might cause conflicts with the access token.
332320
unset CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE
333321
unset GOOGLE_GHA_CREDS_PATH
334-
# If it still hangs, we may need to unset GOOGLE_APPLICATION_CREDENTIALS too.
335-
# unset GOOGLE_APPLICATION_CREDENTIALS
336322
elif [[ "${GOOGLE_GENAI_USE_VERTEXAI:-false}" == "true" && -n "${GOOGLE_API_KEY:-}" ]]; then
337-
echo "Using Vertex AI API Key; unsetting conflicting Gemini API key."
338323
unset GEMINI_API_KEY
339324
elif [[ -n "${GEMINI_API_KEY:-}" ]]; then
340-
echo "Using Gemini API Key; ensuring Vertex AI and Code Assist are disabled."
341325
export GOOGLE_GENAI_USE_VERTEXAI="false"
342326
export GOOGLE_GENAI_USE_GCA="false"
343327
unset GOOGLE_API_KEY
@@ -348,32 +332,16 @@ runs:
348332
# We capture stdout (JSON) to TEMP_STDOUT and stderr to TEMP_STDERR
349333
if [[ "${GEMINI_DEBUG}" = true ]]; then
350334
echo "::warning::Gemini CLI debug logging is enabled. This will stream responses, which could reveal sensitive information if processed with untrusted inputs."
351-
echo "::: Start Gemini CLI STDOUT :::"
352335
if ! gemini --debug --yolo --prompt "${PROMPT}" --output-format json 2> >(tee "${TEMP_STDERR}" >&2) | tee "${TEMP_STDOUT}"; then
353336
FAILED=true
354337
fi
355-
# Wait for async stderr logging to complete. This is because process substitution in Bash is async so let tee finish writing to ${TEMP_STDERR}
356-
sleep 1
357-
echo "::: End Gemini CLI STDOUT :::"
358338
else
359339
if ! gemini --yolo --prompt "${PROMPT}" --output-format json 2> "${TEMP_STDERR}" 1> "${TEMP_STDOUT}"; then
360340
FAILED=true
361341
fi
362342
fi
363343
364-
# Create the artifacts directory and copy full logs
365-
mkdir -p gemini-artifacts
366-
cp "${TEMP_STDOUT}" gemini-artifacts/stdout.log
367-
cp "${TEMP_STDERR}" gemini-artifacts/stderr.log
368-
if [[ -f .gemini/telemetry.log ]]; then
369-
cp .gemini/telemetry.log gemini-artifacts/telemetry.log
370-
else
371-
# Create an empty file so the artifact upload doesn't fail if telemetry is missing
372-
touch gemini-artifacts/telemetry.log
373-
fi
374-
375344
# Parse JSON output to extract response and errors
376-
# If output is not valid JSON, RESPONSE will be empty and we'll rely on stderr for errors
377345
RESPONSE=""
378346
ERROR_JSON=""
379347
if jq -e . "${TEMP_STDOUT}" >/dev/null 2>&1; then
@@ -389,19 +357,7 @@ runs:
389357
fi
390358
fi
391359
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"
394-
fi
395-
396-
if { [[ -s "${TEMP_STDOUT}" ]] && ! jq -e . "${TEMP_STDOUT}" >/dev/null 2>&1; }; then
397-
echo "::warning::Gemini CLI stdout was not valid JSON"
398-
fi
399-
400-
401-
# Set the captured response as a step output, supporting multiline
402-
echo "Finished Gemini CLI execution."
403-
404-
# Use a more unique delimiter to avoid collisions
360+
# Use a unique delimiter to avoid collisions for multiline outputs
405361
EOF_DELIMITER="gh_gemini_out_$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 8 | head -n 1)"
406362
407363
echo "gemini_response<<${EOF_DELIMITER}" >> "${GITHUB_OUTPUT}"
@@ -412,7 +368,6 @@ runs:
412368
fi
413369
echo "${EOF_DELIMITER}" >> "${GITHUB_OUTPUT}"
414370
415-
# Set the captured errors as a step output, supporting multiline
416371
echo "gemini_errors<<${EOF_DELIMITER}" >> "${GITHUB_OUTPUT}"
417372
if [[ -n "${ERROR_JSON}" ]]; then
418373
echo "${ERROR_JSON}" >> "${GITHUB_OUTPUT}"
@@ -462,9 +417,6 @@ runs:
462417
ERROR_MSG=$(jq -r '.message // .' <<< "${ERROR_JSON}")
463418
echo "::error title=Gemini CLI execution failed::${ERROR_MSG}"
464419
fi
465-
echo "::: Start Gemini CLI STDERR :::"
466-
cat "${TEMP_STDERR}"
467-
echo "::: End Gemini CLI STDERR :::"
468420
exit 1
469421
fi
470422
env:

0 commit comments

Comments
 (0)