简体   繁体   English

如何将变量从 github 操作工作流传递到 GAE app.yaml 文件?

[英]How can I pass a variable from github actions workflow to a GAE app.yaml file?

I have a django project I want to put into maintenance mode before I update (migrate) the database.我有一个 django 项目,我想在更新(迁移)数据库之前将其置于维护模式。

So, my github workflow所以,我的 github 工作流程

  1. deploys my project with a variable MAINTENANCE_MODE set to true.使用设置为 true 的变量 MAINTENANCE_MODE 部署我的项目。 This new deploy I understand will reboot any running instances, ensuring all instances only show my 'Site down for maintenance' 503.html page and won't be interacting with the database.据我所知,这个新部署将重启任何正在运行的实例,确保所有实例只显示我的“站点停机维护”503.html 页面,并且不会与数据库交互。
  2. I launch a django VM in github actions, run my migrate, run collectstatic.我在 github 操作中启动了一个 django VM,运行我的迁移,运行 collectstatic。
  3. I set MAINTENANCE_MODE to false, I deploy a second time.我将 MAINTENANCE_MODE 设置为 false,然后进行第二次部署。 This will re-enable production server with new code that now accesses a migrated database.这将使用现在访问已迁移数据库的新代码重新启用生产服务器。

My question is, I am trying to use a single app.yaml file for both deploys.我的问题是,我正在尝试对两个部署使用一个 app.yaml 文件。 To pass the MAINTENANCE_MODE variable from github actions workflow to the app.yaml file, how can I do this?要将 MAINTENANCE_MODE 变量从 github 操作工作流传递到 app.yaml 文件,我该怎么做?

I know you can import secrets like so:我知道你可以像这样导入秘密:

runtime: python38
instance_class: F2
env_variables:
  DB_URL: $ {{ secrets.DB_URL }}

But I don't know how to modify a secret in the workflow.但我不知道如何修改工作流程中的秘密。 Perhaps its not a secret but some other type of variable one can set in the workflow and access in the app.yaml?也许这不是秘密,而是可以在工作流中设置并在应用程序中访问的其他类型的变量。yaml?

So it appears that Google App Engine's yaml files do not support dynamic environment variable substitution.因此,Google App Engine 的 yaml 文件似乎不支持动态环境变量替换。 Static substitution (like using github's secrets) works, because github compiles the file with the github environment variable before the workflow runs, but there's no clear way to modify a file with a variable that is going to change during a workflow. Static 替换(如使用 github 的秘密)有效,因为 github 使用 github 编译文件,在工作流运行之前更改工作流环境变量。

A method however that does work is to compile a new GAE yaml file during the workflow.然而,一种可行的方法是在工作流期间编译一个新的 GAE yaml 文件。 Here's what I came up with in the end...这是我最后想到的......

      - name: Put in Maintenance mode
        run: |
          MAINTENANCE_MODE=1 envsubst < app_eng_staging.yml.template  > app.yaml
          cat app.yaml
          gcloud app deploy --project staging-project --quiet
      - name: Collectstatic and migrate
        env:
          RUNNING_ENVIRONMENT: 'Staging_Server'
          DJANGO_DEBUG: 'False'
        run: |          
          pipenv run python manage.py collectstatic --noinput
          pipenv run python manage.py migrate
      - name: Turn off Maintenance and Deploy
        run: |
          MAINTENANCE_MODE=0 envsubst < app_eng_staging.yml.template  > app.yaml
          gcloud app deploy --project staging-project --quiet

The trick is to use the linux envsubst command.诀窍是使用 linux envsubst命令。 We start with a app_eng_staging.yml.template file, which looks like so:我们从一个 app_eng_staging.yml.template 文件开始,它看起来像这样:

runtime: python38
instance_class: F2
env_variables:
  RUNNING_ENVIRONMENT: 'Staging_Server'
  StagingServerDB: $ {{ secrets.STAGINGSERVER_DB }}
  MAINTENANCE_MODE: ${MAINTENANCE_MODE}
  FRONTEND_URL: $ {{ secrets.STAGING_FRONTEND_URL }}

envsubst then populates ${MAINTENANCE_MODE} with the value 1 and the result is saved to a new file app.yaml. envsubst 然后用值 1 填充 ${MAINTENANCE_MODE},并将结果保存到新文件 app.yaml。

After we finish working with out database migration, we can use envsubst to create a new app.yaml with MAINTENANCE_MODE set to zero (off), and re-deploy.完成数据库迁移后,我们可以使用 envsubst 创建一个新的 app.yaml 并将 MAINTENANCE_MODE 设置为零(关闭),然后重新部署。

Neat huh?整齐吧?

There is a feature in GitHub Action for this called " GAE environment variable compiler "在 GitHub Action 中有一个功能叫做“ GAE 环境变量编译器

Please read here请阅读这里

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

相关问题 如何使用 cloudbuild 将秘密管理器秘密传递给 app.yaml 中的应用引擎环境变量 - How Can I pass secret manager secret using cloudbuild to app engine environment variable in app.yaml 如何通过 app.yaml 将环境变量传递给 google app engine? - How can you pass environment variables to google app engine via app.yaml? 如何将生成的文件从 GitHub 操作工作流复制到云 Function? - How to copy a generated file from a GitHub Actions workflow to a Cloud Function? 使用 app.yaml 在 GAE 中安全存储环境变量 - Securely storing environment variables in GAE with app.yaml Workflow.yaml 文件用于EC2实例持续部署使用GitHub Actions - Workflow .yaml file for EC2 instance continuous deployment using GitHub Actions 无法使用非常简单的 app.yaml 设置 - Can't get a very simple app.yaml setup working 将app.yaml从go114更新到go115时如何处理`app_engine_apis`警告 - How to deal with `app_engine_apis` warning when updating app.yaml from go114 to go115 支持 app.yaml 中的负后视正则表达式 - Support for negative lookbehind regex in app.yaml 如何在 github 操作中连接到 docker 容器? - How can I connect to a docker container in github actions? 在 GitHub 操作中为容器提供与工作流相同的权限 - Giving container the same permissions with the workflow in GitHub actions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM