问题描述
我正在尝试用云构建触发器替换 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 ?
我有一个 app.yaml 到项目的根目录,与 cloudbuild.yaml 处于同一级别
更新
我正在尝试使用以下命令在本地构建和调试 gcloud:
sudo cloud-build-local --config=cloudbuild.yaml --write-workspace=../workspace --dryrun=false --substitutions=_SESSION_SECRET=test --push .
当我查看 app.yaml 文件时,替换按预期工作,完全没有异常。
与 gcloud 构建环境有何不同?
解决方法
好吧,我最终决定使用 github action 而不是 google cloud triggers。
由于 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/[email protected]
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 hub 操作中,您必须转到:设置/秘密
请注意,我可以使用 bash 脚本处理所有替换。所以我不会依赖github项目“ikuanyshbekov/[email protected]”
很遗憾 GAE 没有提供一种最简单的方法来处理 app.yaml 的环境变量。我不想使用 KMS,因为我需要更新 beta-settings/cloud sql 实例。我真的需要将所有内容替换到 app.yaml 中。
通过这种方式,我可以针对正确的环境执行特定操作并管理机密。
,入口点应该是一个可执行文件,使用 /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。