How to Deploy Your Application to Digital Ocean Using GitHub Actions And Save up on CI/CD Services Costs

Mulubwa
The Startup
Published in
6 min readNov 13, 2020

Things you need to achieve this process:

  1. A GitHub account where you repository will be hosted
  2. A Digital Ocean account where you will create your server on a droplet
  3. Basic knowledge of Git,GitHub and GitHub actions
  4. Basic knowledge of Linux commands

STEP 1

CREATE A NEW REPOSITORY THAT WILL HOST YOUR APPLICATION ON GIT HUB

In this step,we will head over to our GitHub account and create a new repository that will host our desired application. Follow this link to see steps on how to create a repository on GitHub: https://docs.github.com/en/free-pro-team@latest/github/getting-started-with-github/create-a-repo

To clone the repository below and skip step 1,2 and 5. Click on this link : https://github.com/Mulubwa17/ci-cd-to-digitalocean-with-github-actions

In this case ,I created one and named it as shown below :

STEP 2

CREATE A GITHUB ACTIONS WORKFLOW FILE TO HANDLE YOU CI/CD PIPELINE TO DIGITAL OCEAN

In the newly created repository,click on the Actions tab and create your a yaml file .This file is called a workflow and it will have all the necessary steps to deploy to digital ocean from GitHub.

For more notes on GitHub actions and creating workflows,click on the clink : https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/introduction-to-github-actions

STEP 3

CREATING A SERVER ON DIGITAL OCEAN AND GENERATING SSH KEYS

In this step we will create a server on a droplet in Digital Ocean and generate the ssh keys we will need for our operation.To create a serve on Digital Ocean,follow the steps in this link: https://www.digitalocean.com/docs/droplets/how-to/create/

In this case ,i have already created my server as seen below:

Next,we ssh into the server and we will create our ssh keys:

ssh command : ssh user@ip

key generation command : ssh-keygen

Now that we have generated our ssh keys,the following steps will be the most crucial in ensuring our operation succeeds:

I. Copy your server pubic key and add it to you GitHub ssh and gpg keys list,this will enable the repository to be cloned from GitHub to the server.

To read more on copying keys from GitHub to servers and vice versa,check out this link: https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/adding-a-new-ssh-key-to-your-github-account

To copy the public key,enter the command : cat .ssh/id_rsa.pub as shown below:

II. Go to you GitHub profile settings and add the copied public key into the SSH AND GPG KEYS tab as shown below:

III. On the server terminal window, type in the command : nano .ssh/authorized_keys, paste the copied public ssh key from I. into the file,press Control+X and accept changes.

Then type in the command : chmod 700 .ssh/authorized_keys to give permission to keys in that folder to perform ssh tasks as shown below:

Note

For Ubuntu 20 or higher

If you are using OpenSSH (this is the default ssh agent)

If you are currently using OpenSSH and are getting the error:

Ssh: handshake failed: ssh: unable to authenticate, attempted methods [ none publickey]

Make sure that your key algorithm of choice is supported.On Ubuntu 20.04 or higher you must explicitly allow the use of the ssh-rsa algorithm.

Add the following line to the end of your OpenSSH daemon file(which is

nano /etc/ssh/sshd_config :

HostbasedAcceptedKeyTypes +ssh-rsa
HostKeyAlgorithms +ssh-rsa
PubkeyAcceptedKeyTypes +ssh-rsa

Restart ssh daemon :

sudo systemctl restart sshd

Restart the server :

sudo reboot

Ssh back and restart the ssh daemon again.

Alternatively, ed25519 keys are accepted by default in OpenSSH. You could use this instead of rsa if needed:

ssh-keygen -t ed25519 -a 200 -C "your_email@example.com"

STEP 4

ADDING SECRETS IN OUR REPOSITORY THAT WILL ENABLE SSH ACCESS

In this step,we will be adding our server ssh information as secrets into our repository settings,so as to enable our GitHub actions workflow to ssh into our server.

To create secrets,click on the settings tabs on your repository page and go to the secrets tab, we will start by creating a secret that will store our host’s IP address and we will name it SSH_HOST as shown below:

Next we copy our private key using the command : cat .ssh/id_rsa from the server as shown below:

And we will create a secret that will store our server’s private key and we will name it SSH_KEY as shown below:

Finally,we will create a secret that will store the user of the server we are going to ssh into ,in this case the user is “root” and we will call our secret SSH_USERNAME as shown below:

The root user is the administrative user in a Linux environment that has very broad privileges. Because of the heightened privileges of the root account, you are discouraged from using it on a regular basis.To create a user on the server that is not root,check this link: https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-20-04

After adding all our secrets,the tab will display as below:

STEP 5

CONFIGURING OUR DEPLOY.YAML FILE TO EXECUTE AFTER A COMMIT AND PUSH TO MAIN

In this step,we will add jobs to run in our workflow file to execute the GitHub action.

In our deploy.yaml file,we will add the following steps that will enable our continuous integration and deployment to run after every push to the main(formerly known as master branch) :e will add the following steps that will enable our continuous integration and deployment to run after every push to the main(formerly known as master branch) :

After adding the above ,we can go on and commit our file to our main branch.

Our deploy.yaml file will look like this:

Make sure to follow the line indents when pasting into the yaml file.

STEP 6

CONFIRMING OUR ACTION HAS EXECUTED AND VIEWING THE ACTION LOGS

In this step,will check our workflow run to make sure our actions have executed accordingly and verify from the action logs.

Head to the ACTIONS tab on our repository page to check if our action is running and has you can see our action has run and completed successfully as shown below:

To confirm, we will click on our action job and check the logs of the process to confirm everything run successfully and as you can see our action completed perfectly as shown below:

STEP 7

CHECK OUR DEPLOYED REPOSITORY ON THE SERVER

Here we will ssh into our server and check that our repository has been successfully deployed into the folder “test” as seen below:

To list directories : ls

To enter directory: cd test

To list files/folders in test directory: ls

There you have it,you have successfully deployed to Digital Ocean using GitHub actions,thus saving you costs of running and using a paid continuous integration and deployment service.

--

--

Mulubwa
The Startup

Software Developer (JavaScript{Typescript,NodeJS}, PHP, Python ) and Mobile Apps(Flutter)