How to apply Continuous Deployment zero cost with Bitbucket Pipelines and Ansible

Recently, we developed a project coinvn.me about comparing price cryptocurrency in Vietnam market.

We developed this site for fun and contributed to community. That’s why we chose the free services.

  1. Google Cloud Engine : free 300 USD credit for servers
  2. Bitbucket for source control tool and CI/CD: 50 free minutes per month.
  3. Ansible for deployment scripts.
  4. CloudFlare for CDN

This tool is very simple. It is a service to get price of cryptocurrencies from exchanges. So we want to make simple deployment process. When there is any commit on master branch, the bitbucket pipelines will trigger the Ansible to deploy the application on GCE. It is straightforward process

Firstly, to kick off your CI/CD pipelines, you need to create a file `bitbucket-pipelines.yml`

Our application was deployed by Ansible. So the content of `bitbucket-pipelines.yml`

image: python:2.7.13
pipelines:
default:
- step:
caches:
- pip
script:
- pip install ansible==2.4.1.0
- git clone git@bitbucket.org:coinvn/coin-ansible.git
- cd coin-ansible
- ansible-playbook -i inventory/hosts -e "hosts=prod" -e "version=master" --private-key /opt/atlassian/pipelines/agent/data/id_rsa deploy_coin_backend.yml
definitions:
caches:
pip: ~/.cache/pip

Let me explain a little bit about this file.

Because Ansible has been developed by Python . So I chose base image is Python 2.7.x. Next, I used script block to install Ansible package. Then, we clone the Ansible repo. Finally, We run command deploy our application by Ansible script.

Pipelines need to pull the Ansible repo and access the remote servers. So we need to add the private in Pipelines. ( Settings -> SSH keys )

As a result, we applied CD for our project.

I welcome any feedback of you guys. Please ask me if you have any question.