-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deployment automation for branches (#837)
On every push on PR the application is deployed. When the PR is closed the app is undeployed.
- Loading branch information
Showing
3 changed files
with
108 additions
and
3 deletions.
There are no files selected for viewing
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
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 |
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
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
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 |