Skip to content

Commit

Permalink
Refactor the layout of the library and prettify the groovy (#50)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
bsquizz authored Nov 7, 2019
1 parent 04d365a commit a75ad8b
Show file tree
Hide file tree
Showing 35 changed files with 1,751 additions and 1,349 deletions.
43 changes: 26 additions & 17 deletions Jenkinsfile-example.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,64 @@
*
* Requires: https://github.com/RedHatInsights/insights-pipeline-lib
*/
import groovy.transform.Field

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

// Code coverage failure threshold
codecovThreshold = 80
@Field def codecovThreshold = 80


node {
// Cancel any prior builds that are running for this job
cancelPriorBuilds()
pipelineUtils.cancelPriorBuilds()

// Only runs runStages() if this job was triggered by a change to the master branch, or a PR
runIfMasterOrPullReq {
pipelineUtils.runIfMasterOrPullReq {
runStages()
}
}


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

stage('Pip install') {
// Helper for projects using pipenv, verifies pip install works, verifies Pipfile is in sync with Pipfile.lock
// Notifies GitHub with the "continuous-integration/jenkins/pipinstall" status
runPipenvInstall(scmVars: scmVars)
// Helper for projects using pipenv, verifies pip install works, verifies Pipfile is in
// sync with Pipfile.lock and notifies GitHub with the
// "continuous-integration/jenkins/pipinstall" status
pythonUtils.runPipenvInstall(scmVars: scmVars)
}

stage('Lint') {
// Runs flake8 lint check and stores results.
// Notifies GitHub with the "continuous-integration/jenkins/lint" status
runPythonLintCheck()
pythonUtils.runLintCheck()
}

stage('UnitTest') {
// withStatusContext runs the body code and notifies GitHub on whether it passed or failed
// 'unitTest' will notify the "continuous-integration/jenkins/unittest" status
withStatusContext.unitTest {
sh "${pipelineVars.userPath}/pipenv run python -m pytest --junitxml=junit.xml --cov=service --cov=db --cov-report html tests/ -s -v"
// withStatusContext runs the body code and notifies GitHub on whether it passed or
// failed. Specifying 'unittest' will update the
// "continuous-integration/jenkins/unittest" status on GitHub
gitUtils.withStatusContext("unittest") {
sh (
"${pipelineVars.userPath}/pipenv run python -m pytest --junitxml=junit.xml" +
"--cov=service --cov=db --cov-report html tests/ -s -v"
)
}
junit 'junit.xml'
}

stage('Code coverage') {
// Checks code coverage results of the above unit tests with coverage.py, this step fails if coverage is below codecovThreshold
// Notifies GitHub with the "continuous-integration/jenkins/coverage" status
checkCoverage(threshold: codecovThreshold)
// Checks code coverage results of the above unit tests with coverage.py, this step
// fails if coverage is below codecovThreshold. Notifies GitHub with the
// "continuous-integration/jenkins/coverage" status
pythonUtils.checkCoverage(threshold: codecovThreshold)
}

if (currentBuild.currentResult == 'SUCCESS') {
Expand Down
97 changes: 97 additions & 0 deletions convert-to-v3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
"""
Run this script to do an in-place conversion of a pipeline job that is currently using
insights-pipeline-lib<v3 to convert it to v3. Note that this will replace the contents of the
file, so take a backup or ensure that you can revert changes (e.x. via git) on the file if
necessary.
$ python convert-to-v3.py /path/to/Jenkinsfile
"""

import os
import sys


# We could use regex matching but at the moment building the regex strings would take more
# time than it took to compile this list the "easy way"...
replacements = {
"cancelPriorBuilds": "pipelineUtils.cancelPriorBuilds",
"changedFiles": "gitUtils.getFilesChanged",
"checkCoverage": "pythonUtils.checkCoverage",
"checkOutRepo": "gitUtils.checkOutRepo",
"deployHelpers": "deployUtils",
"deployServiceSet": "deployUtils.deployServiceSet",
"deploymentPipeline": "execDeployPipeline",
"getFilesChanged": "gitUtils.getFilesChanged",
"ghNotify": "gitUtils.ghNotify",
"openShift": "openShiftUtils",
"pipelineVars.defaultUICloud": "pipelineVars.upshiftCloud",
"pipelineVars.defaultUINameSpace": "pipelineVars.upshiftNameSpace",
"pipfileComment.post": "pythonUtils.postPipfileComment",
"pipfileComment.removeAll": "pythonUtils.removePipfileComments",
"promoteImages": "deployUtils.promoteImages",
"runBundleInstall": "rubyUtils.runBundleInstall",
"runIfMasterOrPullReq": "pipelineUtils.runIfMasterOrPullReq",
"runParallel": "pipelineUtils.runParallel",
"runPipenvInstall": "pythonUtils.runPipenvInstall",
"runPythonLintCheck": "pythonUtils.runLintCheck",
"runSmokeTest": "execSmokeTest",
"slackNotify": "slack.sendMsg",
"stageWithContext": "gitUtils.stageWithContext",
"triggeredByComment": "pipelineUtils.triggeredByComment",
"waitForDeployment": "deployUtils.waitForDeployment",
"withStatusContext.lint": 'gitUtils.withStatusContext("lint")',
"withStatusContext.unitTest": 'gitUtils.withStatusContext("unittest")',
"withStatusContext.integrationTest": 'gitUtils.withStatusContext("integrationtest")',
"withStatusContext.coverage": 'gitUtils.withStatusContext("coverage")',
"withStatusContext.pipInstall": 'gitUtils.withStatusContext("pipinstall")',
"withStatusContext.bundleInstall": 'gitUtils.withStatusContext("bundleinstall")',
"withStatusContext.swagger": 'gitUtils.withStatusContext("swagger")',
"withStatusContext.smoke": 'gitUtils.withStatusContext("smoke")',
"withStatusContext.dbMigrate": 'gitUtils.withStatusContext("dbmigrate")',
"withStatusContext.artifacts": 'gitUtils.withStatusContext("artifacts")',
"withStatusContext.waitForFrontend": 'gitUtils.withStatusContext("waitforfrontend")',
"withStatusContext.lint(": 'gitUtils.withStatusContext("lint", ',
"withStatusContext.unitTest(": 'gitUtils.withStatusContext("unittest", ',
"withStatusContext.integrationTest(": 'gitUtils.withStatusContext("integrationtest", ',
"withStatusContext.coverage(": 'gitUtils.withStatusContext("coverage", ',
"withStatusContext.pipInstall(": 'gitUtils.withStatusContext("pipinstall", ',
"withStatusContext.bundleInstall(": 'gitUtils.withStatusContext("bundleinstall", ',
"withStatusContext.swagger(": 'gitUtils.withStatusContext("swagger", ',
"withStatusContext.smoke(": 'gitUtils.withStatusContext("smoke", ',
"withStatusContext.dbMigrate(": 'gitUtils.withStatusContext("dbmigrate", ',
"withStatusContext.artifacts(": 'gitUtils.withStatusContext("artifacts", ',
"withStatusContext.waitForFrontend(": 'gitUtils.withStatusContext("waitforfrontend", ',
"withStatusContext.custom(": "gitUtils.withStatusContext(",
'@Library("github.com/RedHatInsights/insights-pipeline-lib")': (
'@Library("github.com/RedHatInsights/insights-pipeline-lib@v3")'
),
"@Library('github.com/RedHatInsights/insights-pipeline-lib')": (
"@Library('github.com/RedHatInsights/insights-pipeline-lib')"
)
}


try:
filename = os.path.abspath(sys.argv[1])
except IndexError:
print("You didn't provide a filename")
sys.exit(1)


with open(filename, "r") as readfile, open(f"{filename}-new", "w") as writefile:
for lineno, line in enumerate(readfile):
changed = False
orig_line = line
for orig, new in replacements.items():
if orig in line:
changed = True
line = line.replace(orig, new)
if changed:

print("line {} old: {}".format(lineno, orig_line.strip('\n')))
print("line {} new: {}\n".format(lineno, line.strip('\n')))
writefile.write(line)

os.rename(f"{filename}-new", filename)

print(f"Changes saved to {filename}")
16 changes: 0 additions & 16 deletions vars/cancelPriorBuilds.groovy

This file was deleted.

13 changes: 0 additions & 13 deletions vars/changedFiles.groovy

This file was deleted.

23 changes: 0 additions & 23 deletions vars/checkCoverage.groovy

This file was deleted.

22 changes: 0 additions & 22 deletions vars/checkOutRepo.groovy

This file was deleted.

Loading

0 comments on commit a75ad8b

Please sign in to comment.