-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
133 lines (120 loc) · 5.76 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
pipeline {
agent any
environment {
// 환경 변수 파일 경로 설정
ENV_FILE = '/var/lib/jenkins/environments/.env.ai'
// Docker 이미지 정보
DOCKER_IMAGE = '727646500036.dkr.ecr.ap-northeast-2.amazonaws.com/gitfolio/ai:dev'
// AWS 리전
AWS_REGION = 'ap-northeast-2'
}
stages {
stage('Load Environment Variables') {
steps {
script {
// .env.ai 파일에서 환경 변수 로드
def envContent = readFile(ENV_FILE).trim()
envContent.split('\n').each { line ->
def (key, value) = line.split('=', 2)
env."${key}" = value
}
}
}
}
stage('Checkout') {
steps {
// Git 저장소 URL을 직접 지정하여 체크아웃
git branch: 'develop',
url: 'https://github.com/KTB-Sixmen/gitfolio_AI.git'
}
}
stage('Docker Build & Push') {
steps {
script {
withCredentials([usernamePassword(credentialsId: 'docker-credentials',
usernameVariable: 'DOCKER_USER',
passwordVariable: 'DOCKER_PASS')]) {
sh '''
echo "$DOCKER_PASS" | docker login -u "$DOCKER_USER" --password-stdin
# Docker 이미지 빌드
docker build \
--build-arg OPENAI_API_KEY="$OPENAI_API_KEY" \
--build-arg GH_TOKEN="$GH_TOKEN" \
--build-arg HOST="$HOST" \
--build-arg PORT="$PORT" \
-t ${DOCKER_IMAGE} .
# Docker 이미지 푸시
docker push ${DOCKER_IMAGE}
'''
}
}
}
}
stage('Deploy to EC2') {
steps {
script {
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding',
credentialsId: 'aws-credentials',
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) {
// EC2 인스턴스 ID 조회
def instanceIds = sh(
script: """
aws ec2 describe-instances \
--region ${AWS_REGION} \
--filters 'Name=tag:Service,Values=ai' 'Name=instance-state-name,Values=running' \
--query 'Reservations[].Instances[].InstanceId' \
--output text
""",
returnStdout: true
).trim()
if (instanceIds) {
// docker-compose.yaml 파일 인코딩
def dockerComposeContent = sh(
script: "base64 docker-compose.yaml | tr -d '\n'",
returnStdout: true
).trim()
// SSM 명령 실행 - JSON 형식 수정
def commandId = sh(
script: """
aws ssm send-command \
--instance-ids "${instanceIds}" \
--document-name "AWS-RunShellScript" \
--comment "Deploying AI Server" \
--parameters '{"commands":["cd /home/ec2-user","echo '\\''${dockerComposeContent}'\\'' | base64 -d > docker-compose.yaml","echo '\\''OPENAI_API_KEY=${env.OPENAI_API_KEY}'\\'' > .env","echo '\\''GH_TOKEN=${env.GH_TOKEN}'\\'' >> .env","echo '\\''HOST=${env.HOST}'\\'' >> .env","echo '\\''PORT=${env.PORT}'\\'' >> .env","chmod 600 .env","docker-compose down -v --rmi all","docker-compose pull","docker-compose up -d"]}' \
--timeout-seconds 600 \
--region ${AWS_REGION} \
--output text \
--query 'Command.CommandId'
""",
returnStdout: true
).trim()
// 명령 실행 완료 대기
sh """
aws ssm wait command-executed \
--command-id ${commandId} \
--instance-id ${instanceIds} \
--region ${AWS_REGION}
"""
// 실행 결과 확인
sh """
aws ssm get-command-invocation \
--command-id ${commandId} \
--instance-id ${instanceIds} \
--region ${AWS_REGION}
"""
} else {
error "No running EC2 instances found with the specified tags"
}
}
}
}
}
}
post {
always {
// 작업 완료 후 정리
cleanWs()
}
}
}