- Create a simple REST API with Python.
- Write a unit test for your code.
- Create a Dockerfile.
- Create a GitHub Action workflow file to deploy your code on Cloud Run.
- Make the code acessible for anyone
To make your life easier, export these environment variables so that you can copy and paste the commands used here. Choose whatever name you want, but the $PROJECT_ID has to be a unique name, because project IDs can't be reused in Google Cloud.
export PROJECT_ID=
export ACCOUNT_NAME=
For example, your commands should look something like this:
export PROJECT_ID=project-example
export ACCOUNT_NAME=account-example
Log in with your Google account:
gcloud auth login
Create a project and select that project:
gcloud projects create $PROJECT_ID
gcloud config set project $PROJECT_ID
Enable billing for your project, and create a billing profile if you don’t have one:
open "https://console.cloud.google.com/billing/linkedaccount?project=$PROJECT_ID"
Enable the necessary services:
gcloud services enable cloudbuild.googleapis.com run.googleapis.com containerregistry.googleapis.com
Create a service account:
gcloud iam service-accounts create $ACCOUNT_NAME \
--description="Cloud Run deploy account" \
--display-name="Cloud-Run-Deploy"
Give the service account Cloud Run Admin, Storage Admin, and Service Account User roles. You can’t set all of them at once, so you have to run separate commands:
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member=serviceAccount:$ACCOUNT_NAME@$PROJECT_ID.iam.gserviceaccount.com \
--role=roles/run.admin
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member=serviceAccount:$ACCOUNT_NAME@$PROJECT_ID.iam.gserviceaccount.com \
--role=roles/storage.admin
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member=serviceAccount:$ACCOUNT_NAME@$PROJECT_ID.iam.gserviceaccount.com \
--role=roles/iam.serviceAccountUser
Generate a key.json file with your credentials, so your GitHub workflow can authenticate with Google Cloud:
gcloud iam service-accounts keys create key.json \
--iam-account $ACCOUNT_NAME@$PROJECT_ID.iam.gserviceaccount.com