Skip to content

Merge pull request #74 from merogersdev/hotfix/71-cicd-not-updating #66

Merge pull request #74 from merogersdev/hotfix/71-cicd-not-updating

Merge pull request #74 from merogersdev/hotfix/71-cicd-not-updating #66

Workflow file for this run

---
name: Build and Push Images to GCP
on:
push:
branches: [main]
jobs:
build-push-gcr:
name: Build and Push to GCP
runs-on: ubuntu-latest
env:
DATABASE_IMAGE_NAME: peace-of-mind-db
FRONTEND_IMAGE_NAME: peace-of-mind-frontend
BACKEND_IMAGE_NAME: peace-of-mind-backend
PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }}
GCP_REGION: ${{ secrets.GCP_REGION }}
GCP_ZONE: ${{ secrets.GCP_ZONE }}
GCP_INSTANCE_NAME: ${{ secrets.GCP_INSTANCE_NAME }}
steps:
- name: Checkout
uses: actions/checkout@v4
- uses: google-github-actions/auth@v2
with:
credentials_json: ${{ secrets.SERVICE_ACCOUNT_KEY }}
- name: Build Docker Images
run: |-
docker build --no-cache -t $DATABASE_IMAGE_NAME:latest ./db/
docker build --no-cache -t $FRONTEND_IMAGE_NAME:latest ./frontend/
docker build --no-cache -t $BACKEND_IMAGE_NAME:latest ./backend/
- name: Configure Docker Client
run: |-
gcloud auth configure-docker --quiet
gcloud auth configure-docker $GCP_REGION-docker.pkg.dev --quiet
- name: Tag and Push Images to GCP
run: |-
docker tag $DATABASE_IMAGE_NAME:latest $GCP_REGION-docker.pkg.dev/$PROJECT_ID/peace-of-mind/$DATABASE_IMAGE_NAME:latest
docker tag $BACKEND_IMAGE_NAME:latest $GCP_REGION-docker.pkg.dev/$PROJECT_ID/peace-of-mind/$BACKEND_IMAGE_NAME:latest
docker tag $FRONTEND_IMAGE_NAME:latest $GCP_REGION-docker.pkg.dev/$PROJECT_ID/peace-of-mind/$FRONTEND_IMAGE_NAME:latest
docker push $GCP_REGION-docker.pkg.dev/$PROJECT_ID/peace-of-mind/$DATABASE_IMAGE_NAME:latest
docker push $GCP_REGION-docker.pkg.dev/$PROJECT_ID/peace-of-mind/$BACKEND_IMAGE_NAME:latest
docker push $GCP_REGION-docker.pkg.dev/$PROJECT_ID/peace-of-mind/$FRONTEND_IMAGE_NAME:latest
deploy:
name: Deploy to GCP VM
runs-on: ubuntu-latest
needs: build-push-gcr
steps:
- name: "SSH"
uses: appleboy/ssh-action@v1.0.3
env:
GCP_REGION: ${{ secrets.GCP_REGION }}
POSTGRES_USER: "peaceofmind"
POSTGRES_PASSWORD: "${{ secrets.POSTGRES_PASSWORD }}"
POSTGRES_DB: "peaceofmind"
POSTGRES_DB_TEST: "peaceofmind_test"
POSTGRES_PORT: 5432
DATABASE_IMAGE_NAME: peace-of-mind-db
FRONTEND_IMAGE_NAME: peace-of-mind-frontend
BACKEND_IMAGE_NAME: peace-of-mind-backend
PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }}
JWT_SECRET: "${{ secrets.JWT_SECRET }}"
API_KEY: "${{ secrets.API_KEY }}"
with:
host: ${{ secrets.SERVER_IP }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SERVER_KEY }}
envs: GCP_REGION,POSTGRES_USER,POSTGRES_PASSWORD,POSTGRES_DB,POSTGRES_DB_TEST,POSTGRES_PORT,DATABASE_IMAGE_NAME,FRONTEND_IMAGE_NAME,BACKEND_IMAGE_NAME,PROJECT_ID,JWT_SECRET,API_KEY
script: |
gcloud auth configure-docker --quiet
gcloud auth configure-docker $GCP_REGION-docker.pkg.dev --quiet
docker stop $(docker ps -aq)
docker rm $(docker ps -aq)
docker image prune -af
docker network rm peace-of-mind-network -f
docker network create peace-of-mind-network
docker run -d --restart=unless-stopped --network=peace-of-mind-network -p 5432:5432 -v pgdata:/var/lib/postgresql/data -e POSTGRES_USER=$POSTGRES_USER -e POSTGRES_PASSWORD=$POSTGRES_PASSWORD -e POSTGRES_DB=$POSTGRES_DB -e POSTGRES_DB_TEST=$POSTGRES_DB_TEST --name $DATABASE_IMAGE_NAME $GCP_REGION-docker.pkg.dev/$PROJECT_ID/peace-of-mind/$DATABASE_IMAGE_NAME
docker run -d --restart=unless-stopped --network=peace-of-mind-network -p 5000:5000 -e NODE_ENV='production' -e DB_HOST=$DATABASE_IMAGE_NAME -e DB_PORT=$POSTGRES_PORT -e DB_USER=$POSTGRES_USER -e DB_PASS=$POSTGRES_PASSWORD -e DB_DATA=$POSTGRES_DB -e DB_DATA_TEST=$POSTGRES_DB_TEST -e JWT_SECRET=$JWT_SECRET -e API_KEY=$API_KEY --name $BACKEND_IMAGE_NAME $GCP_REGION-docker.pkg.dev/$PROJECT_ID/peace-of-mind/$BACKEND_IMAGE_NAME
docker run -d --restart=unless-stopped --network=peace-of-mind-network -p 3000:80 --name $FRONTEND_IMAGE_NAME $GCP_REGION-docker.pkg.dev/$PROJECT_ID/peace-of-mind/$FRONTEND_IMAGE_NAME