DevOps Training
  • DevOps
  • What is DevOps
    • What DevOps Look like
    • Why DevOps Needed
    • DevOps Automation Tools
    • DevOps Principles
  • cloud computing
    • How DevOps links with Cloud Computing
    • What is cloud computing?
      • Platform as a service (PaaS)
      • Infrastructure as a service (IaaS)
      • Software as a service (SaaS)
      • Function as a Service
      • SaaS, PaaS, IaaS
  • Version Control
    • Git as Version Control
      • Setting up Remote Repo
      • Git Hooks
      • github vs gitlab vs bitbucket
      • Quick Recap Git
  • DevOps #01 Continuous Integration
    • Continuous Integration & Continuous Delivery
      • Understanding CI Tools
      • Prerequisite
      • Continuous Integration
      • CI Tools
      • Travis-CI
        • Travis CI with S3
        • Static Site Build S3
        • Beanstalk with AWS CLI
          • Elastic Beanstalk with Travis CI
        • Travis using Code Deploy EC2
          • Github and Code Deploy
          • Travis CI with Code Deploy
      • Gitlab-CI
        • CI Setup for application
        • Gitlab Runners on EC2
        • CI Integration with AWS
          • Deploying App using Gitlab CI
          • Gitlab CI with AWS S3
          • Gitlab CI with ECS
          • CI Integration with EC2
            • Update and Clean Gitlab.yml
        • Install Gitlab on EC2
      • CI/CD using Jenkins CI
        • Jenkins Build on EC2
        • Jenkins Build EC2 Ubuntu
        • Jenkins CI/CD
          • Create a Build Item
          • Create a Build Pipleine
            • Pipeline Using Docker
            • Pipeline Examples
          • Jenkins CI with S3
            • Jenkins CI - S3
          • Jenkins CI with EC2
    • Jenkins CI Cluster Mode
    • AWS Code Pipeline CI/CD
      • AWS CI/CD Tools
        • AWS Code Build
        • AWS Code Deploy to Beanstalk
        • AWS Code Deploy to EC2
        • AWS Pipeline - Example CI/CD
  • Docker
    • Docker
      • Docker for Developers
        • Install and setup
        • Docker Commands
        • Docker Images Container
        • Docker Architecture
    • Docker Demos
      • Node JS Container
    • Docker-compose
      • Using Docker Compose
      • Docker Compose Demo
  • AWS Quick Refresh
    • AWS Quick Recap - Videos
    • AWS Quick Recap
  • AWS Architecture - Lab
    • Application Deployment - 01
    • Application Deployment - 02
    • Application 3 tier Architecture
  • Basic Networking
    • Computer Networking for Beginners
      • Basic of Networking
      • Networking Protocols
      • OSI Model
      • Network address and Host address
      • Subnetting Type
    • Network Architecture
    • Networking Layers OSI Model
    • Internet protocol
      • CIDR and subnetting
        • Examples
      • AWS VPC Subnets
  • VPC and Networking
    • AWS VPC
    • VPC Demo
      • Bastion Host | Jump Server
  • AWS Components
    • AWS Components In Depth
      • AWS Storage
        • AWS EBS
        • AWS Cloudfront
        • AWS S3
      • AWS Compute
        • ECS
        • AWS VPC
          • VPC Components
        • AWS EC2
        • AWS ELB
          • Application Load balancer
            • Example
        • AWS EC2 Auto Scaling
          • Demo
        • AWS Route 53
        • AWS Lambda Serverless
          • AWS Lambda Serverless Computing
  • Assignments
    • Assignment 01-Node JS app on EC2
    • Assignment 02-Node JS with Mysql
    • Assignment-03
  • Microservices
    • Microservices Architecture
      • Docker and Docker-Compose
      • Docker-Compose Example 01
      • Docker-Compose Example 02
      • Hand-on | Building Microservices
    • Architecture Components
  • AWS ECS
    • AWS ECS
      • Introduction of ECS
Powered by GitBook
On this page
  • Continuous Integration
  • Step by Step Deployment

