Published on: December 10, 2024
3 min read
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.
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.
We decided to call our project PHP cloud-run
for simplicity.
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);
Navigate to Operate > Google Cloud > Create Service account.
Then configure the region you would like the Cloud Run instance deployed to.
This will open a merge request. Immediately merge this merge request.
Note: GCP_PROJECT_ID
, GCP_REGION
, GCP_SERVICE_ACCOUNT
, and GCP_SERVICE_ACCOUNT_KEY
will all be automatically populated from the previous steps.
Check your pipeline and you will see you have successfully deployed to Google Cloud Run utilizing GitLab CI.
In addition, you can navigate to Operate > Environments to see a list of deployments for your environments.
By clicking on the environment called main, you’ll be able to view a complete list of deployments specific to that environment.
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.
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.