-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
105 lines (105 loc) · 3.7 KB
/
Jenkinsfile
File metadata and controls
105 lines (105 loc) · 3.7 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
pipeline {
agent any
environment {
DOCKER_HUB_IMAGE_REPO = "ninedocs-user-server"
DOCKER_HUB_USERNAME = "gunwoda"
}
options {
disableConcurrentBuilds() // Prevent concurrent builds
}
stages {
stage("Permission") {
steps {
sh "chmod +x ./gradlew"
}
post {
success {
sh 'echo "# chmod success"'
}
failure {
sh 'echo "# chmod failure"'
}
}
}
stage("Gradle Test") {
steps {
withCredentials([file(credentialsId: 'user-server-env', variable: 'ENV_FILE')]){
sh '''
set +x
. ${ENV_FILE}
./gradlew test
'''
}
}
post {
success {
sh 'echo "# gradlew test success"'
}
failure {
sh 'echo "# gradlew test failure"'
}
}
}
stage('Set Tag') {
steps {
script {
// Set TAG to the short commit hash
env.TAG = "V-" + sh(script: "git rev-parse --short HEAD", returnStdout: true).trim()
echo "Git Commit Hash: ${env.TAG}"
}
}
}
stage('Docker Login') {
steps {
withCredentials([usernamePassword(credentialsId: 'gunwoo-dockerhub-cre', usernameVariable: 'username', passwordVariable: 'token')]) {
// Using Groovy string interpolation for the docker login command
sh "echo ${token} | docker login -u ${username} --password-stdin"
}
}
}
stage('Docker Build') {
steps {
// Build docker image with the tag from env.TAG
sh "docker build -t ${DOCKER_HUB_USERNAME}/${DOCKER_HUB_IMAGE_REPO}:${env.TAG} ."
}
}
stage('Push to Registry') {
steps {
// Push the built image to Docker Hub
sh "docker push ${DOCKER_HUB_USERNAME}/${DOCKER_HUB_IMAGE_REPO}:${env.TAG}"
}
}
stage('Update Helm values.yaml') {
steps {
script {
withCredentials([usernamePassword(credentialsId: 'git-credentials', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')]) {
sh '''
#!/bin/bash
echo "Cloning Helm repository..."
git clone --branch eks https://${GIT_USERNAME}:${GIT_PASSWORD}@github.com/nine-docs/infra-manifest.git
cd infra-manifest/charts/user
echo "Updating values.yaml with tag: ${TAG}"
sed -i "s/tag:.*/tag: ${TAG}/" values.yaml
echo "Committing and pushing changes..."
git config user.name "jenkins-john"
git config user.email "john3210of@gmail.com"
git add values.yaml
git commit -m "Update tag to ${TAG}" || echo "No changes to commit"
git push origin eks
'''
}
}
}
}
stage('Clean Up Workspace After Build') {
steps {
deleteDir() // 작업 공간 정리
}
}
stage('Docker Prune') {
steps {
sh "docker system prune -af --filter until=30m"
}
}
}
}