forked from ibm-cloud-architecture/cloudnative_sample_app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
133 lines (116 loc) · 5.02 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
def cloud = env.CLOUD ?: "openshift"
def servicePort = env.SERVICE_PORT ?: "8080"
def imageName = env.IMAGE_NAME ?: "greeting"
def podLabel = "sample"
podTemplate(
label: podLabel,
cloud: cloud,
envVars: [
envVar(key: 'SERVICE_PORT', value: servicePort),
envVar(key: 'IMAGE_NAME', value: imageName),
envVar(key: 'BUILD_NUMBER', value: "${env.BUILD_NUMBER}")
],
containers: [
containerTemplate(
name: 'maven',
image: 'maven:3-alpine',
ttyEnabled: true,
command: '/bin/bash',
workingDir: '/home/jenkins/agent',
envVars: [
envVar(key: 'HOME', value: '/home/jenkins/agent')
]
),
containerTemplate(
name: 'ibmcloud',
image: 'docker.io/garagecatalyst/ibmcloud-dev:1.0.7',
ttyEnabled: true,
command: '/bin/bash',
workingDir: '/home/jenkins/agent',
envVars: [
envVar(key: 'HOME', value: '/home/devops')
]
)
]
) {
node(podLabel) {
checkout scm
container(name:'maven', shell:'/bin/bash') {
stage('Local - Build') {
sh 'mvn -B -DskipTests clean package'
}
stage('Local - Test') {
sh 'mvn test'
}
stage('Local - Run') {
sh """
#!/bin/bash
java -jar ./target/cloudnativesampleapp-1.0-SNAPSHOT.jar &
PID=`echo \$!`
# Wait for the app to start
sleep 20
#sh scripts/health_check.sh
sh scripts/api_tests.sh 127.0.0.1 ${SERVICE_PORT}
# Kill process
kill \${PID}
"""
}
}
container(name: 'ibmcloud', shell: '/bin/bash') {
stage('Build and Push Image') {
withCredentials(
[string(credentialsId: 'registry_url', variable: 'REGISTRY_URL'),
string(credentialsId: 'registry_namespace', variable: 'REGISTRY_NAMESPACE'),
string(credentialsId: 'ibm_cloud_region', variable: 'REGION'),
string(credentialsId: 'ibm_cloud_api_key', variable: 'APIKEY')]) {
sh '''#!/bin/bash
set -x
ibmcloud login -r ${REGION} --apikey ${APIKEY}
ibmcloud cr login
echo "Checking registry namespace: ${REGISTRY_NAMESPACE}"
NS=$( ibmcloud cr namespaces | grep ${REGISTRY_NAMESPACE} ||: )
if [[ -z "${NS}" ]]; then
echo -e "Registry namespace ${REGISTRY_NAMESPACE} not found, creating it."
ibmcloud cr namespace-add ${REGISTRY_NAMESPACE}
else
echo -e "Registry namespace ${REGISTRY_NAMESPACE} found."
fi
echo -e "Existing images in registry"
ibmcloud cr images --restrict "${REGISTRY_NAMESPACE}/${IMAGE_NAME}"
echo -e "=========================================================================================="
echo -e "BUILDING CONTAINER IMAGE: ${REGISTRY_URL}/${REGISTRY_NAMESPACE}/${IMAGE_NAME}:${BUILD_NUMBER}"
set -x
ibmcloud cr build -f Dockerfile.multistage -t ${REGISTRY_URL}/${REGISTRY_NAMESPACE}/${IMAGE_NAME}:${BUILD_NUMBER} .
echo -e "Available images in registry"
ibmcloud cr images --restrict ${REGISTRY_NAMESPACE}/${IMAGE_NAME}
'''
}
}
stage('Push to Deploy repo') {
sh "mkdir -p deploy"
dir ('deploy') {
git url: "https://github.com/omaribm/cloudnative_sample_app_deploy.git",
branch: "master",
credentialsId: "github-credentials-id"
sh "find ."
withCredentials(
[string(credentialsId: 'git-account', variable: 'GIT_USER_ACCOUNT'),
string(credentialsId: 'github-token', variable: 'GITHUB_API_TOKEN')]) {
sh """
git config user.email "[email protected]"
git config user.name "jenkins"
cd chart/cloudnativesampleapp
sed -i.bak '/^image/,/^service/ s/\\(\\s*tag\\s*:\\s*\\).*/\\ \\ tag: '${BUILD_NUMBER}'/g' values.yaml
rm values.yaml.bak
git add . *.yaml
git commit -m "Jenkins commit: ${BUILD_NUMBER}"
git remote rm origin
git remote add origin https://${GIT_USER_ACCOUNT}:${GITHUB_API_TOKEN}@github.com/omaribm/cloudnative_sample_app_deploy.git > /dev/null 2>&1
git push origin master
"""
}
}
}
}
}
}