Last active
January 30, 2023 19:16
-
-
Save JeremyBernier/801e43f9341098e9763afd4faf5f067a to your computer and use it in GitHub Desktop.
Deploy to Google Cloud Run with Environment Variables
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
/** | |
* Update: I just use this script to generate the terminal command, then I copy & paste | |
* that command into the terminal and run it manually. | |
* The reason I don't run the command from the script is that it wasn't properly erroring out | |
* from the script. You can uncomment the `exec(` function if you want to execute from | |
* this script though | |
* | |
* This script deploys to Google Cloud Run while also updating environment variables | |
* Note: This will fail if there are any double quotes (") in your environment variables | |
* Also this script won't delete environment variables that've already been pushed | |
* Documentation: https://cloud.google.com/functions/docs/configuring/env-var | |
* | |
* @author Jeremy Bernier (https://www.jbernier.com/) | |
*/ | |
const config = require("dotenv").config; | |
// const fs = require("fs"); | |
const path = require("path"); | |
const { exec } = require("child_process"); | |
// NOTE: THIS WILL FAIL if there is a quote " in the env file | |
// You can also use the secret manager for database credentials | |
const ENV_VARS_FILENAME = ".env.production"; | |
const CLOUD_PROJECT_NAME = "your-project-name"; | |
const VPC_CONNECTOR_NAME = "your-vpc-connector-name"; // optional | |
// const CLOUD_SQL_INSTANCE = 'cloud-sql-instance' | |
const DELIMITER = "{{TEMP}}"; | |
const CMD_FLAGS = [ | |
"--vpc-connector", | |
VPC_CONNECTOR_NAME, | |
// `--add-cloudsql-instances=${CLOUD_SQL_INSTANCE}` | |
].join(" "); | |
const envVarsMap = config({ | |
path: path.resolve(process.cwd(), ENV_VARS_FILENAME), | |
debug: true, | |
}).parsed; | |
const envVarsStr = `--set-env-vars "^${DELIMITER}^${Object.keys(envVarsMap) | |
.map((key) => `${key}=${envVarsMap[key]}`) | |
.join(DELIMITER)}"`; | |
// const envProduction = fs.readFileSync( | |
// path.resolve(process.cwd(), ENV_VARS_FILENAME), | |
// "utf8" | |
// ); | |
const cmd = `gcloud run deploy ${CLOUD_PROJECT_NAME} --source . ${CMD_FLAGS} ${envVarsStr}`; | |
console.log(cmd); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment