简体   繁体   中英

Can I use Container Registry trigger for GAE flexible Node.js deploy?

I learned how to use Container Registry trigger for Google Cloud Functions deploy from the following tutorial.

Automatic serverless deployments with Cloud Source Repositories and Container Builder

I have Google App engine flexible app. The runtime is Node.js. I want to deploy the app triggered by git push. Are there any good references?

I'm using these example code. Manual deployment works normally.

* 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

Update 1

I found a similar question.

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

I added 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']

However, I got an error. The error message is " error loading template: yaml: line 5: did not find expected key ". I'm looking into it.


Update 2

The reason was invalid yaml format. I changed it like the following.

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

I got another error. The message is " error loading template: unknown field "images" in cloudbuild_go_proto.BuildStep "


Update 3

I noticed that "images" indent was wrong.

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

I encountered new error.

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

Yes. I don't have Dockerfile because I use Google App Engine flexible Environment Node.js runtime. It is not necessary Docker.


Update 4

I added Dockerfile

FROM gcr.io/google-appengine/nodejs

Then new error was occurred.

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.

Update 5

I enabled App Engine Admin API then next error has come.

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.

I changed my code tree but it did not work. I confirmed that Appengine Flexible API has been enabled. I have no idea what should I try next.

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

Update 6

When I deploy manually, the artifact is like the following.

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

Should I use this artifact...? I'm confused..


Update 7

Two builds are executed. I don't know whether this is correct.

在此处输入图片说明

Your Dockerfile doesn't copy source to the image.

You can move everything back to the same directory such that

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

but it doesn't matter.

Paste this into your Dockerfile and it should work:

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"]

Edit: This is the cloudbuild.yaml I used:

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

A tech guy helped me. I changed directory structure and cloudbuild.yaml. Then it worked. Thanks.

* 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']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM