Deploy to Google Cloud Compute Engine with Bitbucket Pipelines

Bitbucket pipelines is a great feature that lets you add ‘Continuous Integration’ and ‘Push to Deploy’ to your deployment process quite easily. This entire process is quite flexible in terms of that you can setup a complete custom build environment using Docker images.

Lets talk about using bitbucket pipelines when your apps are deployed on Google Cloud Platform. If your apps are deployed to typical app engine (Google managed), then this job is relatively easier and you will find a lot about how to set things up on web. For example:

https://cloud.google.com/solutions/continuous-delivery-bitbucket-app-engine

You simply have to add an app.yaml file to your repo and execute the following in deployment script.

gcloud app deploy

But what if your app is deployed to a compute engine instance. “gcloud app deploy” can only deploy apps to Google managed instances. In this post we will see how to setup bitbucket pipelines when your apps are deployed to compute engine instances.

Google cloud compute engine instances give you greate flexibility in terms of that they are fully managed VM instances where you have full control over them. By that I mean you have ssh access over those intances. Yes you gussed it right, you can let bitbucket pipelines access your instances using ssh and then do whatever you want.

First you need to generate SSH keys so that bitbucket can communicate with compute engine instance. SSH to your compute engine instance. Run the following commands:

ssh-keygen -t rsa -f ~/.ssh/my-ssh-key -C [USERNAME]
chmod 400 ~/.ssh/my-ssh-key

Replace [USERNAME] with your username. Now copy the contents of your private and publics keys using:

cat ~/.ssh/my-ssh-key
cat ~/.ssh/my-ssh-key.pub

Now go to Bitbucket >> <your repo> >> Settings >> Pipelines >> SSH keys. Add your private and public keys here and click on ‘Add key pair’. Now add your intance IP address to ‘Host address’ field and click on ‘Fetch’ button in front of it. This will reveal host’s fingerprint, then click ‘Add’. If the fingerprint comes up successfully then it means that bitbucket can now access your instance. At this point you have successfully setup the SSH access to your compute engine instance.

Now following are the steps to update code on your instance from repo:

  • zip code files that you want to deploy.
  • copy zip file to remote instance to an accessible directory. Note that you can access directories on remote instance only which are accessible to the user you used to login to ssh. If you logged into ssh session using user ‘xyz’ then an obvious path accessible to this user on remote instance will be /home/xyz. You can first copy files from repo on this path and then ssh to your instance and run remaining commands there.
  • ssh to remote instance and run your deployment script there.

For example here is a sample bitbucket-pipelines.yml file:

image: samueldebruyn/debian-git

pipelines:
 branches:
  staging_app:
  - step:
     script:
       - apt-get update && apt-get install -y unzip && apt-get install -y zip
       - zip -r new.zip *
       - scp new.zip <user>@<host>:/home/<user>/new.zip
       - scp scripts/deployment.sh <user>@<host>:/home/xyz/deployment.sh
       - rm new.zip
       - ssh <user>@<host> 'sudo chmod 755 /home/<user>/deployment.sh && /home/<user>/deployment.sh $BITBUCKET_COMMIT && sudo rm /home/<user>/deployment.sh && exit'

Replace <user> and <host> with the values you used to configure your SSH keys. “deployment.sh” is a bash script that you can keep inside your repository and that contains commands to execute within your ssh session. For example in that bash script you will want to copy uploaded files to /var/www/html directory or run any other deployment commmands. $BITBUCKET_COMMIT is just a representation of passing default variables from bitbucket environment to your bash script.

You can also use bitbucket pipelines to perform CI. You will just need to use the right build environment. For example if your application is PHP based, you can use the following configuration in your bitbucket-pipelines.yml file:

image: php:7.1.1
pipelines:
 default:
  - step:
   script:
    - phpunit <test files>

 

Leave a Reply

Your email address will not be published. Required fields are marked *