-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile_deploy
94 lines (91 loc) · 3.08 KB
/
Jenkinsfile_deploy
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
pipeline {
agent { label 'ec2-devita-back' }
environment {
ECR_REGISTRY = '860195224276.dkr.ecr.ap-northeast-2.amazonaws.com'
ECR_REPO_NAME = 'devita_deploy_ecr'
IMAGE_TAG = 'latest_back'
AWS_REGION = 'ap-northeast-2'
AWS_CREDENTIALS = credentials('AwsCredentials')
AWS_S3_BUCKET = 'devita-deploy-env'
S3_ENV_FILE = 'application.yaml'
LOCAL_ENV_FILE = 'src/main/resources'
}
stages {
stage('Check for Previous Builds') {
steps {
script {
// 현재 Job에서 실행 중인 이전 빌드를 가져옴
def job = Jenkins.instance.getItemByFullName(env.JOB_NAME)
def currentBuildNumber = currentBuild.number
// 이전 빌드가 실행 중이면 중단시킴
job.builds.each { build ->
if (build.isBuilding() && build.number < currentBuildNumber) {
echo "Cancelling build #${build.number}"
build.doStop() // 이전 빌드를 중단
}
}
}
}
}
stage('Checkout') {
steps {
script {
git branch: 'main', url: 'https://github.com/KTB-FinalProject-Team1/Devita_Backend', credentialsId: "githubAccessToken"
}
}
}
stage('Login to ECR') {
steps {
script {
sh '''
export AWS_ACCESS_KEY_ID=$(echo $AWS_CREDENTIALS | cut -d':' -f1)
export AWS_SECRET_ACCESS_KEY=$(echo $AWS_CREDENTIALS | cut -d':' -f2)
aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $ECR_REGISTRY
'''
}
}
}
stage('Download from S3') {
steps {
script {
sh '''
echo $AWS_ACCESS_KEY_ID
echo $AWS_CREDENTIALS
aws s3 cp s3://$AWS_S3_BUCKET/$S3_ENV_FILE $LOCAL_ENV_FILE --region $AWS_REGION
'''
}
}
}
stage('Build') {
steps {
script {
sh '''
docker build -t $ECR_REPO_NAME:$IMAGE_TAG .
docker tag $ECR_REPO_NAME:$IMAGE_TAG $ECR_REGISTRY/$ECR_REPO_NAME:$IMAGE_TAG
'''
}
}
}
stage('Push to ECR') {
steps {
script {
sh '''
docker push $ECR_REGISTRY/$ECR_REPO_NAME:$IMAGE_TAG
'''
}
}
}
stage('Start Deploy') {
steps {
script {
build job: 'cd_pipeline_deploy'
}
}
}
}
post {
always {
cleanWs()
}
}
}