Skip to content

Commit a75ad8b

Browse files
authored
Refactor the layout of the library and prettify the groovy (#50)
* Refactor the layout of the library, clean things up, fix some bugs for v3 release * Check for null value * Fix stageWithContext * s/plugins/iqePlugins * Switch back to core iqe image for smoke tests * Change git notification for smoke test
1 parent 04d365a commit a75ad8b

35 files changed

+1751
-1349
lines changed

Jenkinsfile-example.groovy

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,64 @@
44
*
55
* Requires: https://github.com/RedHatInsights/insights-pipeline-lib
66
*/
7+
import groovy.transform.Field
78

8-
@Library("github.com/RedHatInsights/insights-pipeline-lib") _
9+
@Library("github.com/RedHatInsights/insights-pipeline-lib@v3") _
910

1011
// Code coverage failure threshold
11-
codecovThreshold = 80
12+
@Field def codecovThreshold = 80
1213

1314

1415
node {
1516
// Cancel any prior builds that are running for this job
16-
cancelPriorBuilds()
17+
pipelineUtils.cancelPriorBuilds()
1718

1819
// Only runs runStages() if this job was triggered by a change to the master branch, or a PR
19-
runIfMasterOrPullReq {
20+
pipelineUtils.runIfMasterOrPullReq {
2021
runStages()
2122
}
2223
}
2324

2425

2526
def runStages() {
26-
// withNode is a helper to spin up a jnlp slave using the Kubernetes plugin, and run the body code on that slave
27-
openShift.withNode(image: "centos/python-36-centos7") {
27+
// withNode is a helper to spin up a pod using the Kubernetes plugin.
28+
// The pod contains a jnlp slave container, and a container specified by 'image' -- the body
29+
// code runs on the specified image.
30+
openShiftUtils.withNode(image: "centos/python-36-centos7") {
2831
// check out source again to get it in this node's workspace
2932
scmVars = checkout scm
3033

3134
stage('Pip install') {
32-
// Helper for projects using pipenv, verifies pip install works, verifies Pipfile is in sync with Pipfile.lock
33-
// Notifies GitHub with the "continuous-integration/jenkins/pipinstall" status
34-
runPipenvInstall(scmVars: scmVars)
35+
// Helper for projects using pipenv, verifies pip install works, verifies Pipfile is in
36+
// sync with Pipfile.lock and notifies GitHub with the
37+
// "continuous-integration/jenkins/pipinstall" status
38+
pythonUtils.runPipenvInstall(scmVars: scmVars)
3539
}
3640

3741
stage('Lint') {
3842
// Runs flake8 lint check and stores results.
3943
// Notifies GitHub with the "continuous-integration/jenkins/lint" status
40-
runPythonLintCheck()
44+
pythonUtils.runLintCheck()
4145
}
4246

4347
stage('UnitTest') {
44-
// withStatusContext runs the body code and notifies GitHub on whether it passed or failed
45-
// 'unitTest' will notify the "continuous-integration/jenkins/unittest" status
46-
withStatusContext.unitTest {
47-
sh "${pipelineVars.userPath}/pipenv run python -m pytest --junitxml=junit.xml --cov=service --cov=db --cov-report html tests/ -s -v"
48+
// withStatusContext runs the body code and notifies GitHub on whether it passed or
49+
// failed. Specifying 'unittest' will update the
50+
// "continuous-integration/jenkins/unittest" status on GitHub
51+
gitUtils.withStatusContext("unittest") {
52+
sh (
53+
"${pipelineVars.userPath}/pipenv run python -m pytest --junitxml=junit.xml" +
54+
"--cov=service --cov=db --cov-report html tests/ -s -v"
55+
)
4856
}
4957
junit 'junit.xml'
5058
}
5159

5260
stage('Code coverage') {
53-
// Checks code coverage results of the above unit tests with coverage.py, this step fails if coverage is below codecovThreshold
54-
// Notifies GitHub with the "continuous-integration/jenkins/coverage" status
55-
checkCoverage(threshold: codecovThreshold)
61+
// Checks code coverage results of the above unit tests with coverage.py, this step
62+
// fails if coverage is below codecovThreshold. Notifies GitHub with the
63+
// "continuous-integration/jenkins/coverage" status
64+
pythonUtils.checkCoverage(threshold: codecovThreshold)
5665
}
5766

5867
if (currentBuild.currentResult == 'SUCCESS') {

convert-to-v3.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
"""
2+
Run this script to do an in-place conversion of a pipeline job that is currently using
3+
insights-pipeline-lib<v3 to convert it to v3. Note that this will replace the contents of the
4+
file, so take a backup or ensure that you can revert changes (e.x. via git) on the file if
5+
necessary.
6+
7+
$ python convert-to-v3.py /path/to/Jenkinsfile
8+
"""
9+
10+
import os
11+
import sys
12+
13+
14+
# We could use regex matching but at the moment building the regex strings would take more
15+
# time than it took to compile this list the "easy way"...
16+
replacements = {
17+
"cancelPriorBuilds": "pipelineUtils.cancelPriorBuilds",
18+
"changedFiles": "gitUtils.getFilesChanged",
19+
"checkCoverage": "pythonUtils.checkCoverage",
20+
"checkOutRepo": "gitUtils.checkOutRepo",
21+
"deployHelpers": "deployUtils",
22+
"deployServiceSet": "deployUtils.deployServiceSet",
23+
"deploymentPipeline": "execDeployPipeline",
24+
"getFilesChanged": "gitUtils.getFilesChanged",
25+
"ghNotify": "gitUtils.ghNotify",
26+
"openShift": "openShiftUtils",
27+
"pipelineVars.defaultUICloud": "pipelineVars.upshiftCloud",
28+
"pipelineVars.defaultUINameSpace": "pipelineVars.upshiftNameSpace",
29+
"pipfileComment.post": "pythonUtils.postPipfileComment",
30+
"pipfileComment.removeAll": "pythonUtils.removePipfileComments",
31+
"promoteImages": "deployUtils.promoteImages",
32+
"runBundleInstall": "rubyUtils.runBundleInstall",
33+
"runIfMasterOrPullReq": "pipelineUtils.runIfMasterOrPullReq",
34+
"runParallel": "pipelineUtils.runParallel",
35+
"runPipenvInstall": "pythonUtils.runPipenvInstall",
36+
"runPythonLintCheck": "pythonUtils.runLintCheck",
37+
"runSmokeTest": "execSmokeTest",
38+
"slackNotify": "slack.sendMsg",
39+
"stageWithContext": "gitUtils.stageWithContext",
40+
"triggeredByComment": "pipelineUtils.triggeredByComment",
41+
"waitForDeployment": "deployUtils.waitForDeployment",
42+
"withStatusContext.lint": 'gitUtils.withStatusContext("lint")',
43+
"withStatusContext.unitTest": 'gitUtils.withStatusContext("unittest")',
44+
"withStatusContext.integrationTest": 'gitUtils.withStatusContext("integrationtest")',
45+
"withStatusContext.coverage": 'gitUtils.withStatusContext("coverage")',
46+
"withStatusContext.pipInstall": 'gitUtils.withStatusContext("pipinstall")',
47+
"withStatusContext.bundleInstall": 'gitUtils.withStatusContext("bundleinstall")',
48+
"withStatusContext.swagger": 'gitUtils.withStatusContext("swagger")',
49+
"withStatusContext.smoke": 'gitUtils.withStatusContext("smoke")',
50+
"withStatusContext.dbMigrate": 'gitUtils.withStatusContext("dbmigrate")',
51+
"withStatusContext.artifacts": 'gitUtils.withStatusContext("artifacts")',
52+
"withStatusContext.waitForFrontend": 'gitUtils.withStatusContext("waitforfrontend")',
53+
"withStatusContext.lint(": 'gitUtils.withStatusContext("lint", ',
54+
"withStatusContext.unitTest(": 'gitUtils.withStatusContext("unittest", ',
55+
"withStatusContext.integrationTest(": 'gitUtils.withStatusContext("integrationtest", ',
56+
"withStatusContext.coverage(": 'gitUtils.withStatusContext("coverage", ',
57+
"withStatusContext.pipInstall(": 'gitUtils.withStatusContext("pipinstall", ',
58+
"withStatusContext.bundleInstall(": 'gitUtils.withStatusContext("bundleinstall", ',
59+
"withStatusContext.swagger(": 'gitUtils.withStatusContext("swagger", ',
60+
"withStatusContext.smoke(": 'gitUtils.withStatusContext("smoke", ',
61+
"withStatusContext.dbMigrate(": 'gitUtils.withStatusContext("dbmigrate", ',
62+
"withStatusContext.artifacts(": 'gitUtils.withStatusContext("artifacts", ',
63+
"withStatusContext.waitForFrontend(": 'gitUtils.withStatusContext("waitforfrontend", ',
64+
"withStatusContext.custom(": "gitUtils.withStatusContext(",
65+
'@Library("github.com/RedHatInsights/insights-pipeline-lib")': (
66+
'@Library("github.com/RedHatInsights/insights-pipeline-lib@v3")'
67+
),
68+
"@Library('github.com/RedHatInsights/insights-pipeline-lib')": (
69+
"@Library('github.com/RedHatInsights/insights-pipeline-lib')"
70+
)
71+
}
72+
73+
74+
try:
75+
filename = os.path.abspath(sys.argv[1])
76+
except IndexError:
77+
print("You didn't provide a filename")
78+
sys.exit(1)
79+
80+
81+
with open(filename, "r") as readfile, open(f"{filename}-new", "w") as writefile:
82+
for lineno, line in enumerate(readfile):
83+
changed = False
84+
orig_line = line
85+
for orig, new in replacements.items():
86+
if orig in line:
87+
changed = True
88+
line = line.replace(orig, new)
89+
if changed:
90+
91+
print("line {} old: {}".format(lineno, orig_line.strip('\n')))
92+
print("line {} new: {}\n".format(lineno, line.strip('\n')))
93+
writefile.write(line)
94+
95+
os.rename(f"{filename}-new", filename)
96+
97+
print(f"Changes saved to {filename}")

vars/cancelPriorBuilds.groovy

Lines changed: 0 additions & 16 deletions
This file was deleted.

vars/changedFiles.groovy

Lines changed: 0 additions & 13 deletions
This file was deleted.

vars/checkCoverage.groovy

Lines changed: 0 additions & 23 deletions
This file was deleted.

vars/checkOutRepo.groovy

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)