博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
zuul集成cloud_如何在具有持续集成的Google Cloud Run上运行Laravel-分步指南
阅读量:2525 次
发布时间:2019-05-11

本文共 14854 字,大约阅读时间需要 49 分钟。

zuul集成cloud

Laravel has in popularity over the last few years. The Laravel community even says that Laravel has made writing PHP more enjoyable instead of a pain. Laravel 6 has some interesting new . Getting a super scaleable working URL for your application takes hours if not days. Setting up something like Kubernetes is a huge task. This is where Google Cloud Run shines: you can get a working HTTPS URL for any of your containerized apps in minutes.

Laravel已经流行,在过去的几年。 Laravel社区甚至说Laravel使编写PHP变得更有趣而不是痛苦。 Laravel 6具有一些有趣的新 。 为您的应用程序获取超级可扩展的工作URL需要花费数小时甚至数天的时间。 设置Kubernetes之类的东西是一项艰巨的任务。 这就是Google Cloud Run的亮点:您可以在几分钟内为任何容器化应用程序获取有效的HTTPS URL。

is serverless and fully managed by Google. You get super scale, billing by the second, HTTPS URLs, and your own domain mapping. If you want to run stateless containers, Cloud Run is hands down the easiest way to do it. In this post, I will detail how to get your Laravel 6 app working on Google cloud run with Continuous Integration (CI).

是无服务器的,由Google完全管理。 您将获得超大规模,按秒计费,HTTPS URL和您自己的域映射。 如果要运行无状态容器,Cloud Run是最简单的方法。 在本文中,我将详细介绍如何通过持续集成(CI)使您的Laravel 6应用程序在Google云上运行。

先决条件 (Prerequisites)

  • You are familiar with PHP/Composer and aware of Laravel (if you’ve landed here you are, I suppose)

    您熟悉PHP / Composer并了解Laravel(如果您已经登陆这里,我想是)
  • You know how to use Git from the CLI

    您知道如何通过CLI使用Git
  • Your code is hosted on GitHub for CI/CD and you are familiar with GitHub

    您的代码托管在GitHub上以用于CI / CD,并且您熟悉GitHub
  • Know a fair bit of Docker, maybe even multi-stage build

    相当了解Docker,甚至可能是多阶段构建
  • Have a working Google cloud account (they give you free for 1 yr, no reasons not to have an account)

    拥有一个有效的Google云帐户(他们会为您提供的1年免费 ,没有理由不开设帐户)

为什么Cloud Run是初学者的绝佳选择? (Why is Cloud Run a great option for beginners?)

For two main reasons:

主要有两个原因:

  1. Learn about the best practices and software like docker and CI/CD

    了解最佳实践和软件,例如docker和CI / CD
  2. Getting the basics going just involves clicking a button, selecting 2 things, waiting for 5 mins, and you get a working HTTPS URL. Can it be any easier than this? :)

    掌握基础知识只需单击一个按钮,选择2项内容,等待5分钟,您就会获得一个有效的HTTPS URL。 可以比这更容易吗? :)

部署步骤 (Steps to deploy)

Below are the steps to set up and deploy Laravel 6 on Cloud Run:

以下是在Cloud Run上设置和部署Laravel 6的步骤:

1.克隆Laravel或新的Laravel项目 (1. Clone Laravel or new Laravel project)

Start by cloning Laravel or using composer or the Laravel CLI as indicated in the official guide. I am using composer to get the latest Laravel as below:

首先按照官方指南中的指示,克隆Laravel或使用composer或Laravel CLI。 我正在使用作曲家来获取最新的Laravel,如下所示:

命令 (Command)

I ran the following command to get the latest Laravel:

我运行以下命令以获取最新的Laravel:

composer create-project --prefer-dist laravel/laravel laravel6-on-google-cloud-run

2.首先在本地测试 (2. Test it locally first)

Then run cd laravel6-on-google-cloud-run then php artisan serve to see if it is working. For me it was fine when I went to http://localhost:8000 on a web browser. I had PHP 7.2 installed locally.

