-
Notifications
You must be signed in to change notification settings - Fork 1
/
Jenkinsfile
188 lines (176 loc) · 6.92 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
properties([
parameters([
string(name: 'PROJECT_URL', defaultValue: 'https://github.com/ossimlabs/omar-wfs', description: 'The project github URL'),
string(name: 'DOCKER_REGISTRY_DOWNLOAD_URL', defaultValue: 'nexus-docker-private-group.ossim.io', description: 'Repository of docker images')
]),
pipelineTriggers([
[$class: "GitHubPushTrigger"]
]),
[$class: 'GithubProjectProperty', displayName: '', projectUrlStr: '${PROJECT_URL}'],
buildDiscarder(logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '3', daysToKeepStr: '', numToKeepStr: '20')),
disableConcurrentBuilds()
])
podTemplate(
containers: [
containerTemplate(
name: 'docker',
image: 'docker:19.03.11',
ttyEnabled: true,
command: 'cat',
privileged: true
),
containerTemplate(
image: "${DOCKER_REGISTRY_DOWNLOAD_URL}/omar-builder:jdk11",
name: 'builder',
command: 'cat',
ttyEnabled: true
),
containerTemplate(
image: "${DOCKER_REGISTRY_DOWNLOAD_URL}/alpine/helm:3.2.3",
name: 'helm',
command: 'cat',
ttyEnabled: true
),
containerTemplate(
name: 'git',
image: 'alpine/git:latest',
ttyEnabled: true,
command: 'cat',
envVars: [
envVar(key: 'HOME', value: '/root')
]
)
],
volumes: [
hostPathVolume(
hostPath: '/var/run/docker.sock',
mountPath: '/var/run/docker.sock'
),
]
)
{
node(POD_LABEL){
stage("Checkout branch") {
APP_NAME = PROJECT_URL.tokenize('/').last()
scmVars = checkout(scm)
Date date = new Date()
String currentDate = date.format("YYYY-MM-dd-HH-mm-ss")
MASTER = "master"
DEV = "dev"
GIT_BRANCH_NAME = scmVars.GIT_BRANCH
BRANCH_NAME = """${sh(returnStdout: true, script: "echo ${GIT_BRANCH_NAME} | awk -F'/' '{print \$2}'").trim()}"""
sh """
echo "${GIT_BRANCH_NAME}"
echo "${BRANCH_NAME}"
touch buildVersion.txt
grep buildVersion gradle.properties | cut -d "=" -f2 > "buildVersion.txt"
"""
preVERSION = readFile "buildVersion.txt"
VERSION = preVERSION.substring(0, preVERSION.indexOf('\n'))
GIT_TAG_NAME = APP_NAME + "-" + VERSION
ARTIFACT_NAME = "ArtifactName"
if (BRANCH_NAME == "${MASTER}") {
buildName "${VERSION}"
TAG_NAME = "${VERSION}"
}
else {
buildName "${BRANCH_NAME}-${currentDate}"
TAG_NAME = "${BRANCH_NAME}-${currentDate}"
}
}
stage("Load Variables") {
withCredentials([string(credentialsId: 'o2-artifact-project', variable: 'o2ArtifactProject')]) {
step ([$class: "CopyArtifact",
projectName: o2ArtifactProject,
filter: "common-variables.groovy",
flatten: true])
}
load "common-variables.groovy"
DOCKER_IMAGE_PATH = "${DOCKER_REGISTRY_PRIVATE_UPLOAD_URL}/${APP_NAME}"
}
// stage('SonarQube Analysis') {
// nodejs(nodeJSInstallationName: "${NODEJS_VERSION}") {
// def scannerHome = tool "${SONARQUBE_SCANNER_VERSION}"
// withSonarQubeEnv('sonarqube'){
// sh """
// ${scannerHome}/bin/sonar-scanner \
// -Dsonar.projectKey=${APP_NAME}
// """
// }
// }
// }
stage('Build') {
container('builder') {
withCredentials([[$class: 'UsernamePasswordMultiBinding',
credentialsId: 'nexusCredentials',
usernameVariable: 'MAVEN_REPO_USERNAME',
passwordVariable: 'MAVEN_REPO_PASSWORD']])
{
sh """
./gradlew assemble -PossimMavenProxy=${MAVEN_DOWNLOAD_URL} -PbranchName=${BRANCH_NAME}
./gradlew copyJarToDockerDir -PossimMavenProxy=${MAVEN_DOWNLOAD_URL} -PbranchName=${BRANCH_NAME}
./gradlew publish -PossimMavenProxy=${MAVEN_DOWNLOAD_URL} -PbranchName=${BRANCH_NAME}
"""
archiveArtifacts "plugins/*/build/libs/*.jar"
archiveArtifacts "apps/*/build/libs/*.jar"
}
}
}
stage('Docker Build') {
container('docker') {
withDockerRegistry(credentialsId: 'dockerCredentials', url: "https://${DOCKER_REGISTRY_DOWNLOAD_URL}") {
sh """
docker build --build-arg BASE_IMAGE=${JDK11_BASE_IMAGE} --network=host -t "${DOCKER_REGISTRY_PUBLIC_UPLOAD_URL}/${APP_NAME}:${TAG_NAME}" ./docker
"""
}
}
}
stage('Docker Push') {
container('docker') {
withDockerRegistry(credentialsId: 'dockerCredentials', url: "https://${DOCKER_REGISTRY_PUBLIC_UPLOAD_URL}") {
sh """
docker tag "${DOCKER_REGISTRY_PUBLIC_UPLOAD_URL}/${APP_NAME}:${TAG_NAME}" "${DOCKER_REGISTRY_PUBLIC_UPLOAD_URL}/${APP_NAME}:${TAG_NAME}"
docker push "${DOCKER_REGISTRY_PUBLIC_UPLOAD_URL}/${APP_NAME}:${TAG_NAME}"
"""
}
}
}
stage('Package & Upload Chart'){
container('helm') {
sh """
mkdir packaged-chart
helm package -d packaged-chart chart
"""
withCredentials([usernameColonPassword(credentialsId: 'helmCredentials', variable: 'HELM_CREDENTIALS')]) {
sh """
apk add curl
curl -u ${HELM_CREDENTIALS} ${HELM_UPLOAD_URL} --upload-file packaged-chart/*.tgz -v
"""
}
}
}
stage('Tag Repo') {
when (BRANCH_NAME == MASTER) {
container('git') {
withCredentials([sshUserPrivateKey(
credentialsId: env.GIT_SSH_CREDENTIALS_ID,
keyFileVariable: 'SSH_KEY_FILE',
passphraseVariable: '',
usernameVariable: 'SSH_USERNAME')]) {
sh """
mkdir ~/.ssh
echo -e "StrictHostKeyChecking=no\nIdentityFile ${SSH_KEY_FILE}" >> ~/.ssh/config
git config user.email "[email protected]"
git config user.name "Jenkins"
git tag -a "${GIT_TAG_NAME}" \
-m "Generated by: ${env.JENKINS_URL}" \
-m "Job: ${env.JOB_NAME}" \
-m "Build: ${env.BUILD_NUMBER}"
git push -v origin "${GIT_TAG_NAME}"
"""
}
}
}
}
}
}