如何使用Google Cloud Build将Spring Boot项目部署到Google App Engine?

时间:2018-08-03 19:43:43

标签: google-app-engine spring-boot google-cloud-build

我正在使用maven和google cloud build构建我的spring boot应用程序,但是无论是使用setlocal set val=%1 if %val% LSS 0 ( set "sign=-" & set "val=%val:~1%" ) :loop if %val% GTR 999 (set newval=,%val:~-3%%newval% & set val=%val:~0,-3% goto loop ) echo %sign%%val:~-3%%newval% 在本地运行还是使用Cloud Build部署,我都能以某种方式获得不同的部署结果。

如果我使用mvn appengine:run在本地运行,则可以按预期访问控制器。使用Cloud Build,我遇到了mvn appengine:run错误。

我的cloudbuild.yaml是以下内容:

404

您如何建议配置云构建,以便在Google App Engine上构建和部署Spring Boot应用程序?


在进一步挖掘之后,该问题似乎与返回的某种错误有关:

steps:
- name: 'gcr.io/cloud-builders/mvn'
  args: ['package']
- name: 'gcr.io/cloud-builders/gcloud'
  args: ['app', 'deploy', 'target/myapp/WEB-INF/appengine-web.xml']

使用javax.servlet.ServletContext log: 2 Spring WebApplicationInitializers detected on classpath 从本地计算机进行部署时,在堆栈跟踪中未收到此消息

我的问题仍然存在,如何创建可以调用mvn appengine:deploy的{​​{1}}?

1 个答案:

答案 0 :(得分:4)

为了构建一个春季启动项目,并使用Google Cloud Build将其部署到google appengine。我最终不得不首先使用下图构建“构建器”图像,并在执行实际的应用程序构建时引用该图像。

Dockerfile

FROM debian:stretch
#
# Google Cloud SDK installation
# https://cloud.google.com/sdk/docs/quickstart-debian-ubuntu
RUN apt-get update -y && \
  apt-get install \
    apt-utils \
    dialog \
    gnupg \
    lsb-release \
    curl -y && \ 
  export CLOUD_SDK_REPO="cloud-sdk-$(lsb_release -c -s)" && \
  echo "deb http://packages.cloud.google.com/apt $CLOUD_SDK_REPO main" | tee -a /etc/apt/sources.list.d/google-cloud-sdk.list && \
  curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - && \
  apt-get update -y && \
  apt-get install google-cloud-sdk -y
# Install all available components
RUN apt-get install google-cloud-sdk \
  google-cloud-sdk \
  google-cloud-sdk-app-engine-go \
  google-cloud-sdk-app-engine-java \
  google-cloud-sdk-app-engine-python \
  google-cloud-sdk-app-engine-python-extras \
  google-cloud-sdk-bigtable-emulator \
  google-cloud-sdk-cbt \
  google-cloud-sdk-datastore-emulator \
  google-cloud-sdk-cloud-build-local \
  google-cloud-sdk-datalab \
  kubectl \
  google-cloud-sdk-pubsub-emulator -y
#
# OpenJDK installation
# https://linuxhint.com/install-openjdk-8-on-debian-9-stretch/
RUN apt-get install openjdk-8-jdk -y

#
# MAVEN installation
# https://github.com/carlossg/docker-maven/blob/f581ea002e5d067deb6213c00a4d217297cad469/jdk-8/Dockerfile
ARG MAVEN_VERSION=3.5.4
ARG USER_HOME_DIR="/root"
ARG SHA=ce50b1c91364cb77efe3776f756a6d92b76d9038b0a0782f7d53acf1e997a14d
ARG BASE_URL=https://apache.osuosl.org/maven/maven-3/${MAVEN_VERSION}/binaries

RUN mkdir -p /usr/share/maven /usr/share/maven/ref \
  && curl -fsSL -o /tmp/apache-maven.tar.gz ${BASE_URL}/apache-maven-${MAVEN_VERSION}-bin.tar.gz \
  && echo "${SHA}  /tmp/apache-maven.tar.gz" | sha256sum -c - \
  && tar -xzf /tmp/apache-maven.tar.gz -C /usr/share/maven --strip-components=1 \
  && rm -f /tmp/apache-maven.tar.gz \
  && ln -s /usr/share/maven/bin/mvn /usr/bin/mvn

ENV MAVEN_HOME /usr/share/maven
ENV MAVEN_CONFIG "$USER_HOME_DIR/.m2"

WORKDIR /workspace

cloudbuild.yaml

# In this directory, run the following command to build this builder.
# $ gcloud builds submit . --config=cloudbuild.yaml

steps:
- name: 'gcr.io/cloud-builders/docker'
  args: ['build', '--tag=gcr.io/$PROJECT_ID/gcloud-maven', '.']
# Simple sanity check: invoke java to confirm that it was installed correctly.
- name: 'gcr.io/$PROJECT_ID/gcloud-maven'
  args: ['java', '-version']
# Simple sanity check: invoke gcloud to confirm that it was installed correctly.
- name: 'gcr.io/$PROJECT_ID/gcloud-maven'
  args: ['gcloud', 'projects', 'list']
# Simple sanity check: invoke maven to confirm that it was installed correctly.
- name: 'gcr.io/$PROJECT_ID/gcloud-maven'
  args: ['mvn', '--version']

images: ['gcr.io/$PROJECT_ID/gcloud-maven']

timeout: 1200s

我的spring boot项目的cloudbuild.yaml现在引用此图像:

steps:
- name: 'gcr.io/$PROJECT_ID/gcloud-maven'
  args: ['mvn', 'appengine:deploy']

我将尝试将此docker映像放在dockerhub和github上,以供其他人查找。我还将感谢更熟悉docker和linux的人们,以帮助改善该映像以减小其大小。 (例如,使用Alpine代替Debian或Debian Stretch Slim)。同时,我希望这会对像我这样的人有所帮助。