From 70fe5231be8ab560e1683b4155e1a9219f4b7b03 Mon Sep 17 00:00:00 2001 From: Daniel Trolezi Date: Tue, 8 Oct 2024 00:17:28 -0300 Subject: [PATCH] Add tool to deploy job on gcp --- .env.example | 9 +- .github/workflows/ci-cd.yml | 2 + docker-compose.yml | 2 +- docker/entrypoint.sh | 2 +- gcp/cloud-run/job.template.yaml | 154 ++++++++++++++++++++++++++++++++ gcp/cloud-run/service.yaml | 7 +- gcp/tools/deploy-job.sh | 34 +++++++ 7 files changed, 202 insertions(+), 8 deletions(-) create mode 100644 gcp/cloud-run/job.template.yaml create mode 100755 gcp/tools/deploy-job.sh diff --git a/.env.example b/.env.example index 52a28f0..df63cd1 100644 --- a/.env.example +++ b/.env.example @@ -15,6 +15,7 @@ DB_DATABASE=laravel DB_USERNAME=root DB_PASSWORD=secret +REDIS_CONNECTION=default REDIS_HOST=redis REDIS_PASSWORD=redis @@ -30,4 +31,10 @@ DISCORD_API_HOST=https://discord.com ROOT_DISCORD_USER_ID= ROOT_DISCORD_USERNAME= -ROOT_DISCORD_CHANNEL_ID= \ No newline at end of file +ROOT_DISCORD_CHANNEL_ID= + +# [ONLY LOCAL] +GCP_PROJECT_ID= +GCP_PROJECT_NUMBER= +GCP_REGION= +GCP_CLOUD_RUN_SERVICE= \ No newline at end of file diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index 7a6eb8e..7ddacca 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -52,6 +52,8 @@ jobs: sed -i 's||${{ vars.GCP_SERVICE_ACCOUNT }}|' ./gcp/cloud-run/service.yaml sed -i 's||${{ vars.GCP_CLOUD_RUN_SERVICE }}|' ./gcp/cloud-run/service.yaml sed -i 's||${{ vars.GCP_REGION }}|' ./gcp/cloud-run/service.yaml + sed -i 's||${{ vars.GCP_VPC }}|' ./gcp/cloud-run/service.yaml + sed -i 's||${{ vars.GCP_VPC_SUBNET }}|' ./gcp/cloud-run/service.yaml sed -i 's||${{ vars.GCP_AR_REPOSITORY }}/app:latest|' ./gcp/cloud-run/service.yaml - name: Set up Google Cloud authentication diff --git a/docker-compose.yml b/docker-compose.yml index b062a7d..f673c77 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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 diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh index 2f627f9..a173f19 100644 --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -25,7 +25,7 @@ case "$RUN_MODE" in migrate) php artisan migrate ;; - init-db) + initdb) php artisan migrate --seed ;; *) diff --git a/gcp/cloud-run/job.template.yaml b/gcp/cloud-run/job.template.yaml new file mode 100644 index 0000000..bc6d24b --- /dev/null +++ b/gcp/cloud-run/job.template.yaml @@ -0,0 +1,154 @@ +apiVersion: run.googleapis.com/v1 +kind: Job +metadata: + name: -task-runner + namespace: '' + labels: + cloud.googleapis.com/location: +spec: + template: + spec: + parallelism: 1 + taskCount: 1 + template: + spec: + containers: + - image: + env: + - name: RUN_MODE + value: + - 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 diff --git a/gcp/cloud-run/service.yaml b/gcp/cloud-run/service.yaml index 86dbed2..512af8d 100644 --- a/gcp/cloud-run/service.yaml +++ b/gcp/cloud-run/service.yaml @@ -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":"","subnetwork":""}]' run.googleapis.com/cpu-throttling: 'false' run.googleapis.com/startup-cpu-boost: 'true' spec: @@ -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: diff --git a/gcp/tools/deploy-job.sh b/gcp/tools/deploy-job.sh new file mode 100755 index 0000000..e0e6d7a --- /dev/null +++ b/gcp/tools/deploy-job.sh @@ -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|" "$SCRIPT_DIR/../job.yaml" +sed -i "s||$GCP_PROJECT_NUMBER|" "$SCRIPT_DIR/../job.yaml" +sed -i "s||$GCP_REGION|" "$SCRIPT_DIR/../job.yaml" +sed -i "s||$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" + +