CI/CD Pipeline using GITLAB with AWS deployment

CI/CD Pipeline using GITLAB with AWS deployment

ยท

2 min read

Node App

Gitlab Repo : https://gitlab.com/shumakmanohar/CI-CD-pipeline-node-app

GitLab Config

Setting Up the Env Variables

AWS File Config For Deployment

Files Available in the repository under /aws folder

CI/CD Pipeline using GitLab

.gitlab-ci.yml

stages:
  - build
  - package
  - test
  - deploy

variables:
  APP_VERSION: $CI_PIPELINE_IID

build website:
  stage: build
  image: node:current-alpine3.17
  script:
    - npm install

build docker Image:
  stage: package
  image: docker
  services:
    - docker:dind
  script:
    - echo $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER  $CI_REGISTRY --password-stdin
    - docker build -t $CI_REGISTRY_IMAGE -t $CI_REGISTRY_IMAGE:$APP_VERSION  .
    - docker image ls
    - docker image push --all-tags $CI_REGISTRY_IMAGE

test website:
  stage: test
  image: curlimages/curl
  services:
    - name: $CI_REGISTRY_IMAGE:$APP_VERSION
      alias: website
  script:
    - echo $PORT
    - curl http://website:$PORT | grep "NODE"

deploy website:
  stage: deploy
  image:
    name: amazon/aws-cli:2.4.11
    entrypoint: [""]
  variables:
    APP_NAME: gitlab-ebs #AWS Elastic Bean Stalk
    APP_ENV_NAME: Gitlab-ebs-env #AWS Elastic Bean Stalk Env
  environment: production
  script:
    - aws --version
    - yum install -y gettext
    - export DEPLOY_TOKEN=$(echo $GITLAB_DEPLOY_TOKEN | tr -d "\n" | base64)
    - envsubst < aws/Dockerrun.aws.json > Dockerrun.aws.json
    - envsubst < aws/auth.json > auth.json
    - cat Dockerrun.aws.json
    - ls
    - aws sts get-caller-identity
    - cat auth.json
    - aws s3 cp Dockerrun.aws.json s3://$AWS_S3_BUCKET/Dockerrun.aws.json
    - aws s3 cp auth.json s3://$AWS_S3_BUCKET/auth.json
    - aws elasticbeanstalk create-application-version --region "us-east-1" --application-name "$APP_NAME" --version-label $APP_VERSION --source-bundle S3Bucket=$AWS_S3_BUCKET,S3Key=Dockerrun.aws.json
    - aws elasticbeanstalk update-environment --region "us-east-1" --application-name "$APP_NAME" --version-label $APP_VERSION --environment-name $APP_ENV_NAME

๐ŸŽ‰ App Launched Successfully on AWS ๐ŸŽŠ

ย