简体   繁体   English

Cloud Build 环境变量未传递到 GAE 上的 Django 应用程序

[英]Cloud Build env variables not passed to Django app on GAE

I have a Django app running on Google AppEngine Standard environment.我有一个在 Google AppEngine 标准环境上运行的 Django 应用程序。 I've set up a cloud build trigger from my master branch in Github to run the following steps:我已经从 Github 中的主分支设置了云构建触发器,以运行以下步骤:

steps:
  - name: 'python:3.7'
    entrypoint: python3
    args: ['-m', 'pip', 'install', '--target', '.', '--requirement', 'requirements.txt']
  - name: 'python:3.7'
    entrypoint: python3
    args: ['./manage.py', 'collectstatic', '--noinput']
  - name: 'gcr.io/cloud-builders/gcloud'
    args: ['app', 'deploy', 'app.yaml']
    env:
    - 'SHORT_SHA=$SHORT_SHA'
    - 'TAG_NAME=$TAG_NAME'

I can see under the Execution Details tab on Cloud Build that the variables were actually set.我可以在 Cloud Build 的 Execution Details 选项卡下看到实际设置了变量。

The problem is, SHORT_SHA and TAG_NAME aren't accessible from my Django app (followed instructions at https://cloud.google.com/cloud-build/docs/configuring-builds/substitute-variable-values#using_user-defined_substitutions ).问题是,我的 Django 应用程序无法访问SHORT_SHATAG_NAME (按照https://cloud.google.com/cloud-build/docs/configuring-builds/substitute-variable-values#using_user-defined_substitution的说明进行操作)。 But if I set them in my app.yaml file with hardcoded values under env_variables , then my Django app can access those hardcoded values (and the values set in my build don't overwrite those hardcoded in app.yaml).但是,如果我在 app.yaml 文件中使用env_variables下的硬编码值设置它们,那么我的 Django 应用程序可以访问这些硬编码值(并且在我的构建中设置的值不会覆盖那些在 app.yaml 中硬编码的值)。

Why is this?为什么是这样? Am I accessing them/setting them incorrectly?我是否访问它们/设置不正确? Should I be setting them in app.yaml somehow?我应该以某种方式在 app.yaml 中设置它们吗?

I even printed the whole os.environ dictionary in one of my views to see if they were just there with different names or something, but they're not present in there.我什至在我的一个视图中打印了整个os.environ字典,以查看它们是否只是用不同的名称或其他东西在那里,但它们不存在在那里。

Not the cleanest solution, but I used this medium post as a guidance to my solution.不是最干净的解决方案,但我使用这篇中等帖子作为我的解决方案的指导。 I hypothesize that runserver command isn't being passed those env variables, and that those variables are only used for the app deploy command.我假设runserver命令没有被传递那些 env 变量,并且这些变量仅用于app deploy命令。

  1. Write a Python script to dump the current environment variables in a .env file in project dir编写 Python 脚本以将当前环境变量转储到项目目录中的.env文件中
  2. In your settings file, read env variables from the.env file (I used django-environ library for this)在您的设置文件中,从 .env 文件中读取 env 变量(我为此使用了 django-environ 库)
  3. Add a step to cloud build file that runs your new Python script and pass env variables in that step (you're essentially dumping these variables into a .env file in this step)向云构建文件添加一个步骤,该步骤运行您的新 Python 脚本并在该步骤中传递 env 变量(在此步骤中,您实际上是将这些变量转储到.env文件中)
  - name: 'python:3.7'
    entrypoint: python3
    args: ['./create_env_file.py']
    env:
    - 'SHORT_SHA=$SHORT_SHA'
    - 'TAG_NAME=$TAG_NAME'
  1. Set the variables through Substitution Variables section in Edit Trigger page in Cloud Build通过 Cloud Build 的 Edit Trigger 页面中的 Substitution Variables 部分设置变量
  2. Now your application should have these env variables when app deploy happens现在,当app deploy发生时,您的应用程序应该具有这些环境变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM