DEV Community

Booranasak Kanthong
Booranasak Kanthong

Posted on

Gitlab Deploy though SSH

1.) Generate an SSH Key Locally (or Use an Existing One)

Run the following command in your local terminal to generate a new SSH key:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Enter fullscreen mode Exit fullscreen mode

Simply press Enter through the prompts to accept the defaults and create the key pair.

2.) Add the Public Key to the VM Server

On your VM server, append the contents of the generated public key (typically located at ~/.ssh/id_rsa.pub) to the authorized_keys file:

cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
Enter fullscreen mode Exit fullscreen mode

3.) Add the Private Key to Your GitLab Repository

To allow GitLab CI/CD jobs to connect to your VM via SSH:

3.1) Go to your GitLab repository.
3.2) Navigate to Settings → CI/CD → Variables.
3.3) Click "Add variable".
3.4) Set the key (e.g., SSH_PRIVATE_KEY) and paste your private key (from ~/.ssh/id_rsa) as the value.
3.5) Make sure to mask and protect the variable as appropriate.

Never share your private key. Treat it like a password.

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.