How to use Gitlab's CI/CD Pipelines with Dokku to Push your Application to Production
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.
Inside the folder we can run git init and add the origin.
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 email@example.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
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 firstname.lastname@example.org "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.
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
.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.