loading...
Cover image for Auto deploy on AppEngine from Gitlab

Auto deploy on AppEngine from Gitlab

giulianobr profile image Giuliano Ribeiro 🇪🇺 ・3 min read

Nowadays, CI/CD is a common task during Software Development and almost all platforms enable us to do this automatically. It is not different on Gitlab.
Today I'll gonna show you how to configure your Gitlab project to automatically deploy on Google Cloud App Engine.

First things first

First, you need to setup your App Engine project on GCP console. It is necessary because App Engine requires you to set the region of your app.
If you didn't do this, first you will need to setup using this cmd:

gcloud app create


Setup you App Engine app.

and follow the instructions.

Service Account and Permissions

The entire process should be made without human intervention. So, to do this on GCP you'll need a Service Account.

First, create a Service Account to be used during the process: Service Account Create

Step 1


Step 1

Step2: Very important step, here you MUST give all those permissions. Without them the automation will not work properly.

Step 2


Step 2: VERY IMPORTANT!

Step 3: Create a Key


Step 3: Create a Key

Step 4: choose the JSON option.


Step 4: choose the JSON option!

In the last step you should open the file and copy the content. This content will be necessy to configure the Gitlab.

API Enabling

Some APIs must be enabled before trying to run the deploy.

  1. Enable the App Engine Admin API, here:
    Enable App Engine Admin API

    App Engine Admin API enabling
    App Engine Admin API enabling
  2. Enable Cloud Builder API: Cloud Builder API Enable

    Cloud Build API enabling
    Cloud Build API enabling

Gitlab Config

Now you have all done on GCP, Gitlab part!
Go to your project and configure the CI/CD options:

Gitlab CI/CD menu option


CI/CD menu option

Instead of having the content of your Service Account in a file inside your repository, is a best practice to have this as variable. So, configure a variable, following the image:

Configure the SERVICE_ACCOUNT_KEY variable with the JSON content downloaded in the Step 4.


Configure the SERVICE_ACCOUNT_KEY variable with the JSON content downloaded in the Step 4.

Gitlab CI file descriptor

To enable all these pieces together is required to "tell" to Gitlab: "Please my friend, run all these now". To do this you should create a file in your repository called .gitlab-ci.yml, this file describes what you want to do.

xxxxxxxxx


Basic gitlab ci descriptor.

File content available here.
https://gitlab.com/giulianobr/simple-go-app/blob/235de13e310d7614f9b8cb2dab0f804c6b82a041/.gitlab-ci.yml

The CI process runs inside the container platform in Gitlab. At the glance, it seems like a Dockerfile, as you should define and base image where your deploy will runs.
In this case, I'm using Google's base image that contains the Google Cloud SDK already installed.
To understand more about all configs available on Gitlab CI, take a look here.

Conclusion

This post aims to help you to deploy an app to App Engine Standard. As my example is a Golang app in the 2nd gen runtime, it is a bit easier. To deploy a Java 8 app is a little bit different. If you want any help to setup your Java 8 app, ask me in the comments, I also have an example for this scenario.

I hope it helps you, people!
See you soon.

You can find this project in my Gitlab project.

Posted on by:

giulianobr profile

Giuliano Ribeiro 🇪🇺

@giulianobr

Golang enthusiast, Google Cloud Architect and Web dev in spare times.

Discussion

markdown guide