Deploy Directly From GitLab to Google App Engine
A simple tutorial to make your application ready for continuous integration. Just push to your GitLab repository and it is deployed.
Google App Engine (GAE) is a robust platform for deploying web applications quickly and safely. I have been using GAE for quite some while now and have certainly faced lesser road blocks than my friends using competitive services.
GitLab, on the other hand, started off as a Github alternative if you wanted private repositories for free. With time they grew into something bigger. Providing services like agile tools and team management. In my start-up I used just GitLab along with Slack for running the whole operation and to be honest that was enough for a small team working on a budget.
I created my personal HTML web page and deployed it on GAE. As a habit, my code was already on GitLab. I was familiar with Continuous Integration (CI) due to a course on Udacity I took for boosting my start-up. Later, I found out that we can use CI on GitLab and deploy your applications quickly onto GAE. This was a breather since I didn’t have to lookup the GAE commands for deployment ‘every time’ I had to change a single letter on my HTML page.
Today I want to share this easy process with you. Don’t worry if you don’t know anything about CI. All you need to know is that following this tutorial, GitLab will do the work of deploying for you. All you have to do is push..
Continuous Integration (CI)
CI is a huge topic for discussion. To make it short, it is a method for teams to work on software projects using version control tools like Git. One of the benefits of CI is the availability of automated testing and deployment.
Let’s take a simple scenario, if four developers are working on the same project. Then in the language of version control: everyone will be working on their own branch. Which will be merged to a branch named development after removing all issues. Once development is verified the code will be merged into master branch, which is now ready to be released.
Now, if we throw CI into the aforementioned mix, then each branch that the developers were working on are automatically tested for bugs and syntax errors before merging into development. Once the issues are resolved the development branch will be tested before merging into master. Once master is verified the app will be automatically deployed to the target platform (e.g. GAE, Play Store or App Store). Convenient huh?
Today we will focus on deployment on GAE, however, if someone is interested in the intermediate testing then the procedure is quite the same.
Setting the Console
The first step is preparing your GAE developer console, essentially telling it to expect CI requests.
- Assuming you already have a GAE project setup, note the project name e.g. “my-awesome-project”.
- Open “IAM & Admin” in your Google Cloud Platform console.
- Go into “Service Accounts” from the side menu and click on “Create Service Account”.
- Name your service account to anything you like e.g. “gitlab-ci”.
- Click create and the page will ask for a service role, select “App Engine Deployer” from the App Engine tab (shown below). This allows specific permissions to your CI.
- On the next page you click on “CREATE KEY” and select JSON from the side window thus appearing (shown below).
- Once you click “CREATE” a file will be downloaded, this is your key file and must be stored securely.
Keep the key file safe, we’ll need it for GitLab. Afterwards, permanently delete it. Things could go bad if it ends up in the wrong hands.
Now, let’s tell GitLab what we want to do.
- Head over to your GitLab project, find CI/CD under the Settings menu and expand the Variables card (you’ll see something like below).
- Now enter two variables PROJECT_ID_PRODUCTION which is your GAE project id (e.g. my-awesome-project) and DEPLOY_KEY_FILE_PRODUCTION which is the contents of the JSON key file downloaded above.
Configure your Project
Now that GAE and GitLab are setup, its time to upgrade our repository. This done by crating a YML file named “.gitlab-ci.yml” in the root of the repository.
- Create a “.gitlab-ci.yml” file with contents provided in the snippet below and push it to your repository at the root (for simplicity we are assuming there is just one branch master, However, the process will still work if you merge any other branch into master).
That’s it! once you push something to the master branch the app should start deploying and you can track the progress on GitLab.
By tweaking the YML file you can do all sorts of magic with your CI. Along with assigning different jobs for different branches. Moreover, you can find different configurations for different platforms other than GAE.
If you have a simple project like a web page, developed by only you then you can follow my style of working.
- I always have a development branch where I keep pushing minor code changes just for safe keeping. Note this development branch is not executed by the CI because in the YML file we clearly state to deploy only master branch.
- Once I am satisfied, then I submit a merge request to master.
- When the merge is completed, GitLab picks up the change and starts deploying the master into GAE.
This provides me freedom to push as many code changes as I want without firing up the CI to deploying everything.
I hope you enjoyed the article. Cheers!