-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
117 lines (107 loc) · 4.36 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
pipeline {
agent any
tools {
go 'kkam_go'
}
environment {
GITNAME = 'war-oxi'
GITMAIL = '[email protected]'
GITWEBADD = 'https://github.com/War-Oxi/ACE-Team-KKamJi.git'
GITSSHADD = '[email protected]:hjk1996/aws-app-eks-manifests.git'
GITCREDENTIAL = 'kkam_git_cre'
ECR_URL = '109412806537.dkr.ecr.us-east-1.amazonaws.com/app-picture-backend'
ECR_CREDENTIAL = 'aws_cre'
}
stages {
stage('Checkout Github') {
steps {
slackSend (channel: '#jenkins', color: '#FFFF00', message:
"STARTED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
checkout([$class: 'GitSCM', branches: [[name: '*/main']], extensions: [],
userRemoteConfigs: [[credentialsId: GITCREDENTIAL, url: GITWEBADD]]])
}
post {
failure {
echo 'Repository clone failure'
}
success {
echo 'Repository clone success'
}
}
}
stage('go build') {
steps {
sh "mkdir -p ./env"
sh "echo DB_USER=admin >> ./env/.env"
sh "echo DB_NAME=app >> ./env/.env"
sh "go mod tidy"
sh "go build -o main ./cmd/main.go"
sh "chmod +x ./main"
}
post {
failure{
echo 'go build failure'
}
success{
echo 'go build success'
}
}
}
stage('image build') {
steps {
sh "docker build -t ${ECR_URL}:${currentBuild.number} ."
sh "docker build -t ${ECR_URL}:latest ."
}
}
stage('image push') {
steps {
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', accessKeyVariable: 'AWS_ACCESS_KEY_ID', secretKeyVariable: 'AWS_SECRET_ACCESS_KEY', credentialsId: ECR_CREDENTIAL]]) {
script {
def ecrLogin = sh(script: "aws ecr get-login-password --region us-east-1", returnStdout: true).trim()
sh "docker login -u AWS -p ${ecrLogin} ${ECR_URL}"
}
}
sh "docker push ${ECR_URL}:${currentBuild.number}"
sh "docker push ${ECR_URL}:latest"
}
post {
failure {
echo 'AWS ECR로 이미지 푸시 실패 '
sh "docker image rm -f ${ECR_URL}:${currentBuild.number}"
sh "docker image rm -f ${ECR_URL}:latest"
}
success {
echo 'AWS ECR로 이미지 푸시 성공 '
sh "docker image rm -f ${ECR_URL}:${currentBuild.number}"
sh "docker image rm -f ${ECR_URL}:latest"
}
}
}
stage('k8s manifest file update') {
steps {
git credentialsId: GITCREDENTIAL,
url: GITSSHADD,
branch: 'main'
sh "git config --global user.email ${GITMAIL}"
sh "git config --global user.name ${GITNAME}"
sh "sed -i 's@${ECR_URL}:.*@${ECR_URL}:${currentBuild.number}@g' ingress/app_group/picture_backend/picture_backend_deployment.yaml"
sh "git add ."
sh "git commit -m 'fix:${ECR_URL} ${currentBuild.number} image versioning'"
sh "git branch -M main"
sh "git remote remove origin"
sh "git remote add origin ${GITSSHADD}"
sh "git push -u origin main"
}
post {
failure {
echo 'k8s manifest file update failure'
slackSend (channel: '#jenkins', color: '#FF0000', message: "FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' has failed. Check the Jenkins logs for details. (${env.BUILD_URL})")
}
success {
echo 'k8s manifest file update success'
slackSend (channel: '#jenkins', color: '#00FF00', message: "COMPLETED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' was successful. (${env.BUILD_URL})")
}
}
}
}
}