Skip to content

Commit

Permalink
Adds Terraform and deployment code
Browse files Browse the repository at this point in the history
  • Loading branch information
vlidholt committed Mar 13, 2023
1 parent 8807548 commit f571842
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 0 deletions.
88 changes: 88 additions & 0 deletions .github/workflows/deployment-gcp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
name: Deploy to GCP
on:
push:
branches: [ deployment-gcp-production, deployment-gcp-staging ]
workflow_dispatch:
inputs:
target:
description: 'Target'
required: true
default: 'production'
type: choice
options:
- 'staging'
- 'production'

env:
PROJECT: serverpod-deployment-demo # TODO: update Google Cloud project id
REGION: us-central1 # TODO: update Cloud Run service region
ZONE: us-central1-c # TODO: Template

jobs:
deploy:
name: Deploy to Google Cloud Run
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3
with:
submodules: recursive

- name: Setting Target Mode from Input
if: ${{ github.event.inputs.target != '' }}
run: echo "TARGET=${{ github.event.inputs.target }}" >> $GITHUB_ENV

- name: Setting Target mode based on branch
if: ${{ github.event.inputs.target == '' }}
run: echo "TARGET=${GITHUB_REF##*-}" >> $GITHUB_ENV

- name: Set repository
run: echo "REPOSITORY=serverpod-${{ env.TARGET }}-container" >> $GITHUB_ENV

- name: Set Image Name
run: echo "IMAGE_NAME=serverpod" >> $GITHUB_ENV

- name: Set Service Name
run: echo "SERVICE_NAME=$(echo $IMAGE_NAME | sed 's/[^a-zA-Z0-9]/-/g')" >> $GITHUB_ENV

- name: Test
run: echo $SERVICE_NAME


- id: "auth"
name: "Authenticate to Google Cloud"
uses: "google-github-actions/auth@v1"
with:
credentials_json: "${{ secrets.GOOGLE_CREDENTIALS }}"

- name: Create passwords file
working-directory: gcp_server
shell: bash
env:
SERVERPOD_PASSWORDS: ${{ secrets.SERVERPOD_PASSWORDS }}
run: |
pwd
echo "$SERVERPOD_PASSWORDS" > config/passwords.yaml
ls config/
- name: Configure Docker
run: gcloud auth configure-docker ${{ env.REGION }}-docker.pkg.dev

- name: Build the Docker image
run: "cd gcp_server && docker build -t $IMAGE_NAME . --build-arg mode=$TARGET"

- name: Tag the Docker image
run: docker tag $IMAGE_NAME ${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT }}/${{ env.REPOSITORY }}/$IMAGE_NAME

- name: Push Docker image
run: docker push ${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT }}/${{ env.REPOSITORY }}/$IMAGE_NAME

# - name: Restart servers in instance group
# run: |
# gcloud compute instance-groups managed rolling-action replace serverpod-${{ env.TARGET }}-group \
# --project=${{ env.PROJECT }} \
# --replacement-method='substitute' \
# --max-surge=1 \
# --max-unavailable=1 \
# --zone=${{ env.ZONE }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ gcp_demo_flutter/windows
gcp_demo_flutter/android
gcp_demo_flutter/ios
credentials.json
gcp_demo_server/gcp/terraform/.terraform
15 changes: 15 additions & 0 deletions gcp_demo_server/gcp/terraform/config.auto.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# The Project ID from the Google Cloud Console.
project = "serverpod-deployment-demo"

# The service account email address authorized by your Google Cloud Console.
service_account_email = "[email protected]"

# The name of your DNS zone.
dns_managed_zone = "examplepod"

# The top domain of your DNS zone.
top_domain = "examplepod.com"

# The region and zone to use for the deployment. Default values work.
region = "us-central1"
zone = "us-central1-c"
65 changes: 65 additions & 0 deletions gcp_demo_server/gcp/terraform/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Set up and configure Terraform and the Google Cloud provider.
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "4.51.0"
}
}
}

provider "google" {
credentials = file("credentials.json")

project = var.project
region = var.region
zone = var.zone
}

# Add a Serverpod module configured for production. Full documentation is
# available at https://github.com/serverpod/google_cloud_serverpod_gce
module "serverpod_production" {
# References the Serverpod module from GitHub.
source = "github.com/serverpod/google_cloud_serverpod_gce?ref=dev"

# Required parameters.
project = var.project
service_account_email = var.service_account_email

runmode = "production"

region = var.region
zone = var.zone

dns_managed_zone = var.dns_managed_zone
top_domain = var.top_domain

# Size of the auto scaling group.
autoscaling_min_size = 1
autoscaling_max_size = 2

database_password = var.DATABASE_PASSWORD_PRODUCTION

# Makes it possible to SSH into the individual server instances.
enable_ssh = true
}

# module "serverpod_staging" {
# source = "./modules/serverpod"
# count = var.enable_staging ? 1 : 0

# project = var.project
# runmode = "staging"

# region = var.region
# zone = var.zone

# top_domain = "examplepod.com"

# autoscaling_min_size = var.autoscaling_min_size
# autoscaling_max_size = var.autoscaling_max_size

# service_account_email = var.service_account_email

# database_password = var.DATABASE_PASSWORD_STAGING
# }
39 changes: 39 additions & 0 deletions gcp_demo_server/gcp/terraform/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Project setup.

variable "project" {
type = string
}

variable "service_account_email" {
type = string
}

variable "dns_managed_zone" {
type = string
}

variable "top_domain" {
type = string
}

variable "region" {
type = string
default = "us-central1"
}

variable "zone" {
type = string
default = "us-central1-c"
}

# Database

variable "DATABASE_PASSWORD_PRODUCTION" {
description = "The production database password, you can find it in the config/passwords.yaml file."
type = string
}

variable "DATABASE_PASSWORD_STAGING" {
description = "The staging database password, you can find it in the config/passwords.yaml file (no need to specify if you aren't deployning a staging environment)."
type = string
}

0 comments on commit f571842

Please sign in to comment.