简体   繁体   English

cloudbuild.yaml 为 gcloud 计算引擎上的 python flask 应用程序构建管道

[英]cloudbuild.yaml build pipeline for python flask app on gcloud compute engine

I have a Python Flask app which needs a library which is not preinstalled on Gcloud compute instances libsndfile1 — also why I can't run this through app engine.我有一个 Python Flask 应用程序,它需要一个未预安装在 Gcloud 计算实例libsndfile1上的库——这也是我无法通过应用程序引擎运行它的原因。 I have set up a cloud build pipeline so my cloudbuild.yaml triggers every time I push to master on my repository.我已经设置了一个云构建管道,所以每次我推送到我的存储库上的 master 时,我的cloudbuild.yaml触发。

Here are my 2 questions:这是我的2个问题:

  1. Where does the cloudbuild.yaml execute it's instructions? cloudbuild.yaml 在哪里执行它的指令?
  2. How do I make the cloudbuild.yaml execute apt-get install libsndfile1 python3 python3-pip and then also execute pip3 install requirements.txt where requirements.txt is a file in the repository that triggers the cloud build?如何让 cloudbuild.yaml 执行apt-get install libsndfile1 python3 python3-pip然后还执行pip3 install requirements.txt其中requirements.txt是存储库中触发云构建的文件?

Disclaimer: I am an ultra-beginner with docker but if you think that docker is the only way to do it then please explain how something like this can be done.免责声明:我是 docker 的超级初学者,但如果您认为 docker 是唯一的方法,那么请解释如何完成这样的事情。

If you are interested in App Engine, my recommendation would be to migrate your application to the Google App Engine Flexible environment, where you could choose a Custom runtime for your Flask application.如果您对 App Engine 感兴趣,我的建议是将您的应用程序迁移到 Google App Engine 柔性环境,您可以在其中为 Flask 应用程序选择自定义运行时 Simply include a step on your Dockerfile to install the required library.只需在 Dockerfile 上包含一个步骤即可安装所需的库。 It should look similar to this:它应该类似于:

FROM python:3.7
WORKDIR /app
COPY . /app
RUN apt-get update &&\
    apt-get install -y libsndfile1
RUN pip install -r requirements.txt
EXPOSE 8080
CMD ["gunicorn", "main:app", "-b", ":8080", "--timeout", "300"]

And within this context you could use Cloud Build to automate your deployments in a fairly easy manner by following the relevant section of the documentation .在这种情况下,您可以按照文档的相关部分,使用 Cloud Build 以相当简单的方式自动执行您的部署。

Now regarding your questions.现在关于你的问题。

(1) Where does the cloudbuild.yaml execute it's instructions? (1) cloudbuild.yaml 在哪里执行它的指令?

Here you can find all the relevant information of how Cloud Build works here and to answer your first question Cloud Build is a separate service and runs each step of the build in a Docker container within Google's Infrastructure.在这里,你可以找到的云构建如何工作的所有相关信息在这里并回答你的第一个问题,云计算打造的是一个单独的服务,并运行在一个多克容器谷歌的基础架构中构建的每个步骤。

(2) How do I make the cloudbuild.yaml execute apt-get install libsndfile1 python3 python3-pip and then also execute pip3 install requirements.txt where requirements.txt is a file in the repo that triggers the cloud build. (2) 如何让 cloudbuild.yaml 执行apt-get install libsndfile1 python3 python3-pip然后还执行pip3 install requirements.txt其中 requirements.txt 是 repo 中触发云构建的文件。

Please notice that Cloud Build is more oriented to be used with other products like Cloud Run, GKE, Cloud Functions, etc. And the only fairly straightforward that I've seen Cloud Build being used in the context of Compute Engine would be to build VM images using Packer .请注意,Cloud Build 更倾向于与 Cloud Run、GKE、Cloud Functions 等其他产品一起使用。我见过在 Compute Engine 上下文中使用 Cloud Build 的唯一相当简单的方法是构建 VM使用 Packer 的图像 In which case you'll need to create a custom image with all the dependencies already installed along within your application.在这种情况下,您需要使用已安装在应用程序中的所有依赖项创建自定义映像

The specifics of the solution will vary depending on the OS of your Compute Engine instance.解决方案的细节会因您的 Compute Engine 实例的操作系统而异。 But you'll basically need to do the following:但您基本上需要执行以下操作:

  1. Upload a startup script that installs libsndfile1 as well as all the dependencies specified on the requirements.txt file to a bucket in Cloud Storage.将用于安装libsndfile1以及requirements.txt文件中指定的所有依赖项的启动脚本上传到 Cloud Storage 中的存储libsndfile1
  2. Take advantage that you can use gcloud to apply a startup script to running instances in a similar fashion to.利用 gcloud 以类似的方式将启动脚本应用于运行实例
gcloud compute instances add-metadata example-instance \
    --metadata startup-script-url=gs://bucket/file

and there is cloud builder for gcloud 3. Build a cloudbuild.yaml file that uses 2. and restart the instance (not recommended if your app is in production since it will cause downtime) in a similar fashion to:并且有gcloud 3 的云构建器。构建一个使用 2. 的 cloudbuild.yaml 文件并以类似于以下方式重新启动实例(如果您的应用程序正在生产中,则不推荐,因为它会导致停机):

cloudbuild.yaml云构建.yaml

steps:
- name: gcr.io/cloud-builders/gcloud
  args: ['compute', 'instances', 'add-metadata', 'example-instance','--metadata startup-script-url=gs://bucket/file']
- name: gcr.io/cloud-builders/gcloud
  args: ['compute', 'instances', 'reset', 'example-instance']
  1. Give the cloud build service account enough permissions to execute that task ( Compute Admin and Storage Admin ).授予云构建服务帐户足够的权限来执行该任务( Compute AdminStorage Admin )。
  2. Run the build.运行构建。

I shared the previous steps in aims of answering your concrete question but I would insist that my recommendation would be to migrate the app to App Engine Flexible environment with custom runtime as explained before.我分享了前面的步骤以回答您的具体问题,但我坚持认为我的建议是将应用程序迁移到具有自定义运行时的 App Engine 柔性环境,如前所述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过cloudbuild.yaml进行的Google Cloud Build随机超时 - Google Cloud Build via cloudbuild.yaml times out randomly GCP cloudbuild.yaml 创建“workspace/tests”但需要“workspace/my_app/tests” - GCP cloudbuild.yaml creates "workspace/tests" but need "workspace/my_app/tests" 如何为基于 zip 的 CloudFunction 部署启用 cloudbuild.yaml? - How to enable cloudbuild.yaml for zip-based CloudFunction deployments? Python Flask:从Swagger YAML转到Google App Engine? - Python Flask: Go from Swagger YAML to Google App Engine? Google App Engine Devserver + dispatch.yaml(gcloud) - Google App Engine Devserver + dispatch.yaml (gcloud) 应用引擎python gcloud不更新实例 - app engine python gcloud not updating instance 从python + flask + gunicorn + nginx + Compute Engine应用程序中读取来自Google Cloud Storage的文件失败 - Reading of a file from Google Cloud Storage fails in a python + flask + gunicorn + nginx + Compute Engine app 重试python App Engine管道 - Retrying a python App Engine pipeline Google App Engine | Python |的app.yaml - Google App Engine | Python | APP.YAML 无法使用 Apache 将 sklearn 导入 Google Compute Engine 上的 Flask App - Can’t Import sklearn into Flask App on Google Compute Engine with Apache
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM