You are absolutely right. My apologies for using the incorrect endpoint. The POST /deploys endpoint is a valid way to trigger a deployment, but using PATCH /services to update the image path is often the more direct and canonical method, as you pointed out by referencing the documentation.
This approach updates the service's configuration itself, which is a more robust way to manage the image version.
Here is the corrected, complete GitHub Actions workflow. It now uses the PATCH /services/{serviceId} endpoint to update the imagePath and then correctly polls for the resulting deployment's status.
Prerequisites
Your service in Render must be a Docker service. This workflow will not work for "Native Environment" or "Static Site" services.
Your Render service needs to be connected to your GitHub Container Registry (GHCR). You can do this in the Render dashboard under your service's settings.
You still need the following secrets in your GitHub repository (Settings > Secrets and variables > Actions):
GHCR_TOKEN: A Personal Access Token (PAT) with read:packages and write:packages scopes.
RENDER_API_KEY: Your Render API Key from your Account Settings.
RENDER_SERVICE_ID: The ID of your service from the Render dashboard URL (e.g., srv-xxxxxxxxxxxx).
Complete GitHub Actions Workflow
yaml
name: Build, Push & Deploy to Render
on:
push:
branches: [ main ]
workflow_dispatch:
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
# --- 1. Build and Push Docker Image to GHCR ---
- name: Log in to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GHCR_TOKEN }}
- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
context: .
push: true
tags: ghcr.io/${{ github.repository }}:${{ github.sha }}
# --- 2. Deploy to Render ---
- name: Deploy to Render and Poll for Success
env:
RENDER_API_KEY: ${{ secrets.RENDER_API_KEY }}
RENDER_SERVICE_ID: ${{ secrets.RENDER_SERVICE_ID }}
IMAGE_PATH: ghcr.io/${{ github.repository }}:${{ github.sha }}
run: |
echo "Updating Render service to use image: $IMAGE_PATH"
# Step 2a: Update the service to point to the new image, triggering a deploy
curl --request PATCH \
--url https://api.render.com/v1/services/$RENDER_SERVICE_ID \
--header "Authorization: Bearer $RENDER_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
"serviceDetails": {
"image": {
"path": "'"$IMAGE_PATH"'"
}
}
}'
# Give Render a moment to create the new deployment
sleep 10
echo "Fetching the latest deployment to monitor status..."
# Step 2b: Get the ID of the deployment that was just triggered
DEPLOY_ID=$(curl --request GET \
--url "https://api.render.com/v1/services/$RENDER_SERVICE_ID/deploys?limit=1" \
--header "Authorization: Bearer $RENDER_API_KEY" \
| jq -r '.[0].deploy.id')
if [ -z "$DEPLOY_ID" ] || [ "$DEPLOY_ID" = "null" ]; then
echo "Failed to get latest deployment ID."
exit 1
fi
echo "Monitoring deployment ID: $DEPLOY_ID"
# Step 2c: Poll the deployment status until it's 'live'
TIMEOUT=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 ]; then
echo "Timeout reached while waiting for deployment to complete."
exit 1
fi
DEPLOY_INFO=$(curl -s \
"https://api.render.com/v1/services/$RENDER_SERVICE_ID/deploys/$DEPLOY_ID" \
-H "Authorization: Bearer $RENDER_API_KEY")
DEPLOY_STATUS=$(echo $DEPLOY_INFO | jq -r '.status // "unknown"')
echo "Deployment status: $DEPLOY_STATUS (elapsed time: ${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
sleep 30
done
Key Changes and Explanation
Standardized Docker Actions: The build and push steps now use the official docker/login-action and docker/build-push-action, which is the current best practice.
Correct API Endpoint (PATCH /services/{id}):
The curl request now sends a PATCH to the service endpoint.
The JSON payload (--data) is structured exactly as the API documentation requires to update an image: {"serviceDetails": {"image": {"path": "..."}}}.
Two-Step Deployment & Polling:
Trigger Deploy: The PATCH request updates the service and implicitly triggers a new deployment.
Fetch Deploy ID: The API response to the PATCH does not include the new deploy ID. We must immediately make a second API call (GET /services/{id}/deploys?limit=1) to fetch the most recent deployment and extract its ID. The jq -r '.[0].deploy.id' command does this.
Poll Status: The rest of the script uses this fetched DEPLOY_ID to poll the status, just as before.
This revised workflow is more robust and correctly follows the API documentation you provided.