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.
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.
Then configure the region you would like the Cloud Run instance deployed to.
3. Utilizing the Google Cloud integration, configure Cloud Run via merge request.
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.
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.
By clicking on the environment called main, you’ll be able to view a complete list of deployments specific to that 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.