Skip to content

Commit

Permalink
Cloud run deploy job (#39)
Browse files Browse the repository at this point in the history
Refactor github action to build docker image and set it in cloud run
  • Loading branch information
var77 authored Apr 20, 2024
1 parent 85fae0f commit 769e549
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 46 deletions.
80 changes: 80 additions & 0 deletions .github/workflows/build-migrate-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
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
else
echo "branch=staging" >> $GITHUB_OUTPUT
fi
echo "image=${IMAGE}" >> $GITHUB_OUTPUT
env:
IMAGE: ${{ format('{0}-docker.pkg.dev/{1}/lanterndata/lantern-ubicloud', secrets.GCP_REGION, secrets.GCP_PROJECT_ID) }}

- 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: ${{ secrets.GCP_WORKLOAD_IDENTITY_PROVIDER }}
service_account: ${{ secrets.GCP_SERVICE_ACCOUNT }}
- name: "Set up Cloud SDK"
uses: "google-github-actions/setup-gcloud@v2"

# 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: 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.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.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
46 changes: 0 additions & 46 deletions .github/workflows/migrate-and-deploy.yml

This file was deleted.

0 comments on commit 769e549

Please sign in to comment.