Skip to content

feat: migrate examples to Cloud Run #5

feat: migrate examples to Cloud Run

feat: migrate examples to Cloud Run #5

name: Deploy Examples to Cloud Run and Firebase
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
types: [opened, synchronize, reopened, closed]
env:
PROJECT_ID: recaptcha-demo-php
SERVICE_NAME: recaptcha-examples
REGION: us-central1
jobs:
deploy:
if: github.event.action != 'closed'
runs-on: ubuntu-latest
permissions:
contents: 'read'
id-token: 'write'
pull-requests: 'write'
steps:
- name: Checkout code
uses: actions/checkout@v3
- id: 'auth'
uses: 'google-github-actions/auth@v1'
with:
credentials_json: '${{ secrets.GCP_SA_KEY }}'
- name: 'Set up Cloud SDK'
uses: 'google-github-actions/setup-gcloud@v1'
- name: 'Build and Push Container'
run: |
BUILD_ID=$(gcloud builds submit --tag gcr.io/${{ env.PROJECT_ID }}/${{ env.SERVICE_NAME }}:${{ github.sha }} --async --format='value(id)')
echo "Build started with ID: $BUILD_ID"
while true; do
STATUS=$(gcloud builds describe $BUILD_ID --format='value(status)')
echo "Current status: $STATUS"
if [[ "$STATUS" == "SUCCESS" ]]; then
break
fi
if [[ "$STATUS" == "FAILURE" || "$STATUS" == "INTERNAL_ERROR" || "$STATUS" == "TIMEOUT" || "$STATUS" == "CANCELLED" ]]; then
echo "Build failed with status: $STATUS"
exit 1
fi
sleep 10
done
- name: 'Set Service Name'
id: set-service-name
run: |
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
echo "name=recaptcha-examples-pr-${{ github.event.number }}" >> $GITHUB_OUTPUT
else
echo "name=${{ env.SERVICE_NAME }}" >> $GITHUB_OUTPUT
fi
- name: 'Deploy to Cloud Run'
id: deploy
uses: 'google-github-actions/deploy-cloudrun@v1'
with:
service: '${{ steps.set-service-name.outputs.name }}'
image: 'gcr.io/${{ env.PROJECT_ID }}/${{ env.SERVICE_NAME }}:${{ github.sha }}'
region: '${{ env.REGION }}'
env_vars: |
RECAPTCHA_V2_SITE_KEY=${{ secrets.RECAPTCHA_V2_SITE_KEY }}
RECAPTCHA_V2_SECRET_KEY=${{ secrets.RECAPTCHA_V2_SECRET_KEY }}
RECAPTCHA_V3_SITE_KEY=${{ secrets.RECAPTCHA_V3_SITE_KEY }}
RECAPTCHA_V3_SECRET_KEY=${{ secrets.RECAPTCHA_V3_SECRET_KEY }}
flags: '--allow-unauthenticated'
- name: 'Deploy to Firebase Hosting'
uses: FirebaseExtended/action-hosting-deploy@v0
with:
repoToken: '${{ secrets.GITHUB_TOKEN }}'
firebaseServiceAccount: '${{ secrets.FIREBASE_SERVICE_ACCOUNT_RECAPTCHA_DEMO_PHP }}'
projectId: ${{ env.PROJECT_ID }}
channelId: ${{ github.event_name == 'pull_request' && format('pr-{0}', github.event.number) || 'live' }}
cleanup:
if: github.event_name == 'pull_request' && github.event.action == 'closed'
runs-on: ubuntu-latest
steps:
- id: 'auth'
uses: 'google-github-actions/auth@v1'
with:
credentials_json: '${{ secrets.GCP_SA_KEY }}'
- name: 'Set up Cloud SDK'
uses: 'google-github-actions/setup-gcloud@v1'
- name: 'Delete PR Preview Service'
run: |
gcloud run services delete recaptcha-examples-pr-${{ github.event.number }} --region ${{ env.REGION }} --quiet