Was this helpful?

  1. DevOps #01 Continuous Integration
  2. Continuous Integration & Continuous Delivery
  3. Gitlab-CI
  4. CI Integration with AWS
  5. CI Integration with EC2

Update and Clean Gitlab.yml

PreviousCI Integration with EC2NextInstall Gitlab on EC2

Last updated 5 years ago

Was this helpful?

Continuous Integration

Consider an application which has its code stored in a Git repository in GitLab. Developers push code changes every day, multiple times a day. For every push to the repository, you can create a set of scripts to build and test your application automatically, decreasing the chance of introducing errors to your app.

This practice is known as ; for every change submitted to an application - even to development branches - it’s built and tested automatically and continuously, ensuring the introduced changes pass all tests, guidelines, and code compliance standards you established for your app.

is an example of using Continuous Integration as a software development method. For every push to the project, there’s a set of scripts the code is checked against.

Continuous Delivery

is a step beyond Continuous Integration. Your application is not only built and tested at every code change pushed to the codebase, but, as an additional step, it’s also deployed continuously, though the deployments are triggered manually.

This method ensures the code is checked automatically but requires human intervention to manually and strategically trigger the deployment of the changes.

Continuous Deployment

is also a further step beyond Continuous Integration, similar to Continuous Delivery. The difference is that instead of deploying your application manually, you set it to be deployed automatically. It does not require human intervention at all to have your application deployed.

How GitLab CI/CD works

To use GitLab CI/CD, all you need is an application codebase hosted in a Git repository, and for your build, test, and deployment scripts to be specified in a file called , located in the root path of your repository.

In this file, you can define the scripts you want to run, define include and cache dependencies, choose commands you want to run in sequence and those you want to run in parallel, define where you want to deploy your app, and specify whether you will want to run the scripts automatically or trigger any of them manually. Once you’re familiar with GitLab CI/CD you can add more advanced steps into the configuration file.

To add scripts to that file, you’ll need to organize them in a sequence that suits your application and are in accordance with the tests you wish to perform. To visualize the process, imagine that all the scripts you add to the configuration file are the same as the commands you run on a terminal in your computer.

Once you’ve added your .gitlab-ci.yml configuration file to your repository, GitLab will detect it and run your scripts with the tool called , which works similarly to your terminal.

The scripts are grouped into jobs, and together they compose a pipeline. A minimalist example of .gitlab-ci.yml file could contain:

Lets do it with Node JS app which we will deploy on EC2 instance using gitlab runner gitlab-ci.yml file for deploying code with two different stage

First Add all required env variables

# Select image from https://hub.docker.com/r/_/php/
image: nodesource/trusty

cache:
    paths:
    - ./node_modules
    #- env.sh
# Install dependencies
before_script:
  #  # Install ssh-agent if not already installed, it is required by Docker.
  #  # (change apt-get to yum if you use a CentOS-based image)
  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -yqq )'
  #  # Run ssh-agent (inside the build environment)
  - eval $(ssh-agent -s)
  #  # Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
  - ssh-add <(echo "$SSH_PRIVATE_KEY")
  #  # For Docker builds disable host key checking. Be aware that by adding that
  #  # you are suspectible to man-in-the-middle attacks.
  #  # WARNING: Use this only with the Docker executor, if you use it with shell
  #  # you will overwrite your user's SSH config.
  - mkdir -p ~/.ssh
  - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
  #- export PATH=$PATH:/node_modules/@angular/cli/bin

stages:
  - prepare-dev
  - dev-server-restart

