Deploying a Laravel app to Google App Engine with Gitlab CI
A few months ago, working on a php7.2 Laravel project running on Google App Engine, my team and I decided to automate the deployment process.
I decided to write this article because it would have really helped us (and saved us a lot of time) hoping it will help a lost soul somewhere. I will try to keep this as simple and concise as possible.
For the deployment process to work, we are going to proceed with the 3 following steps:
- Configuring Google App Engine.
- Setting Gitlab environment variables.
- Setting up our Laravel app.
Configuring Google App Engine
We are going to create a service account that we will use in Gitlab to deploy to GAE. Go to IAM-Admin->service-account and create a service account.
Create a service account
Grant the service account the necessary roles
After you click create, in the next panel, we will give the account the following roles:
- App Engine Deployer : enables Gitlab to use the account to deploy our app.
- App Engine Service Admin : without it, the deployment succeeds but the account cannot change traffic splits.
- Cloud Build Editor : used to build the app with GCP Cloud Build.
- Storage Object Creator : access to storage area to create the project deployment temporary files.
- Storage Object Viewer : access to storage area to read the temporary files.
Create the service account key
In the next panel, click create a key, choose JSON and click the create on the right panel. A file will automatically be downloaded. We will need the key in Gitlab to set up the CI deployment environment variables.
And we are done with the first step !
Setting Gitlab environment variables
For step 2, we will need the key from step 1 and the project ID.
Go to settings -> CI/CD on your Gitlab project. Expand the variables panel and add two variables:
- PROJECT_ID : fill it with your project ID (the ID on the top I blacked out in the screenshots).
- SERVICE_ACCOUNT : fill it with the content of the JSON file we got in step 1.
Step 2 is now completed!
Setting up our Laravel app
In this step, we need to add the Gitlab CI (.gitlab-ci.yml) deployment file and edit our app yaml (app.yaml) file.
.gitlab-ci.yml
We will set Google cloud-sdk image to tell App Engine which image to use when dockering our app.
So now, changes to gitlab-ci branch will be automatically deployed to Dev environment.
app.yaml (Or in my case app-dev.yaml)
The issue we need to solve is that Google App Engine uses /srv/ folder by default, which will generate a lot of errors because we do not have the necessary permissions. Instead, we will tell GAE to use a temporary folder.
You may stumble across a lot of errors (/srv/ or bootstrap/cache errors) if you do not edit your app.yaml, using a /tmp folder with the following lines works like a charm (thanks to this PR discussion).
And we are all set ! You may have seen somewhere that you need to edit the composer file, rest assured you do not. Bellow you can see Laravel composer.json file scripts section, it is the same section we use in our project, no need to edit it.
Enjoy!
Now all you have to do is push your code to Gitlab and go get yourself a cup of coffee while you wait for the magic to happen.