I'll help you create a GitHub Action to build and deploy a Docker image to Render. Here's a complete workflow:
name: Deploy to Render
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
env:
DOCKER_IMAGE_NAME: your-app-name
RENDER_SERVICE_ID: ${{ secrets.RENDER_SERVICE_ID }}
RENDER_API_KEY: ${{ secrets.RENDER_API_KEY }}
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: |
${{ secrets.DOCKER_USERNAME }}/${{ env.DOCKER_IMAGE_NAME }}:latest
${{ secrets.DOCKER_USERNAME }}/${{ env.DOCKER_IMAGE_NAME }}:${{ github.sha }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Deploy to Render
run: |
# Update the Render service with the new image
response=$(curl -X PATCH "https://api.render.com/v1/services/${RENDER_SERVICE_ID}" \
-H "Authorization: Bearer ${RENDER_API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"image": {
"ownerId": "'${RENDER_SERVICE_ID}'",
"imagePath": "'${DOCKER_USERNAME}'/'${DOCKER_IMAGE_NAME}':'"${GITHUB_SHA}"'"
}
}')
echo "Deploy response: $response"
# Extract deploy ID from response
deploy_id=$(echo $response | jq -r '.deployId')
if [ "$deploy_id" = "null" ] || [ -z "$deploy_id" ]; then
echo "Failed to get deploy ID"
exit 1
fi
echo "Deploy ID: $deploy_id"
# Wait for deployment to complete
max_attempts=60 # 10 minutes with 10 second intervals
attempt=0
while [ $attempt -lt $max_attempts ]; do
echo "Checking deployment status (attempt $((attempt + 1))/$max_attempts)..."
status_response=$(curl -s "https://api.render.com/v1/services/${RENDER_SERVICE_ID}/deploys/${deploy_id}" \
-H "Authorization: Bearer ${RENDER_API_KEY}")
status=$(echo $status_response | jq -r '.status')
echo "Current status: $status"
if [ "$status" = "live" ]; then
echo "Deployment successful!"
exit 0
elif [ "$status" = "build_failed" ] || [ "$status" = "update_failed" ] || [ "$status" = "canceled" ]; then
echo "Deployment failed with status: $status"
exit 1
fi
sleep 10
attempt=$((attempt + 1))
done
echo "Deployment timed out"
exit 1
env:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
To use this GitHub Action, you'll need to set up the following secrets in your GitHub repository:
- RENDER_SERVICE_ID: Your Render service ID (found in your Render dashboard)
- RENDER_API_KEY: Your Render API key (generate from Account Settings > API Keys)
- DOCKER_USERNAME: Your Docker Hub username
- DOCKER_PASSWORD: Your Docker Hub password or access token
If you're using a private Docker registry other than Docker Hub, you can modify the login step accordingly. For example, for GitHub Container Registry:
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
The workflow does the following:
- Checks out your code
- Sets up Docker Buildx for improved build performance
- Logs into your Docker registry
- Builds and pushes the Docker image with both
latest and commit SHA tags
- Updates the Render service to use the new image
- Monitors the deployment status until it's live or fails
The deployment monitoring will check the status every 10 seconds for up to 10 minutes. You can adjust these values by modifying max_attempts and the sleep duration.