forked from davidcampos/k8s-jenkins-example
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJenkinsfile
69 lines (69 loc) · 2.41 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
pipeline {
//agent any
environment {
DEPLOY = "${env.BRANCH_NAME == "master" || env.BRANCH_NAME == "develop" ? "true" : "false"}"
NAME = "${env.BRANCH_NAME == "master" ? "example" : "example-staging"}"
VERSION = readMavenPom().getVersion()
DOMAIN = 'localhost'
REGISTRY = 'smvkumar/k8s-jenkins-example'
REGISTRY_CREDENTIAL = 'muthu-dockerhub'
// -- These credentials should be set under Jenkins credentials
// -- Aws cli will take the credentials from the below environment
// -- variables to access aws resources
AWS_ACCESS_KEY_ID = credentials('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = credentials('AWS_SECRET_ACCESS_KEY')
AWS_DEFAULT_REGION = credentials('AWS_DEFAULT_REGION')
// -- Set configfile to Kubectl configuration environment variable
// -- so that kubectl will take the cluster configuration from here
KUBECONFIG = credentials('AWS_EKS_KUBECONFIG_FILE')
// -- This is an user defined variables
AWS_ACCOUNT_ID = credentials('AWS_ACCOUNT_ID')
}
agent {
kubernetes {
defaultContainer 'jnlp'
yamlFile 'build.yaml'
}
}
stages {
stage('Build') {
steps {
container('maven') {
sh 'mvn package'
}
}
}
stage('Docker Build') {
when {
environment name: 'DEPLOY', value: 'true'
}
steps {
container('docker') {
sh "docker build -t ${REGISTRY}:${VERSION} ."
}
}
}
stage('Docker Publish') {
when {
environment name: 'DEPLOY', value: 'true'
}
steps {
container('docker') {
withDockerRegistry([credentialsId: "${REGISTRY_CREDENTIAL}", url: ""]) {
sh "docker push ${REGISTRY}:${VERSION}"
}
}
}
}
stage('Kubernetes Deploy') {
when {
environment name: 'DEPLOY', value: 'true'
}
steps {
container('helm') {
sh "helm upgrade --install --force --set name=${NAME} --set image.tag=${VERSION} --set domain=${DOMAIN} ${NAME} ./helm"
}
}
}
}
}