Skip to content

Instantly share code, notes, and snippets.

@buckle2000
Last active November 15, 2018 20:56
Show Gist options
  • Save buckle2000/935db75bb1767d63cae3e48730bbc52d to your computer and use it in GitHub Desktop.
Save buckle2000/935db75bb1767d63cae3e48730bbc52d to your computer and use it in GitHub Desktop.
How to push from Gitlab CI to Dokku

How to deploy to Dokku using Gitlab CI

In this tutorial, we are using a custom docker image to push a git repo to a Dokku deployment (in fact, any remote git repo) in Gitlab CI runner.

Prerequisites

Make sure you have a Gitlab account and a Dokku project hosted on Gitlab. This method works whether if you are using buildpacks or Dockerfile.

Make sure you have set up an app on the remote machine following these instructions and can successfully deploy to it from the local machine.

Add a secret variable

Visit "the Gitlab project > Settings > CI/CD." (or https://gitlab.com/**username**/**repo-name**/settings/ci_cd)

Click on "Secret variables > Expand" and fill in the blanks.

Key: SSH_PRIVATE_KEY

Value: paste in an armored SSH private key registered in Dokku:

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

Environment scope: production (This make sure that SSH_PRIVATE_KEY is not available on merge requests or tests)

Protected: don't check this checkbox unless you know what you are doing

Add CI script

Add a file named exactly .gitlab-ci.yml at the root directory of the repo.

Here is a minimal Example:

stages:
  - deploy

deploy to dokku: # job name, can be anything
  image: ilyasemenov/gitlab-ci-git-push
  stage: deploy
  environment: production
  only: [master] # only deploy and set SSH_PRIVATE_KEY on master branch
  script: git-push ssh://dokku@<hostname>/<app name> # replace these with appropriate names

Commit this file.

And it is done

Try git push to Gitlab and visit "project > Pipelines" to see the deployment log.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment