-
Notifications
You must be signed in to change notification settings - Fork 3
/
Jenkinsfile
115 lines (101 loc) · 3.45 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
#!groovy
// -*- coding: utf-8; mode: Groovy; -*-
@Library('common-utils') _
properties([
buildDiscarder(logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '7', daysToKeepStr: '', numToKeepStr: '7')),
disableConcurrentBuilds(),
disableResume()
]);
env.SA = "";
env.ARTIFACTORY_URL = "";
env.DOCKER_REGISTRY = "";
env.APP_NAME = "";
env.DOCKER_IMAGE = "${env.DOCKER_REGISTRY}.${ARTIFACTORY_URL}/${env.APP_NAME}";
//slackUtils.sendBuildStatusNotification(channelId: 'XXX') {
node ('dockerhost') {
if (branch_name == "main") {
// env.K8S_CLUSTER = "";
// env.K8S_NAMESPACE = "";
// env.DOCKER_IMAGE_TAG = env.COMMIT_SHA
// vaultSecretsToEnv()
// checkout()
// buildFrontend()
// dockerPush()
// deployK8S()
// cleanUp()
}
else if (branch_name == "staging") {
env.K8S_CLUSTER = "";
env.K8S_NAMESPACE = "";
checkout()
env.DOCKER_IMAGE_TAG = "${env.COMMIT_SHA}-staging";
// vaultSecretsToEnv()
runTests()
buildFrontend()
dockerPush()
deployK8S()
cleanUp()
}
else {
checkout()
sh "echo NOTHING WILL BE DEPLOYED"
cleanUp()
}
}
// }
def checkout() {
stage('Checkout sources') {
checkout scm
env.COMMIT_SHA = gitUtils.getCommitShaShort()
}
}
def runTests() {
stage('Testing & Sonar checking') {
sh """
docker-compose -f docker/docker-compose.tests.yml build
"""
}
}
def buildFrontend() {
stage('Build') {
env.DOCKER_IMAGE_WITH_TAG = "${DOCKER_IMAGE}:${DOCKER_IMAGE_TAG}"
sh """
docker-compose -f docker/docker-compose.prod.yml build
"""
}
}
def dockerPush() {
stage('Docker Push to Artifactory') {
// Artifactory settings
def artifactoryServer = Artifactory.newServer(url: "https://${ARTIFACTORY_URL}", credentialsId: env.SA);
artifactoryServer.connection.timeout = 1200;
def artifactoryDocker = Artifactory.docker (server: artifactoryServer);
// Artifactory push frontend
def buildInfoFrontend = artifactoryDocker.push ("${env.DOCKER_IMAGE_WITH_TAG}", env.DOCKER_REGISTRY);
artifactoryServer.publishBuildInfo buildInfoFrontend
}
}
def deployK8S() {
stage('Deploy to K8S') {
withCredentials([usernamePassword(credentialsId: "${env.SA}", usernameVariable: "", passwordVariable: "")]) {
def deployer = docker.image('dockerimage');
deployer.pull();
withEnv (["CLUSTER=${env.K8S_CLUSTER}"]) {
deployer.inside('-u root') {
sh ("get-kubeconfig-basic");
sh ("envsubst < .helm/values.yaml > .helm/values_generated.yaml");
sh ("mv -f .helm/values_generated.yaml .helm/values.yaml");
sh ("helm3 upgrade --install ${env.APP_NAME} .helm --namespace ${env.K8S_NAMESPACE} --set app.frontend.image.name=${env.DOCKER_IMAGE} --set app.frontend.image.tag=${env.DOCKER_IMAGE_TAG}");
}
}
}
}
}
def cleanUp() {
stage ('Clean Workspace') {
sh """
docker image rm ${env.DOCKER_IMAGE_WITH_TAG} || echo 'No image to clean'
"""
cleanWs();
}
}