我可以使用Container Registry触发器来进行GAE灵活的Node.js部署吗?

时间:2018-03-15 15:23:56

标签: 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引擎灵活的应用。运行时是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行:找不到预期的密钥"。我正在研究它。

更新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 "

更新3

我注意到"图像"缩进是错误的。

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灵活的Environment Node.js运行时。这不是Docker。

更新4

我添加了Dockerfile

FROM gcr.io/google-appengine/nodejs

然后发生了新的错误。

Step #2: ERROR: (gcloud.app.deploy) User [xxxxxxx@cloudbuild.gserviceaccount.com] 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 Flexible API。我不知道接下来应该尝试什么。

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

更新6

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

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

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

更新7

执行两个构建。我不知道这是否正确。

enter image description here

2 个答案:

答案 0 :(得分:4)

您的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']

答案 1 :(得分:1)

一个技术人员帮助了我。我更改了目录结构和cloudbuild.yaml。然后它奏效了。感谢。

* Code Tree

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

* cloudbuild.yaml

steps:
- name: gcr.io/cloud-builders/npm
  args: ['install', 'app']
- name: 'gcr.io/cloud-builders/gcloud'
  args: ['app', 'deploy', 'app/app.yaml']