-
Notifications
You must be signed in to change notification settings - Fork 4
/
Jenkinsfile.application
171 lines (161 loc) · 5.57 KB
/
Jenkinsfile.application
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
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env groovy
/**
TEMPLATE DIRECTIONS:
1. Copy this file to Jenkinsfile at root of new repo, or merge in appropriate items if one already exists
2. Find/replace "{{SERVICE}}" with your package name
3. If {{SERVICE}} doesn't live at the root level, replace all ${STAGE_NAME} references
with the path to your package, relative to root
4. Change environment parameters registry and registry credentials to yours (ideally make them Jenkins secrets)
Reference: https://medium.com/@gustavo.guss/jenkins-building-docker-image-and-sending-to-registry-64b84ea45ee9
This Jenkinsfile utilizes a pre-built python3.6 image to lint, test, build, and upload a Docker image.
DISCLAIMER: This template was modified from a project-specific Jenkinsfile. It has not been tested
explicitly so some modifications may be required. The general structure should be valid. YMMV.
**/
// Python {{SERVICE}} Service Variables
def {{SERVICE}}Path = '{{SERVICE}}'
def {{SERVICE}}ImageName = 'image-{{SERVICE}}'
def {{SERVICE}}Image
/**
* Executes code style linting for python
*
* @param directories Space-separated string indicating directories to check
*/
def validateStyle(directory) {
sh """
# Install dependencies
python3 -m pip install black flake8
# Execute linters: black and flake8.
black --check ${directory}
flake8 --config=${env.WORKSPACE}/.flake8 ${directory}
"""
}
/**
* Executes unit testing with pytest
*
* @param previously created image variable using docker.build()
* @param directory Directory to execute tests
*/
def runTests(image, directory) {
script {
image.inside("--entrypoint=''") {
sh """
cd ${directory}
python3 -m pip install -r requirements_dev.txt
pytest .
"""
}
}
}
/**
* Exports the current test-results directory as a Jenkins report
* Includes test results and code coverage
*
* Run this after executing runTests inside the post { always {...} } stage
*
* @param directory Directory to export back to Jenkins
*/
def exportTestResults(directory) {
junit(
allowEmptyResults: true,
keepLongStdio: true,
testResults: "**/test-results/*.xml"
)
publishHTML([
allowMissing: true,
alwaysLinkToLastBuild: false,
keepAll: true,
reportDir: "${env.WORKSPACE}/${directory}/test-results/coverage",
reportFiles: "index.html",
reportName: "Coverage Report - ${directory}",
reportTitles: "${directory}:${env.BUILD_NUMBER} Coverage Index"
])
}
/**
* Build Docker image for a given directory
*
* @param directory Directory to export back to Jenkins
* @imageName Name to give the image when building
*
* @return builtImage Docker image object for later testing/upload
*/
def buildImage(directory, imageName) {
dir(directory) {
script {
builtImage = docker.build("${imageName}:${env.BUILD_NUMBER}")
return builtImage
}
}
}
/**
* BEGIN PIPELINE
*
*/
pipeline {
agent any
environment {
// CHange these to your credentials
registry = "docker_hub_account/repository_name"
registryCredential = ‘dockerhub’
}
stages {
stage('Checkout repo') {
steps {
checkout scm
}
}
stage('Python') {
parallel {
stage({{SERVICE}}Path) {
stages {
stage('Lint') {
agent {
docker {
image "python:3.6-stretch"
alwaysPull "true"
}
}
steps {
validateStyle({{SERVICE}}Path)
}
}
stage('Build Image') {
steps {
script {
{{SERVICE}}Image = buildImage({{SERVICE}}Path, {{SERVICE}}ImageName, true)
}
}
}
stage('Run Tests') {
steps {
runTests({{SERVICE}}Image, {{SERVICE}}Path)
}
post {
always {
exportTestResults({{SERVICE}}Path)
}
}
}
stage('Upload Image') {
when {
branch 'dev'
}
steps {
script {
docker.withRegistry( registry, registryCredential ) {
{{SERVICE}}Image.push()
{{SERVICE}}Image.push('latest')
}
}
}
}
}
post {
cleanup {
sh "docker rmi ${{{SERVICE}}Image.id}"
}
}
}
}
}
}
}