feat: migrate examples to Cloud Run #7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' | |
| checks: '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: 'Deploy to Cloud Run' | |
| id: deploy-cloud-run | |
| uses: 'google-github-actions/deploy-cloudrun@v1' | |
| with: | |
| service: '${{ env.SERVICE_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' | |
| # For PRs, we want to route to the new revision without affecting prod traffic | |
| no_traffic: ${{ github.event_name == 'pull_request' }} | |
| tag: ${{ github.event_name == 'pull_request' && format('pr-{0}', github.event.number) || 'live' }} | |
| - name: 'Update firebase.json with Tag' | |
| if: github.event_name == 'pull_request' | |
| run: | | |
| TAG="pr-${{ github.event.number }}" | |
| sed -i "s/\"serviceId\": \"recaptcha-examples\"/\"serviceId\": \"recaptcha-examples\", \"tag\": \"$TAG\"/" firebase.json | |
| - 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 Tag from Cloud Run' | |
| run: | | |
| # We don't delete the service, just remove the tag or ignore | |
| gcloud run services update ${{ env.SERVICE_NAME }} --remove-tags pr-${{ github.event.number }} --region ${{ env.REGION }} || true |