Setup CI/CD with Gitlab for Jelastic Cloud using AWS ECR and Docker Image

Cover Image

Setup Dockerfile

FROM node:14 as dependencies # Use node:14 image as base and name or tag it as dependencies
# Create app directory
WORKDIR /root/ROOT # Specify the folder where project lies, it will be created for you if not exists
COPY package.json ./ # Copy a package.json file to the container
RUN npm install # Install dependency from package.json
FROM node:14 as runner # Use node:14 image as base and name or tag it as runner
WORKDIR /root/ROOT # Specify the folder where project lies, it will be created for you if not exists
COPY . . # Copy all the files and folder from project to container
COPY --from=dependencies /root/ROOT/node_modules ./node_modules # Copy node module we have installed in dependencies
ENV NODE_ENV production #SET ENV VARIABLES
ENV PORT 3000
EXPOSE 3000
# Expose port so that outside world can access the port of container
CMD ["npm","start"] # Start Application Command

Step 3: Build Our Docker Image

  • To build Docker Image we run command from root of the project
docker build -t tag-name . 

Push Docker file to AWS ECR Repository

First you need to create a repository in your aws account, I have already created it. I will skip this for this tutorial if you need i can make another tutorial specific for AWS ECR.

aws ecr get-login-password | docker login --username AWS --password-stdin your-registry-urldocker tag tag-name  your-registry-url/image_namedocker push your-registry-url/image_name
View Push Command Header
Push Commands For AWS ECR
  • Goto New Environment
  • Choose Custom and Select Image
  • Public images can be searched there but we need to import our custom docker image here so click Custom
  • Click Add New Image
  • You have to add your image name which is the uri of the AWS ECR Image copy that and paste it
URI of Image
  • For Password its little tricky we have get password from our system aws cli, open terminal in your computer and enter
aws ecr get-login-password
  • So Copy the password and paste it in password box
  • Click Add
  • Then Image will be added, Other basic configuration can still be done like adding database, scaling horizontally and vertically etc.
  • Then, Your Application will start running after some time, Hurray!!!!!!

Lets Write CI/CD file

  • I am using gitlab so, Lets create .gitlab-ci.yml file in root of your project
  • I will divide this process into 3 part
image: docker:latest
stages:
— build
— release
— deploy
build:
image: node:14
stage: build
only:
— deploy
allow_failure: false
script:
— rm -rf package-lock.json
— npm install
docker-release:
image: docker:latest
stage: release
only:
— deploy-branch
services:
— docker:dind
script:
— apk add — update — no-cache build-base python3-dev python3 libffi-dev libressl-dev bash git gettext curl && curl -O https://bootstrap.pypa.io/get-pip.py && python3 get-pip.py && pip install awscli
— aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
— aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY
— aws configure set region $AWS_REGION
— aws ecr get-login-password — region $AWS_REGION | docker login — username $DOCKER_USER — password-stdin $AWS_REGISTRY
— docker build -t $AWS_REGISTRY_IMAGE_NAME -f Dockerfile .
— docker tag $AWS_REGISTRY_IMAGE_NAME $AWS_REGISTRY_IMAGE_LINK
— docker push $AWS_REGISTRY_IMAGE
— docker logout $AWS_REGISTRY
dependencies:
— build
needs:
— build
deploy:
image: openjdk:8-jre
only:
— deploy-branch
stage: deploy
script:
- echo "Deploy "
- if [ -z "$JELASTIC_ID" ]; then echo "$JELASTIC_ID is not specified"; exit 2; fi
- if [ -z "$JELASTIC_PASSWORD" ]; then echo "$JELASTIC_PASSWORD is not specified"; exit 2; fi
- if [ -z "$PLATFORM" ]; then echo "$PLATFORM is not specified"; exit 2; fi
- if [ -z "$ENV_NAME" ]; then echo "$ENV_NAME is not specified"; exit 2; fi
- if [ -z "$NODE_ID" ]; then echo "$NODE_ID is not specified"; exit 2; fi
- curl -s ftp://ftp.jelastic.com/pub/cli/jelastic-cli-installer.sh | bash
- echo "jelastic.cli.send_err_confirm=false" >> ~/.config/jelastic/jelastic.properties
- apt install curl unzip && curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" \-o "awscliv2.zip" && unzip awscliv2.zip && ./aws/install- aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
- aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY
- aws configure set region $AWS_REGION
- ~/jelastic/users/authentication/signin --silent true --login $JELASTIC_ID --password $JELASTIC_PASSWORD --platformUrl $PLATFORM
- new_password=$( aws ecr get-login-password )- ~/jelastic/environment/control/editregistrycredentials --filter {"registryUser":"$DOCKER_USER"} --password $new_password- ~/jelastic/environment/control/redeploycontainerbyid --envName $ENV_NAME --nodeId $NODE_ID --tag latest --sequential true;- ~/jelastic/users/authentication/signout --silent truedependencies:
— build
— docker-release
  • These Variables have to be set on gitlab-project->Settings->ci/cd->Variables

Thank you if you have followed all the way to this far. I hope this article have been helpfull to you.

If you like more Devops, Nodejs, Nginx, Spring Boot and other technology related blog then make sure to follow me.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store