app.yaml 中的 Gcloud 构建触发器环境变量替换 for appEngine

Gcloud build trigger environment variable substitution in app.yaml for appEngine

我正在尝试用云构建触发器替换 app.yaml 中的变量。

我在构建触发器中添加了替换变量。

将环境变量添加到 app.yaml 中,以一种可以轻松替换为构建触发变量的方式。像这样:

env_variables:
 SECRET_KEY: %SECRET_KEY%

在 cloudbuild.yaml 中添加一个步骤,用构建触发器中的值替换 app.yaml 中的所有 %XXX% 变量。

    steps:
    - name: node:10.15.1
       entrypoint: npm
       args: ["install"]
    - name: 'gcr.io/cloud-builders/gcloud'
       entrypoint: bash
       args:
         - '-c'
         - |
           sed -i 's/%SESSION_SECRET%/'${_SESSION_SECRET}'/g' app.yaml
    timeout: "1600s"

问题是 Gcloud Build 抛出异常:

Already have image (with digest): gcr.io/cloud-builders/gcloud
bash: _L/g: No such file or directory

为什么?我怎样才能替换我的 app.yaml ?

我在cloudbuild.yaml

同级项目的根目录有一个app.yaml

已更新

我正在尝试使用此命令在本地构建和调试 gcloud:

sudo cloud-build-local --config=cloudbuild.yaml --write-workspace=../workspace --dryrun=false --substitutions=_SESSION_SECRET=test --push .

当我查看 app.yaml 文件时,替换按预期工作并且没有任何异常。

gcloud 构建环境有什么区别?

入口点应该是可执行文件,使用/bin/bash/bin/sh

如何检查图像内部(一般):

$ docker pull gcr.io/cloud-builders/gcloud
Using default tag: latest
latest: Pulling from cloud-builders/gcloud
...
$ docker images
REPOSITORY                     TAG       IMAGE ID       CREATED             SIZE
gcr.io/cloud-builders/gcloud   latest    8499764c4ef6   About an hour ago   4.01GB
$ docker run -ti --entrypoint '/bin/bash' 8499764c4ef6
root@60354dfb588a:/#

您可以从那里测试您的命令,而不必每次都将其发送到 Cloud Build。

好的,我最终决定使用 git 集线器操作而不是 google 云触发器。

因为 Google 云触发器无法找到自己的 app.yaml 并自行管理该死的环境变量。

操作方法如下:

我的环境: 应用引擎, 标准(非弹性), Nodejs Express 应用程序, 一个 PostgreSQL CloudSql

首先设置:

1. Create a new Google Cloud Project (or select an existing project).

2. Initialize your App Engine app with your project.

[Create a Google Cloud service account][sa] or select an existing one.

3. Add the the following Cloud IAM roles to your service account:

    App Engine Admin - allows for the creation of new App Engine apps

    Service Account User - required to deploy to App Engine as service account

    Storage Admin - allows upload of source code

    Cloud Build Editor - allows building of source code

[Download a JSON service account key][create-key] for the service account.

4. Add the following [secrets to your repository's secrets][gh-secret]:

    GCP_PROJECT: Google Cloud project ID

    GCP_SA_KEY: the downloaded service account key

app.yaml

runtime: nodejs14
env: standard
env_variables:
  SESSION_SECRET: $SESSION_SECRET
beta_settings:
  cloud_sql_instances: SQL_INSTANCE

然后 github 操作

name: Build and Deploy to GKE

on: push

env:
  PROJECT_ID: ${{ secrets.GKE_PROJECT }}
  DATABASE_URL: ${{ secrets.DATABASE_URL}}
jobs:
  setup-build-publish-deploy:
    name: Setup, Build, Publish, and Deploy
    runs-on: ubuntu-latest

steps:
 - uses: actions/checkout@v2
 - uses: actions/setup-node@v2
   with:
    node-version: '12'
 - run: npm install
 - uses: actions/checkout@v1
 - uses: ikuanyshbekov/app-yaml-env-compiler@v1.0
   env:
    SESSION_SECRET: ${{ secrets.SESSION_SECRET }}  
 - shell: bash
   run: |
        sed -i 's/SQL_INSTANCE/'${{secrets.DATABASE_URL}}'/g' app.yaml
 - uses: actions-hub/gcloud@master
   env:
    PROJECT_ID: ${{ secrets.GKE_PROJECT }}
    APPLICATION_CREDENTIALS: ${{ secrets.GCLOUD_AUTH }}
    CLOUDSDK_CORE_DISABLE_PROMPTS: 1
   with:
    args: app deploy app.yaml

要将机密添加到 git 集线器操作中,您必须转到:Settings/secrets

请注意,我可以使用 bash 脚本处理所有替换。所以我不会依赖 github 项目“ikuanyshbekov/app-yaml-env-compiler@v1.0”

遗憾的是,GAE 没有提供最简单的方法来处理 app.yaml 的环境变量。我不想使用 KMS,因为我需要更新 beta-settings/cloud sql 实例。我真的需要将所有内容替换为 app.yaml.

这样我就可以针对正确的环境执行特定操作并管理机密。