Skip to content

Add preview channel

Add preview channel #2

Workflow file for this run

name: Deploy Preview Channel
on:
pull_request:
branches: [ "main" ]
env:
FRONTEND_IMAGE: aphorist-frontend-preview
BACKEND_IMAGE: aphorist-backend-preview
REGION: us-central1
USE_GKE_GCLOUD_AUTH_PLUGIN: True
PROJECT_ID: ${{ secrets.PROJECT_ID }}
BACKEND_URL: "https://backend-preview-${{ github.event.pull_request.number }}-132704325993.us-central1.run.app"
FRONTEND_URL: "https://frontend-preview-${{ github.event.pull_request.number }}-132704325993.us-central1.run.app"
jobs:
deploy-preview:
name: Deploy Preview
runs-on: ubuntu-latest
permissions:
contents: 'read'
id-token: 'write'
pull-requests: 'write'
steps:
- name: Checkout repository
uses: actions/checkout@v3
- id: 'auth'
name: 'Authenticate to Google Cloud'
uses: 'google-github-actions/auth@v1'
with:
credentials_json: '${{ secrets.SERVICE_ACCOUNT_KEY }}'
- name: Set up Cloud SDK
uses: 'google-github-actions/setup-gcloud@v1'
with:
project_id: ${{ secrets.PROJECT_ID }}
- name: Configure Docker
run: |
gcloud auth configure-docker
- name: Build Frontend Docker image
run: |
docker build -t gcr.io/${{ secrets.PROJECT_ID }}/$FRONTEND_IMAGE:${{ github.event.pull_request.number }} -f frontend/Dockerfile.prod ./frontend
- name: Build Backend Docker image
run: |
docker build -t gcr.io/${{ secrets.PROJECT_ID }}/$BACKEND_IMAGE:${{ github.event.pull_request.number }} -f backend/Dockerfile.prod ./backend --build-arg BACKEND_URL=$BACKEND_URL --build-arg FRONTEND_URL=$FRONTEND_URL
- name: Push Docker images
run: |
docker push gcr.io/${{ secrets.PROJECT_ID }}/$FRONTEND_IMAGE:${{ github.event.pull_request.number }}
docker push gcr.io/${{ secrets.PROJECT_ID }}/$BACKEND_IMAGE:${{ github.event.pull_request.number }}
- name: Deploy Backend Preview
run: |
cat > env.yaml << EOF
DB_TYPE: firebase
NODE_ENV: production
CORS_ORIGIN: "$FRONTEND_URL,https://aphorist.firebaseapp.com,https://aphorist.web.app,https://aphori.st,https://www.aphori.st,$BACKEND_URL"
EOF
gcloud run deploy backend-preview-${{ github.event.pull_request.number }} \
--image gcr.io/${{ secrets.PROJECT_ID }}/$BACKEND_IMAGE:${{ github.event.pull_request.number }} \
--platform managed \
--region $REGION \
--port 5000 \
--env-vars-file env.yaml \
--set-secrets "FIREBASE_CREDENTIAL=firebase-admin-key:latest,\
FIREBASE_DATABASE_URL=firebase-database-url:latest,\
EMAIL_HOST=EMAIL_HOST:latest,\
EMAIL_PORT=EMAIL_PORT:latest,\
EMAIL_USERNAME=EMAIL_USERNAME:latest,\
EMAIL_PASSWORD=EMAIL_PASSWORD:latest,\
MAGIC_LINK_SECRET=JWT_SECRET:latest,\
AUTH_TOKEN_SECRET=JWT_SECRET:latest" \
--allow-unauthenticated
- name: Deploy Frontend Preview
run: |
docker build \
--build-arg REACT_APP_API_URL=$BACKEND_URL \
-t gcr.io/${{ secrets.PROJECT_ID }}/$FRONTEND_IMAGE:${{ github.event.pull_request.number }} \
-f frontend/Dockerfile.prod ./frontend
docker push gcr.io/${{ secrets.PROJECT_ID }}/$FRONTEND_IMAGE:${{ github.event.pull_request.number }}
gcloud run deploy frontend-preview-${{ github.event.pull_request.number }} \
--image gcr.io/${{ secrets.PROJECT_ID }}/$FRONTEND_IMAGE:${{ github.event.pull_request.number }} \
--platform managed \
--region $REGION \
--port 8080 \
--timeout 300s \
--cpu=1 \
--memory=512Mi \
--min-instances=1 \
--allow-unauthenticated
- name: Setup Firebase CLI
run: npm install -g firebase-tools
- name: Initialize Firebase Project
run: |
echo 'y' | firebase use ${{ secrets.PROJECT_ID }} --token ${{ secrets.FIREBASE_TOKEN }}
- name: Create Firebase Preview Channel
id: preview_channel
run: |
CHANNEL_ID="pr-${{ github.event.pull_request.number }}"
firebase hosting:channel:create $CHANNEL_ID --token ${{ secrets.FIREBASE_TOKEN }}
firebase hosting:channel:deploy $CHANNEL_ID --token ${{ secrets.FIREBASE_TOKEN }}
PREVIEW_URL=$(firebase hosting:channel:list --token ${{ secrets.FIREBASE_TOKEN }} | grep $CHANNEL_ID | awk '{print $4}')
echo "::set-output name=preview_url::$PREVIEW_URL"
- name: Comment Preview URL
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const previewUrl = '${{ steps.preview_channel.outputs.preview_url }}';
const comment = `🚀 Preview deployment is ready!\n\nYou can view your changes at: ${previewUrl}`;
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.name,
issue_number: context.issue.number,
body: comment
});