How to use Gitlab's CI/CD Pipelines with Dokku to Push your Application to Production

How to use Gitlab's CI/CD Pipelines with Dokku to Push your Application to Production

GitLab is a DevOps lifecycle tool similar to that of GitHub. With GitLab we can host our git-repository and utilize its CI/CD pipeline features to push our development code to production for free.

In this tutorial I will show you how you can use GitLab's CI/CD pipelines to push our development project to our Dokku instance.

1. Create a New Gitlab Project

Start by logging in to your Gitlab account, if you don't have one you can create it for free at gitlab.com. GitLab allows as to have both public and private repos, you choose if you want your project shown to the world or not.

To get stared we create a new project by following the link or clicking the green button "New Project".

2. Connect your development project with gitlab

When you have created your new project we can go ahead and navigate to your local application folder via the terminal.

cd application-folder

Inside the folder we can run git init and add the origin.

git init

When adding the origin make sure that you specify your GitLab user name and project name specific to your account. You can find the correct information in "Command Line Instructions" given to you when you created the new GitLab project.

git remote add origin git@gitlab.com:Username/projectname.git

We can then go ahead and push our local application to our Git repository.

git add .
git commit -m "Initial commit"
git push -u origin master

To be able to utilze GitLab CI/CD Pipelines we need to create an SSH key that GitLab can use to authenticate and push the code to our server.

Create a new SSH key that will only will be used for this purpose.

ssh-keygen -t rsa -f name-of-key

☝️ Important

Do not use your regular SSH Key, create a new one for this specific use-case

Copy and add the public key to your dokku instance.

cat ~/.ssh/name-of-key.pub | ssh root@123.456.789.101 "sudo sshcommand acl-add dokku name-of-key"

3. Add your SSH private key to GitLab

Go to your project > settings > CI/CD and then find the section Variables, and click add variable.

Set the key to SSH_PRIVATE_KEY

Select type var

Set the value to the contents of your newly created private key

-----BEGIN RSA PRIVATE KEY-----
......
......
......
-----END RSA PRIVATE KEY-----

You can get the contents of your private key by running the cat command in your terminal.

cat ~/.ssh/name-of-key

5. Prepare your project for GitLab CI/CD

To use GitLab CI/CD Piplines to push our code to production we need to add .gitlab-ci.yml file to our project.

image: ilyasemenov/gitlab-ci-git-push

stages:
  - deploy

variables:
  APP_NAME: backend
  APP_URL: yourdomain.com

deploy:
  image: ilyasemenov/gitlab-ci-git-push
  stage: deploy
  environment:
    name: production
    url: https://$APP_NAME.dokku.me/
  only:
    - master
  script:
    - git-push ssh://dokku@$APP_URL:22/$APP_NAME

Our .gitlab-ci.yml file uses image ilyasemenov/gitlab-ci-git-push to make the push possible.

Make sure to specify the variables APP_NAME, APP_URL according to your project.

In the next tutorial we will take a closer look how we can install an SSL certificate so that we can serve our application over HTTPS (https://). Dokku provides a Let's Encrypt plugin that makes it super simple to setup free SSL certificates which can be automatically auto renewed when it's close to reaching it expiration date.

Freddie Freddie 2 years, 10 months ago 0
Login to Comment
No comments have been posted yet, be the first one to comment.
10 Ways to Manage Environment Variables with Dokku Config Commands
10 Ways to Manage Environment Variables with Dokku Config Commands
Dokku is an awesome open-soure project that makes it really simple to setup and manage your own docker-powered PaaS (Platform-as-a-Service). If you're a solo developer or working in a small team with a low budget, Dokku is godsend. Most applications requires some sort configuration to...
How to Install & Setup Redis for Dokku
How to Install & Setup Redis for Dokku
Redis is a free and open-source in-memory data store which can be used as cache or in same cases even a database. Redis is super simple to install if you manage your applications with your own Dokku PaaS (Platform as a Service). In today's tutorial I will show you how we can install a...
Setup SSL Certificates & Serve your Applications over HTTPS for Free with Dokku & Let's Encrypt
Setup SSL Certificates & Serve your Applications over HTTPS for Free with Dokku & Let's Encrypt
In this tutorial we are going to take a look at how we can setup free SSL certificates for our Dokku powered applications with Let's Encrypt. Let's Encrypt is a non-profit organization that provides TLS (Transport Layer Security) encryption at no charge. Their goal is to make the web ...