Skip to content

Commit

Permalink
Deployment automation for branches (#837)
Browse files Browse the repository at this point in the history
On every push on PR the application is deployed. When the PR is closed the app is undeployed.
  • Loading branch information
mkuthan authored Feb 18, 2022
1 parent 4dc8ebf commit e9b1123
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 3 deletions.
70 changes: 70 additions & 0 deletions .github/workflows/deploy-branch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: Deploy Branch

on:
pull_request:
types: [ opened, synchronize, reopened ]

jobs:
deploy:

runs-on: ubuntu-latest

env:
IMAGE_NAME: eu.gcr.io/${{ secrets.GCP_PROJECT_ID }}/turnilo
REF_NAME: ${{ github.head_ref }}

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Authenticate on GCP
uses: google-github-actions/setup-gcloud@master
with:
project_id: ${{ secrets.GCP_PROJECT_ID }}
service_account_key: ${{ secrets.GCP_SA_KEY }}
export_default_credentials: true

- name: Configure Docker
run: gcloud auth configure-docker --quiet

- name: Build Docker image
run: docker build . -t $IMAGE_NAME:$REF_NAME

- name: Push Docker image
run: docker push $IMAGE_NAME:$REF_NAME

- name: Deploy app
run: |
gcloud run deploy turnilo-${REF_NAME//[^a-z0-9]/-} \
--image $IMAGE_NAME:$REF_NAME \
--region europe-west1 \
--platform managed \
--allow-unauthenticated \
--quiet \
--port 9090 \
--cpu 1 --memory 1G --max-instances 1 --concurrency 80 \
--args="--examples"
- name: Get app URL
id: app-url
run: |
echo ::set-output name=app_url::$(gcloud run services describe turnilo-${REF_NAME//[^a-z0-9]/-} --region europe-west1 --format 'value(status.url)')
- name: Print app URL
uses: actions/github-script@v6
env:
APP_URL: ${{ steps.app-url.outputs.app_url }}
with:
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: ':white_check_mark: Deployed successfully to: ' + process.env.APP_URL
})
- name: Delete previous Docker image(s)
run: |
gcloud container images list-tags $IMAGE_NAME:$REF_NAME --filter='-tags:*' --format='get(digest)' --limit=unlimited | \
xargs -I {digest} gcloud container images delete "$IMAGE_NAME@{digest}" --quiet
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Deploy
name: Deploy Master

on:
push:
Expand Down Expand Up @@ -33,7 +33,7 @@ jobs:
- name: Push Docker image
run: docker push $IMAGE_NAME:latest

- name: Deploy on GCP
- name: Deploy app
run: |
gcloud run deploy turnilo \
--image $IMAGE_NAME:latest \
Expand All @@ -45,7 +45,7 @@ jobs:
--cpu 1 --memory 1G --max-instances 1 --concurrency 80 \
--args="--examples"
- name: Delete previous Docker image
- name: Delete previous Docker image(s)
run: |
gcloud container images list-tags $IMAGE_NAME --filter='-tags:*' --format='get(digest)' --limit=unlimited | \
xargs -I {digest} gcloud container images delete "$IMAGE_NAME@{digest}" --quiet
35 changes: 35 additions & 0 deletions .github/workflows/undeploy-branch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Undeploy Branch

on:
pull_request:
types: [closed]

jobs:
undeploy:

runs-on: ubuntu-latest

env:
IMAGE_NAME: eu.gcr.io/${{ secrets.GCP_PROJECT_ID }}/turnilo
REF_NAME: ${{ github.head_ref }}

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Authenticate on GCP
uses: google-github-actions/setup-gcloud@master
with:
project_id: ${{ secrets.GCP_PROJECT_ID }}
service_account_key: ${{ secrets.GCP_SA_KEY }}
export_default_credentials: true

- name: Undeploy app
run: |
gcloud run services delete turnilo-${REF_NAME//[^a-z0-9]/-} \
--region europe-west1 \
--quiet
- name: Delete Docker image
run: |
gcloud container images delete $IMAGE_NAME:$REF_NAME --quiet

0 comments on commit e9b1123

Please sign in to comment.