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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With