visit
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:composer global require laravel/installer
composer create-project --prefer-dist laravel/laravel laravel6-on-google-cloud-run
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 //localhost:8000
on a web browser. I had PHP 7.2 installed locally.3. Create a new GitHub repo
Create a new repository on Github like below: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:git init
code . # I used VS code to change the readme
git add readme.md
git commit -m "Initial commit -- App Readme"
git remote add origin [email protected]:geshan/laravel6-on-google-cloud-run.git
git push -u origin master
5. Add Full Laravel, Open PR
Now let’s add the whole app as a PR to the Github repo by executing the following commands:git checkout -b laravel6-full-app
git add .gitignore
git add .
git commit -m "Add the whole Laravel 6 app"
git push origin laravel6-full-app
6. Setup tests with
Not some fun part, after you merged your PR now Github knows that this is a Laravel project. Click on
Actions
tab on your repo page and you should be able to see something like below:Click the
Set up this workflow
under Laravel
then on the next page click Start commit
button on the top right. After that add a commit message like below and click Commit new file
.No more creating
.myCIname.yml
file anymore :).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. We will need the container to run Laravel on Google Cloud Run too. This part is inspired by post by Nicolas. If you want to learn more about and Laravel please refer to this .Run the following commands first to get your master up to date as we added the workflow file from Github interface:git checkout master
git fetch
git pull --rebase origin master # as we added the workflow file from github interface
git checkout -b docker
Add a key to the
.env.example
file, copy it from .env
file like below:APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:DJkdj8L5Di3rUkUOwmBFCrr5dsIYU/s7s+W52ClI4AA=
APP_DEBUG=true
APP_URL=//localhost
Add the following
Dockerfile
on the project root:FROM composer:1.9.0 as build
WORKDIR /app
COPY . /app
RUN composer global require hirak/prestissimo && composer install
FROM php:7.3-apache-stretch
RUN docker-php-ext-install pdo pdo_mysql
EXPOSE 8080
COPY --from=build /app /var/www/
COPY docker/000-default.conf /etc/apache2/sites-available/000-default.conf
COPY .env.example /var/www/.env
RUN 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
<VirtualHost *:8080>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/public/
<Directory /var/www/>
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
After that add the following
docker-compose.yml
version: '3'
services:
app:
build:
context: ./
volumes:
- .:/var/www
ports:
- "8080:8080"
environment:
- APP_ENV=local
Boil down to main things
If you try to understand everything here it might be overwhelming, let me boil down the main parts:8080
we are using 000-default.conf
to configure this//localhost:8080
to see that Laravel 6 is running locally on Docker. Below is my
docker-compose up
output towards the end:git status
On branch docker
Untracked files:
(use "git add <file>..." 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)
git add .
git commit -m "Add docker and docker compose"
git push origin docker
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 :).8. Add deploy to
Now let’s deploy this Laravel setup to Google Cloud Run the easy way. Given you have merged your PR from the docker branch. Let’s run the following commands:git checkout master
git fetch
git pull --rebase origin master
git checkout -b cloud-run-button
Then add the following to your
readme.md
file:### Run on Google cloud run
[![Run on Google Cloud](//storage.googleapis.com/cloudrun/button.svg)](//console.cloud.google.com/cloudshell/editor?shellonly=true&cloudshell_image=gcr.io/cloudrun/button&cloudshell_git_repo=//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
//github.com/ghaleroshan/laravel6-on-google-cloud-run
it will be //github.com/ghaleroshan/laravel6-on-google-cloud-run.git
, then commit and push. Your PR should look something like one.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.1. Choose the project –
Choose a project to deploy this application
Choose a region to deploy this application
, I usually go with us-central-1
10. Hurray, Your App is Working
After you git the URL you should see your app working on Google Cloud Run like below:Any containerized web app can be run on Google Cloud Run, it is a great service.