forked from imejri/nodejs-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
121 lines (107 loc) · 4.79 KB
/
Jenkinsfile
File metadata and controls
121 lines (107 loc) · 4.79 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* Pipeline Jenkins to build et deploy nodejs Shark App */
/* Auteur: Mejri Issam */
pipeline {
// We will use any agent available.
agent any
// set a timeout of 30 minutes for this pipeline.
options
{
timeout(time: 30, unit: 'MINUTES')
buildDiscarder(logRotator(numToKeepStr: '5', artifactNumToKeepStr: '5'))
}
// List of parameters required to configure before the launch of the pipeline.
parameters{
string(name: 'BRANCH', defaultValue: 'master', description: 'Name of the branch to use.')
string(name: 'NB_REPLICAS', defaultValue: '1', description: 'Number of replicas for deployment.')
choice(name: 'ENVIRONMENT', choices: ['mejriis-dev', 'mejriis-prod'], description: 'The name of the environement where we want to deploy/build resources.')
choice(name: 'DEPLOYMENT_TYPE', choices: ['','BUILD', 'DEPLOY'], description: 'The name of the type of deployment to process, can be either Build or Deploy.')
string(name: 'IMAGE_TAG', defaultValue: '', description: 'tag for the build image.')
string(name: 'SOURCES_URL', defaultValue: 'https://github.com/imejri/nodejs-app.git', description: 'Gitlab repository source of the application.')
}
// List of stages used on the pipelines.
stages
{
stage('Checkout.')
{
steps
{
script{
if ( params.DEPLOYMENT_TYPE.isEmpty() )
{
// Use SUCCESS FAILURE or ABORTED
currentBuild.result = "FAILURE"
throw new Exception("The Parameters DEPLOYMENT_TYPE is empty, the value must be either BUILD or DEPLOY, Please set DEPLOYMENT_TYPE parameters.")
}
}
checkout([$class: 'GitSCM',
branches: [[name: "*/${params.BRANCH}"]],
userRemoteConfigs:[[credentialsId: 'gitlab', url: "${params.SOURCES_URL}"]]
])
} // steps
} // stage
// stage de création du buildconfig et de l'image
stage("Build Source Code")
{
when {
// Only for BUILD Deployment type.
expression { params.DEPLOYMENT_TYPE == 'BUILD' }
}
steps
{
script
{
sh """
oc delete bc -l build=nodejs-image
oc new-build --name=nodejs-image --image-stream=openshift/nodejs:16-ubi8 ${params.SOURCES_URL} --to='nodejs-image:${params.IMAGE_TAG}'
"""
} // script
} // steps
} // stage
stage("Deploy resources for DEV/STAGE environments.")
{
when {
expression { params.DEPLOYMENT_TYPE == 'DEPLOY' }
}
steps
{
script
{
sh """
oc apply -f nodejs-image-demo-deploy.yaml -n ${params.ENVIRONMENT}
oc set image deployment/nodejs-image-demo nodejs-image-demo=image-registry.openshift-image-registry.svc:5000/mejriis-dev/nodejs-image:${params.IMAGE_TAG}
oc scale --replicas=${params.NB_REPLICAS} deployment/nodejs-image-demo
oc apply -f nodejs-image-demo-svc.yaml -n ${params.ENVIRONMENT}
"""
} // script
} // steps
} // stage
stage("Create route to access the Application")
{
when {
expression { params.ENVIRONMENT == 'mejriis-dev' && params.DEPLOYMENT_TYPE == 'DEPLOY' }
}
steps
{
script
{
sh """
oc delete route nodejs-image-demo --ignore-not-found
oc expose svc/nodejs-image-demo
oc get route nodejs-image-demo
"""
} // script
} // steps
} // stage
} // stages
post
{
always
{
deleteDir() // Delete the current workspace.
dir("${env.WORKSPACE}@script")
{
deleteDir() // Delete the script workspace, this allow us to update the pipeline script without deleting the builconfig.
} // dir
} // always
} // post
} // pipeline