然后运行cd laravel6-on-google-cloud-run然后cd laravel6-on-google-cloud-run php artisan serve以查看其是否正常运行。 对我来说,当我通过网络浏览器访问http://localhost:8000时,这很好。 我在本地安装了PHP 7.2。

3.创建一个新的GitHub存储库 (3. Create a new GitHub repo)

Create a new repository on Github like below:

在Github上创建一个新的存储库,如下所示:

You can use any Git hosting provider, but for this example I will be using to run tests (and Github is the most popular git hosting tool).

您可以使用任何Git托管服务提供商,但是在本示例中,我将使用运行测试(Github是最受欢迎的git托管工具)。

4.添加回购,推送自述文件 (4. Add repo, push readme)

Now after you have the repo created, add it to your local Laravel copy and push the Readme file. To do this run the following commands on your CLI:

现在,在创建存储库之后,将其添加到本地Laravel副本中并推送自述文件。 为此,请在CLI上运行以下命令:

git initcode . # I used VS code to change the readmegit add readme.mdgit commit -m "Initial commit -- App Readme"git remote add origin git@github.com:geshan/laravel6-on-google-cloud-run.gitgit push -u origin master

运行以上命令后,我在Github存储库上有了这个 (After running the above commands I had this on my Github repo)

5.添加完整的Laravel,打开PR (5. Add full Laravel, open a PR)

Now let’s add the whole app as a PR to the Github repo by executing the following commands:

现在,通过执行以下命令,将整个应用程序作为PR添加到Github存储库中:

git checkout -b laravel6-full-appgit add .gitignoregit add .git commit -m "Add the whole Laravel 6 app"git push origin laravel6-full-app

After that go and open a Pull Request (PR), on the repo like one. You might be thinking I am the only one working on this, why do I need a PR? Well, it is always better to do things methodically even if it is just one person working on the project :).

此后去打开一个pull请求(PR),就像回购一个。 您可能会认为我是唯一从事此工作的人,为什么我需要PR? 好吧,即使只有一个人在项目上工作,也最好有条理地做事:)。

After that merge your pull request.

之后,合并您的拉取请求。

6.使用设置测试 (6. Setup tests with )

Now the fun part: after you merged your PR now Github knows that this is a Laravel project. Click on the  Actions tab on your repo page and you should be able to see something like below:

现在最有趣的部分是:合并PR后,Github知道这是一个Laravel项目。 单击回购页面上的“ Actions选项卡,您应该能够看到如下所示的内容:

Click the Set up this workflow under Laravel then on the next page click the Start commit button on the top right. After that add a commit message like below and click Commit new file.

单击Laravel下的“ Set up this workflow ,然后在下一页上,单击右上角的“ Start commit按钮。 之后,添加如下所示的提交消息,然后单击Commit new file

There you go, you have your CI setup. Laravel default tests will run on each git push now. Wasn’t that easy? Thank Github for this great intelligence. No more creating .myCIname.yml files :).

到这里,您就可以设置CI。 Laravel默认测试现在将在每个git push上运行。 那不是那么容易吗? 感谢Github如此出色的情报。 不再创建.myCIname.yml文件:)。

7.添加docker和docker-compose以在本地运行应用 (7. Add docker and docker-compose to run app locally)

Now let’s add docker and docker-compose to run the app locally without PHP or artisan serve.

现在,让我们添加docker和docker-compose,以在本地运行应用程序,而无需PHP或artisan服务。

We will need the container to run Laravel on Google Cloud Run too. This part is inspired by the post by Nicolas. If you want to learn more about and Laravel please refer to this .

我们也需要该容器在Google Cloud Run上运行Laravel。 这部分的灵感来自发表的文章。 如果您想了解有关和Laravel的更多信息,请参考这篇 。

Run the following commands first to get your master up to date as we added the workflow file from Github interface:

