-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathJenkinsfile
182 lines (166 loc) · 6.78 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
@Library('ecdc-pipeline')
import ecdcpipeline.ContainerBuildNode
import ecdcpipeline.PipelineBuilder
container_build_nodes = [
'centos7': ContainerBuildNode.getDefaultContainerBuildNode('centos7-gcc11')
]
// Define number of old builds to keep.
num_artifacts_to_keep = '1'
// Set number of old builds to keep.
properties([[
$class: 'BuildDiscarderProperty',
strategy: [
$class: 'LogRotator',
artifactDaysToKeepStr: '',
artifactNumToKeepStr: num_artifacts_to_keep,
daysToKeepStr: '',
numToKeepStr: ''
]
]]);
pipeline_builder = new PipelineBuilder(this, container_build_nodes)
pipeline_builder.activateEmailFailureNotifications()
builders = pipeline_builder.createBuilders { container ->
pipeline_builder.stage("${container.key}: Checkout") {
dir(pipeline_builder.project) {
scm_vars = checkout scm
}
container.copyTo(pipeline_builder.project, pipeline_builder.project)
} // stage
pipeline_builder.stage("${container.key}: Dependencies") {
container.sh """
which python
python --version
python -m pip install --user -r ${pipeline_builder.project}/requirements-dev.txt
"""
} // stage
pipeline_builder.stage("${container.key}: Formatting (black) ") {
container.sh """
cd ${pipeline_builder.project}
python -m black --check .
"""
} // stage
pipeline_builder.stage("${container.key}: Static Analysis (flake8) ") {
container.sh """
cd ${pipeline_builder.project}
python -m flake8
"""
} // stage
pipeline_builder.stage("${container.key}: Type Checking (mypy) ") {
container.sh """
cd ${pipeline_builder.project}
python -m mypy .
"""
} // stage
pipeline_builder.stage("${container.key}: Test") {
def test_output = "TestResults.xml"
container.sh """
cd ${pipeline_builder.project}
pyenv global 3.8 3.9 3.10
pyenv versions
export PATH="/home/jenkins/.pyenv/shims:$PATH"
python -m tox -- --cov=forwarder --cov-report=xml --junitxml=${test_output}
"""
container.copyFrom("${pipeline_builder.project}/${test_output}", ".")
xunit thresholds: [failed(unstableThreshold: '0')], tools: [JUnit(deleteOutputFiles: true, pattern: '*.xml', skipNoTestFiles: false, stopProcessingIfError: true)]
container.copyFrom("${pipeline_builder.project}/coverage.xml", ".")
withCredentials([string(credentialsId: 'forwarder-codecov-token', variable: 'TOKEN')]) {
sh "curl -s https://codecov.io/bash | bash -s - -t ${TOKEN} -C ${scm_vars.GIT_COMMIT} -f coverage.xml"
}
} // stage
} // createBuilders
node {
dir("${pipeline_builder.project}") {
scm_vars = checkout scm
}
if ( env.CHANGE_ID ) {
builders['integration tests'] = get_contract_tests_pipeline()
}
try {
parallel builders
} catch (e) {
throw e
}
// Delete workspace when build is done
cleanWs()
}
def get_contract_tests_pipeline() {
return {
node('docker') {
cleanWs()
dir("${pipeline_builder.project}") {
try {
stage("Integration tests: Checkout") {
checkout scm
} // stage
stage("Integration tests: Install requirements") {
sh """
scl enable rh-python38 -- python --version
scl enable rh-python38 -- python -m venv test_env
source test_env/bin/activate
which python
pwd
pip install --upgrade pip
pip install docker-compose 'requests<2.30.0'
"""
} // stage
stage("Integration tests: Prepare") {
// Stop and remove any containers that may have been from the job before,
// i.e. if a Jenkins job has been aborted.
// Then pull the latest image versions
sh """
mkdir integration_tests/shared_volume || true
rm -rf integration_tests/shared_volume/*
docker stop \$(docker ps -a -q) && docker rm \$(docker ps -a -q) || true
cd integration_tests
grep "image:" docker-compose.yml | sed 's/image://g' | while read -r class; do docker pull \$class; done
"""
} // stage
stage("Contract tests: Run") {
timeout(time: 120, activity: true){
sh """
source test_env/bin/activate
cd integration_tests
docker-compose up &
sleep 60
rsync -av .. shared_volume/forwarder --exclude=shared_volume --exclude=".*" --exclude=test_env
docker exec integration_tests_forwarder_1 bash -c 'cd shared_source/forwarder/; python -m pip install --proxy http://192.168.1.1:8123 -r requirements.txt'
docker exec integration_tests_forwarder_1 bash -c 'cd shared_source/forwarder/; python -m pip install --proxy http://192.168.1.1:8123 pytest'
docker exec integration_tests_forwarder_1 bash -c 'cd shared_source/forwarder/integration_tests/contract_tests; pytest --junitxml=ContractTestsOutput.xml'
cp shared_volume/forwarder/integration_tests/contract_tests/ContractTestsOutput.xml .
"""
}
} // stage
stage("Smoke tests: Run") {
timeout(time: 150, activity: true){
sh """
source test_env/bin/activate
cd integration_tests
docker exec integration_tests_forwarder_1 bash -c 'cd shared_source/forwarder/integration_tests/smoke_tests; python prepare.py'
docker exec integration_tests_forwarder_1 bash -c 'cd shared_source/forwarder/; python forwarder_launch.py --config-topic=kafka1:9092/forwarder_commands --status-topic=kafka1:9092/forwarder_status --storage-topic=kafka1:9092/forwarder_storage --output-broker=kafka1:9092 --pv-update-period=10000' &
sleep 30
docker exec integration_tests_forwarder_1 bash -c 'cd shared_source/forwarder/integration_tests/smoke_tests; pytest --junitxml=SmokeTestsOutput.xml'
cp shared_volume/forwarder/integration_tests/smoke_tests/SmokeTestsOutput.xml .
"""
}
} // stage
} finally {
stage ("Integration tests: Clean Up") {
// The statements below return true because cleaning up should
// not affect the results of the tests.
sh """
source test_env/bin/activate
docker-compose down || true
rm -rf test_env || true
rm -rf integration_tests/shared_source/* || true
docker stop \$(docker ps -a -q) && docker rm \$(docker ps -a -q) || true
"""
} // stage
stage("Integration tests: Archive") {
junit "integration_tests/ContractTestsOutput.xml"
junit "integration_tests/SmokeTestsOutput.xml"
}
} // try/finally
} // dir
} // node
} // return
} // def