-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJenkinsfile
105 lines (93 loc) · 4.48 KB
/
Jenkinsfile
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
pipeline {
agent any
environment {
AWS_REGION = 'us-east-1'
ECR_REPO = 'your ecr repo id'
IMAGE_TAG = 'latest'
}
stages {
stage('Checkout Code') {
steps {
git url: 'https://github.com/shaheennabi/Production-Ready-LeafLogic-Internship-Project', branch: 'main'
}
}
stage('Build and Push to ECR') {
steps {
withCredentials([
string(credentialsId: 'aws_access_key_id', variable: 'AWS_ACCESS_KEY_ID'),
string(credentialsId: 'aws_secret_access_key', variable: 'AWS_SECRET_ACCESS_KEY'),
string(credentialsId: 'openai_api_key', variable: 'OPENAI_API_KEY'),
string(credentialsId: 'serper_api_key', variable: 'SERPER_API_KEY'),
string(credentialsId: 'sender_password', variable: 'SENDER_PASSWORD'),
string(credentialsId: 'sender_email', variable: 'SENDER_EMAIL'),
string(credentialsId: 'exa_api_key', variable: 'EXA_API_KEY')
]) {
script {
sh '''
# Configure AWS CLI
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
# Login to AWS ECR
aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $ECR_REPO
# Build Docker Image with Build Args for Credentials
docker build -t leaflogic-app:$IMAGE_TAG \
--build-arg OPENAI_API_KEY="$OPENAI_API_KEY" \
--build-arg SERPER_API_KEY="$SERPER_API_KEY" \
--build-arg SENDER_PASSWORD="$SENDER_PASSWORD" \
--build-arg SENDER_EMAIL="$SENDER_EMAIL" \
--build-arg EXA_API_KEY="$EXA_API_KEY" \
.
# Tag Image for ECR
docker tag leaflogic-app:$IMAGE_TAG $ECR_REPO:$IMAGE_TAG
# Push to ECR
docker push $ECR_REPO:$IMAGE_TAG
'''
}
}
}
}
stage('Deploy to EC2') {
steps {
withCredentials([
string(credentialsId: 'aws_access_key_id', variable: 'AWS_ACCESS_KEY_ID'),
string(credentialsId: 'aws_secret_access_key', variable: 'AWS_SECRET_ACCESS_KEY'),
string(credentialsId: 'openai_api_key', variable: 'OPENAI_API_KEY'),
string(credentialsId: 'serper_api_key', variable: 'SERPER_API_KEY'),
string(credentialsId: 'sender_password', variable: 'SENDER_PASSWORD'),
string(credentialsId: 'sender_email', variable: 'SENDER_EMAIL'),
string(credentialsId: 'exa_api_key', variable: 'EXA_API_KEY')
]) {
script {
sh '''
# Authenticate with ECR
aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $ECR_REPO
# Stop and remove old container if it exists
if docker ps -q --filter "name=leaflogic-app" | grep -q .; then
docker stop leaflogic-app && docker rm leaflogic-app
fi
# Pull latest image from ECR
docker pull $ECR_REPO:$IMAGE_TAG
# Run the container
docker run -d --name leaflogic-app -p 5000:5000 \
-e OPENAI_API_KEY="$OPENAI_API_KEY" \
-e SERPER_API_KEY="$SERPER_API_KEY" \
-e SENDER_PASSWORD="$SENDER_PASSWORD" \
-e SENDER_EMAIL="$SENDER_EMAIL" \
-e EXA_API_KEY="$EXA_API_KEY" \
$ECR_REPO:$IMAGE_TAG
'''
}
}
}
}
}
post {
always {
echo '✅ Pipeline execution completed!'
}
failure {
echo '❌ Pipeline failed!'
}
}
}