当我们从Github界面添加workflow文件时,首先运行以下命令以使您的母版保持最新:

git checkout mastergit fetchgit pull --rebase origin master # as we added the workflow file from github interfacegit checkout -b docker

Add a key to the .env.example file. Copy it from the .env file like below:

将密钥添加到.env.example文件。 从.env文件复制它,如下所示:

APP_NAME=LaravelAPP_ENV=localAPP_KEY=base64:DJkdj8L5Di3rUkUOwmBFCrr5dsIYU/s7s+W52ClI4AA=APP_DEBUG=trueAPP_URL=http://localhost

As this is just a demo this is ok to do. For a real app always be careful with secrets. For production-ready apps do turn of the debugging and other dev related things.

因为这只是一个演示,所以可以这样做。 对于真正的应用程序,请始终谨慎对待秘密。 对于可用于生产环境的应用程序,请关闭调试和其他与开发人员相关的工作。

Add the following Dockerfile on the project root:

在项目根目录上添加以下Dockerfile

FROM composer:1.9.0 as buildWORKDIR /appCOPY . /appRUN composer global require hirak/prestissimo && composer installFROM php:7.3-apache-stretchRUN docker-php-ext-install pdo pdo_mysqlEXPOSE 8080COPY --from=build /app /var/www/COPY docker/000-default.conf /etc/apache2/sites-available/000-default.confCOPY .env.example /var/www/.envRUN chmod 777 -R /var/www/storage/ && \    echo "Listen 8080" >> /etc/apache2/ports.conf && \    chown -R www-data:www-data /var/www/ && \    a2enmod rewrite

Then add the following file at docker/000-default.conf

然后在docker/000-default.conf添加以下文件

ServerAdmin webmaster@localhost DocumentRoot /var/www/public/
AllowOverride All Require all granted ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined

After that add the following docker-compose.yml

之后添加以下docker-compose.yml

version: '3'services:  app:    build:      context: ./    volumes:      - .:/var/www    ports:      - "8080:8080"    environment:      - APP_ENV=local

让我们归结为主要内容 (Let's Boil down to main things)

If you try to understand everything here it might be overwhelming, so let me boil down the main parts:

如果您试图了解这里的所有内容,那么可能会令人不知所措,所以让我归纳一下主要部分:

  1. We are using the official PHP Apache docker image to run Laravel, which has PHP version 7.3.

    我们使用官方PHP Apache docker镜像运行Laravel,该版本具有PHP 7.3。
  2. We are using a multistage build to get the dependencies with Composer then we're copying them to the main docker image that has PHP 7.3 and Apache.

    我们正在使用多阶段构建来获取Composer的依赖关系,然后将它们复制到具有PHP 7.3和Apache的主docker映像中。
  3. As Google Cloud Run requires the web-server to be listening to port 8080 and we are using 000-default.conf to configure this

    由于Google Cloud Run要求网络服务器监听8080端口,因此我们使用000-default.conf进行配置

  4. To make things easy to run with the single command docker-compose up we are using docker-compose.

    为了使单个命令docker-compose up易于运行,我们使用docker-compose。

  5. Now as you have read this far, run docker-compose up on your root and then after everything runs go to http://localhost:8080 to see that Laravel 6 is running locally on Docker. Below is my docker-compose up output towards the end:

    现在,您已经阅读了到目前为止,在根目录上运行docker-compose up ,然后在一切运行之后转到http://localhost:8080以查看Laravel 6在Docker本地运行。 下面是我的docker-compose up输出:

As Laravel is running fine with Docker, let’s open a PR like one to add Docker to our project. I ran the following commands on the root of the project before opening the Pull Request (PR):

由于Laravel在Docker上运行良好,让我们打开一个像的PR,将Docker添加到我们的项目中。 在打开请求请求(PR)之前,我在项目的根目录上运行了以下命令:

git status

It should give you something like below:

它应该给您类似以下内容:

On branch dockerUntracked files:  (use "git add 
..." to include in what will be committed) Dockerfile docker-compose.yml docker/nothing added to commit but untracked files present (use "git add" to track)

Now run the following commands:

现在运行以下命令:

git add .git commit -m "Add docker and docker compose"git push origin docker

As a bonus it will run the Laravel default test on the push, like you can see below:

作为奖励,它将在推送时运行Laravel默认测试,如下所示:

Only the owner of the repo has access to the Actions tab so other people don’t necessarily need to know the results of your test builds :).

只有仓库的所有者才可以访问“ Actions选项卡,因此其他人不必知道测试构建的结果:)。

