run init sql script after promoting server #117
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Run Database Migrations & Deploy | |
on: | |
push: | |
branches: | |
- lantern-main | |
- lantern-staging | |
jobs: | |
migrate: | |
name: Migrate Database & Deploy | |
runs-on: ubicloud | |
permissions: | |
contents: "read" | |
id-token: "write" | |
steps: | |
- name: Set up environment | |
id: set_env | |
run: | | |
if [[ ${{ github.ref }} == 'refs/heads/lantern-main' ]]; then | |
echo "branch=production" >> $GITHUB_OUTPUT | |
echo "gcr_image=${IMAGE_PRODUCTION}" >> $GITHUB_OUTPUT | |
else | |
echo "branch=staging" >> $GITHUB_OUTPUT | |
echo "gcr_image=${IMAGE_STAGING}" >> $GITHUB_OUTPUT | |
fi | |
echo "image=lanterndata/lantern-ubicloud" >> $GITHUB_OUTPUT | |
env: | |
IMAGE_STAGING: ${{ format('{0}-docker.pkg.dev/{1}/lanterndata/lantern-ubicloud', secrets.GCP_REGION, secrets.GCP_PROJECT_ID_STAGING) }} | |
IMAGE_PRODUCTION: ${{ format('{0}-docker.pkg.dev/{1}/lanterndata/lantern-ubicloud', secrets.GCP_REGION, secrets.GCP_PROJECT_ID_PRODUCTION) }} | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
# Setup Gcloud | |
- name: "GCP Auth" | |
id: "auth" | |
uses: "google-github-actions/auth@v2" | |
with: | |
token_format: access_token | |
workload_identity_provider: ${{ ( steps.set_env.outputs.branch == 'production' && secrets.GCP_WORKLOAD_IDENTITY_PROVIDER_PRODUCTION) || secrets.GCP_WORKLOAD_IDENTITY_PROVIDER_STAGING }} | |
service_account: ${{ ( steps.set_env.outputs.branch == 'production' && secrets.GCP_SERVICE_ACCOUNT_PRODUCTION) || secrets.GCP_SERVICE_ACCOUNT_STAGING }} | |
# Build and push image | |
- name: Login to GCR Container Registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ${{ secrets.GCP_REGION }}-docker.pkg.dev | |
username: oauth2accesstoken | |
password: ${{ steps.auth.outputs.access_token }} | |
- name: Login to DockerHub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Build and push | |
uses: docker/build-push-action@v5 | |
id: build_image | |
with: | |
context: . | |
platforms: | | |
linux/amd64 | |
file: Dockerfile | |
push: true | |
tags: | | |
${{ format('{0}:{1}', steps.set_env.outputs.gcr_image, github.sha) }} | |
${{ format('{0}:{1}', steps.set_env.outputs.gcr_image, steps.set_env.outputs.branch) }} | |
${{ (steps.set_env.outputs.branch == 'production' && format('{0}:latest', steps.set_env.outputs.gcr_image)) || '' }} | |
${{ format('{0}:{1}', steps.set_env.outputs.image, github.sha) }} | |
${{ format('{0}:{1}', steps.set_env.outputs.image, steps.set_env.outputs.branch) }} | |
${{ (steps.set_env.outputs.branch == 'production' && format('{0}:latest', steps.set_env.outputs.image)) || '' }} | |
# Migrate & Deploy | |
- name: Set up Ruby | |
uses: ruby/setup-ruby@v1 | |
with: | |
ruby-version: .tool-versions | |
bundler-cache: true | |
- name: Install dependencies | |
run: bundle install | |
- name: Run Migrate & Deploy | |
env: | |
CLOVER_DATABASE_URL: ${{ ( steps.set_env.outputs.branch == 'production' && secrets.DATABASE_URL_PRODUCTION) || secrets.DATABASE_URL_STAGING }} | |
IMAGE_URL: ${{ format('{0}:{1}', steps.set_env.outputs.gcr_image, github.sha) }} | |
SERVICE: ${{ ( steps.set_env.outputs.branch == 'production' && secrets.GCP_SERVICE_NAME_PRODUCTION) || secrets.GCP_SERVICE_NAME_STAGING }} | |
REGION: ${{ secrets.GCP_REGION }} | |
run: | | |
bundle exec rake prod_up && \ | |
gcloud run deploy $SERVICE --image $IMAGE_URL --region $REGION |