gitlab CI-CD — Deploy to Remote Linux servers

Gaurav Parmar
1 min readSep 6, 2019

--

To accomplish the task, you have to use PHP:7.2-apache image to run basic shell commands. you can use any bash shell docker image.

#The code

image: php:7.2-apachebefore_script:- apt-get update -qq- apt-get install -qq git# Setup SSH deploy keys- 'which ssh-agent || ( apt-get install -qq openssh-client )'- eval $(ssh-agent -s)- ssh-add <(echo "$SSH_PRIVATE_KEY")- mkdir -p ~/.ssh- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'deploy_staging:type: deploywhen: manualenvironment:name: stagingurl: website.xyz.comscript:- ssh -p 2122 root@<servername> "cd /var/www/<website>/httpdocs/ &&  git checkout staging && git pull && exit"only:- staging

Before script code does ssh configuration in order to connect remote Linux servers

Deploy script code does SSH on Linux server and runs desired shell commands. Furthermore, you can also run a shell script which is hosted on a remote server.

--

--