prepare-dev:
  stage: prepare-dev
  tags:
    - TAG
  only:
    - master
  script:
    - echo $ENV > ./env.sh && chmod a+x ./env.sh
    - rm -rf ../build && mkdir -p ../build && cp -rf * ../build && tar -zcvf build.tar ../build && scp build.tar $GID@$DEVSERVERIP:$BUILD_PATH
    - >
     ssh $GID@$DEVSERVERIP << EOF
       cd $BUILD_PATH && rm -rf build/* && tar -zxvf build.tar && rm -f build.tar && cd build && npm install
     EOF
dev-server-restart:
  stage: dev-server-restart
  tags:
    - TAG
  only:
    - master
  script:
    - >
     ssh $GID@$DEVSERVERIP << EOF
       cd $BUILD_PATH/build && source ./env.sh && npm run deploy
     EOF

Here we are deploying Node JS application and will be running on PM2, our package json look like

{
  "name": "appbackend",
  "version": "1.0.0",
  "description": "backend for ubmas application",
  "main": "index.js",
  "scripts": {
    "deploy": ". ./env.sh && pm2 startOrReload ecosystem.config.js --update-env",
    "start": ". ./env.sh && nodemon app/server.js",
    "test": ". ./env.sh && mocha"
  },
  "engines": {
    "node": "10.0.0"
    },
  }

Code is available in above branch of this Repository

We need gitlab runner and gitlab CI to deploy application, we can previous tutorials how to setup gitlab runner on any linux machine on EC2 container.

Lets see what gitlab yml instructions are doing in pipeline, it is a two stage pipeline with npm start and npm run deploy

npm install to install all dependancies and then npm run deploy which will use pm2 to redeploy application on EC2 instance

prepare-dev:
  stage: prepare-dev
  tags:
    - TAG
  only:
    - master
  script:
    - echo $ENV > ./env.sh && chmod a+x ./env.sh
    - rm -rf ../build && mkdir -p ../build && cp -rf * ../build && tar -zcvf build.tar ../build && scp build.tar $GID@$DEVSERVERIP:$BUILD_PATH
    - >
     ssh $GID@$DEVSERVERIP << EOF
       cd $BUILD_PATH && rm -rf build/* && tar -zxvf build.tar && rm -f build.tar && cd build && npm install
     EOF

Stage - 1 prepare environment

  • only has master - so this will trigger only on code push on master

  • Script is getting env variables and creating env.sh at runtime and putting it in current working directory

  • we have all different variable getting values from gitlab secret variable like

    $BUILD_PATH, $GID, $DEVSERVERIP
  • We are creating .zip bundle and doing SCP with out EC2 instance

dev-server-restart:
  stage: dev-server-restart
  tags:
    - TAG
  only:
    - master
  script:
    - >
     ssh $GID@$DEVSERVERIP << EOF
       cd $BUILD_PATH/build && source ./env.sh && npm run deploy
     EOF

Stage - 2 deploy environment

  • Here we will SSH to that EC2 instance and will cd to build path and deploy application using npm run deploy command

Step by Step Deployment

  1. Install gitlab runner on EC2 Instance

  2. update tags added in gitlab runner to gitlab-ci yml

Run in the command:
sudo apt-get update
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | sudo bash
sudo apt-get install gitlab-runner
// If you ever want to update the runner, you just do:
sudo apt-get update
sudo apt-get install gitlab-runner
// Now you must register the runner:
sudo gitlab-runner register
  1. Now update gitlab yml and add tag name as node-dev for master branch

  2. Install Docker on gitlab runner machine [do not forgot to install it]

Installing Docker on the GitLab runner

  1. Update the apt package index:

sudo apt-get update

2. Install packages to allow apt to use a repository over HTTPS:

sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"
   
sudo apt-get update
sudo apt-get install docker-ce      

Just install node js & npm also on this instance and finally our gitlab-ci.yml ready for deployment , here we go we are able to deploy node JS application using gitlab-ci to EC2 Instances

This follows the normal installation of Docker in an Ubuntu server. Check for the official method of installation. I’ll go through the steps here:

Continuous Integration
GitLab itself
Continuous Delivery
Continuous Deployment
.gitlab-ci.yml
GitLab Runner
this link
gitlab ci with gitlab runner running docker containers
gitlab runner now started
runner gets activated on gitlab so till now we are good
containers now coming up on pipeline trigger
GitHub - tkssharma/DevOpsTraining at gitlab-ci-ec2-nodeGitHub
Github Repo
Logo