Skip to content

Commit

Permalink
Add tool to deploy job on gcp
Browse files Browse the repository at this point in the history
  • Loading branch information
danieltrolezi committed Oct 8, 2024
1 parent a1a2cde commit 70fe523
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 8 deletions.
9 changes: 8 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=secret

REDIS_CONNECTION=default
REDIS_HOST=redis
REDIS_PASSWORD=redis

Expand All @@ -30,4 +31,10 @@ DISCORD_API_HOST=https://discord.com

ROOT_DISCORD_USER_ID=
ROOT_DISCORD_USERNAME=
ROOT_DISCORD_CHANNEL_ID=
ROOT_DISCORD_CHANNEL_ID=

# [ONLY LOCAL]
GCP_PROJECT_ID=
GCP_PROJECT_NUMBER=
GCP_REGION=
GCP_CLOUD_RUN_SERVICE=
2 changes: 2 additions & 0 deletions .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ jobs:
sed -i 's|<GCP_SERVICE_ACCOUNT>|${{ vars.GCP_SERVICE_ACCOUNT }}|' ./gcp/cloud-run/service.yaml
sed -i 's|<GCP_CLOUD_RUN_SERVICE>|${{ vars.GCP_CLOUD_RUN_SERVICE }}|' ./gcp/cloud-run/service.yaml
sed -i 's|<GCP_REGION>|${{ vars.GCP_REGION }}|' ./gcp/cloud-run/service.yaml
sed -i 's|<GCP_VPC>|${{ vars.GCP_VPC }}|' ./gcp/cloud-run/service.yaml
sed -i 's|<GCP_VPC_SUBNET>|${{ vars.GCP_VPC_SUBNET }}|' ./gcp/cloud-run/service.yaml
sed -i 's|<DOCKER_IMAGE>|${{ vars.GCP_AR_REPOSITORY }}/app:latest|' ./gcp/cloud-run/service.yaml
- name: Set up Google Cloud authentication
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ services:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
test: ["CMD-SHELL", "mysql -u root -p${DB_PASSWORD} -e 'SHOW DATABASES;'"]
interval: 5s
timeout: 10s
retries: 5
Expand Down
2 changes: 1 addition & 1 deletion docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ case "$RUN_MODE" in
migrate)
php artisan migrate
;;
init-db)
initdb)
php artisan migrate --seed
;;
*)
Expand Down
154 changes: 154 additions & 0 deletions gcp/cloud-run/job.template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
apiVersion: run.googleapis.com/v1
kind: Job
metadata:
name: <GCP_CLOUD_RUN_SERVICE>-task-runner
namespace: '<GCP_PROJECT_NUMBER>'
labels:
cloud.googleapis.com/location: <GCP_REGION>
spec:
template:
spec:
parallelism: 1
taskCount: 1
template:
spec:
containers:
- image: <DOCKER_IMAGE>
env:
- name: RUN_MODE
value: <RUN_MODE>
- name: APP_NAME
valueFrom:
secretKeyRef:
name: GAMEWATCH_APP_NAME
key: latest
- name: APP_VERSION
valueFrom:
secretKeyRef:
name: GAMEWATCH_APP_VERSION
key: latest
- name: APP_URL
valueFrom:
secretKeyRef:
name: GAMEWATCH_APP_URL
key: latest
- name: APP_ENV
valueFrom:
secretKeyRef:
name: GAMEWATCH_APP_ENV
key: latest
- name: APP_KEY
valueFrom:
secretKeyRef:
name: GAMEWATCH_APP_KEY
key: latest
- name: APP_DEBUG
valueFrom:
secretKeyRef:
name: GAMEWATCH_APP_DEBUG
key: latest
- name: DB_CONNECTION
valueFrom:
secretKeyRef:
name: GAMEWATCH_DB_CONNECTION
key: latest
- name: DB_HOST
valueFrom:
secretKeyRef:
name: GAMEWATCH_DB_HOST
key: latest
- name: DB_PORT
valueFrom:
secretKeyRef:
name: GAMEWATCH_DB_PORT
key: latest
- name: DB_DATABASE
valueFrom:
secretKeyRef:
name: GAMEWATCH_DB_DATABASE
key: latest
- name: DB_USERNAME
valueFrom:
secretKeyRef:
name: GAMEWATCH_DB_USERNAME
key: latest
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: GAMEWATCH_DB_PASSWORD
key: latest
- name: REDIS_CONNECTION
valueFrom:
secretKeyRef:
name: GAMEWATCH_REDIS_CONNECTION
key: latest
- name: REDIS_HOST
valueFrom:
secretKeyRef:
name: GAMEWATCH_REDIS_HOST
key: latest
- name: REDIS_PORT
valueFrom:
secretKeyRef:
name: GAMEWATCH_REDIS_PORT
key: latest
- name: REDIS_USERNAME
valueFrom:
secretKeyRef:
name: GAMEWATCH_REDIS_USERNAME
key: latest
- name: REDIS_PASSWORD
valueFrom:
secretKeyRef:
name: GAMEWATCH_REDIS_PASSWORD
key: latest
- name: RAWG_API_KEY
valueFrom:
secretKeyRef:
name: GAMEWATCH_RAWG_API_KEY
key: latest
- name: RAWG_API_HOST
valueFrom:
secretKeyRef:
name: GAMEWATCH_RAWG_API_HOST
key: latest
- name: JWT_EXPIRES
valueFrom:
secretKeyRef:
name: GAMEWATCH_JWT_EXPIRES
key: latest
- name: DISCORD_APP_ID
valueFrom:
secretKeyRef:
name: GAMEWATCH_DISCORD_APP_ID
key: latest
- name: DISCORD_PUBLIC_KEY
valueFrom:
secretKeyRef:
name: GAMEWATCH_DISCORD_PUBLIC_KEY
key: latest
- name: DISCORD_BOT_TOKEN
valueFrom:
secretKeyRef:
name: GAMEWATCH_DISCORD_BOT_TOKEN
key: latest
- name: DISCORD_API_HOST
valueFrom:
secretKeyRef:
name: GAMEWATCH_DISCORD_API_HOST
key: latest
- name: ROOT_DISCORD_USER_ID
valueFrom:
secretKeyRef:
name: GAMEWATCH_ROOT_DISCORD_USER_ID
key: latest
- name: ROOT_DISCORD_USERNAME
valueFrom:
secretKeyRef:
name: GAMEWATCH_ROOT_DISCORD_USERNAME
key: latest
- name: ROOT_DISCORD_CHANNEL_ID
valueFrom:
secretKeyRef:
name: GAMEWATCH_ROOT_DISCORD_CHANNEL_ID
key: latest
7 changes: 2 additions & 5 deletions gcp/cloud-run/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ spec:
autoscaling.knative.dev/minScale: "1"
autoscaling.knative.dev/maxScale: "2"
run.googleapis.com/vpc-access-egress: all-traffic
run.googleapis.com/network-interfaces: '[{"network":"codelab92-vpc","subnetwork":"codelab92-vpc-primary-subnet"}]'
run.googleapis.com/network-interfaces: '[{"network":"<GCP_VPC>","subnetwork":"<GCP_VPC_SUBNET>"}]'
run.googleapis.com/cpu-throttling: 'false'
run.googleapis.com/startup-cpu-boost: 'true'
spec:
Expand Down Expand Up @@ -57,10 +57,7 @@ spec:
periodSeconds: 10
env:
- name: RUN_MODE
valueFrom:
secretKeyRef:
name: GAMEWATCH_RUN_MODE
key: latest
value: octane
- name: APP_NAME
valueFrom:
secretKeyRef:
Expand Down
34 changes: 34 additions & 0 deletions gcp/tools/deploy-job.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash

