Just as an example, lets suppose you have a server with requirements already installed 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 ubuntu image. Also notice that we are using some gitlab secret variables. $QA_URL, $PRIVATE_KEY, $DB_CONNECTION, $QA_SERVER.
The important ones are $PRIVATE_KEY and QA_SERVER. Private key is the one you need to authenticate with the QA_SERVER (if you are using private key). And obviously QA_SERVER is the address that you want to deploy your code.
For creating new variable access gitlab->settings->CI/CD.
Within before_script what we are doing is creating and adding ssh key, also we are disabling command line to ask for password. 'StrictHostKeyChecking no'
ssh-add <(echo "$PRIVATE_KEY")
Add ssh key to the agent.
ssh -o StrictHostKeyChecking=no user@"$QA_SERVER" 'rm -rf /var/www/html/*'
Not required: this line uses ssh for deleting any file within /var/www/html
scp -P22 -r . ubuntu@"$QA_SERVER":/var/www/html
Finally, files are copied from current directory to /var/www/html
Be careful with permissions, it depends of the directory you want to copy.