forked from ArunMagi/sample-node-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjenkins
More file actions
117 lines (105 loc) · 3.47 KB
/
jenkins
File metadata and controls
117 lines (105 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
pipeline {
agent none
environment {
AWS_ACCOUNT_ID = '951042686423'
AWS_REGION = 'ap-south-1'
REPO_NAME = 'sample-node-app'
IMAGE_TAG = 'latest'
ECR_REGISTRY = "${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com"
IMAGE_URI = "${ECR_REGISTRY}/${REPO_NAME}:${IMAGE_TAG}"
GIT_REPO = 'https://github.com/MANIKANDAN242221/sample-node-app.git'
AWS_CREDENTIALS_ID = 'aws-jenkins-creds'
}
stages {
stage('Clone Repository') {
agent { label 'master' }
steps {
git url: "${GIT_REPO}", branch: 'main'
}
}
stage('Ensure .env File') {
agent { label 'master' }
steps {
sh '''
echo "PORT=9000" > .env
echo "NODE_ENV=production" >> .env
echo "CUSTOM_MESSAGE=Hello from Jenkins pipeline" >> .env
'''
}
}
stage('Install Dependencies & Build UI') {
agent { label 'master' }
steps {
sh '''
echo "📦 Installing Node.js dependencies..."
npm install
echo "🏗️ Building frontend..."
npm run build
'''
}
}
stage('Build Docker Image') {
agent { label 'master' }
steps {
sh '''
echo "🗑️ Removing old Docker image..."
docker rmi -f ${REPO_NAME}:latest || true
echo "🐳 Building new Docker image..."
docker-compose build --no-cache
'''
}
}
stage('Tag Image for ECR') {
agent { label 'master' }
steps {
sh "docker tag ${REPO_NAME}:latest ${IMAGE_URI}"
}
}
stage('Login to ECR') {
agent { label 'master' }
steps {
withCredentials([
usernamePassword(
credentialsId: "${AWS_CREDENTIALS_ID}",
usernameVariable: 'AWS_ACCESS_KEY_ID',
passwordVariable: 'AWS_SECRET_ACCESS_KEY'
)
]) {
sh '''
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 default.region ${AWS_REGION}
aws ecr get-login-password --region ${AWS_REGION} \
| docker login --username AWS --password-stdin ${ECR_REGISTRY}
'''
}
}
}
stage('Push Image to ECR') {
agent { label 'master' }
steps {
sh "docker push ${IMAGE_URI}"
}
}
stage('Deploy Container on Agent') {
agent { label 'agent' }
steps {
sh '''
docker-compose down || true
docker-compose up -d
'''
}
}
}
post {
always {
echo '🧹 Cleaning up...'
}
success {
echo '✅ Pipeline executed successfully!'
}
failure {
echo '❌ Pipeline failed!'
}
}
}