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 publish
Before you can publish, you will need to set your environment variables. Open Google Cloud Shell and run the following command:
gcloud init
Next, 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.com
Create a secret with:
gcloud secrets create \
cannlytics_api_settings \
--replication-policy automatic
Allow Cloud Run to access this secret:
PROJECT_ID=cannlytics
PROJECTNUM=$(gcloud projects describe ${PROJECT_ID} --format 'value(projectNumber)')
CLOUDRUN=${PROJECTNUM}[email protected]
gcloud secrets add-iam-policy-binding \
cannlytics_api_settings \
--member serviceAccount:${CLOUDRUN} \
--role roles/secretmanager.secretAccessor
Then 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}[email protected]\" >> .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 .env
Update your IAM policy:
gcloud beta run services add-iam-policy-binding --region=us-central1 --member=allUsers --role=roles/run.invoker cannlytics
You can confirm that the secret was created or updated with:
gcloud secrets versions list cannlytics_api_settings
Helpful 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_ID
must 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 managed
You 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-tools
Afterwards, you can login to Firebase in the command line with:
firebase login
Then, you can deploy the site with:
firebase deploy
You 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!