Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ankit pull #44

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions Jenkins(ci-cd)/Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
pipeline {
agent any

environment {
REPO_URL = "https://github.com/ankitsubhamjyoti2005/DevOps-Assessment"
IMAGE_NAME = "your-ecr-repo-url/nx-app:latest"
AWS_REGION = "us-west-2"
DOCKER_CREDENTIALS_ID = "docker-hub-credentials"
TERRAFORM_CREDENTIALS_ID = "aws-credentials"
}

stages {
stage('Checkout Code') {
steps {
git branch: 'main', url: "${env.REPO_URL}"
}
}

stage('Build Docker Image') {
steps {
script {
docker.build("${env.IMAGE_NAME}")
}
}
}

stage('Push Docker Image to ECR') {
steps {
withCredentials([usernamePassword(credentialsId: "${env.DOCKER_CREDENTIALS_ID}", passwordVariable: 'DOCKER_PASSWORD', usernameVariable: 'DOCKER_USERNAME')]) {
sh """
echo $DOCKER_PASSWORD | docker login -u $DOCKER_USERNAME --password-stdin
docker push ${env.IMAGE_NAME}
"""
}
}
}

stage('Terraform Init') {
steps {
withCredentials([usernamePassword(credentialsId: "${env.TERRAFORM_CREDENTIALS_ID}", passwordVariable: 'AWS_SECRET_ACCESS_KEY', usernameVariable: 'AWS_ACCESS_KEY_ID')]) {
dir('terraform') {
sh 'terraform init'
}
}
}
}

stage('Terraform Apply') {
steps {
withCredentials([usernamePassword(credentialsId: "${env.TERRAFORM_CREDENTIALS_ID}", passwordVariable: 'AWS_SECRET_ACCESS_KEY', usernameVariable: 'AWS_ACCESS_KEY_ID')]) {
dir('terraform') {
sh 'terraform apply -auto-approve -var="docker_image=${env.IMAGE_NAME}"'
}
}
}
}

stage('Deploy to AWS Fargate') {
steps {
script {
echo "Deploying the application using ECS Fargate..."
dir('terraform') {
withCredentials([usernamePassword(credentialsId: "${env.TERRAFORM_CREDENTIALS_ID}", passwordVariable: 'AWS_SECRET_ACCESS_KEY', usernameVariable: 'AWS_ACCESS_KEY_ID')]) {
sh 'terraform apply -auto-approve'
}
}
}
}
}
}

post {
always {
cleanWs()
}
}
}
11 changes: 11 additions & 0 deletions Jenkins(ci-cd)/requirments
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Jenkins Server: You need a running Jenkins instance


Jenkins Plugins:
-Docker Pipeline Plugin
-Terraform Plugin
-Git Plugin

Jenkins Credentials:
-Docker Registry credentials (e.g., Docker Hub or AWS ECR).
-AWS credentials (Access Key ID and Secret Access Key) for Terraform.
25 changes: 25 additions & 0 deletions terraform/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
FROM node:18-alpine AS build

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

RUN npx nx build pt-notification-service --configuration=production

FROM node:18-alpine

WORKDIR /app

COPY --from=build /app/dist/apps/pt-notification-service ./pt-notification-service

# Install production dependencies
COPY package*.json ./
RUN npm install --only=production

# Expose the port
EXPOSE 3000
CMD ["node", "pt-notification-service/main.js"]
Empty file removed terraform/autoscaling.tf
Empty file.
Empty file removed terraform/cluster.tf
Empty file.
Empty file removed terraform/ecr.tf
Empty file.
25 changes: 25 additions & 0 deletions terraform/ecs-cluster.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
resource "aws_ecs_task_definition" "nx_task" {
family = "nx-task"
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]
cpu = "4096"
memory = "8192"

execution_role_arn = aws_iam_role.ecs_task_execution_role.arn
task_role_arn = aws_iam_role.ecs_task_execution_role.arn

container_definitions = jsonencode([
{
name = "nx-app-container"
image = "your-docker-image:latest"
portMappings = [
{
containerPort = 3000
hostPort = 3000
protocol = "tcp"
}
]
essential = true
}
])
}
19 changes: 19 additions & 0 deletions terraform/ecs-service.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
resource "aws_ecs_service" "nx_service" {
name = "nx-app-service"
cluster = aws_ecs_cluster.main.id
task_definition = aws_ecs_task_definition.nx_task.arn
desired_count = 1
launch_type = "FARGATE"

network_configuration {
subnets = aws_subnet.main[*].id
security_groups = [aws_security_group.web_sg.id]
assign_public_ip = true
}

load_balancer {
target_group_arn = aws_lb_target_group.main.arn
container_name = "nx-app-container"
container_port = 3000
}
}
20 changes: 20 additions & 0 deletions terraform/iam-roles.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
resource "aws_iam_role" "ecs_task_execution_role" {
name = "ecsTaskExecutionRole"

assume_role_policy = jsonencode({
Version = "2012-10-17",
Statement = [
{
Action = "sts:AssumeRole",
Effect = "Allow",
Principal = {
Service = "ecs-tasks.amazonaws.com"
}
}
]
})

managed_policy_arns = [
"arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
]
}
25 changes: 25 additions & 0 deletions terraform/load-balancer.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
resource "aws_lb" "main" {
name = "nx-app-lb"
internal = false
load_balancer_type = "application"
security_groups = [aws_security_group.web_sg.id]
subnets = aws_subnet.main[*].id
}

resource "aws_lb_target_group" "main" {
name = "nx-app-tg"
port = 3000
protocol = "HTTP"
vpc_id = aws_vpc.main.id
}

resource "aws_lb_listener" "main" {
load_balancer_arn = aws_lb.main.arn
port = 3000
protocol = "HTTP"

default_action {
type = "forward"
target_group_arn = aws_lb_target_group.main.arn
}
}
Empty file removed terraform/main.tf
Empty file.
25 changes: 25 additions & 0 deletions terraform/network.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
}

resource "aws_subnet" "main" {
count = 2
vpc_id = aws_vpc.main.id
cidr_block = cidrsubnet(aws_vpc.main.cidr_block, 4, count.index)
}

resource "aws_security_group" "web_sg" {
vpc_id = aws_vpc.main.id
ingress {
from_port = 3000
to_port = 3000
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
3 changes: 3 additions & 0 deletions terraform/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
provider "aws" {
region = "us-west-2"
}
Empty file removed terraform/secrets-manager.tf
Empty file.
Empty file removed terraform/service.tf
Empty file.