8.添加部署到 (8. Add deploy to )

Now let’s deploy this Laravel setup to Google Cloud Run the easy way. Given that you have merged your PR from the docker branch, let’s run the following commands:

现在,让我们将这种Laravel设置部署到Google Cloud Run变得简单。 鉴于您已经从docker分支合并了PR,让我们运行以下命令:

git checkout mastergit fetchgit pull --rebase origin mastergit checkout -b cloud-run-button

Then add the following to your readme.md file:

然后将以下内容添加到您的readme.md文件中:

### Run on Google cloud run[![Run on Google Cloud](https://storage.googleapis.com/cloudrun/button.svg)](https://console.cloud.google.com/cloudshell/editor?shellonly=true&cloudshell_image=gcr.io/cloudrun/button&cloudshell_git_repo=https://github.com/geshan/laravel6-on-google-cloud-run.git)

Be careful and replace the last part with your repo’s HTTPs URL. For example, if your repo is at https://github.com/ghaleroshan/laravel6-on-google-cloud-run it will be https://github.com/ghaleroshan/laravel6-on-google-cloud-run.git, then commit and push. Your PR should look something like one.

请注意,将最后一部分替换为您的仓库的HTTPs URL。 例如,如果您的存储库位于https://github.com/ghaleroshan/laravel6-on-google-cloud-run则它将为https://github.com/ghaleroshan/laravel6-on-google-cloud-run.git ,然后提交并推送。 您的PR应该看起来像 。

9.在Google Cloud Run上部署 (9. Deploy on Google Cloud Run)

After you merge your Pull Request (PR), then go to your repo page and click on the Run on Google Cloud button.

合并您的请求请求(PR)之后,然后转到您的回购页面并单击Run on Google CloudRun on Google Cloud按钮。

After that, if you are logged into your Google account and have Google cloud setup with 1 project, click “Proceed”. You might need to wait a bit, then

之后,如果您已登录Google帐户并设置了包含1个项目的Google云,请单击“继续”。 您可能需要稍等一下,然后

  1. Choose the project – Choose a project to deploy this application

    选择项目– Choose a project to deploy this application

  2. Choose the region – Choose a region to deploy this application, I usually go with us-central-1

    选择区域– Choose a region to deploy this application ,我通常选择us-central-1

  3. Then wait for the container to be built and deployed. You can see my process below:

    然后等待容器的构建和部署。 您可以在下面看到我的流程:

If everything goes fine on your Google Cloud Shell, you will see the HTTPS URL that you can hit to see your Laravel app running like below:

如果您在Google Cloud Shell上一切正常,您将看到可以单击以查看Laravel应用运行的HTTPS URL,如下所示:

What just happened above is:

上面发生的事情是:

  1. After choosing the region, the script built a docker container image from the Dockerfile in the repo

    选择区域后,脚本从Dockerfile中的Dockerfile中构建了一个Dockerfile容器映像

  2. Then it pushed the built image to

    然后将生成的图像推送到

  3. After that using the CLI it deployed the built image to Cloud Run, which gave back the URL.

    之后,使用 CLI将生成的映像部署到Cloud Run,这会返回URL。

10.万岁,您的应用正在运行 (10. Hurray, your app is working)

After you git the URL you should see your app working on Google Cloud Run like below:

混入网址后,您应该会看到您的应用在Google Cloud Run上运行,如下所示:

