google-app-engine - 我可以使用容器注册表触发器进行 GAE 灵活的 Node.js 部署吗?

标签 google-app-engine google-cloud-platform google-container-registry google-container-builder

我通过以下教程学习了如何使用 Container Registry 触发器进行 Google Cloud Functions 部署。

Automatic serverless deployments with Cloud Source Repositories and Container Builder

我有 Google App Engine 灵活应用程序。运行时是 Node.js。我想部署由 git Push 触发的应用程序。有什么好的引用资料吗?

我正在使用这些示例代码。手动部署工作正常。

* tree

.
├── app.js
├── app.yaml
└── package.json

* app.js

'use strict';
const express = require('express');
const app = express();
app.get('/', (req, res) => {
  res.status(200).send('Hello, world!').end();
});
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
  console.log(`App listening on port ${PORT}`);
console.log('Press Ctrl+C to quit.');
});

* app.yaml

runtime: nodejs
env: flex

* package.json

{
  "name": "appengine-hello-world",
  "description": "Simple Hello World Node.js sample for Google App Engine Flexible Environment.",
  "version": "0.0.1",
  "private": true,
  "license": "Apache-2.0",
  "author": "Google Inc.",
  "repository": {
    "type": "git",
    "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
  },
  "engines": {
    "node": ">=4.3.2"
  },
  "scripts": {
    "deploy": "gcloud app deploy",
    "start": "node app.js",
    "lint": "samples lint",
    "pretest": "npm run lint",
    "system-test": "samples test app",
    "test": "npm run system-test",
    "e2e-test": "samples test deploy"
  },
  "dependencies": {
    "express": "4.15.4"
  },
  "devDependencies": {
    "@google-cloud/nodejs-repo-tools": "1.4.17"
  },
  "cloud-repo-tools": {
    "test": {
      "app": {
        "msg": "Hello, world!"
      }
    },
    "requiresKeyFile": true,
    "requiresProjectId": true
  }
}

* deploy command

$ gcloud app deploy

更新1

我发现了类似的问题。

How to auto deploy google app engine flexible using Container Registry with Build Trigger

我添加了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']

但是,我遇到了一个错误。错误消息为“加载模板时出错:yaml:第 5 行:未找到预期的 key ”。我正在调查。


更新2

原因是 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']

我遇到了另一个错误。消息是“加载模板时出错:cloudbuild_go_proto.BuildStep 中的未知字段“images”


更新3

我注意到“images”缩进是错误的。

steps:
  ...
# Note that this build pushes this image.
images: ['gcr.io/$PROJECT_ID/app']

我遇到了新错误。

starting build "e3e00749-9c70-4ac7-a322-d096625b695a"

FETCHSOURCE
Initialized empty Git repository in /workspace/.git/
From https://source.developers.google.com/p/xxxx/r/bitbucket-zono-api-btc
* branch 0da6c8bf209c72b6406f3801f3eb66d346187f4e -> FETCH_HEAD
HEAD is now at 0da6c8b fix invalid yaml
BUILD
Starting Step #0
Step #0: Already have image (with digest): gcr.io/cloud-builders/docker
Step #0: unable to prepare context: unable to evaluate symlinks in Dockerfile path: lstat /workspace/Dockerfile: no such file or directory
Finished Step #0
ERROR
ERROR: build step 0 "gcr.io/cloud-builders/docker" failed: exit status 1

是的。我没有 Dockerfile,因为我使用 Google App Engine 灵活环境 Node.js 运行时。不需要 Docker。


更新4

我添加了 Dockerfile

FROM gcr.io/google-appengine/nodejs

然后出现了新的错误。

Step #2: ERROR: (gcloud.app.deploy) User [<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4a323232323232320a2926253f2e283f23262e642d392f383c23292f2b2929253f243e64292527" rel="noreferrer noopener nofollow">[email protected]</a>] does not have permission to access app [xxxx] (or it may not exist): App Engine Admin API has not been used in project xxx before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/appengine.googleapis.com/overview?project=xxx then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.

