如何使用带有构建触发器的容器注册表灵活地自动部署谷歌应用引擎

Posted

技术标签:

【中文标题】如何使用带有构建触发器的容器注册表灵活地自动部署谷歌应用引擎【英文标题】:How to auto deploy google app engine flexible using Container Registry with Build Trigger 【发布时间】:2018-01-05 11:49:55 【问题描述】:

我使用 GitHub 来保存我的项目。使用 php Storm 我将我的脚本推送到 GitHub 存储库。

我正在使用 Google App Engine 柔性环境应用程序。每当我推送到我的 GitHub 存储库时。 最新的脚本会自动部署到 Google App Engine Flexible Environment PHP 项目中。所以,我在 GCP Container Registry 中创建了构建触发器。

这是我的带有 Docker 文件的 PHP 示例项目

web/index.php

<?php

Echo 'this is sample project';

?>

App.yaml

runtime: php
env: flex

runtime_config:
 document_root: web

manual_scaling:
 instances: 1

handlers:

- url: .*
 script: index.php

Composer.json


   "require": 
       "php": "7.1.*"
   

Dockerfile

# Docker: Google App Engine

FROM gcr.io/google-appengine/php

使用 Google Cloud Platform Container Registry 构建触发器。创建了一个构建触发器。我得到了以下日志

构建记录器

starting build "4d51b494-5f7d-48e5-a2a2-6d7d6a9318a9"

FETCHSOURCE
Initialized empty Git repository in /workspace/.git/
From https://source.developers.google.com/p/project-id/r/github-test
* branch 5f9ac268123a3cfef5876034f2933eb83b34cd6d -> FETCH_HEAD
HEAD is now at 5f9ac26 v 1
BUILD
Already have image (with digest): gcr.io/cloud-builders/docker
Sending build context to Docker daemon 87.04kB

