As an example, let's assume that you have a server with predefined requirements and you want to deploy to that server using ssh.
image: ubuntu:latest stages: - deploy deploy_QA: stage: deploy environment: name: Staging url: "$QA_URL" before_script: - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )' - mkdir -p ~/.ssh - eval $(ssh-agent -s) - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config' script: - ssh-add <(echo "$PRIVATE_KEY") - ssh -o StrictHostKeyChecking=no user@ "$QA_SERVER" 'rm -rf /var/www/html/*' - scp -P22 -r . ubuntu@ "$QA_SERVER":/var/www/html
First, in this example, we are using an Ubuntu image. Also note that we use some secret gitlab variables. $ QA_URL, $ PRIVATE_KEY, $ DB_CONNECTION, $ QA_SERVER. The important ones are $ PRIVATE_KEY and QA_SERVER. The private key is the one you need to authenticate with QA_SERVER (if you use the private key). And, obviously, QA_SERVER is the address where you want to deploy your code.
To create a new variable, go to gitlab-> settings-> CI / CD.
As part of before_script, we create and add the ssh key, and also disable the command line to request a password. 'StrictHostKeyChecking no'
ssh-add <(echo "$PRIVATE_KEY")
Add the ssh key to the agent.
ssh -o StrictHostKeyChecking=no user@ "$QA_SERVER" 'rm -rf /var/www/html/*'
Not required: this line uses ssh to delete any file in / var / www / html scp -P22 -r. ubuntu @ "$ QA_SERVER": / var / www / html Finally, the files are copied from the current directory to / var / www / html
Be careful with permissions, it depends on the directory you want to copy.
source share