feat: enhance build and deploy workflows with debug information and environment variable handling
All checks were successful
Build And Publish Production Image / Build And Publish Production Image (push) Successful in 7s

This commit is contained in:
2026-03-28 14:52:50 -03:00
parent 0f9d35311f
commit af5def1e71
2 changed files with 83 additions and 2 deletions

View File

@@ -13,7 +13,7 @@ jobs:
runs-on: ubuntu-latest
env:
STACK_NAME: condado-newsletter-stack
PORTAINER_URL: http://portainer.lab/
PORTAINER_URL: ${{ secrets.PORTAINER_URL }}
PORTAINER_API_KEY: ${{ secrets.PORTAINER_API_KEY }}
PORTAINER_ENDPOINT_ID: ${{ secrets.PORTAINER_ENDPOINT_ID }}
ENV_VARS: ${{ secrets.ENV_VARS }}
@@ -35,7 +35,34 @@ jobs:
set -u
set +e
PORTAINER_BASE_URL=$(printf '%s' "${PORTAINER_URL}" | sed -E 's/[[:space:]]+$//; s#/*$##')
if ! command -v curl >/dev/null 2>&1; then
echo "curl is not available in this runner image"
exit 1
fi
if ! command -v jq >/dev/null 2>&1; then
echo "jq is not available in this runner image"
exit 1
fi
PORTAINER_BASE_URL=$(printf '%s' "${PORTAINER_URL:-http://portainer.lab/}" | sed -E 's/[[:space:]]+$//; s#/*$##')
echo "Portainer deploy debug"
echo "PORTAINER_URL=${PORTAINER_URL:-http://portainer.lab/}"
echo "PORTAINER_BASE_URL=${PORTAINER_BASE_URL}"
echo "STACK_NAME=${STACK_NAME}"
echo "PORTAINER_ENDPOINT_ID=${PORTAINER_ENDPOINT_ID}"
echo "HTTP_PROXY=${HTTP_PROXY:-<empty>}"
echo "HTTPS_PROXY=${HTTPS_PROXY:-<empty>}"
echo "NO_PROXY=${NO_PROXY:-<empty>}"
echo "Current runner network info:"
if command -v ip >/dev/null 2>&1; then
ip -4 addr show || true
ip route || true
else
hostname -I || true
fi
ENV_JSON=$(printf '%s\n' "${ENV_VARS}" | jq -R -s '
split("\n")
@@ -48,6 +75,9 @@ jobs:
')
echo "Loaded $(printf '%s' "${ENV_JSON}" | jq 'length') env entries from ENV_VARS"
echo "Portainer base URL: ${PORTAINER_BASE_URL}"
echo "Target stack: ${STACK_NAME}"
echo "Endpoint id set: $([ -n "${PORTAINER_ENDPOINT_ID}" ] && echo yes || echo no)"
PORTAINER_HOST=$(printf '%s' "${PORTAINER_BASE_URL}" | sed -E 's#^[a-zA-Z]+://##; s#/.*$##; s/:.*$//')
PORTAINER_IP=""
@@ -57,7 +87,13 @@ jobs:
PORTAINER_IP=$(getent hosts "${PORTAINER_HOST}" | awk 'NR==1{print $1}')
if [ -n "${PORTAINER_IP}" ]; then
PORTAINER_IP_BASE_URL="${PORTAINER_BASE_URL/${PORTAINER_HOST}/${PORTAINER_IP}}"
echo "Portainer DNS resolved ${PORTAINER_HOST} -> ${PORTAINER_IP}"
echo "IP fallback URL: ${PORTAINER_IP_BASE_URL}"
else
echo "DNS lookup returned no IP for ${PORTAINER_HOST}"
fi
else
echo "getent not available; skipping DNS pre-check"
fi
STACKS_BODY=$(mktemp)
@@ -72,7 +108,11 @@ jobs:
2>"${STACKS_ERR}")
STACKS_CURL_EXIT=$?
echo "GET /api/stacks curl exit: ${STACKS_CURL_EXIT}"
echo "GET /api/stacks http code: ${STACKS_HTTP_CODE}"
if [ "${STACKS_CURL_EXIT}" -eq 6 ] && [ -n "${PORTAINER_IP:-}" ]; then
echo "Retrying stack list with IP fallback due to DNS failure"
STACKS_HTTP_CODE=$(curl -sS \
--noproxy "*" \
-o "${STACKS_BODY}" \
@@ -84,14 +124,18 @@ jobs:
if [ "${STACKS_CURL_EXIT}" -eq 0 ]; then
ACTIVE_PORTAINER_BASE_URL="${PORTAINER_IP_BASE_URL}"
fi
echo "Retry GET /api/stacks curl exit: ${STACKS_CURL_EXIT}"
echo "Retry GET /api/stacks http code: ${STACKS_HTTP_CODE}"
fi
if [ "${STACKS_CURL_EXIT}" -ne 0 ]; then
echo "GET /api/stacks stderr:"
cat "${STACKS_ERR}" || true
exit "${STACKS_CURL_EXIT}"
fi
if [ "${STACKS_HTTP_CODE}" -lt 200 ] || [ "${STACKS_HTTP_CODE}" -ge 300 ]; then
echo "GET /api/stacks body:"
cat "${STACKS_BODY}" || true
exit 1
fi
@@ -102,6 +146,7 @@ jobs:
APPLY_ERR=$(mktemp)
if [ -n "${STACK_ID}" ]; then
echo "Updating existing stack id=${STACK_ID}"
PAYLOAD=$(jq -n \
--rawfile stack_file docker-compose.prod.yml \
--argjson env_vars "${ENV_JSON}" \
@@ -118,6 +163,7 @@ jobs:
2>"${APPLY_ERR}")
APPLY_CURL_EXIT=$?
else
echo "Creating new stack ${STACK_NAME}"
PAYLOAD=$(jq -n \
--arg name "${STACK_NAME}" \
--rawfile stack_file docker-compose.prod.yml \
@@ -136,12 +182,17 @@ jobs:
APPLY_CURL_EXIT=$?
fi
echo "Apply curl exit: ${APPLY_CURL_EXIT}"
echo "Apply http code: ${APPLY_HTTP_CODE}"
if [ "${APPLY_CURL_EXIT}" -ne 0 ]; then
echo "Apply stderr:"
cat "${APPLY_ERR}" || true
exit "${APPLY_CURL_EXIT}"
fi
if [ "${APPLY_HTTP_CODE}" -lt 200 ] || [ "${APPLY_HTTP_CODE}" -ge 300 ]; then
echo "Apply response body:"
cat "${APPLY_BODY}" || true
exit 1
fi