Uploading files to server from GitLab CI using scp

This tutorial will show you how to use the GitLab Continuous Integration service to upload files to server using the scp command line tool. This tutorial assumes you already have a working GitLab CI configuration created.

Setting up ssh keys

SSH keys are the most secure way to log in to your server. On top of that, they are also a great way to avoid typing your password every time. Setup your ssh keys using the ssh-keygen tool and make sure you do not use a passphrase.

Create necessary GitLab variables

We're going to use the following variables:

Add them under Settings -> CI/CD -> variables and mark them as protected.
IMPORTANT NOTE! When copying your private ssh key, also include the header and footer with all the dashes!

Setup the CI job

First, create a new job (we'll call it "deploy"):

deploy:
  stage: deploy
  when: manual
  only:
	- master
  script:
	- scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -r ./folder/** $USERNAME@$HOST:/path/

Now, ssh needs some work to be done to get your keys working, so add this under your "deploy" job:

before_script:
  # the following script is taken from https://docs.gitlab.com/ee/ci/ssh_keys/
  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
  - eval $(ssh-agent -s)
  - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh

Now, you're all set up! Run the job and all files from ./folder will be uploaded to /path on your server! This script is set to run only when invoked manually and will only care about files on the "master" branch.