Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 70 additions & 26 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,14 @@ runs:
GITHUB_EVENT_NAME: ${{ github.event_name }}
GITHUB_REF_NAME: ${{ github.ref_name }}
GITHUB_EVENT_REF: ${{ github.event.ref }}
GITHUB_HEAD_REF: ${{ github.head_ref }}
run: |
if [[ "$GITHUB_EVENT_NAME" == "delete" ]]; then
TARGET_BRANCH="$GITHUB_EVENT_REF"
echo "Using deleted branch name: $TARGET_BRANCH"
elif [[ "$GITHUB_EVENT_NAME" == "pull_request" ]]; then
TARGET_BRANCH="$GITHUB_HEAD_REF"
echo "Using pull request head branch: $TARGET_BRANCH"
else
TARGET_BRANCH="$GITHUB_REF_NAME"
echo "Using current branch name: $TARGET_BRANCH"
Expand All @@ -65,7 +69,7 @@ runs:
- name: Create or Locate Container
id: manage-container
shell: bash
if: ${{ (github.event_name == 'create' || github.event_name == 'push') && steps.should-run.outputs.should_run == 'true' }}
if: ${{ (github.event_name == 'workflow_dispatch' || github.event_name == 'create' || github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.action != 'closed')) && steps.should-run.outputs.should_run == 'true' }}
env:
API_URL: ${{ inputs.api_url }}
API_KEY: ${{ inputs.api_key }}
Expand Down Expand Up @@ -146,9 +150,9 @@ runs:
fi
fi

# ── Phase 2: Wait for the container to be running ──
- name: Wait for Container to be Running
id: wait-container
# ── Phase 2: Wait for the creation job to finish ──
- name: Wait for Job to Complete
id: wait-job
shell: bash
if: ${{ steps.manage-container.outputs.container_created == 'true' && env.FAILED != '1' }}
env:
Expand All @@ -166,7 +170,6 @@ runs:
while [ $ATTEMPT -lt $MAX_ATTEMPTS ]; do
ATTEMPT=$((ATTEMPT + 1))