Step 1/1 : FROM gcr.io/google-appengine/php
latest: Pulling from google-appengine/php
Digest: sha256:578584d281828af48df03b85dcdcfd5792e8eb2916567497b56468b
Status: Downloaded newer image for gcr.io/google-appengine/php:latest
[91m# Executing 3 build triggers...
[0mStep 1/1 : COPY . $APP_DIR
Step 1/1 : RUN chown -R www-data.www-data $APP_DIR
---> Running in c9d98191fa2b
Step 1/1 : RUN /build-scripts/composer.sh
---> Running in ec2b02e5f0d1
[91m+ DEFAULT_PHP_VERSION=7.1
+ '[' -f /app/composer.json ']'
[0m[91m+ '[' -n '' ']'
+ CMD='php /build-scripts/detect_php_version.php /app/composer.json'
[0m[91m++ su www-data -c 'php /build-scripts/detect_php_version.php /app/composer.json'
[0mPHP_VERSION: 7.1
[91m+ PHP_VERSION=7.1
+ echo 'PHP_VERSION: 7.1'
+ '[' 7.1 == exact ']'
+ '[' 7.1 '!=' 5.6 ']'
+ '[' 7.1 '!=' 7.0 ']'
+ '[' 7.1 '!=' 7.1 ']'
+ '[' 7.1 == 5.6 ']'
+ '[' 7.1 == 7.0 ']'
+ COMPOSER_GITHUB_OAUTH_TOKEN=
+ [[ -n '' ]]
+ unset COMPOSER_GITHUB_OAUTH_TOKEN
+ rm -rf /app/vendor
[0m[91m+ php -d auto_prepend_file= /build-scripts/install_extensions.php /app/composer.json /opt/php/lib/conf.d/extensions.ini 7.1
[0m[91m+ '[' -n '' ']'
+ NOSCRIPT=--no-scripts
+ cd /app
+ su -m www-data -c 'php -d auto_prepend_file='\'''\'' /usr/local/bin/composer install --no-scripts --no-dev --prefer-dist --optimize-autoloader --no-interaction --no-ansi --no-progress'
[0m[91mLoading composer repositories with package information
[0m[91mUpdating dependencies
[0m[91mNothing to install or update
[0m[91mWriting lock file
Generating optimized autoload files
[0m ---> 1c5b266fe5ef
Removing intermediate container 473f7595359f
Removing intermediate container c9d98191fa2b
Removing intermediate container ec2b02e5f0d1
Successfully built 1c5b266fe5ef
Successfully tagged gcr.io/project-id/github-test:5f9ac268123a3cfef5876034f2933eb83b34cd6d
PUSH
Pushing gcr.io/project-id/github-test:5f9ac268123a3cfef5876034f2933eb83b34cd6d
The push refers to a repository [gcr.io/project-id/github-test]
09e0b63e8b63: Preparing
9ab9de3bae7c: Preparing
7ca730235cb1: Preparing
bca74f739283: Preparing
83430675961e: Preparing
c4b086b9d099: Preparing
acf227e1b2a5: Preparing
40de2f738e9e: Preparing
0ef981ddb200: Preparing
73acc0dfea4b: Preparing
825db9ad6441: Preparing
23088c61aa93: Preparing
122586f0e47f: Preparing
357a1f7d4b42: Preparing
d7fe08acdf1d: Preparing
cd1b43d6d268: Preparing
23b7338cf1fe: Preparing
9301d4a8f319: Preparing
2126b271c428: Preparing
c4b086b9d099: Waiting
acf227e1b2a5: Waiting
40de2f738e9e: Waiting
0ef981ddb200: Waiting
73acc0dfea4b: Waiting
825db9ad6441: Waiting
23088c61aa93: Waiting
122586f0e47f: Waiting
357a1f7d4b42: Waiting
d7fe08acdf1d: Waiting
cd1b43d6d268: Waiting
23b7338cf1fe: Waiting
9301d4a8f319: Waiting
2126b271c428: Waiting
83430675961e: Layer already exists
bca74f739283: Layer already exists
c4b086b9d099: Layer already exists
40de2f738e9e: Layer already exists
acf227e1b2a5: Layer already exists
0ef981ddb200: Layer already exists
73acc0dfea4b: Layer already exists
825db9ad6441: Layer already exists
122586f0e47f: Layer already exists
23088c61aa93: Layer already exists
357a1f7d4b42: Layer already exists
d7fe08acdf1d: Layer already exists
cd1b43d6d268: Layer already exists
23b7338cf1fe: Layer already exists
9301d4a8f319: Layer already exists
2126b271c428: Layer already exists
09e0b63e8b63: Pushed
7ca730235cb1: Pushed
9ab9de3bae7c: Pushed
5f9ac268123a3cfef5876034f2933eb83b34cd6d: digest: sha256:c604792b8b384e1186b55d11a1d04e8e06051e3f3083c25bd13668dcf size: 4285
DONE

我想将容器映像部署到 GAE FE,所以,我像这样更改了我的 docker 文件

Dockerfile

 # Docker: Google App Engine
   FROM gcr.io/google-appengine/php
   gcloud app deploy -image-url=gcr.io/project-id/github-test

我遇到了这个问题

starting build "43508598-4542-4e1c-a9e7-d29343878202"

FETCHSOURCE
Initialized empty Git repository in /workspace/.git/
From https://source.developers.google.com/p/project-id/r/github-test
* branch ea7511b88ad5c59736037c37685ea699b5c0 -> FETCH_HEAD
HEAD is now at ea7511b v 1
BUILD
Already have image (with digest): gcr.io/cloud-builders/docker
Sending build context to Docker daemon 87.04kB

Error response from daemon: Dockerfile parse error line 7: Unknown instruction: GCLOUD
ERROR
ERROR: build step "gcr.io/cloud-builders/docker@sha256:59e98ecdfe9229f080085953611ffb708a0df5b7f37a253ec" failed: exit status 1

我不知道在哪里使用 gcloud app deploy -image url。

帮助我如何使用 git 和构建触发器在 Container Registry 中 gcloud 应用部署映像 url

【问题讨论】:

【参考方案1】:

尝试使用类似以下内容的cloudbuild.yaml 文件:

steps:
# Build the Docker image.
- name: gcr.io/cloud-builders/docker
  args: ['build', '-t', 'gcr.io/$PROJECT_ID/app', '.']
# Push it to GCR.
- name: gcr.io/cloud-builders/docker
  args: ['push', 'gcr.io/$PROJECT_ID/app']
# Deploy your Flex app from the image in GCR.
- name: gcr.io/cloud-builders/gcloud
  args: ['app', 'deploy', 'app.yaml', '--image-url=gcr.io/$PROJECT_ID/app']
# Note that this build pushes this image.
images: ['gcr.io/$PROJECT_ID/app']

将此文件添加到您的存储库并配置一个构建触发器,当您推送到您的分支时执行该触发器。

【讨论】:

谢谢,Jason Hall,我按照您的回复尝试了 cloudbuild.yaml。但我收到此错误“已经有图像(带有摘要):gcr.io/cloud-builders/gcloud 错误:(gcloud.app.deploy)获取应用程序[apps / project-id]的权限错误。请确保您正在使用正确的项目 ID,并且您有权查看项目中的应用程序。”我启用了项目计费,并且 GAE ADMIN API 也启用了。我正在使用正确的项目 ID。请问我知道缺少什么吗? 哦,对了,我忘了说您需要为项目的构建器服务帐户授予应用部署权限,命名为 PROJECT-NUM@cloudbuild.gserviceaccount.com @Sattanathan 关于授予应用部署权限的文档可以在cloud.google.com/container-builder/docs/how-to/…找到 感谢 Jason hall 和 David Bendory - 从 git 自动部署 - 容器注册表到 GAE FE 成功。但是打开 URL 我得到 404 Not found nginx,请查看此构建日志详细信息 (docs.google.com/spreadsheets/d/…)。为什么我得到 404? 嗨,请任何人帮助我,为什么我在自动部署成功后找不到 404?

以上是关于如何使用带有构建触发器的容器注册表灵活地自动部署谷歌应用引擎的主要内容,如果未能解决你的问题,请参考以下文章

如何在谷歌云构建中访问 git 标签?

jenkins的触发远程构建时403或需要登录问题

谷歌云构建不使用环境变量替换 Firebase 令牌

如何使用谷歌云数据库部署 Laravel 8 谷歌云运行

谷歌云防火墙暴露端口 Docker

Docker