更新5

我启用了 App Engine Admin API,然后出现了下一个错误。

Step #2: Do you want to continue (Y/n)? 
Step #2: WARNING: Unable to verify that the Appengine Flexible API is enabled for project [xxx]. You may not have permission to list enabled services on this project. If it is not enabled, this may cause problems in running your deployment. Please ask the project owner to ensure that the Appengine Flexible API has been enabled and that this account has permission to list enabled APIs.
Step #2: Beginning deployment of service [default]...
Step #2: WARNING: Deployment of service [default] will ignore the skip_files field in the configuration file, because the image has already been built.
Step #2: Updating service [default] (this may take several minutes)...
Step #2: ...............................................................................................................................failed.
Step #2: ERROR: (gcloud.app.deploy) Error Response: [9] 
Step #2: Application startup error:
Step #2: npm ERR! path /app/package.json
Step #2: npm ERR! code ENOENT
Step #2: npm ERR! errno -2
Step #2: npm ERR! syscall open
Step #2: npm ERR! enoent ENOENT: no such file or directory, open '/app/package.json'
Step #2: npm ERR! enoent This is related to npm not being able to find a file.

我更改了代码树,但它不起作用。我确认Appengine灵活API已启用。我不知道下一步应该尝试什么。

.
├── Dockerfile
├── app
│   ├── app.js
│   └── package.json
├── app.yaml
└── cloudbuild.yaml

更新6

当我手动部署时,工件如下所示。

us.gcr.io/xxxxx/appengine/default.20180316t000144

我应该使用这个神器吗...?我很困惑..


更新7

执行了两个构建。不知道这个说法是否正确。

enter image description here

最佳答案

您的 Dockerfile 不会将源代码复制到镜像。

您可以将所有内容移回同一目录,这样

.
├── app.js
├── app.yaml
├── cloudbuild.yaml
├── Dockerfile
└── package.json

但这并不重要。

将其粘贴到您的 Dockerfile 中,它应该可以工作:

FROM gcr.io/google-appengine/nodejs

# Working directory is where files are stored, npm is installed, and the application is launched
WORKDIR /app

# Copy application to the /app directory.
# Add only the package.json before running 'npm install' so 'npm install' is not run if there are only code changes, no package changes
COPY package.json /app/package.json
RUN npm install
COPY . /app

# Expose port so when container is launched you can curl/see it.
EXPOSE 8080

# The command to execute when Docker image launches.
CMD ["npm", "start"]

编辑:这是我使用的cloudbuild.yaml:

steps:
- name: gcr.io/cloud-builders/docker
  args: ['build', '-t', 'gcr.io/$PROJECT_ID/app', '.']
- name: gcr.io/cloud-builders/docker
  args: ['push', 'gcr.io/$PROJECT_ID/app']
- name: gcr.io/cloud-builders/gcloud
  args: ['app', 'deploy', 'app.yaml', '--image-url=gcr.io/$PROJECT_ID/app'] 
images: ['gcr.io/$PROJECT_ID/app']

关于google-app-engine - 我可以使用容器注册表触发器进行 GAE 灵活的 Node.js 部署吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49303285/

相关文章:

java - 如果 App Engine 自动将 stdout & stderr 记录到 INFO & WARNING,你为什么要使用 logging.properties?

google-app-engine - 何时选择 App Engine 而不是 Cloud Functions?

apache - 两种服务 HTTP 谷歌计算引擎

docker - 从 Docker 容器内部将镜像推送到 Google Container Registry

python - 我什么时候使用谷歌的 MapReduce

google-app-engine - c.Infof undefined (type context.Context has no field or method Infof) google.golang.org/appengine/log 错误

python - 获取 URL 时如何删除 Google App Engine 默认 header ?

ruby-on-rails - 访问部署在 Google Cloud Run 中的应用程序的 Rails 控制台

docker - 无法从 Google Compute Engine 访问 Google Container Registry

Docker 容器内的 Docker golang 客户端