DEV Community

Nelson Romero
Nelson Romero

Posted on

How to deploy to dokku with Gitlab CI

Recently in the company I work for we had the need to do automatic deployments from gitlab to staging or production, but we didn't know how to do it. So I started researching on the internet, but I couldn't find accurate information. After a good time searching and trying I could do it, so I decided to do a tutorial about it.

In this tutorial we will use a Laravel(6.0) application.

Get started

Assuming you already have dokku installed and configured. We need to create our project in gitlab in push some code.

Generating an SSH keypair for Gitlab

We need a SSH keypair to let gitlab push our project to dokku, so we will create a new one. With the next command.

ssh-keygen -t rsa

Alt Text

In this case I’ll left the passphrase empty. The command will generate to files gitlab-test and

Adding the key to dokku

We will add the new key to our dokku instance so that gitlab can deploy for us.

cat ~/.ssh/ | ssh -i "KeyPair.pem"  user@<yourdomainorip> "sudo sshcommand acl-add dokku gitlab-test"

I use the argument -i "KeyPair.pem" because in my case, dokku is hosted in EC2, if your dokku instance is hosted in another provider, you don't need that argument.

cat ~/.ssh/ | ssh user@<yourdomainorip> "sudo sshcommand acl-add dokku gitlab-test"

Adding the key to Gitlab
Print out the content of your private key and copy all the content:

then open gitlab, go to the the project that you want to deploy and go the Settings, then CI/CD.
Alt Text

Expand the Variables tab and add a new variable with the key name SSH_PRIVATE_KEY and the VALUE will be the content of your private key that you copied(let the state unprotected). Then save the variables.

The .gitlab-ci.yml file

Create a file called .gitlab-ci.yml in your project and add the following content:

More info about this file here

image: ilyasemenov/gitlab-ci-git-push

  ## Install ssh-agent if not already installed, it is required by Docker.
  ## (change apt-get to yum if you use an RPM-based image)
  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'

  ## Run ssh-agent (inside the build environment)
  - eval $(ssh-agent -s)

  ## Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
  ## We're using tr to fix line endings which makes ed25519 keys work
  ## without extra base64 encoding.
  - echo "$SSH_PRIVATE_KEY"
  - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -

  ## Create the SSH directory and give it the right permissions
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh

  - deploy

deploy to staging:
  stage: deploy
  environment: staging
    - master
  script: git-push dokku@<yourdomain>:<dokkuappname>

Replace with the domain or ip of your dokku instance and the with the name of your app in dokku. More info about the before_script action here.

The image that we use in the file is a CI runner image that allows to deploy a Gitlab project to a git repo (useful for Dokku, Heroku, Deis, etc.). Here the github repo.

After addd this file, push your code to gitlab and open your project in gitlab, go to the CI/CD options and go to Pipelines. You will see a pipeline in process.

Alt Text

Alt Text

This pipeline will deploy your project to dokku automatically. And this is all, thanks for reading.

Top comments (1)

eamirgh profile image
Amir Ghaffari

@nromero125 Could you please share the project?