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
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.
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
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 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.
This pipeline will deploy your project to dokku automatically. And this is all, thanks for reading.
Top comments (1)
@nromero125 Could you please share the project?