Gitlab CI how to deploy an application via SSH

I use Hosted Gitlab to host my Git repositories, and most recently I used it to create / deploy PHP and Java applications on servers.

What I would like to do is when the build is complete, deploy the application using SSH. Sometimes it can just be loading the contents of the final assembly (PHP files) to the server via SSH, or in other cases it can be downloading the compiled .jar file and then executing a command on the remote server to restart the service.

I installed my own Docker container as the build environment, including things like Java, PHP, Composer, and Maven that I need to build. I use this image to start the build.

I would like to know how I can use SSH on an external server in another to execute the deployment commands that I can specify in the gitlab-ci.yaml file?

+19
source share
3 answers

You can save your SSH key as a secret variable in gitlab-ci.yaml and use it during build to execute SSH commands, see our documentation here for details.

Once accessed via SSH, you can use commands such as rsync and scp to copy files to your server. I found an example of this in another post here , which you can use as a reference.

+32
source

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.

+5
source

Working with ssh on gitlab.com is not so simple.

This is why I wrote the SSH helper for .gitlab-ci.yml.
You can check it here https://gitlab.com/x4v13r/gitlab-ci

Just include: this is in your .gitlab-ci.yml, and then you can go with:

ssh_run root myhostname $ MYHOST_PKEY "touch foo; cp foo bar; ls -al; rm foo bar; ls -al"

+2
source

Source: https://habr.com/ru/post/1015592/


All Articles