Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass system environment variables to app.yaml?

Is it possible? Here is my app.yaml:

runtime: nodejs8
env_variables:
  NODE_ENV: production
  PORT: 8080
  API_KEY: ${API_KEY}

${API_KEY} is like a placeholder.

When I run API_KEY=xdfj212c gcloud app deploy app.yaml command, I want to pass API_KEY=xdfj212c to app.yaml and replace the placeholder with xdfj212c.

Expect result:

runtime: nodejs8
env_variables:
  NODE_ENV: production
  PORT: 8080
  API_KEY: xdfj212c

Or, After I run

  1. export API_KEY=xdfj212c

  2. gcloud app deploy

I want the same behavior.

Is this make sense for google app engine deployment workflow?

like image 218
slideshowp2 Avatar asked Jan 11 '19 02:01

slideshowp2


2 Answers

In app.yaml you can include another YAML config

includes:
- extra_env_vars.yaml

that you can create on the fly while inserting the environment variables

# Unix-like OS
export DB_PASSWORD=your_password
export DB_HOST=your_host
echo -e "env_variables:\n    DB_PASSWORD: $DB_PASSWORD\n    DB_HOST: $DB_HOST" > extra_env_vars.yaml

# Windows
set DB_PASSWORD=your_password
set DB_HOST=your_host
(echo env_variables: & echo.    DB_PASSWORD: %DB_PASSWORD% & echo.    DB_HOST: %DB_HOST%) > extra_env_vars.yaml

The resulting extra_env_vars.yaml looks like this:

env_variables: 
    DB_PASSWORD: your_password 
    DB_HOST: your_host

Finally, ignore extra_env_vars.yaml in your version control system.

like image 107
tuomastik Avatar answered Oct 20 '22 08:10

tuomastik


You could always use sed:

$ sed -i 's/${API_KEY}/xdfj212c/g' app.yaml && gcloud app deploy

The 'bad' thing is that this stores the key back, but you can always append a new sed command to replace the key again with the placeholder, or use your VCS mechanism to just reset the change the file.

Another option is saving your app.yaml file as something like app_template.yaml and do this for your deployments:

$ sed 's/${API_KEY}/xdfj212c/g' app_template.yaml | tee app.yaml; gcloud app deploy

This will do the replacement in a new file, app.yaml, and then do the deployment.

like image 1
Mangu Avatar answered Oct 20 '22 08:10

Mangu