Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gitlab CI how to deploy an application via SSH

I'm using Hosted Gitlab to host my Git repositories, and more recently I've been using it to build/deploy PHP and Java applications to servers.

What I'd like to do is once a build is complete, deploy the application using SSH. Sometimes this might just be uploading the contents of the final build (PHP files) to a server via SSH, or other times it may be uploading a compiled .jar file and then executing a command on the remote server to restart a service.

I've set up my own Docker container as a build environment, this includes things such as Java, PHP, Composer, and Maven all that I need for builds to complete. I'm using this image to run builds.

What I'd like to know is, how can I SSH into an external server in order to perform deployment commands that I can specify in my gitlab-ci.yaml file?

like image 525
SheppardDigital Avatar asked Mar 08 '17 16:03

SheppardDigital


People also ask

Can you SSH into GitLab?

Log into GitLab and click on your account preferences. Click the SSH Keys link and paste the copied value into the text field. Set an expiration date, and then click the blue button to persistently add the GitLab SSH key. Configure GitLab SSH keys under your account preferences.

Does GitLab runner use SSH?

You can generate the SSH key from the machine that GitLab Runner is installed on, and use that key for all projects that are run on this machine.


1 Answers

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

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

like image 84
Adam Mulvany Avatar answered Oct 12 '22 22:10

Adam Mulvany