Deploy to GCP Cloud Run using GitHub Actions Workflows.
No bs. TLDR included.
Lets get started. Requirements for today are:
GitHub repo page to your repo
your repo open in your preferred editor
a Dockerfile in your repo with build instructions
Note: Anything marked with “< >”(eg <value>) must be changed to reflect your repo/choice
Step 1: Create the workflow file
Head over to your repo in your editor
create a file under .github/workflows called <workflow name>.yml. the .github/workflows directory must be in the root of your project
example: .github/workflows/cloud_run.yml
Step 2: start editing your file
Step 3: paste the following code in your file
This code was created based on this page and the gcp auth workflow repo
on:
pull_request: #you can also setup your own triggers
branches:
- main
jobs:
gc-cloud_run:
name: 'cloud_run'
runs-on: ubuntu-latest
permissions:
contents: 'read'
deployments: 'write'
id-token: 'write'
steps:
- uses: 'actions/checkout@v3'
- id: 'auth'
name: 'Gcloud Auth'
uses: 'google-github-actions/auth@v1'
with:
credentials_json: '${{secrets.GCLOUD_AUTH}}'
#you can replace "us-central1" with your preferred region
- run: |-
gcloud run deploy <service name> --platform managed --source <path to deployment dir> --region us-central1;
Step 4: Publish your changes to GitHub
Step 5: IAM service account
head over to https://console.cloud.google.com/iam-admin/serviceaccounts
click create service account at the top of the page
add a service account name, let the account ID be generated automatically
press create and continue . Dont skip step the other steps
click the dropdown: select a role
find “Editor” under Quick Access > Basic > Editor
click continue
click done
Step 6: Service account credentials
head back to https://console.cloud.google.com/iam-admin/serviceaccounts
click on your service account
navigate to the KEYS tab
click “Add Key” > “Create new Key”
select JSON format and Create . a key file will be downloaded
go to your downloads and copy the contents of the key file
Step 7: create the `GCLOUD_AUTH` env
go to your github repo page
navigate to the “Settings” tab
on the sidebar, look under “Security” to find “Secrets and Variables” > “Actions”
click on “New repository secret”
name it “GCLOUD_AUTH”
paste in the contents of the key file
You're done! Now, if the correct conditions are met, the action will be triggered, deploying your app to cloud run!
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
TLDR
create a new workflow file with the contents of Step 3, replacing the required values
create a new GCP service account with ‘Editor’ permissions
get json credentials for service account
create an env GCLOUD_AUTH with the json credentials as contents