Deploy Laravel 10 on Google Cloudrun

Deploy Laravel 10 on Google Cloudrun

Are you ready to take your Laravel 10 application to the next level? If you’re eager to ensure lightning-fast performance, scalability, and seamless deployment, Google Cloud Run has got your back. In this guide, we’ll walk you through the process of deploying your Laravel 10 project on Google Cloud Run. Whether you’re a seasoned developer or just starting out, this step-by-step tutorial will help you harness the power of containerized applications and serverless computing.

deploy process

Let’s dive into the world of effortless Laravel deployment on Google Cloud Run, where performance meets simplicity.

Create a new project using laravel.build

curl -s "https://laravel.build/my-app" | bash

Configuring the Project on Google Cloud Platform

Go to google cloud console and create a new project.

Activate billing and enable the following services:

  • Cloud Run
  • Cloud KMS

Access Cloud Build > Configuration and enable the same services mentioned above.

Encrypt Environments

Add a keyring and a key in the Cloud Key Management Service (KMS). Name both of them “envs” in the location “global,” leaving the rest of the fields as default.

Install the gcloud CLI on your computer: gcloud install docs.

Log in to gcloud with your Google account:

gcloud auth login
gcloud config set project PROJECT_ID

Setting up the Laravel Project

In your Laravel project, create a folder to store the encrypted environments. You can name it “envs” and copy the .env file to ./envs/.env.dev. Create another file for production with the name ./envs/.env.prod.

folder -------------------> result after encrypt
./envs/.env.dev -----> ./envs/.env.dev.enc
./envs/.env.prod ----> ./envs/.env.prod.enc
./envs/.env.stage ---> ./envs/.env.stage.enc

Add a .gitignore file to only keep encrypted files in the git repository:

*
/*
!.gitignore
!.env.dev.enc
!.env.prod.enc
!.env.stage.enc

Add the following scripts to your package.json in the “scripts” section:

"enc-env": "gcloud kms encrypt --plaintext-file=./envs/.env.dev --ciphertext-file=./envs/.env.dev.enc --location=global --keyring=envs --key=envs"
"enc-env-prod": "gcloud kms encrypt --plaintext-file=./envs/.env.prod --ciphertext-file=./envs/.env.prod.enc --location=global --keyring=envs --key=envs"
"enc-all-envs": "npm run enc-env && npm run enc-env-prod"

In your project folder, using a terminal with an LTS version of Node and npm, run the following command:

npm run enc-all

Dockerfile and docker-compose.yaml configuration

If you don’t have a Laravel 10 project yet, create one:

curl -s "https://laravel.build/laravel-app" | bash

Navigate to the project folder:

cd laravel-app

Create the Dockerfile.

FROM laratips/laravel10:latest

ENV APP_ENV=production
ENV APP_DEBUG=false
ENV PHP_MEMORY_LIMIT=1024M

COPY . /var/www/html

RUN composer install --optimize-autoloader

RUN php artisan config:cache && \
    php artisan cache:clear && \
    php artisan route:cache && \
    php artisan view:cache && \
    echo "environment to apply: ${APP_ENV}" && \
    if ["${APP_ENV}}" = "local"]; then php artisan migrate; fi && \
    chmod 777 -R /var/www/html/storage/ && \
    chown -R www-data:www-data /var/www/ && \
    a2enmod rewrite

Where does laratips/laravel10:latest come from? I’m providing the link on how it’s formed so that you can improve or modify it according to your requirements.

laratips/laravel10:latest

We create the “cloudbuild.yaml” file.


steps:
  - name: gcr.io/cloud-builders/gcloud
    args:
      - kms
      - decrypt
      - --ciphertext-file=/workspace/envs/.env.prod.enc
      - --plaintext-file=/workspace/.env
      - --location=global
      - --keyring=env
      - --key=env
      - --verbosity=debug
  - name: gcr.io/cloud-builders/npm
    args: ["install"]
  - name: gcr.io/cloud-builders/npm
    args: ["run", "build"]
  - name: "gcr.io/cloud-builders/docker"
    args:
      [
        "build",
        "-t",
        "gcr.io/$PROJECT_ID/[REPOSITORY_URL]:$SHORT_SHA",
        "-t",
        "gcr.io/$PROJECT_ID/[REPOSITORY_URL]:latest",
        ".",
      ]
  - name: "gcr.io/cloud-builders/docker"
    args:
      [
        "push",
        "gcr.io/$PROJECT_ID/[REPOSITORY_URL]:$SHORT_SHA",
      ]
  - name: "gcr.io/cloud-builders/docker"
    args:
      [
        "push",
        "gcr.io/$PROJECT_ID/[REPOSITORY_URL]:latest",
      ]
  - name: "gcr.io/cloud-builders/gcloud"
    args:
      [
        "run",
        "deploy",
        "laravel-app",
        "--image",
        "gcr.io/$[REPOSITORY_URL]:$SHORT_SHA",
        "--region",
        "europe-west1",
        "--platform",
        "managed",
        "--verbosity",
        "debug",
        "--memory",
        "2048Mi",
        "--cpu",
        "2000m",
        "--port",
        "8080",
        "--timeout",
        "3600",
        "--min-instances",
        "0",
      ]
    env:
      - "PORT=8080"
images:
  - gcr.io/$PROJECT_ID/[REPOSITORY_URL]:$SHORT_SHA
  - gcr.io/$PROJECT_ID/[REPOSITORY_URL]:latest
timeout: 2400s

Where I’ve placed [REPOSITORY_URL], we should add the repository, for example:

bitbucket.org/USER_NAME/laravel-app

Configuring Cloud Build

Create a trigger.

  • Select the Bitbucket/GitHub repository.
  • In the branch, add: ^master$ or ^main$ depending on what you are using.
  • Choose the YAML type and specify the file name: cloudbuild.yaml.
  • Create.

That’s it! Once you push a change to the master or main branch (as configured), the process will execute, building and deploying your app on Cloud Run.

Final Note: Access the Cloud Run service to obtain the HTTPS URL for your app. In the NETWORKING section, set the Ingress control to “all”. You can also add a custom domain from the Cloud Run listing.