mirror of
https://github.com/MetaFam/TheGame.git
synced 2026-04-24 03:00:09 -04:00
add workflow that deploys previews of pull requests
This commit is contained in:
126
.github/workflows/gcp-deploy.yaml
vendored
Normal file
126
.github/workflows/gcp-deploy.yaml
vendored
Normal file
@@ -0,0 +1,126 @@
|
||||
name: Build and Deploy Preview to Cloud Run
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
branches:
|
||||
- develop
|
||||
types:
|
||||
- opened
|
||||
- reopened
|
||||
|
||||
env:
|
||||
PROJECT_ID: metagame-thegame
|
||||
REGISTRY_REGION: us-east4
|
||||
REGISTRY_REPO: thegame
|
||||
CLOUDRUN_REGION: us-east4
|
||||
CLOUDRUN_SUFFIX: mjhnbmqqna-uk
|
||||
CLOUDSQL_INSTANCE_NAME: thegame
|
||||
CLOUDSQL_CONNECTION_NAME: metagame-thegame:us-east4:thegame
|
||||
|
||||
jobs:
|
||||
build-and-push:
|
||||
name: Build and Push Containers
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v2
|
||||
|
||||
- name: Login to Registry
|
||||
uses: docker/login-action@v1
|
||||
with:
|
||||
registry: ${{ env.REGISTRY_REGION }}-docker.pkg.dev
|
||||
username: _json_key
|
||||
password: ${{ secrets.GCP_SA_KEY }}
|
||||
|
||||
- name: Build Backend Container
|
||||
uses: mattes/cached-docker-build-action@v1
|
||||
with:
|
||||
args: ". -f ./docker/backend/Dockerfile --tag ${REGISTRY_REGION}-docker.pkg.dev/${PROJECT_ID}/${REGISTRY_REPO}/backend:pr-${{ github.event.number }} --build-arg GRAPHQL_HOST=hasura-pr-${{ github.event.number }}-${CLOUDRUN_SUFFIX} --build-arg GRAPHQL_DOMAIN=a.run.app"
|
||||
cache_key: "${{ hashFiles('**/lockfiles') }}"
|
||||
|
||||
- name: Build Hasura Container
|
||||
uses: mattes/cached-docker-build-action@v1
|
||||
with:
|
||||
args: "./hasura --tag ${REGISTRY_REGION}-docker.pkg.dev/${PROJECT_ID}/${REGISTRY_REPO}/hasura:pr-${{ github.event.number }} --build-arg BACKEND_HOST=backend-pr-${{ github.event.number }}-${CLOUDRUN_SUFFIX}.a.run.app --build-arg BACKEND_PROTOCOL=https"
|
||||
cache_key: "${{ hashFiles('**/lockfiles') }}"
|
||||
|
||||
- name: Build Frontend Container
|
||||
uses: mattes/cached-docker-build-action@v1
|
||||
with:
|
||||
args: ". -f ./docker/frontend/Dockerfile --tag ${REGISTRY_REGION}-docker.pkg.dev/${PROJECT_ID}/${REGISTRY_REPO}/frontend:pr-${{ github.event.number }} --build-arg GRAPHQL_HOST=api --build-arg GRAPHQL_DOMAIN=metagame.wtf"
|
||||
cache_key: "${{ hashFiles('**/lockfiles') }}"
|
||||
|
||||
- name: Push Backend Container
|
||||
run: docker push ${REGISTRY_REGION}-docker.pkg.dev/${PROJECT_ID}/${REGISTRY_REPO}/backend:pr-${{ github.event.number }}
|
||||
|
||||
- name: Push Hasura Container
|
||||
run: docker push ${REGISTRY_REGION}-docker.pkg.dev/${PROJECT_ID}/${REGISTRY_REPO}/hasura:pr-${{ github.event.number }}
|
||||
|
||||
- name: Push Frontend Container
|
||||
run: docker push ${REGISTRY_REGION}-docker.pkg.dev/${PROJECT_ID}/${REGISTRY_REPO}/frontend:pr-${{ github.event.number }}
|
||||
|
||||
deploy:
|
||||
name: Deploy Containers to Cloud Run
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Set up gcloud CLI
|
||||
uses: google-github-actions/setup-gcloud@v0.2.1
|
||||
with:
|
||||
project_id: ${{ env.PROJECT_ID }}
|
||||
service_account_key: ${{ secrets.GKE_SA_KEY }}
|
||||
export_default_credentials: true
|
||||
|
||||
- name: Create User and Database for Hasura
|
||||
run: |
|
||||
HASURA_DB_PASSWORD=$(cat /dev/urandom | tr -cd [:alnum:] | head -c 16)
|
||||
echo "HASURA_DB_PASSWORD=${HASURA_DB_PASSWORD}" >> $GITHUB_ENV
|
||||
gcloud -q sql users create hasura-pr-${{ github.event.number }} -i ${CLOUDSQL_INSTANCE_NAME} --password ${HASURA_DB_PASSWORD}
|
||||
gcloud -q sql databases create hasura-pr-${{ github.event.number }} -i ${CLOUDSQL_INSTANCE_NAME}
|
||||
|
||||
- name: Deploy Backend
|
||||
run: |
|
||||
gcloud -q run deploy backend-pr-${{ github.event.number }} \
|
||||
--image ${REGISTRY_REGION}-docker.pkg.dev/${PROJECT_ID}/${REGISTRY_REPO}/backend:pr-${{ github.event.number }} \
|
||||
--region ${CLOUDRUN_REGION} \
|
||||
--port 4000 \
|
||||
--cpu 1 \
|
||||
--memory 512Mi \
|
||||
--ingress all \
|
||||
--allow-unauthenticated \
|
||||
--set-env-vars HASURA_GRAPHQL_ADMIN_SECRET=metagame_secret
|
||||
|
||||
- name: Deploy Hasura
|
||||
run: |
|
||||
gcloud -q run deploy hasura-pr-${{ github.event.number }} \
|
||||
--image ${REGISTRY_REGION}-docker.pkg.dev/${PROJECT_ID}/${REGISTRY_REPO}/hasura:pr-${{ github.event.number }} \
|
||||
--region ${CLOUDRUN_REGION} \
|
||||
--port 8080 \
|
||||
--cpu 1 \
|
||||
--memory 512Mi \
|
||||
--ingress all \
|
||||
--allow-unauthenticated \
|
||||
--add-cloudsql-instances metagame-thegame:us-east4:thegame \
|
||||
--set-env-vars HASURA_GRAPHQL_DATABASE_URL=postgres://hasura-pr-${{ github.event.number }}:${HASURA_DB_PASSWORD}@/hasura-pr-${{ github.event.number }}?host=/cloudsql/${CLOUDSQL_CONNECTION_NAME},HASURA_GRAPHQL_ADMIN_SECRET=metagame_secret
|
||||
|
||||
- name: Deploy Frontend
|
||||
run: |
|
||||
gcloud -q run deploy frontend-pr-${{ github.event.number }} \
|
||||
--image ${REGISTRY_REGION}-docker.pkg.dev/${PROJECT_ID}/${REGISTRY_REPO}/frontend:pr-${{ github.event.number }} \
|
||||
--region ${CLOUDRUN_REGION} \
|
||||
--port 3000 \
|
||||
--cpu 1 \
|
||||
--memory 512Mi \
|
||||
--ingress all \
|
||||
--allow-unauthenticated \
|
||||
--set-env-vars GRAPHQL_HOST=hasura-pr-${{ github.event.number }}-${CLOUDRUN_SUFFIX},GRAPHQL_DOMAIN=a.run.app
|
||||
|
||||
- name: Comment on Pull Request
|
||||
uses: thollander/actions-comment-pull-request@v1
|
||||
with:
|
||||
message: |
|
||||
Successfully deployed a Preview of this Pull Request
|
||||
[Frontend](https://frontend-pr-${{ github.event.number }}-${CLOUDRUN_SUFFIX}.a.run.app)
|
||||
[Hasura](https://hasura-pr-${{ github.event.number }}-${CLOUDRUN_SUFFIX}.a.run.app)
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
Reference in New Issue
Block a user