Create Automatic Deploy with GitLab CI

Alfredo Barron
Jun 11, 2020 · 2 min read

Create automatic deploy with GitLab CI in 10 minutes

Step 1. Add public key on GitLab profile

Add the server’s public key to the GitLab profile's public keys. This is done by copying in the contents of cat ~/.ssh/ and pasting them to

Step 2. Add public key to authorized keys

Add the server’s public key to the server’s authorized keys. Inside the server, this is done by simply running this line:

Step 3. Add private key on GitLab project

Add the server’s private key to the Gitlab project variables. This is done by copying in the contents of cat ~/.ssh/id_rsa and pasting them to with name SSH_PRIVATE_KEY

Step 4. Create .gitlab-ci-yml file

Create .gitlab-ci.yml file into root project and put bellow code:

  ## 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.
- ssh-add <(echo "$SSH_PRIVATE_KEY")
## Create the SSH directory and give it the right permissions
- mkdir -p ~/.ssh
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config' ## Optionally, if you will be using any Git commands, set the user name and email.
#- git config --global ""
#- git config --global "User name"
- deploy_production
stage: deploy_production
type: deploy
environment: production
- ssh ubuntu@yourIP "cd /var/www/project && git checkout master && git pull && exit"
- master

Thanks for reading. If you like this… Give me your clapping and follow me!!


Tutorials for Developers Laravel/AWS/Linux

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store