# Capture both body and HTTP status code
RAW_RESPONSE=$(curl -s -w "\n%{http_code}" \
-H "Authorization: Bearer $API_KEY" \
-H "Accept: application/json" \
Expand All @@ -177,41 +180,28 @@ runs:

echo " Attempt $ATTEMPT/$MAX_ATTEMPTS — HTTP $HTTP_CODE"

# If we got a non-2xx response, retry
if [ "$HTTP_CODE" -lt 200 ] || [ "$HTTP_CODE" -ge 300 ]; then
echo " API returned HTTP $HTTP_CODE"
echo " Response: $RESPONSE_BODY"
sleep $POLL_INTERVAL
continue
fi

# Validate the response is valid JSON before parsing
if ! echo "$RESPONSE_BODY" | jq empty 2>/dev/null; then
echo " Response is not valid JSON — retrying."
sleep $POLL_INTERVAL
continue
fi

JOB_STATUS=$(echo "$RESPONSE_BODY" | jq -r '.status // "unknown"')
CONTAINER_STATUS=$(echo "$RESPONSE_BODY" | jq -r '.container.status // "unknown"')
echo " Job: $JOB_STATUS | Container: $CONTAINER_STATUS"

if [ "$JOB_STATUS" == "completed" ] || [ "$JOB_STATUS" == "success" ]; then
if [ "$CONTAINER_STATUS" == "running" ]; then
echo "Job completed and container is running!"
echo "Waiting 10s for container to fully stabilize..."
sleep 10
echo "container_ready=true" >> $GITHUB_OUTPUT
exit 0
else
echo "::error::Job completed but container status is '$CONTAINER_STATUS'."
echo " Full response: $RESPONSE_BODY"
echo "FAILED=1" >> $GITHUB_ENV
exit 1
fi
echo " Job status: $JOB_STATUS"

if [ "$JOB_STATUS" == "success" ] || [ "$JOB_STATUS" == "completed" ]; then
echo "Job completed successfully."
echo "job_done=true" >> $GITHUB_OUTPUT
exit 0
fi

if [ "$JOB_STATUS" == "failed" ] || [ "$JOB_STATUS" == "error" ]; then
if [ "$JOB_STATUS" == "failed" ] || [ "$JOB_STATUS" == "error" ] || [ "$JOB_STATUS" == "cancelled" ]; then
echo "::error::Job entered '$JOB_STATUS' state."
echo " Full response: $RESPONSE_BODY"
echo "FAILED=1" >> $GITHUB_ENV
Expand All @@ -226,10 +216,64 @@ runs:
echo "FAILED=1" >> $GITHUB_ENV
exit 1

# ── Phase 3: Query the container for its final details ──
- name: Query Container Status
id: container-status
shell: bash
if: ${{ (steps.manage-container.outputs.container_ready == 'true' || steps.wait-job.outputs.job_done == 'true') && env.FAILED != '1' }}
env:
API_URL: ${{ inputs.api_url }}
API_KEY: ${{ inputs.api_key }}
SITE_ID: ${{ inputs.site_id }}
CONTAINER_NAME: ${{ steps.manage-container.outputs.container_name }}
run: |
echo "Querying container '$CONTAINER_NAME'..."

RAW_RESPONSE=$(curl -s -w "\n%{http_code}" \
-H "Authorization: Bearer $API_KEY" \
-H "Accept: application/json" \
"$API_URL/sites/$SITE_ID/containers?hostname=$CONTAINER_NAME")

HTTP_CODE=$(echo "$RAW_RESPONSE" | tail -1)
RESPONSE_BODY=$(echo "$RAW_RESPONSE" | sed '$d')

if [ "$HTTP_CODE" -lt 200 ] || [ "$HTTP_CODE" -ge 300 ]; then
echo "::error::Failed to query container. HTTP $HTTP_CODE"
echo " Response: $RESPONSE_BODY"
echo "FAILED=1" >> $GITHUB_ENV
exit 1
fi

CONTAINER_STATUS=$(echo "$RESPONSE_BODY" | jq -r '.containers[0].status // "unknown"')
CONTAINER_IP=$(echo "$RESPONSE_BODY" | jq -r '.containers[0].ipv4Address // empty')
SSH_PORT=$(echo "$RESPONSE_BODY" | jq -r '.containers[0].sshPort // empty')
HTTP_PORT=$(echo "$RESPONSE_BODY" | jq -r '.containers[0].httpPort // empty')
NODE_NAME=$(echo "$RESPONSE_BODY" | jq -r '.containers[0].nodeName // empty')

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo " Container : $CONTAINER_NAME"
echo " Status : $CONTAINER_STATUS"
echo " IP : ${CONTAINER_IP:-pending}"
echo " SSH Port : ${SSH_PORT:-N/A}"
echo " HTTP Port : ${HTTP_PORT:-N/A}"
echo " Node : ${NODE_NAME:-unknown}"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

if [ "$CONTAINER_STATUS" != "running" ]; then
echo "::warning::Container status is '$CONTAINER_STATUS', expected 'running'."
fi

echo "container_status=$CONTAINER_STATUS" >> $GITHUB_OUTPUT
echo "container_ip=$CONTAINER_IP" >> $GITHUB_OUTPUT
echo "ssh_port=$SSH_PORT" >> $GITHUB_OUTPUT
echo "http_port=$HTTP_PORT" >> $GITHUB_OUTPUT

# ── Container Deletion on Branch Deletion ──
- name: Container Deletion on Branch Deletion
shell: bash
if: ${{ github.event_name == 'delete' && steps.should-run.outputs.should_run == 'true' }}
if: ${{ (github.event_name == 'delete' || (github.event_name == 'pull_request' && github.event.action == 'closed')) && steps.should-run.outputs.should_run == 'true' }}
env:
GITHUB_REPOSITORY_FULL: ${{ github.repository }}
GITHUB_REPOSITORY_OWNER: ${{ github.repository_owner }}
Expand Down