Continuous Deployment(CD) using Bitbucket Pipelines and Ubuntu Linux Server
Source — https://www.youtube.com/watch?v=U0TirWGUlqs
In this tutorial, you will learn how to set up Bitbucket Pipelines for a PHP & Node.js application and deploy them to your Ubuntu 18.04 server.
I’m writing this because I wasn’t able to get a step-by-step guide for how to do it from one source. I had to do some research and use different sources(some of which can be found at the bottom of the article) to achieve what I wanted. Hopefully this will be of help to someone.
Let’s get to it, shall we?
For starters, let’s define terms — I was just copying the first results off a google search to be honest so don’t be too harsh on me.
- Continuous Integration(CI) — is a development practice where developers integrate code into a shared repository frequently, preferably several times a day. Each integration can then be verified by an automated build and automated tests.
- Continuous Deployment(CD) — is a strategy for software releases wherein any code commit that passes the automated testing phase is automatically released into the production environment, making changes that are visible to the software’s users.
My primary target was deploying an application to my Ubuntu DigitalOcean server(get some credit when you sign up by clicking here — Full disclosure: I get something in return)with out having to SSH into it and run the deployment script.
Enable Pipelines in Bitbucket
Go to Your repository Settings -> Under the pipelines section, click on Settings -> Click on the Enable Pipelines Switch to enable pipelines.
Pipelines are now enabled.
Next up, we’ll set up SSH keys for our repository.
Set up repository SSH Keys
These are the keys you’ll set up on your production or staging server to enable external logins to your server from bitbucket during the deployment steps which we will discuss later on.
To set these up, got to SSH Keys(still within the Pipelines Section in your repository settings) -> then click on Generate keys. You could use your own keys but there are a couple of sources that have had issues and ended up advice against it. Generating your own keys is the best alternative.
After that, set up Known Hosts. Enter the IP address of your Ubuntu server as the Host address then click Fetch to generate the host’s fingerprint. After the fingerprint is generated, click on the Add Host button.
Next up, we’ll add the public key from our repository to the authorized_keys file of our Ubuntu server.
Adding public key from Bitbucket Repository to Ubuntu server authorized_keys
Login to your Ubuntu server. It’s important to note that SSH login should be enabled for your server. If you haven’t enabled it, kindly follow the steps outlined here.
# Enter .ssh folder
$ cd ~/.ssh
$ sudo nano authorized_keys
# Add the copied public key from bitbucket on a new line then save(there's a 'Copy public key' button on the SSH keys page)
Permanently add private key identity to the authentication agent
To prevent entering the passphrase to your private key while pulling your bitbucket repositories to your servers, you need to persist your identity using ssh-add. By default, the identity is lost every time you log out hence the need to persist it. This is important if your key has a passphrase set up. If not, you can ignore this part.
Open up your .bashrc using the following command.
$ sudo nano ~/.bashrc
Copy the following contents to the bottom of your .bashrc file.
This keeps the ssh-agent running even when you log out of your server and persists your private key identity. To get it going however, you’ll need to log out and login into your server. One you login, the SSH Agent will be initialized and you’ll be requested to add the passphrase to your private key.
Set up a deployment script on your server
This is the script that we will run to deploy your application. It is basically a list of the commands you use to deploy your application on your server.
Run the following commands to set up an executable deployment script.
A sample PHP application deployment script:
cd $root
echo -e '\e[1m\e[34mPulling code from remote..\e[0m\n'
git pull origin master
echo -e '\e[1m\e[34m\nInstalling required packages..\e[0m\n'
echo -e '\e[1m\e[34m\nAPI deployed\e[0m\n'
A sample Node.js application deployment script:
#!/bin/bash
root="/var/www/example.com/html"
cd $root
echo -e '\e[1m\e[34mPulling code from remote..\e[0m\n'
git pull origin master
echo -e '\e[1m\e[34m\nChecking for new npm version and installing..\e[0m\n'
npm install -g npm
echo -e '\e[1m\e[34m\nInstalling required packages..\e[0m\n'
npm install
echo -e '\e[1m\e[34m\nRestarting service..\e[0m\n'
echo -e '\n\e[1m\e[34mDeployment successful\e[0m'
Setting up this script is important as bitbucket will run it once it’s logged into your server.
Once you are done with that, we can now set up the bitbucket-pipelines.yml.
Setting up bitbucket-pipelines.yml
This is the file that defines your build, test and deployment configurations. It can be configured per branch i.e. what tests to run when some code is pushed to master and where it will be deployed. If you are using a staging server, you can set up the server details separate from the production server details.
Bitbucket has made it easy to set this up by providing templates based on the type of application you are running.
c. Set up YML file.
*Note: These are sample yml files. In your script section, you can run the commands necessary for your application.
Setting up bitbucket-pipelines.yml file for our PHP application
This script showcases how you can deploy changes based on different branches.
Setting up bitbucket-pipelines.yml file for your Node.js application
Conclusion
By following this tutorial, you will be able to deploy your applications to your Ubuntu server.
Bitbucket allocates 50 free build minutes per account so keep that in mind as your write your YML files. The builds won’t run if you are past your allocated minutes. Keep the YML files short and sweet.
Thank you for reading ❤. Click here for more blogs
If you find any errors, have any issues or questions don’t hesitate to reach out via the comment section.
Sources:
- Configure bitbucket pipelines — https://confluence.atlassian.com/bitbucket/configure-bitbucket-pipelines-yml-792298910.html
- Branch deployment workflows — https://community.atlassian.com/t5/Bitbucket-questions/How-to-deploy-to-remote-server-after-building-assets-in-pipeline/qaq-p/937061
- Start SSH agent on login — https://stackoverflow.com/questions/18880024/start-ssh-agent-on-login