2
votes

For Google App Engine, I want to inject env_variables into my app.yaml upon deploy. In the docs for gcloud app deploy, I see that there is the flag --appyaml that will:

Deploy with a specific app.yaml that will replace the one defined in the DEPLOYABLE.

https://cloud.google.com/sdk/gcloud/reference/app/deploy

So I have a Fabric script that reads my app.yaml, injects my extra env variables, writes this new app.yaml to a tempfile, and then runs gcloud app deploy. The command to gcloud ends up looking like this:

gcloud app deploy . --version dev --project myproject --appyaml='/var/folders/1z/qk45g9p934lg75byl8b74xyh0000gn/T/tmpgdjVUG.yaml'

The deploy succeeds and new code gets uploaded, however my new env variables don't seem to make it up.

If I provide a bogus value to --appyaml then it throws an error, so it does seem to use my input.

Any ideas what I could be doing wrong?

2
Can you intervene, manually copy the YAML file from /var/folders... into the project directory and try deploying without the --appyaml flag? This will confirm whether the YAML that is being generated works correctly. If that works, then the issue is in resolving the --appyaml flag. - DazWilkin
Yes, doing it that way works, and my env variables make it through - Alex
Good. OK. Next please keep the file in the app directory but rename it and reapply the --appyaml flag using the in directory reference. It may be that the deployment is unable to pull YAML files from outside the deployment context. - DazWilkin
i called it newapp.yaml put it at the project root and ran the command gcloud app deploy . --version dev --project myproject --appyaml='newapp.yaml' and it didnt work. I then tried again, but i removed the original app.yaml from the folder and gcloud app deploy just hung - Alex

2 Answers

2
votes

I have tested it on standard AppEngine with small HelloWorld modification in node.JS like this:

'use strict';

const express = require('express');
const app = express();

app.get('/', (req, res) => {
    var my_respond = "variables: ";
    res.status(200).send(Object.entries(process.env)).end();
});

const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
  console.log(`App listening on port ${PORT}`);
  console.log('Press Ctrl+C to quit.');
});

module.exports = app;

The sample is showing environment variables as result.

Than I added simplest app.yaml:

runtime: nodejs10
env_variables:
  MY_VAR: "my value from app.yaml"

Than in other location I have added different yaml called app1.yaml:

runtime: nodejs12
env_variables:
  MY_VAR: "my value from app1.yaml"

So this is changing node version and value of MY_VAR.

According to my tests this seems to be working little bit differently than explained in the doc, at least I understand it differently.

It's possible to use --appyaml flag, but only when there is no app.yaml file in the directory. If you do not have it in app directory, than using the flag you can point to different yaml.

So when I was trying to deploy with gcloud app deploy --appyaml="/home/vitooh/app1.yaml" and app.yaml was in application directory, the app was deployed with it - so the flag does not do anything. However when I done it without app.yaml the flag works, variable value is changed.

Actually you can spot it just after submitting command where there is a summary shown, just before you confirm deployment, in descriptor value like this:

descriptor:      [/home/vitooh/app1.yaml]
source:          [/home/vitooh/appEngine/nodejs-docs-samples/appengine/hello-world/standard]
target project:  [xxxxx-test-01]
target service:  [default]
target version:  [20200819t094956]
target url:      [https://xxxxx-test-01.appspot.com]

Do you want to continue (Y/n)?
1
votes

The question is a blackbox without any details: "I have a fabfile and then things happen but they happen wrong". However here's a tip how to do this a bit simpler without messing with app.yaml. app.yaml can contain a directive called includes, which can reference another yaml file, where you can have the environment variables, so you dont have to touch the original file.

app.yaml:

(...)

includes:
  - my_vars.yaml

my_vars.yaml:

env_variables:
    MY_VAR: value