How to deploy to dokku with Gitlab CI
Tags: dokku, laravel, devops
Published: 2019-09-20 02:18:20

---
title: "How to deploy to dokku with Gitlab CI"
published: true
description: 
tags: dokku, laravel, devops
---

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](https://thepracticaldev.s3.amazonaws.com/i/f77f5py9o82gq80iru4f.png)

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

**Adding the key to dokku**

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

```
cat ~/.ssh/gitlab-test.pub | 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/gitlab-test.pub | 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:
-----BEGIN RSA PRIVATE KEY-----
......
......
......
-----END RSA PRIVATE KEY-----

then open gitlab, go to the the project that you want to deploy and go the Settings, then CI/CD.
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/4bl1f45bcewyr72rb4v0.png)

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](https://docs.gitlab.com/ee/ci/yaml/)

```
image: ilyasemenov/gitlab-ci-git-push

before_script:
  ##
  ## 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.
  ## https://gitlab.com/gitlab-examples/ssh-private-key/issues/1#note_48526556
  ##
  - 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

stages:
  - deploy

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

```

Replace <yourdomain> with the domain or ip of your dokku instance and the <dokkuappname> with the name of your app in dokku. [More info about the before_script action here](https://docs.gitlab.com/ce/ci/ssh_keys/README.html).

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](https://github.com/IlyaSemenov/gitlab-ci-git-push).


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](https://thepracticaldev.s3.amazonaws.com/i/yvhg9dxarcfql91dunsp.png)

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/6l4lnvyv3muhtecnia3f.png)


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