-
Notifications
You must be signed in to change notification settings - Fork 1
/
Jenkinsfile
311 lines (298 loc) · 11.9 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// this file when collection is public made will not be present. tis is also a PR test
pipeline {
agent none
stages {
stage('Matrix Build') {
matrix {
axes {
axis {
name 'PYTHON_VERSION'
values '3.9.0', '3.10.0', '3.11.0', '3.12.0'
}
axis {
name 'ANSIBLE_VERSION'
values 'stable-2.15', 'stable-2.16', 'stable-2.17', 'devel', 'milestone'
}
}
stages {
stage('Check Exclusions and Setup Environment') {
agent {
docker {
image 'fermendy/tenable_dependencias:latest'
args '-u root:root'
}
}
environment {
EXCLUSIONS = "3.9.0:stable-2.16,3.9.0:stable-2.17,3.9.0:devel,3.9.0:milestone,3.12.0:stable-2.15,3.12.0:milestone"
}
stages {
stage('Check Exclusions') {
steps {
script {
def exclusions = EXCLUSIONS.split(",").collect { it.trim() }
def currentCombination = "${PYTHON_VERSION}:${ANSIBLE_VERSION}"
echo "Verifying exclusion for combination: ${currentCombination}"
if (exclusions.any { it == currentCombination }) {
echo "Skipping detected not posible combination: Python ${PYTHON_VERSION} y Ansible ${ANSIBLE_VERSION}"
error "Exclusion detected. Stopping the build for this combination."
}
}
}
}
stage('Setup Environment') {
steps {
echo "Setting up environment for Python ${PYTHON_VERSION} and Ansible ${ANSIBLE_VERSION}"
}
}
stage('Clean Previous Builds') {
steps {
sh '''
find ${WORKSPACE} -name 'valkiriaaquatica-tenable-*.tar.gz' -exec rm {} \\; || true
find ${WORKSPACE} -name 'venv_*' -exec rm -rf {} \\; || true
'''
}
}
stage('Clean Workspace and Checkout') {
steps {
sh 'rm -rf *'
checkout scm
sh 'ls -la'
}
}
stage('Install System Dependencies and Pyenv') {
steps {
script {
installPyenv("${PYTHON_VERSION}")
}
}
}
stage('Setup Ansible and Virtualenv') {
steps {
script {
setupAnsibleEnv("${PYTHON_VERSION}", "${ANSIBLE_VERSION}")
}
}
}
stage('Extract Galaxy Metadata') {
steps {
script {
extractGalaxyMetadata()
}
}
}
stage('Build and Install Collection') {
steps {
script {
buildAndInstallCollection("${PYTHON_VERSION}", "${ANSIBLE_VERSION}")
}
}
}
stage('Run Tox Ansible-Lint Testing') {
steps {
script {
runToxTesting("${PYTHON_VERSION}", "${ANSIBLE_VERSION}")
}
}
}
stage('Run Ansible Sanity Test') {
steps {
script {
runAnsibleSanityTest("${PYTHON_VERSION}")
}
}
}
stage('Run Ansible Unit Tests') {
steps {
script {
runAnsibleUnitTest("${PYTHON_VERSION}")
}
}
post {
always {
junit 'results.xml'
}
}
}
stage('Run Ansible Integration Test') {
steps {
script {
runAnsibleIntegrationTest("${PYTHON_VERSION}")
}
}
}
}
}
}
}
}
}
post {
always {
cleanWorkspace()
}
}
}
def installPyenv(pythonVersion) {
sh """
curl https://pyenv.run | bash
echo 'export PYENV_ROOT="\$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="\$PYENV_ROOT/bin:\$PATH"' >> ~/.bashrc
echo 'eval "\$(pyenv init --path)"' >> ~/.bashrc
echo 'eval "\$(pyenv init -)"' >> ~/.bashrc
echo 'eval "\$(pyenv virtualenv-init -)"' >> ~/.bashrc
export PYENV_ROOT="\$HOME/.pyenv"
export PATH="\$PYENV_ROOT/bin:\$PATH"
eval "\$(pyenv init --path)"
eval "\$(pyenv init -)"
eval "\$(pyenv virtualenv-init -)"
pyenv install -s ${pythonVersion}
pyenv global ${pythonVersion}
python -m pip install --upgrade pip
python --version
"""
}
def setupAnsibleEnv(pythonVersion, ansibleVersion) {
def venvName = "/tmp/venv_${pythonVersion}_${ansibleVersion.replace('.', '_')}"
sh """
export PYENV_ROOT="/root/.pyenv"
export PATH="\$PYENV_ROOT/bin:\$PATH"
eval "\$(pyenv init --path)"
eval "\$(pyenv init -)"
eval "\$(pyenv virtualenv-init -)"
pyenv global ${pythonVersion}
python -m venv ${venvName}
. ${venvName}/bin/activate
python -m pip install https://github.com/ansible/ansible/archive/${ansibleVersion}.tar.gz
pip install -r test-requirements.txt
apt-get install jq -y
pip install yq
"""
}
def extractGalaxyMetadata() {
def venvName = "/tmp/venv_${env.PYTHON_VERSION}_${env.ANSIBLE_VERSION.replace('.', '_')}"
def envVars = sh(script: """
. ${venvName}/bin/activate
NAMESPACE=\$(yq -r '.namespace' galaxy.yml)
COLLECTION_NAME=\$(yq -r '.name' galaxy.yml)
VERSION=\$(yq -r '.version' galaxy.yml)
echo "NAMESPACE=\$NAMESPACE"
echo "COLLECTION_NAME=\$COLLECTION_NAME"
echo "VERSION=\$VERSION"
echo "NAMESPACE=\$NAMESPACE" > \$WORKSPACE/metadata.env
echo "COLLECTION_NAME=\$COLLECTION_NAME" >> \$WORKSPACE/metadata.env
echo "VERSION=\$VERSION" >> \$WORKSPACE/metadata.env
cat \$WORKSPACE/metadata.env
""", returnStdout: true).trim()
def props = readFile("${env.WORKSPACE}/metadata.env").split('\n')
env.NAMESPACE = props[0].split('=')[1].trim()
env.COLLECTION_NAME = props[1].split('=')[1].trim()
env.VERSION = props[2].split('=')[1].trim()
echo "NAMESPACE=${env.NAMESPACE}"
echo "COLLECTION_NAME=${env.COLLECTION_NAME}"
echo "VERSION=${env.VERSION}"
}
def buildAndInstallCollection(pythonVersion, ansibleVersion) {
def venvName = "/tmp/venv_${pythonVersion}_${ansibleVersion.replace('.', '_')}"
def tarFileName = "${env.NAMESPACE}-${env.COLLECTION_NAME}-${env.VERSION}.tar.gz"
sh """
export PYENV_ROOT="\$HOME/.pyenv"
export PATH="\$PYENV_ROOT/bin:\$PATH"
eval "\$(pyenv init --path)"
eval "\$(pyenv init -)"
eval "\$(pyenv virtualenv-init -)"
pyenv global ${pythonVersion}
. ${venvName}/bin/activate
ansible-galaxy collection build -vvv
tar -tzf ${tarFileName}
ansible-galaxy collection install ${tarFileName} -vvv
cp galaxy.yml /root/.ansible/collections/ansible_collections/valkiriaaquatica/tenable/galaxy.yml
"""
}
def runToxTesting(pythonVersion, ansibleVersion) {
def venvName = "/tmp/venv_${pythonVersion}_${ansibleVersion.replace('.', '_')}"
sh """
. ${venvName}/bin/activate
cd /root/.ansible/collections/ansible_collections/valkiriaaquatica/tenable/
rm -rf MANIFEST.json
ls -la
tox -m lint -vv --skip-missing-interpreters=false
rm -rf .tox
"""
}
def runAnsibleSanityTest(pythonVersion) {
def venvName = "/tmp/venv_${pythonVersion}_${env.ANSIBLE_VERSION.replace('.', '_')}"
def shortPythonVersion = pythonVersion.replaceAll(/\\.\\d+$/, '')
sh """
. ${venvName}/bin/activateS
echo "Using Python version: ${shortPythonVersion}"
cd /root/.ansible/collections/ansible_collections/valkiriaaquatica/tenable/
ls -la
ansible-test sanity --requirements --color --python default
"""
}
def runAnsibleUnitTest(pythonVersion) {
def venvName = "/tmp/venv_${pythonVersion}_${env.ANSIBLE_VERSION.replace('.', '_')}"
sh """
. ${venvName}/bin/activate
ls -la
python -m pytest tests/unit --junitxml=results.xml --showlocals
"""
}
def runAnsibleIntegrationTest(pythonVersion) {
def venvName = "/tmp/venv_${pythonVersion}_${env.ANSIBLE_VERSION.replace('.', '_')}"
def excludeList = [
"add_agent_to_group/",
"create_report/",
"get_agent_details/",
"get_asset_activity_log/",
"get_asset_information/",
"get_asset_vulnerability_details/",
"get_report_status/",
"list_agents_by_group/",
"list_asset_vulnerabilities/",
"list_asset_vulnerabilities_for_plugin/",
"list_tags_for_an_asset/",
"rename_agent/",
"update_agent_group_name/",
"upload_file/",
"get_scanner_details/",
"launch_scan/",
"list_agents/",
"stop_scan/",
"update_scan/",
"add_or_remove_asset_tags/",
"get_asset_details/",
"create_network/",
"delete_network/",
"get_network_asset_count/",
"get_network_details/",
"list_networks/",
"list_network_scanners/",
"list_assignable_scanners",
"update_network/",
]
def excludeArgs = excludeList.collect { "--exclude ${it}" }.join(" ")
sh """
. ${venvName}/bin/activate
cd tests/integration/
ls -la
cat <<EOF > integration_config.yml
tenable_access_key: ${env.TENABLE_ACCESS_KEY}
tenable_secret_key: ${env.TENABLE_SECRET_KEY}
EOF
ls -la
cd ../../..
ls -la
ansible-test integration ${excludeArgs} -v
"""
}
//ansible-test integration ${excludeArgs}
def cleanWorkspace() {
node('master_node') {
cleanWs()
sh '''
find ${WORKSPACE} -name '*.tar.gz' -exec rm {} \\;
find ${WORKSPACE} -name 'venv_*' -exec rm -rf {} \\;
'''
}
}