First, you will need to save your environment variables as a secret. Then, you may want to walk through the build process one time manually. Before publishing, change DEBUG=False to DEBUG=True in your .env file. Afterwards, publishing the website is done with one command:
npm run publishBefore you can publish, you will need to set your environment variables. Open Google Cloud Shell and run the following command:
gcloud initNext, enable the Cloud APIs that are used:
gcloud services enable \
run.googleapis.com \
sql-component.googleapis.com \
sqladmin.googleapis.com \
compute.googleapis.com \
cloudbuild.googleapis.com \
secretmanager.googleapis.comCreate a secret with:
gcloud secrets create \
cannlytics_api_settings \
--replication-policy automaticAllow Cloud Run to access this secret:
PROJECT_ID=cannlytics
PROJECTNUM=$(gcloud projects describe ${PROJECT_ID} --format 'value(projectNumber)')
CLOUDRUN=${PROJECTNUM}-compute@developer.gserviceaccount.com
gcloud secrets add-iam-policy-binding \
cannlytics_api_settings \
--member serviceAccount:${CLOUDRUN} \
--role roles/secretmanager.secretAccessorThen create your environment variables and save them to the secret:
REGION=us-central1
DJPASS="$(cat /dev/urandom | LC_ALL=C tr -dc 'a-zA-Z0-9' | fold -w 30 | head -n 1)"
echo DATABASE_URL=\"postgres://djuser:${DJPASS}@//cloudsql/${PROJECT_ID}:${REGION}:cannlytics-sql/cannlytics-sql-database\" > .env
echo GS_BUCKET_NAME=\"${GS_BUCKET_NAME}\" >> .env
echo GCS_SA=\"${PROJECT_NUM}-compute@developer.gserviceaccount.com\" >> .env
echo SECRET_KEY=\"$(cat /dev/urandom | LC_ALL=C tr -dc 'a-zA-Z0-9' | fold -w 50 | head -n 1)\" >> .env
echo DEBUG=\"False\" >> .env
gcloud secrets versions add cannlytics_api_settings --data-file .env
rm .envUpdate your IAM policy:
gcloud beta run services add-iam-policy-binding --region=us-central1 --member=allUsers --role=roles/run.invoker cannlyticsYou can confirm that the secret was created or updated with:
gcloud secrets versions list cannlytics_api_settingsHelpful resources:
The publishing process contains three steps:
You can build your container image using Cloud Build by running the following command from the directory containing the Dockerfile:
set PROJECT_ID=cannlytics
set APP_ID=cannlytics_api
gcloud builds submit --tag gcr.io/%PROJECT_ID%/%APP_ID%Note that your
APP_IDmust be in snake case.
This project uses a fully managed Cloud Run platform. Cloud Run for Anthos can be used as an alternative. Deploy the container to Cloud Run with:
set REGION=us-central1
gcloud run deploy %PROJECT_ID% --image gcr.io/%PROJECT_ID%/%APP_ID% --region %REGION% --allow-unauthenticated --platform managedYou can retrieve the service URL with:
gcloud run services describe cannlytics-website \
--platform managed \
--region $REGION \
--format "value(status.url)"This step provides access to this containerized app from a Firebase Hosting URL, so that the app can generate dynamic content for the Firebase-hosted site. You will need to have Firebase's command line tool installed:
npm install -g firebase-toolsAfterwards, you can login to Firebase in the command line with:
firebase loginThen, you can deploy the site with:
firebase deployYou can then view logs for your deployment in the Cloud run console.
You now have a simple, yet powerful, API running on Cloud Run, which will automatically scale to handle your API's traffic, optimizing CPU and memory so that your API runs with the smallest footprint possible, saving you money and letting society breathe easier. If you desire, then you can now seamlessly integrate services such as Cloud Storage into your Django API. You can now plug and play and tinker to your heart's content while your users enjoy your beautiful material!