Blog Engineering How to deploy a PHP app using GitLab's Cloud Run integration
Published on: December 10, 2024
3 min read

How to deploy a PHP app using GitLab's Cloud Run integration

Are you using PHP and want an easy way to deploy your application to Google Cloud? Follow this guide to deploy your app with Google Cloud Run in under 10 minutes.

migration - abstract - cover

Writing PHP application code and ensuring the application is running smoothly in production are often two different skills sets owned by two different engineers. GitLab aims to bridge the gap by enabling the engineer who has written the PHP application code to also deploy it into Google Cloud Platform with little effort.

Whether you own event-driven, long-running services or deploy containerized jobs to process data, Google Cloud Run automatically scales your containers up and down from zero — this means you only pay when your code is running.

If you are a PHP developer who would like to deploy your application with minimal effort to Google Cloud Platform, this guide will show you how using the GitLab Google Cloud Run integration.

Overview

  • Create a new project in GitLab
  • Set up your PHP application
  • Utilizing the Google Cloud integration, create a Service account
  • Utilizing the Google Cloud integration, configure Cloud Run via merge request
  • Try adding another endpoint
  • Clean up

Prerequisites

  • Owner access on a Google Cloud Platform project
  • Working knowledge of PHP, an open-source, general-purpose scripting language
  • Working knowledge of GitLab CI
  • 10 minutes

1. Create a new project in GitLab.

We decided to call our project PHP cloud-run for simplicity.

PHP cloud- run project

Then, create an index.php apphttps://gitlab.com/demos/templates/php-cloud-run/-/blob/main/index.php.

<?php

$name = getenv('NAME', true) ?: 'World';
echo sprintf('Hello %s!', $name);

2. Utilizing the Google Cloud integration, create a Service account.

Navigate to Operate > Google Cloud > Create Service account.

Create Service account screen

Then configure the region you would like the Cloud Run instance deployed to.

Configure region screen

3. Utilizing the Google Cloud integration, configure Cloud Run via merge request.

Deployment configuration screen

This will open a merge request. Immediately merge this merge request.

Enable Deployments to Cloud run screen

Note: GCP_PROJECT_ID, GCP_REGION, GCP_SERVICE_ACCOUNT, and GCP_SERVICE_ACCOUNT_KEY will all be automatically populated from the previous steps.

Variables screen

Check your pipeline and you will see you have successfully deployed to Google Cloud Run utilizing GitLab CI.

merge branch screen



Google Cloud Run deployed with GitLab CI

4. Click the Service URL to view your newly deployed Flask server.

In addition, you can navigate to Operate > Environments to see a list of deployments for your environments.

Environments screen

By clicking on the environment called main, you’ll be able to view a complete list of deployments specific to that environment.

Main environment

5. Add another endpoint

To get started with developing your PHP application, try adding another endpoint. For example, in your main file, you can add a /bye endpoint like this:


<?php

$name = getenv('NAME', true) ?: 'World';

if ($_SERVER['REQUEST_URI'] == '/bye') {
    echo sprintf('Goodbye %s!', $name);
} else {
    echo sprintf('Hello %s!', $name);
}

Push the changes to the repo, and watch the deploy-to-cloud-run job deploy the updates. Once the job is complete, go back to the Service URL and navigate to the /bye endpoint to see the new functionality in action.

Clean up

To prevent incurring charges on your Google Cloud account for the resources used in this tutorial, you can either delete the specific resources or delete the entire Google Cloud project. For detailed instructions, refer to the cleanup guide here.

Check out more easy-to-follow tutorials from our Solutions Architecture team.

We want to hear from you

Enjoyed reading this blog post or have questions or feedback? Share your thoughts by creating a new topic in the GitLab community forum. Share your feedback

Ready to get started?

See what your team could do with a unified DevSecOps Platform.

Get free trial

Find out which plan works best for your team

Learn about pricing

Learn about what GitLab can do for your team

Talk to an expert