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

CI/CD CI/CD
DevOps DevOps
Dokku Dokku
GitLab GitLab

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
☝️ Note:
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"

4. 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_NAMEAPP_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.

Did You Enjoy the Tutorial?

Consider sending some love to Freddie the creator.

Comments (0)

You need to be logged in to comment..

Topics Mentioned in this Tutorial

CI/CD
1
0
7
CI/CD stands for Continuous Integration and Continuous Delivery and is used in the world of software engineering. The CI/CD process helps software engineers to push code changes more frequently and reliably by creating pipelines that handles version control, staging and production deployment, all in one step....
DevOps
6
0
24
DevOps stands for Development Operations and is a set of practices used to improve software development and information-technology operations. Part of the DevOps responsibility is to setup servers, integrate continuous integration and continuous deployment (CI/CD) to maintain and improve the development life cycle....
Dokku
7
0
0
With Dokku web developers can build their own PaaS (Platform-as-a-Service) with help of Docker and open sourced Heroku build packs. Dokku Installs on any hardware and drastically lowers your cost as Dokku is both free and open source. The dokku setup process is extremly simple and only takes a matter of minutes to complete. Once Dokku is installed and your PaaS is ready for use you can push your local development project using Git. Which builds your applicaion using Heroku buildpacks that runs in isolated Docker containers. As a result, you can run your own PaaS on the cloud provider of your c...
GitLab
1
0
0
GitLab is a web-based DevOps lifecycle tool and open source platform that provides a Git-repository manager providing wiki, issue-tracking and CI/CD pipeline features. GitLab is an open core project providing some of its software open source and others closed source. GitLab CE is open source and Gitlab EE is closed source, which makes GitLab more open than its competior GitHub which is fully closed source....