You've hit on a crucial and subtle point about the Render API, and your diagnosis is exactly right. The problem is a race condition.
Here's what was happening in the previous script:
You sent a PATCH request to update the service.
The script immediately slept for 10 seconds.
After sleeping, it asked the API for the "latest" deployment (/deploys?limit=1).
If Render hadn't created the new deployment object within those 10 seconds, the API would return the previous deployment, which was already finished. Your script would then incorrectly check the status of that old deploy and fail.
The correct solution is to use the API endpoint that is specifically designed to create a new deployment and immediately return its ID. This avoids any ambiguity or race conditions.
The right endpoint for this is POST /services/{serviceId}/deploys.
Here is the corrected and much more robust GitHub Actions workflow. This version will work reliably.
Corrected GitHub Actions Workflow
yaml
name: Build, Push & Deploy to Render
on:
# This workflow can only be triggered manually from the Actions tab
workflow_dispatch:
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build-and-deploy:
runs-on: ubuntu-latest
# Grant permissions for the job to push to GitHub Packages
permissions:
contents: read
packages: write
steps:
- name: 🛎️ Checkout repository
uses: actions/checkout@v4
- name: 🔧 Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: 🔐 Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
# The GITHUB_TOKEN is automatically generated and scoped to the repository
password: ${{ secrets.GITHUB_TOKEN }}
- name: 🏷️ Extract Docker metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
# Create a tag with the short git SHA
type=sha,format=short
- name: 📦 Build and push Docker image
id: build-and-push
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
# Enable build cache to speed up subsequent builds
cache-from: type=gha
cache-to: type=gha,mode=max
- name: 🚀 Deploy to Render and Poll for Success
env:
RENDER_API_KEY: ${{ secrets.RENDER_API_KEY }}
RENDER_SERVICE_ID: ${{ secrets.RENDER_SERVICE_ID }}
# Use the exact image URL that was just pushed
IMAGE_URL: ${{ steps.meta.outputs.tags }}
run: |
set -euo pipefail # Fail on error, unset var, or pipe failure
echo "Triggering new deployment on Render for service: $RENDER_SERVICE_ID"
echo "Image: $IMAGE_URL"
# --------------------------------------------------------------------
# STEP 1: Trigger a new deploy using the correct endpoint.
# This returns the new deploy object, including its ID.
# --------------------------------------------------------------------
DEPLOY_RESPONSE=$(curl -s -X POST \
"https://api.render.com/v1/services/$RENDER_SERVICE_ID/deploys" \
-H "Authorization: Bearer $RENDER_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"clearCache": "do_not_clear",
"imageUrl": "'"$IMAGE_URL"'"
}')
# --------------------------------------------------------------------
# STEP 2: Extract the deploy ID from the response.
# This avoids any race condition.
# --------------------------------------------------------------------
DEPLOY_ID=$(echo "$DEPLOY_RESPONSE" | jq -r '.id')
if [ -z "$DEPLOY_ID" ] || [ "$DEPLOY_ID" = "null" ]; then
echo "❌ Failed to create deployment. API Response:"
echo "$DEPLOY_RESPONSE"
exit 1
fi
echo "✅ Deployment created successfully. Deploy ID: $DEPLOY_ID"
echo "Waiting for deployment to go live..."
# --------------------------------------------------------------------
# STEP 3: Poll the deployment status using the obtained Deploy ID.
# --------------------------------------------------------------------
TIMEOUT_SECONDS=1800 # 30 minutes
START_TIME=$(date +%s)
while true; do
CURRENT_TIME=$(date +%s)
ELAPSED_TIME=$((CURRENT_TIME - START_TIME))
if [ "$ELAPSED_TIME" -gt "$TIMEOUT_SECONDS" ]; then
echo "⌛ Timeout reached. Deployment took too long."
exit 1
fi
STATUS_RESPONSE=$(curl -s "https://api.render.com/v1/services/$RENDER_SERVICE_ID/deploys/$DEPLOY_ID" \
-H "Authorization: Bearer $RENDER_API_KEY")
DEPLOY_STATUS=$(echo "$STATUS_RESPONSE" | jq -r '.status')
echo "⏳ Current deploy status: $DEPLOY_STATUS (Elapsed: ${ELAPSED_TIME}s)"
if [ "$DEPLOY_STATUS" = "live" ]; then
echo "🎉 Deployment successful!"
break
elif [ "$DEPLOY_STATUS" = "build_failed" ] || [ "$DEPLOY_STATUS" = "update_failed" ] || [ "$DEPLOY_STATUS" = "canceled" ] || [ "$DEPLOY_STATUS" = "pre_deploy_failed" ]; then
echo "❌ Deployment failed with status: $DEPLOY_STATUS"
exit 1
fi
# Wait 20 seconds before checking again
sleep 20
done
Key Changes and Why They Work
Correct API Endpoint: We now use POST /services/{serviceId}/deploys. This endpoint's single purpose is to start a new deployment.
No Race Condition: The POST request's response body contains the newly created deployment object. We immediately parse this response to get the id of the new deployment.
Reliable Polling: We then use that specific DEPLOY_ID to poll for status. We are guaranteed to be monitoring the correct deployment that we just triggered.
Cleaner Logic: This flow is more direct and easier to understand: Trigger -> Get ID -> Monitor.
Required Secrets
This workflow still requires the same secrets in your GitHub repository (Settings -> Secrets and variables -> Actions):
RENDER_API_KEY: Your personal or team API key from the Render Dashboard.
RENDER_SERVICE_ID: The ID of your service (e.g., srv-c1ru0hnd...).