A collection of gitlab-ci examples
Please note that these workflows are compatible with dokku >= 0.11.6
.
All examples require a SSH_PRIVATE_KEY
environment variable set for the Gitlab CI pipeline. This may be set via a "secret variable". See this doc for instructions on creating a new ssh key. Be careful not to overwrite existing keys on the generating machine by using a new name.
Browse to the repository in question and visit the following path: the Gitlab project > Settings > CI/CD.
Click on Secret variables > Expand
and fill in the blanks.
Key: SSH_PRIVATE_KEY
Value: paste in an 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: Do not check this checkbox unless you know what you are doing
BRANCH
: (optional) The branch to deploy when pushing to Dokku. Useful when a custom deploy branch is set on Dokku.
master
main
CI_BRANCH_NAME
: (optional) The branch name that triggered the deploy. Automatically detected from CI_COMMIT_REF_NAME
.
develop
CI_COMMIT
: (optional) The commit sha that will be pushed. Automatically detected from CI_COMMIT_SHA
.
0aa00d8dd7c971c121e3d1e471d0a35e1daf8abe
COMMAND
: (optional) The command to run for the action.
deploy
deploy
review-apps:create
: Used to create a review app - via dokku apps:clone
- based on the appname
configured in the git_remote_url
. If the review app already exists, this action will not recreate the app. In both cases, the current commit will be pushed to the review app.review-apps:destroy
: Destroys an existing review app.GIT_PUSH_FLAGS
: (optional) A string containing a set of flags to set on push. This may be used to enable force pushes, or trigger verbose log output from git.
--force -vvv
GIT_REMOTE_URL
: (required) The dokku app's git repository url in SSH format.
ssh://dokku@dokku.myhost.ca:22/appname
REVIEW_APP_NAME
: (optional) The name of the review app to create or destroy. Computed as review-$APPNAME-$BRANCH_NAME
if not specified, where:
$APPNAME: The parsed app name from the `git_remote_url`
$BRANCH_NAME: The inflected git branch name
review-appname
SSH_HOST_KEY
: (optional) The results of running ssh-keyscan -t rsa $HOST
. The github-action will otherwise generate this on the fly via ssh-keyscan
.
# dokku.com:22 SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.1
dokku.com ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCvS+lK38EEMdHGb...
SSH_PRIVATE_KEY
: (required) A private ssh key that has push access to the Dokku instance.
-----BEGIN OPENSSH PRIVATE KEY-----
MIIEogIBAAKCAQEAjLdCs9kQkimyfOSa8IfXf4gmexWWv6o/IcjmfC6YD9LEC4He
qPPZtAKoonmd86k8jbrSbNZ/4OBelbYO0pmED90xyFRLlzLr/99ZcBtilQ33MNAh
...
SvhOFcCPizxFeuuJGYQhNlxVBWPj1Jl6ni6rBoHmbBhZCPCnhmenlBPVJcnUczyy
zrrvVLniH+UTjreQkhbFVqLPnL44+LIo30/oQJPISLxMYmZnuwudPN6O6ubyb8MK
-----END OPENSSH PRIVATE KEY-----
All examples below are functionally complete and can be copy-pasted into a .gitlab-ci.yml
file, with some minor caveats:
GIT_REMOTE_URL
should be changed to match the server and app.SSH_PRIVATE_KEY
containing the contents of a private ssh key that has been added to the Dokku installation via the dokku ssh-keys:add
command.GIT_DEPTH
of 0
. All examples below have this option set correctly.For simplicity, each example is standalone, but may be combined as necessary to create the desired effect.
Avoid SSH Host Keyscan: By default, this action will scan the host for it's SSH host key and use that value directly. This may not be desirable for security compliance reasons.
The SSH_HOST_KEY
value can be retrieved by calling ssh-keyscan -t rsa $HOST
, where $HOST
is the Dokku server's hostname.
master
. In the following example, we push to the develop
branch.git
client to enable verbose log outputdokku apps:clone
and dokku apps:destroy
. Review apps are a great way to allow folks to preview pull request changes before they get merged to production.
bin/ci-pre-deploy
can be used to reconfigure the app, as shown in this example.