if [ -f .env ]; then
export $(cat .env | grep -v '^#' | xargs)
fi

# Stop the script if any command fails
set -e

# Get the RUN_MODE from the first argument
RUN_MODE=$1

# Get the directory where the script is located
SCRIPT_DIR="$(dirname "$0")"

# Copy the template to a new job.yaml file
cp "$SCRIPT_DIR/../job.template.yaml" "$SCRIPT_DIR/../job.yaml"

# Replace placeholders
sed -i "s|<RUN_MODE>|$RUN_MODE|" "$SCRIPT_DIR/../job.yaml"
sed -i "s|<GCP_PROJECT_NUMBER>|$GCP_PROJECT_NUMBER|" "$SCRIPT_DIR/../job.yaml"
sed -i "s|<GCP_REGION>|$GCP_REGION|" "$SCRIPT_DIR/../job.yaml"
sed -i "s|<GCP_CLOUD_RUN_SERVICE>|$GCP_CLOUD_RUN_SERVICE|" "$SCRIPT_DIR/../job.yaml"

# Deploy the job using the modified job.yaml
gcloud beta run jobs replace "$SCRIPT_DIR/../job.yaml" --region $GCP_REGION --project $GCP_PROJECT_ID

# Execute the job after replacing it
gcloud beta run jobs execute gamewatch-init-db --region $GCP_REGION --project $GCP_PROJECT_ID

# Clean up the temporary job.yaml file
rm "$SCRIPT_DIR/../job.yaml"


0 comments on commit 70fe523

Please sign in to comment.