12

I am currently migrating an application to Google App Engine that relies on environment variables for various external connections. It looks like the app.yaml file is where I should setup these configurations however how do I manage different environments (development, testing, staging, production) where these variables may be different?

Is the easiest solution just to create a branch for each of these environments with just a different app.yaml file?

5
  • 1
    Perhaps better to have different files, e.g. dev.yaml staging.yaml and so on. Then you can run the dev_server with those files explicitly and when you deploy it'll always use app.yaml. Commented Feb 2, 2015 at 17:31
  • @PaulCollingwood is there a command to run with a specific config file? I didn't see that in the documentation. Commented Feb 2, 2015 at 20:47
  • you can just say dev_appserver somefile.yaml and it'll use it. very handy. but only locally, not prod afaik. Commented Feb 2, 2015 at 20:48
  • Have you looked at cloud.google.com/appengine/docs/python/tools/appengineconfig ? You could switch on appid for different settings. Commented Feb 3, 2015 at 1:03
  • You can now do gcloud app deploy --appyaml=... Commented Feb 18, 2022 at 21:00

4 Answers 4

10

app.yaml can set environment variables for the rest of the application, but it has no way to check them and do different things depending on their incoming values. Thus, you do need to present different app.yaml files to whatever deployment procedure you're using.

As for the best way to prepare the right app.yaml as a preliminary step to GAE deployment, that's a subtler devops problem. Branches in your git or hg or whatever, as you mention, would work, but personally (maybe just bad luck?-) I've often found that the simpler my structure, the better, and branches intended to be long-lived (as opposed to temporary deviation intended to be soon merged back into the trunk) have given me the worse headaches.

So, were it up to me, I'd have a preapp.yaml template (maybe jinja2, whatever) with the needed if/else logic, and prepare the right app.yaml from it, as the very first step of any deployment, with a simple Python script.

Pretty much the kind of architecture used (for all kind of configuration files, and thus with more inevitable complications) for the currently-beta gcloud preview deployment-manager, see https://cloud.google.com/deployment-manager/ , so of course I could be biased towards the approach (but as I mentioned my bias comes essentially from previous bad deployment experiences:-).

Sign up to request clarification or add additional context in comments.

1 Comment

I would like to understand a bit better how that could work in practice - maybe, since the app.yaml needs to be in the same folder as the service, multiple files could be generated (such as app_dev.yaml, app_testing.yaml), and then you would invoke gcloud app deploy which the respective file as argument? Thank you in advance.
1

I just came across this issue for the same reason. I ended up creating aliases for each dev, and prod:

alias dev-deploy='cat dev.yaml > app.yaml; gcloud app deploy dev.yaml; rm app.yaml' alias prod-deploy='cat prod.yaml > app.yaml; gcloud app deploy; rm app.yaml' 

Comments

0

I needed a solution for this that would work with a CD pipeline. I use Build Triggers on my git repositories. Using these, I can template the branch name I'm launching from into my cloudbuild.yaml file. My first build step is then to generate app.yaml from a shell script using the branch_name as a conditional value.

# cloudbuild.yaml - name: 'ubuntu' args: ['bash', 'app.yaml.sh', '$BRANCH_NAME'] 

Comments

0

If you run gcloud app deploy --help it says you can pass it a path to the .yaml that you'd like to use for deployment:

EXAMPLES To deploy a single service, run: $ gcloud app deploy ~/my_app/app.yaml 

So say you have a qa, uat, and prod environments that you want to deploy to. You could easily create files app.[env].yaml for each, and then just pass in the file path that you want to target based on some env variable or something in your CI/CD pipeline.

https://cloud.google.com/sdk/gcloud/reference/app/deploy#:~:text=POSITIONAL%20ARGUMENTS,in%20the%20DEPLOYABLE.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.