-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
132 lines (118 loc) · 4.1 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
def checkout(String repositoryGit, String credentialsGit, String branchGit){
checkout([
$class: 'GitSCM',
branches: [[name: branchGit]],
doGenerateSubmoduleConfigurations:false,
extensions: [[$class: 'RelativeTargetDirectory',
relativeTargetDir: 'MyGit/']],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: credentialsGit,url: repositoryGit]]
])
}
def buildImagePush(String urlRegistry, String credentialRegistry, String repositoryRegistry, String tagApplication){
def appImg
script {
docker.withRegistry(urlRegistry,credentialRegistry){
appImg = docker.build(repositoryRegistry+':'+"${tagApplication}",'MyGit/')
appImg.push()
}
}
}
def deleteImageLocal(String urlRegistry, String repositoryRegistry, String tagApplication){
sh 'docker rmi '+"${repositoryRegistry}"+':'+"${tagApplication}"
}
node {
def nameApplication = 'laravel'
def buildNumberApplication = 'v'+"${BUILD_NUMBER}"
def buildMustApplication = 'latest'
def repositoryRegistry = 'lerufic/laravel'
def credentialRegistry = 'REGISTRY'
def urlRegistry = 'https://registry.hub.docker.com'
def repositoryGit = 'https://github.com/LERUfic/laravel-docker'
def branchGit = '*/master'
def credentialsGit = 'GITHUB'
def repositoryTerraform = 'https://github.com/LERUfic/terraform-laravel'
def folderTerraform = 'terraform-laravel'
def commitMsg = "chore(terraform-state): change terraform state "+"${BUILD_NUMBER}"
stage('Checkout Git'){
checkout(
repositoryGit as String,
credentialsGit as String,
branchGit as String
)
}
stage('Build for Latest Tag'){
buildImagePush(
urlRegistry as String,
credentialRegistry as String,
repositoryRegistry as String,
buildMustApplication as String
)
}
stage('Clone Infrastructure') {
dir("${env.WORKSPACE}"){
sh 'docker pull hashicorp/terraform:light'
}
try {
dir("${env.WORKSPACE}"){
sh 'git clone '+"${repositoryTerraform}"
}
}catch(Exception e) {
dir("${env.WORKSPACE}/terraform-laravel"){
sh 'git pull'
}
}
}
stage('Terraform Init') {
dir("${env.WORKSPACE}/${folderTerraform}"){
sh 'docker run --user `id -u` -w /app -v `pwd`:/app hashicorp/terraform:light init'
}
}
stage('Terraform Apply') {
dir("${env.WORKSPACE}/${folderTerraform}"){
sh 'docker run --user `id -u` -w /app -v `pwd`:/app hashicorp/terraform:light apply -auto-approve'
}
}
stage('Push TFState'){
dir("${env.WORKSPACE}/${folderTerraform}"){
withCredentials([usernamePassword(credentialsId: 'GITHUB', passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
sh 'git add -A'
sh 'git config --global user.email "aguelsw@gmail.com"'
sh 'git config --global user.name "Aguel Satria Wijaya"'
try{
sh 'git commit -m "'+"${commitMsg}"+'"'
}catch(Exception e) {
echo "Skip commit! Try push..."
}
try{
sh 'git push https://'+"${GIT_USERNAME}"+":"+"${GIT_PASSWORD}"+'@github.com/'+"${GIT_USERNAME}"+'/'+"${folderTerraform}"+'.git'
}catch(Exception e) {
echo "Skip push! Next step..."
}
}
}
}
stage('Build by Tag'){
buildImagePush(
urlRegistry as String,
credentialRegistry as String,
repositoryRegistry as String,
buildNumberApplication as String
)
}
stage('Cleanup Docker'){
dir("${env.WORKSPACE}"){
sh 'rm -rf MyGit/'
}
deleteImageLocal(
urlRegistry as String,
repositoryRegistry as String,
buildNumberApplication as String
)
deleteImageLocal(
urlRegistry as String,
repositoryRegistry as String,
buildMustApplication as String
)
}
}