Back to blog
Feb 26, 2024
3 min read

GIT with CI/CD Pipepline deployment

Complete guide on how to setup deployment environment

Aaarggh! this was my first successful CI/CD pipeline COMPLETE setup. Super happy.

With good experience, it took me 2 Hours. But remember it took me whole 2 months to understand CI/CD. I am confident this will help you to get this setup done much faster than me, just follow the steps and understand the basic structural knowledge of linux, server, keys and CI/D.

  • login to EC2 server
  • generate ssh key
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" // add email id of gitlab account
  • Add public of the id_rsa generated to gitlab > settings > repository > deploy keys (grant it write access
  • Come back to EC2 instance and test gitlab.com connection with ssh:
ssh -T git@gitlab.com //gives you confirmation connection to gitlab
  • This should display something like ‘Welcome, @your_name’
  • Now clone you repo
git clone your_repo_url

Time to setup CI/CD

  • Download gitlab runner package, You will need to figure out which packages is compatible with your EC2 instance type.
curl -LJO "https://s3.dualstack.us-east-1.amazonaws.com/gitlab-runner-downloads/latest/XYZ"
// Make sure find correct URL for your EC2
// You can DM me on twitter @lunar_Sutar incase you have any query.
  • Use curl command to download the respective library as per your requirement
rpm -i gitlab-runner_xyz.rpm

// try sudo if doesn't work

rpm -Uvh gitlab-runner_xyz.rpm

sudo gitlab-runner register
  • enter gitlab instance url https://gitlab.com/

  • It will ask you for registration code for runner.

  • Go to GitLab > Settings > CI/CD > Runner

  • Copy registration code of runner and add it on ec2 console which is asking you enter REGISTER

  • skip Description, skip tag

  • select executor as SHELL

  • Once it shows successful..

gitlab-runner start

Go to Gitlab > check runner if registered succesfully.

Disable shared runner

Go to registered runner and make sure ‘picks job without tag’ is checked mark.

Done. Now you have whitelisted gitlab for EC2 instance deployment.

  • Come back to Gitlab

    • Left side > Build > Pipeline editor
    //Below is a basic structure of YML file, You can easily get it from ChatGPT.
    stages:
      - deploy
    
    variables:
    $USERNAME;
    $KEY;
    
    before_script:
    
    deploy:
      stage: deploy
      script:
    
      only:
        - main
    
    • We have to declare variable accordinlgy incase you are using one
    • left side > Settings > CI/CD > Variable
    • Add variable > variable name = $KEY and in value now paste contents the private which is used to connect to the EC2 server.
    • tick Protect variable and Expand variable reference.. Save it
    • Make sure you pushing changes to the correct branch. MAIN.. sometimes you may have different branch name.

Final task:

  • go to AWS security group of the VPC and add SSH access to the same EC2 server’s ip.
  • The runner will do the job

That’s all for the setup.