Nowadays we cannot work without continuous integration and deployment, it is no secret. Developers and QA engineers have long used tools to save themselves time and avoid mistakes thanks to the automation of tasks and deployments. At Solid GEAR we develop custom software systems using Agile methodologies, our customers see the product we are building together early and iteratively so we can get their feedback and incorporate it into the product. Let’s suppose that every time we finish an iteration of development we want to deploy a web application for the customer, we would have to take into account several steps:
- Compile de application
- Configure the connection with external services if necessary
- Deploy the application in a Staging environment
As we can see, these are repetitive tasks that can be executed without supervision, automatically. In addition to the time savings, we also minimize the probability of human error when performing manual tasks. If we also want our system to be robust, automatic tests cannot be missing, so one more step would be added in each deployment. At Solid GEAR we lay on continuous integration and continuous deployment to be more agile and efficient when it comes to showing the product to the customer.
In this post, we will configure a continuous deployment in The Cloud.
We will deploy an Angular application in Google App Engine automating the continuous deployment. With this we will be able, for example, to accelerate the deployment of MVPs or launch environments to test the application very quickly and most importantly: without having to modify any code, preventing human origin errors. The first thing we are going to do is configuring Google Cloud to use its services.
Google Cloud Configuration
The next step is creating a project in Google Cloud, if you do not have an account you can register for free, you will receive $ 300 to try Google Cloud services, you can read more about it here.
Create a new project in Google Cloud
- If you have never entered Google Cloud Platform, you will be prompted to accept the terms of service.
- In the top bar, a dropdown saying “Select a project” will appear in case you have never created a project if you had created a project previously, a project name will appear.
- Click on “New Project”, enter the name and click on create.
- Once it has been created, the name of the project will appear in the dropdown where it says “Select A Project”, select it from the dropdown if it does not appear.
Create a service account in Google Cloud To use Google Cloud from Gitlab we need a user who is registered in our Google Cloud project as an Editor, for this we will create a Google Cloud service account by following the steps below:
- In the side menu, click on “IAM and Administration” and within this, on “Service Accounts”
- Click on “Create Service Account”
- Enter name and description, then click on create
- Select the role “Editor” and click on continue
- Create a key in JSON format (a file will be downloaded) and click ready
Activate the App Engine Administration API
For the service account to use any of the services offered by Google Cloud, the proper API must be enabled, in this case, Google App Engine administration API.
- In the side menu, look for API and Services
- Within this section, click on Library
- Look for App Engine Admin API and click on enable
With this last step, all the Google Cloud configurations to deploy an application on Google App Engine would be finished.
Having all the Google Cloud part ready, let’s start with the Gitlab part. I have already created a repository with a simple application to be able to deploy it, you can clone or fork it from here.
Creating a new project (skip this section if you have forked the repository)
- Click on the “+” in the navigation bar, then click on “New Project”
- Choose a name and level of privacy, then click to create.
Clone the previously created repository
git clone https://gitlab.com/luis.revilla.zan/avengers-app.git
Now, push it to the repository of the project that was created within the previous step.
git remote rename origin old-origin git remote add origin [clone URL from your Gitlab repository] git push -u origin --all git push -u origin --tags
Now the code should be living on your Gitlab repository.
We are one step away from deploying this application in Google Cloud, you just need to configure the keys from the previously created service account to be able to use them in Gitlab and create the two configuration files:
- app.yaml: File specifying Google App Engine settings
- .gitlab-ci.yml: File that configures the continuous integration of Gitlab
Configuring the service account keys:
- Within the project, Go to the setting, then click on the “CI / CD” section
- Expand the variables section
- Change the type of variable to “File”, the key will be named GOOGLE_SERVICE_ACCOUNT_FILE and in the value the content of the file that has been previously downloaded (when the service account has been created)
- Create another variable, in this case, the key will be named GOOGLE_PROJECT_ID and the value will be the ID of the project that we have created in Google Cloud
The application deployed will be an Angular application so we want to expose the files compiled by the
ng build --prod, so we will configure this file as it follows.
runtime: python27 threadsafe: yes service: avengers-app handlers: - url: /(.+) static_files: dist/\1 upload: dist/(.*) - url: / static_files: dist/index.html upload: dist/index.html skip_files: - ^(?!dist)
- threadsafe indicates if you want to use concurrent file upload requests or not (in this case we do not want concurrent requests)
- service indicates the name of the service to be created
- handlers indicates which files run in each url
- skip_files indicates the files that will not be uploaded
To use the Google Cloud SDK on Gitlab CD we need Google’s cloud-sdk Docker image, which is published on Docker Hub, in this case, we will use the slim version as it brings the functionality we need. The file would be as the following one:
image: google/cloud-sdk:slim stages: deploy deploy app engine: stage: deploy script: - gcloud auth activate-service-account --key-file $GOOGLE_SERVICE_ACCOUNT_FILE - gcloud app deploy app.yaml --quiet --project $GOOGLE_PROJECT_ID --version 1 only: - develop
This creates a “Deploy” stage on Gitlab CD with a “deploy app engine” job that runs when there are new commits on the develop branch. This work is authenticated with the service account data previously created and deploys the service indicated in the app.yaml on Google App Engine. As you can see, it is easy having something that can be shown, and not only that but as the project progresses, the product iterations will have an almost zero cost in terms of deployment, so we can save many hours over of the course of the project, which we can devote to create more tests to have a robust and bug-free application, for example. I hope this is useful for you all!