If you want to deploy another version you can merge your PR to master and click the button again to deploy.

如果要部署另一个版本,可以将PR合并到master,然后再次单击按钮进行部署。

有关Google Cloud Run的更多信息 (More about Google Cloud Run)

The for Google Cloud Run is very generous. You can run any containerized app or web app on Google cloud run. I ran a pet project that got ~ 1 request per minute and I did not have to pay anything.

Google Cloud Run的非常优惠。 您可以在Google Cloud Run上运行任何容器化的应用程序或Web应用程序。 我运行了一个宠物项目,每分钟收到约1个请求,而我无需支付任何费用。

Behind the scenes, it is using and . It can also be run on your Kubernetes cluster but who would choose to manage a K8s cluster if you can just push and get a scaleable serverless fully managed app :).

在后台,它使用和 。 它也可以在您的Kubernetes集群上运行,但是如果您可以推送并获得可扩展的无服务器完全托管应用程序,谁会选择管理K8s集群:)。

TLDR (TLDR)

To run Laravel 6 on Google Cloud Run quickly follow the steps below:

要在Google Cloud Run上运行Laravel 6,请快速执行以下步骤:

  1. Make sure you are logged into your

    确保您已登录

  2. Go to

    转到

  3. Click the “Run On Google Cloud” blue button

    点击“在Google Cloud上运行”蓝色按钮
  4. Select your project

    选择你的项目
  5. Select your region

    选择您的地区
  6. Wait and get the URL of your Laravel App as below, Enjoy!

    等待并获取如下所示的Laravel应用的URL,尽情享受吧!


结论 (Conclusion)

There you go – running a Laravel app on Google cloud run was pretty easy. You have even got test running on Github with Github actions. Hope it helps. To do a CI/CD approach you can check out this . It shows deployment using Cloud build. As the same container is running for local and production (Google Cloud Run) environment you don’t need to learn a new framework to go Serverless.

随您去吧–在Google云上运行Laravel应用非常简单。 您甚至已经使用Github动作在Github上运行了测试。 希望能帮助到你。 要执行CI / CD方法,您可以查看这篇 。 它显示了使用Cloud build进行的部署。 由于同一容器正在本地和生产环境(Google Cloud Run)上运行,因此您无需学习新框架即可使用无服务器。

Any containerized web app can be run on Google Cloud Run, it is a great service. You can read more at https://geshan.com.np
任何容器化的网络应用都可以在Google Cloud Run上运行,这是一项出色的服务。 您可以在https://geshan.com.np上了解更多信息。

翻译自:

zuul集成cloud

转载地址:http://sihwd.baihongyu.com/

你可能感兴趣的文章
Java 虚拟机:互斥同步、锁优化及synchronized和volatile
查看>>
2.python的基本数据类型
查看>>
python学习笔记-day10-01-【 类的扩展: 重写父类,新式类与经典的区别】
查看>>
查看端口被占用情况
查看>>
浅谈css(块级元素、行级元素、盒子模型)
查看>>
Ubuntu菜鸟入门(五)—— 一些编程相关工具
查看>>
PHP开源搜索引擎
查看>>
12-FileZilla-响应:550 Permission denied
查看>>
ASP.NET MVC 3 扩展生成 HTML 的 Input 元素
查看>>
LeetCode 234. Palindrome Linked List
查看>>
编译HBase1.0.0-cdh5.4.2版本
查看>>
结构体指针
查看>>
迭代器
查看>>
Food HDU - 4292 (结点容量 拆点) Dinic
查看>>
Ubuntu安装Sun JDK及如何设置默认java JDK
查看>>
[经典算法] 排列组合-N元素集合的M元素子集
查看>>
Codeforces 279D The Minimum Number of Variables 状压dp
查看>>
打分排序系统漫谈2 - 点赞量?点赞率?! 置信区间!
查看>>
valgrind检测linux程序内存泄露
查看>>
Hadoop以及组件介绍
查看>>