forked from bigproject5/operation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
124 lines (113 loc) · 4.33 KB
/
Copy pathJenkinsfile
File metadata and controls
124 lines (113 loc) · 4.33 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
118
119
120
121
122
123
124
pipeline {
agent {
kubernetes {
yaml """
apiVersion: v1
kind: Pod
spec:
containers:
- name: docker
image: docker:20.10.12-dind
securityContext:
privileged: true
command:
- cat
tty: true
env:
- name: DOCKER_TLS_CERTDIR
value: ""
volumeMounts:
- name: shared-volume
mountPath: /shared
- name: aws-kubectl
image: amazon/aws-cli:latest
command:
- cat
tty: true
volumeMounts:
- name: shared-volume
mountPath: /shared
volumes:
- name: shared-volume
emptyDir: {}
"""
}
}
environment {
AWS_ACCOUNT_ID = "956463122808"
AWS_DEFAULT_REGION = "ap-northeast-2"
ECR_REPOSITORY_NAME = "operation"
K8S_DEPLOYMENT_NAME = "operation-deployment"
K8S_NAMESPACE = "default"
ECR_IMAGE_URI = "${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com/${ECR_REPOSITORY_NAME}"
EKS_CLUSTER_NAME = "aivle-5-eks"
// 파이프라인 레벨에서 이미지 태그 정의
IMAGE_TAG = "build-${BUILD_NUMBER}"
}
stages {
stage('Checkout Source') {
steps {
checkout scm
}
}
stage('Setup kubectl') {
steps {
container('aws-kubectl') {
sh '''
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x kubectl
mv kubectl /usr/local/bin/
kubectl version --client
'''
}
}
}
stage('Build & Push Docker Image') {
steps {
container('docker') {
sh 'dockerd-entrypoint.sh &'
sh 'sleep 10'
sh 'docker --version'
sh "docker build -t ${ECR_IMAGE_URI}:${IMAGE_TAG} ."
sh "docker tag ${ECR_IMAGE_URI}:${IMAGE_TAG} ${ECR_IMAGE_URI}:latest"
}
// ECR 로그인 토큰을 공유 볼륨에 저장
container('aws-kubectl') {
withAWS(credentials: 'aws-credentials', region: "${AWS_DEFAULT_REGION}") {
sh """
aws ecr get-login-password --region ${AWS_DEFAULT_REGION} > /shared/ecr-password
"""
}
}
// Docker push
container('docker') {
sh """
# ECR 로그인 (공유 볼륨에서 패스워드 읽기)
cat /shared/ecr-password | docker login --username AWS --password-stdin ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com
# 이미지 푸시
docker push ${ECR_IMAGE_URI}:${IMAGE_TAG}
docker push ${ECR_IMAGE_URI}:latest
# 정리
rm -f /shared/ecr-password
"""
}
}
}
stage('Deploy to EKS') {
steps {
container('aws-kubectl') {
withAWS(credentials: 'aws-credentials', region: "${AWS_DEFAULT_REGION}") {
sh """
aws eks update-kubeconfig --region ${AWS_DEFAULT_REGION} --name ${EKS_CLUSTER_NAME}
# 유니크한 태그로 배포
kubectl set image deployment/${K8S_DEPLOYMENT_NAME} \
${ECR_REPOSITORY_NAME}=${ECR_IMAGE_URI}:${IMAGE_TAG} \
-n ${K8S_NAMESPACE}
kubectl rollout status deployment/${K8S_DEPLOYMENT_NAME} -n ${K8S_NAMESPACE}
"""
}
}
}
}
}
}