GitLab CI Deploy to EC2 using SSH

In this article we are going to cover GitLab CI Deploy to EC2 using SSH | GitLab CI Deploy to Remote Server | Deploy Code to AWS EC2 Instance using SSH with GitLab CI/CD Pipeline.

#1:Install Gitlab-Runner on Ubuntu EC2 Instance

Install GitLab Runner using below command to install latest GitLab Runner visit official GitLab Runner page.

Add the official GitLab repository using below command

curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | sudo bash

To Install the latest version of GitLab Runner on Ubuntu EC2 Instance

sudo apt-get install gitlab-runner

#2:Grant sudo Permission to GitLab Runner User

After install GitLab Runner you will see gitlab-runner user in /home directory

cd /home
ls

Output:

gitlab-runner ubuntu

Add the gitlab-runner user in sudoers group and set NOPASSWD as shown below

sudo visudo
gitlab-runner ALL=(ALL:ALL) ALL
ubuntu ALL=(ALL:ALL) ALL
GitLab CI Deploy to EC2 using SSH 1
root

#3:Register GitLab Runner to GitLab on Ubuntu

1. First login to GitLab Server with Username and Password.

login to gitlab server with username and password

2. Click on your project and select Settings

goto gitlab project settings

3. Navigate to Settings and click on CI/CD inside this click on Expand of Runners section

goto gitlab CICD and expand

4. Copy GitLab server URL and Registration Token as shown below.

copy the gitlab server url and token

5. Paste GitLab Server URL and Token in registration command as below

$ gitlab-runner register --name project-name-runner --url https://gitlab.fosstechnix.com --registration-token  Uc7yzxTMzsXhXJx3zgM

OR

sudo gitlab-runner register

OR

if you want to register gitlab-runner with docker executor then use below command

sudo gitlab-runner register \
  --non-interactive \
  --url "GITLAB_SERVER_URL" \
  --registration-token "REGISTRATION_TOKEN" \
  --description "docker-runner" \
  --executor "docker" \
  --docker-image ubuntu:latest

6. Copy above command. Login to GitLab runner server through SSH and paste & run copied command on command line.

Follow the default options until which agent you need to register and then select agent as shown below

register gitlab runner using ssh

7. Once you registered Runner for project then you will get runner as below,

gitlab runner activated

Successfully, We have performed GitLab Runner Registration.

Error: This job is stuck because the project doesn’t have any runners online assigned to it. Go to Runners page.

Solution:

You have added tags while registering GitLab Runner however you have not added tags for your Job.

Follow below steps to add tags.

1. Navigate to Settings and click on CI/CD inside this click on Expand of Runners section

2. Go to Runners and check on Indicates whether this runner can pick jobs without tags Box

How to Install GitLab Runner on Ubuntu 20.04 LTS 1

#4:Add SSH server details as GitLab Variable

Add EC2 instance or SSH server details like SSH Private key, EC2 IP Address and SSH in GitLab CI CD Variables. To add variables in GitLab follow below steps.

Go to settings<<CI/CD<<variables<<Expand it

#Add private key in $SSH_PRIVATE_KEY variable

#Add Server Ip in $EC2_IPADDRESS variable

#Add server user in $SSH_USER variable

GitLab CI Deploy to EC2 using SSH 2
add variables

When creating variables you have to remove protect variable flag

GitLab CI Deploy to EC2 using SSH 3
remove flag

#7:GitLab CI Deploy to EC2 using SSH

Below is gitlab-ci.yml with explanation to deploy code to EC2 instance using ssh.

stages:
  - deploy
#In this we have only one stage deploy
Deploy: 
  stage: deploy
  before_script:
  - command -v ssh-agent >/dev/null || ( apk add --update openssh )' 
  #The ssh-agent command outputs commands to set ce - cp -r * .public
rtain environment variables in the shell
  - eval $(ssh-agent -s)
 #The eval command tells the shell to run the output of ssh-agent as shell commands
  - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
#"$SSH_PRIVATE_KEY" in this we added private key in variable
  - mkdir -p ~/.ssh
#create a directory
  - chmod 700 ~/.ssh
# usually the tools which use that directory will ask you to assign permissions to it:
  - ssh-keyscan $EC2_IPADDRESS >> ~/.ssh/known_hosts
 #"$EC2_IPADDRESS" in this we added our instance ip_address in variable
  - chmod 644 ~/.ssh/known_hosts
#this is key file permission

  script:
    - mkdir .public
#create public directory
    - cp -r * .public
    - mv .public public
    - zip -r public.zip public
    - scp -o StrictHostKeyChecking=no public.zip [email protected]:/var/www/html
    - ssh -o StrictHostKeyChecking=no [email protected] "cd /var/www/html; touch foo.txt; unzip public.zip"

#fristly you need to inside /var/www/html/ this file 
#with touch command create foo.txt and unzip public.zip

Below is complete code without comment

stages:
  - deploy

Deploy: 
  stage: deploy
  before_script:
  - 'command -v ssh-agent >/dev/null || ( apk add --update openssh )' 
  - eval $(ssh-agent -s)
  - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh
  - ssh-keyscan $EC2_IPADDRESS >> ~/.ssh/known_hosts
  - chmod 644 ~/.ssh/known_hosts
  script:
    - mkdir .public
    - cp -r * .public
    - mv .public public
    - zip -r public.zip public
    - scp -o StrictHostKeyChecking=no public.zip [email protected]:/var/www/html
    - ssh -o StrictHostKeyChecking=no [email protected] "cd /var/www/html; touch foo.txt; unzip public.zip"
GitLab CI Deploy to EC2 using SSH 4
pipeline

If you get error permission denied /var/www/html so run the below command for permission to html

sudo chmod -R 777 /var/www/html

After that lets access in your favorite browser

ip_address/public
GitLab CI Deploy to EC2 using SSH 5
access in browser

Conclusion:

In this article we have covered GitLab CI Deploy to EC2 using SSH | GitLab CI Deploy to Remote Server | Deploy Code to AWS EC2 Instance using SSH with GitLab CI/CD Pipeline.

Related Articles:

Reference:

Using SSH keys with GitLab CI/CD

Shweta Mamidwar

I am Shweta Mamidwar working as a Intern in Product Company. Likes to share knowledge.

3 thoughts on “GitLab CI Deploy to EC2 using SSH”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap