Skip to content

Instantly share code, notes, and snippets.

@ahmadawais
Created August 1, 2018 03:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmadawais/d64fec03c29cd42cbd8cb0df5a27c1b3 to your computer and use it in GitHub Desktop.
Save ahmadawais/d64fec03c29cd42cbd8cb0df5a27c1b3 to your computer and use it in GitHub Desktop.
GitLab Deploy To Server

Connect to your server

First SSH into your server:

ssh username@domain.com -p 7822 (the 7822 is the port number and may be different on your server)

Go to the directory when you want to have the GitLab repo deployed to. Next get the GitLab ssh url from the GibLab project it will be like git@gitlab.com:username/project.git

To clone the repo type

git clone git@gitlab.com:username/project.git

On the first time you may get this warning:

The authenticity of host 'gitlab.com (52.167.219.168)' can't be established.
RSA key fingerprint is b6:03:0e:39:97:9e:d0:e7:24:ce:a3:77:3e:01:42:09.
Are you sure you want to continue connecting (yes/no)?

Enter yes, next you will get:

Warning: Permanently added 'gitlab.com,52.167.219.168' (RSA) to the list of known hosts.
Connection closed by 52.167.219.168
fatal: The remote end hung up unexpectedly
Now trying to do a git clone again results in the error:

Initialized empty Git repository in /home/username/public_html/project/.git/
Permission denied (publickey).
fatal: The remote end hung up unexpectedly

The reason for this is the server has not been authenticated again.

Create a public/private key

To get setup follow these steps:

type

ssh-keygen

The output:

Generating public/private rsa key pair.
Enter file in which to save the key (/home/mvc/.ssh/id_rsa):

Press enter to accept the default path

Enter passphrase (empty for no passphrase):

Press enter to accept no password or enter a password first.

The response will look something like:

The key's randomart image is:
+--[ RSA 2048]----+
|        .. .  .oB|
|       . .. + .++|
|        . o. o...|
|         o   .   |
|        S   .. . |
|           o. o  |
|          o..+   |
|          ....o  |
|           oE.   |
+-----------------+

Get the public key

Go to .ssh folder

cd ~/.ssh
cat id_rsa.pub

Copy the full key.

On Gitlab go to Settings -> Repository -> Deploy Keys

Add a title this is a reference so it can be anything then copy the key into the key box.

Now you should be all set up so once again go to where you want the repo to be pulled into and again type

git clone git@gitlab.com:username/project.git

the output will be:

initialized empty Git repository in /home/username/public_html/project/.git/
remote: Counting objects: 3048, done.
remote: Compressing objects: 100% (2125/2125), done.
remote: Total 3048 (delta 688), reused 2994 (delta 648)
Receiving objects: 100% (3048/3048), 9.83 MiB | 5.81 MiB/s, done.
Resolving deltas: 100% (688/688), done.

This will create a directory and pull in the repo contents.

Now anytime you want to update go into the directory and type

git pull
ssh-keygen # Press enter to everything, no passwords.
cat ~/.ssh/id_rsa.pub # Copy the full key on Gitlab go to `Settings -> Repository -> Deploy Keys`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment