Deploy your application to Digital Ocean using Docker and Bitbucket Pipelines
In this article, I’ll show you how to build a CI/CD pipeline for your application using bitbucket pipelines to deploy to a digital ocean droplet.
what we’ll be using.
Quick Workflow Overview
When our team of developers proposes a change to our application, they submit a pull request, that pull-requests get tested, reviewed, and then merged to the master branch. whenever a merge to the master branch is done,
- we’ll build the docker image tagging it with the latest commit id.
- we’ll push the latest image to docker hub
- deploy the latest image to the droplet.
DIGITAL OCEAN SETUP
Before we get started, you’ll have to create a digital ocean account using this link and get $100 free credit. (I also get something in return).
- create a project.
- add a docker droplet to the project you just created.
BITBUCKET SETUP
before we start, let’s enable pipelines on our repo.
- click on pipelines.
- choose docker.
- click
Commit file
to enable pipelines.
Dockerfile
this is a basic Dockerfile to build and run our node app.
next, create a bitbucket-pipelines.yml
file in the root of your project.
Let’s examine the file above.
- Whenever a new commit is made (to any branch), we install npm packages and run tests.
- when a new commit is made to
master
branch, we execute 2 steps.
you can validate your pipeline config using this validator.
Step 1
named Deploy to Registry,
this will build the docker image using the docker file above and push it to your docker hub account under the repository your_name/project_name
with a tag of the commit id. This becomes useful when a particular commit causes an issue in production, we can quickly deploy the last working commit of the application.
Step 2
named Deploy to Droplet
, this step is responsible for deploying the Docker image to the digital ocean droplet. This will ssh into your droplet and run the commands listed under the COMMAND
variable.
Repository Variables.
Bitbucket gives us a way to set variables that act like environment variables for our pipeline. This allows us to securely store sensitive variables our pipeline will need. Bitbucket provides a set of default variables.
In the pipeline above, we set the following variables.
- DOCKER_HUB_USERNAME: your docker hub login name
- DOCKER_HUB_PASSWORD: password for docker hub
- SSH_SERVER: IP address of the droplet we created earlier on digital ocean
- SSH_USER: the ssh user to the digital ocean droplet. usually
root
- SERVER_PORT: the port which our node application will be running on. this should be the same as the port exposed by the docker file.
3030
in this example - CONTAINERS_TO_STOP
to set repository variables, click on Repository Settings > Repository variable
on the repository. lean more about repo variables here
SSH AUTHENTICATION
This final step will allow the pipeline ssh successfully into your droplet.
- go to
Repository settings > ssh keys
- copy and paste your droplet IP into the
Host address
input under theKnown host section
and click onfetch
- the
Fingerprint
will be fetched automatically. - Once the fingerprint is added, click on
Add host
.
5. generate a new ssh key on the same page.
6. copy the public key, ssh into your droplet and add the public key to the authorized_keys
file located at ~/.ssh/authorized_keys
in your droplet.
That’s it. anytime you push to any branch on your repository, the Build and Test
step of your repo should run.
Create a pull request and merge to master. if everything worked,
- you should see the image pushed to your docker hub account
- the image deployed to your droplet on
http://droplet_ip_address:3030
find full source code in the repo here