-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
95 lines (85 loc) · 4.39 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
// 필요한 변수를 선언할 수 있다. (내가 직접 선언하는 변수, 젠킨스 환경변수를 끌고 올 수 있음)
def ecrLoginHelper="docker-credential-ecr-login" // ECR credential helper 이름
def deployHost = "172.31.10.141" // 배포서버 private ip address
// 젠킨스의 선언형 파이프라인 정의부 시작 (그루비 언어)
pipeline {
agent any // 어느 젠킨스 서버에서도 실행이 가능
environment {
REGION = "ap-northeast-2"
ECR_URL = "124355678220.dkr.ecr.ap-northeast-2.amazonaws.com"
SERVICE_DIRS = "config-service,discoveryservice,gateway-service,main-service,member-service,mypage-service,travelboard-service,travelplan-service"
}
stages {
stage('Pull Codes from Github'){ // 스테이지 제목 (맘대로 써도 됨.)
steps{
checkout scm // 젠킨스와 연결된 소스 컨트롤 매니저(git 등)에서 코드를 가져오는 명령어
}
}
stage('Build Codes by Gradle') {
steps {
script {
def serviceDirs = env.SERVICE_DIRS.split(",")
serviceDirs.each { service ->
sh """
echo "Building ${service}"
cd ${service}
chmod +x ./gradlew
./gradlew clean build -x test
ls -al ./build/libs
cd ..
"""
}
}
}
}
stage('Build Docker Image & Push to AWS ECR') {
steps {
script {
// withAWS를 통해 리전과 계정의 access, secret 키를 가져옴.
withAWS(region: "${REGION}", credentials: "aws-key") {
def serviceDirs = env.SERVICE_DIRS.split(",")
serviceDirs.each { service ->
// AWS에 접속해서 ECR을 사용해야 하는데, 젠킨스에는 aws-cli를 설치하지 않았어요.
// aws-cli 없이도 접속을 할 수 있게 도와주는 라이브러리 설치.
// helper가 여러분들 대신 aws에 로그인을 진행. 그리고 그 인증 정보를 json으로 만들어서
// docker에게 세팅해 줍니다. -> docker가 ECR에 push가 가능해짐.
sh """
curl -O https://amazon-ecr-credential-helper-releases.s3.us-east-2.amazonaws.com/0.4.0/linux-amd64/${ecrLoginHelper}
chmod +x ${ecrLoginHelper}
mv ${ecrLoginHelper} /usr/local/bin/
echo '{"credHelpers": {"${ECR_URL}": "ecr-login"}}' > ~/.docker/config.json
# Docker 이미지 빌드(서비스 이름으로)
docker build -t ${service}:latest ${service}
# ECR 레포지토리로 태깅
docker tag ${service}:latest ${ECR_URL}/${service}:latest
# ECR 로 푸시
docker push ${ECR_URL}/${service}:latest
"""
}
}
}
}
}
stage('Deploy to AWS EC2 VM') {
steps {
sshagent(credentials: ["jenkins-ssh-key"]) {
//
sh """
# Jenkins에서 배포 서버로 docker-compose.yml 복사
scp -o StrictHostKeyChecking=no docker-compose.yml ubuntu@${deployHost}:/home/ubuntu/docker-compose.yml
ssh -o StrictHostKeyChecking=no ubuntu@${deployHost} '
# Docker compose 파일이 있는 경로로 이동
cd /home/ubuntu && \
# 기존 컨테이너 중지 및 제거
docker-compose down && \
aws ecr get-login-password --region ${REGION} | docker login --username AWS --password-stdin ${ECR_URL} && \
# Docker Compose로 컨테이너 재배포
docker-compose pull && \
docker-compose up -d
'
"""
}
}
}
}
}