-
Notifications
You must be signed in to change notification settings - Fork 26
/
Jenkinsfile
161 lines (144 loc) · 5.33 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
pipeline {
// pipelineを実行するagentの設定。yamlファイルで設定を渡せる
// 可能な限りJenkinsfileにagentの設定をもたせたほうが自動化とGit管理が進むためおすすめ。
agent {
kubernetes {
cloud 'openshift'
yaml """\
apiVersion: v1
kind: Pod
spec:
serviceAccountName: jenkins
containers:
- name: jnlp
image: image-registry.openshift-image-registry.svc:5000/app-devops/custom-jenkins-agent-maven
args: ['\$(JENKINS_SECRET)', '\$(JENKINS_AGENT_NAME)']
- name: postgres
image: image-registry.openshift-image-registry.svc:5000/openshift/postgresql:13-el8
env:
- name: POSTGRESQL_USER
value: 'freelancer'
- name: POSTGRESQL_PASSWORD
value: 'password'
- name: POSTGRESQL_DATABASE
value: 'freelancerdb_test'
""".stripIndent()
}
}
environment {
deploy_branch = "origin/main"
deploy_project = "userxx-development"
app_name = 'pipeline-practice-java'
app_image = "image-registry.openshift-image-registry.svc:5000/${deploy_project}/${app_name}"
//sonar_name = 'userxx-app'
//sonar_key = credentials('common-sonar-key')
// Agent Pod内のJavaのバージョンを切り替える場合、alternativesから取得も可能
//JAVA_HOME = """${sh(
// returnStdout: true,
// script: 'alternatives --list | grep "java_sdk_1.8.0\\s" | awk \'{print $3}\' | tr -d \'\\n\''
// )}"""
}
stages {
stage('Setup') {
steps {
sh 'java -version'
sh 'mvn -v'
sh 'mvn clean package -DskipTests'
}
}
stage('Application test') {
steps {
echo "Exec unit test"
sh 'PGPASSWORD=password psql -U freelancer -d freelancerdb_test -h localhost -f etc/testdata.sql'
sh 'mvn test jacoco:report'
}
}
stage('Analysis code') {
steps {
echo "Exec sonar scanner"
//sh 'mvn verify sonar:sonar -DskipTests=true \
// -Dsonar.projectName=$sonar_name \
// -Dsonar.projectKey=$sonar_name \
// -Dsonar.host.url=http://sonarqube-sonarqube.app-devops:9000 \
// -Dsonar.login=$sonar_key \
// -Dsonar.qualitygate.wait=true'
}
}
stage('Build and Tag OpenShift Image') {
when {
expression {
return env.GIT_BRANCH == "${deploy_branch}" || params.FORCE_FULL_BUILD
}
}
steps {
echo "Building OpenShift container image"
script {
openshift.withCluster() {
openshift.withProject("${deploy_project}") {
// update build config
openshift.apply(openshift.process('-f', 'openshift/application-build.yaml', '-p', "NAME=${app_name}"))
openshift.selector("bc", "${app_name}").startBuild("--from-file=./target/freelancer-service.jar", "--wait=true")
openshift.tag("${app_name}:latest", "${app_name}:${env.GIT_COMMIT}")
// ocコマンドで記述することも可能。
//sh "oc process -f openshift/templates/application-build.yaml | oc apply -n ${deploy_project} -f -"
}
}
}
}
}
stage('Deploy application') {
when {
expression {
return env.GIT_BRANCH == "${deploy_branch}" || params.FORCE_FULL_BUILD
}
}
steps {
echo "deploy"
script {
openshift.withCluster() {
openshift.withProject("${deploy_project}") {
// Apply application manifests
openshift.apply(openshift.process('-f', 'openshift/application-deploy.yaml', '-p', "NAME=${app_name}", '-p', "APP_IMAGE=${app_image}", "-p", "APP_IMAGE_TAG=${env.GIT_COMMIT}"))
// Wait for application to be deployed
def dc = openshift.selector("dc", "${app_name}").object()
def dc_version = dc.status.latestVersion
def rc = openshift.selector("rc", "${app_name}-${dc_version}").object()
echo "Waiting for ReplicationController ${app_name}-${dc_version} to be ready"
while (rc.spec.replicas != rc.status.readyReplicas) {
sleep 5
rc = openshift.selector("rc", "${app_name}-${dc_version}").object()
}
}
}
}
}
}
stage('Integration test') {
when {
expression {
return env.GIT_BRANCH == "${deploy_branch}" || params.FORCE_FULL_BUILD
}
}
steps {
echo "integration-test, e2e-test"
script {
openshift.withCluster() {
openshift.withProject("${deploy_project}") {
def dc = openshift.selector("route", "${app_name}").object()
def url = dc.spec.host
echo "${url}"
while (true) {
def app_status = sh(returnStdout: true, script: "curl ${url}/hello -o /dev/null -w '%{http_code}' -s").trim()
if(app_status == "200") {
break;
}
sleep 5
}
// 任意のインテグレーションテストを実装
}
}
}
}
}
}
}