Skip to content

Commit

Permalink
feat: Add linter to Jenkinsfile
Browse files Browse the repository at this point in the history
  • Loading branch information
turboBasic committed Dec 29, 2023
1 parent 1dc1b1e commit 024bcc4
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 21 deletions.
64 changes: 51 additions & 13 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,62 @@ withNode {
stage('checkout') {
checkout scm
}
stage('unit-test') {
try {
buildAndRunTests()
}
catch(e) { throw e }
finally {
publishReports()
}
stage('verify') {
parallel failFast: false,
lint: {
lint()
},
'unit-test': {
unitTest()
}
}
}

void withNode(Closure body) {
Closure wrappedBody = { -> ansiColor 'xterm', body }
if (env.NODE_LABEL) {
node(env.NODE_LABEL, body)
node env.NODE_LABEL, wrappedBody
}
else {
node(body)
node wrappedBody
}
}

void buildAndRunTests() {
ansiColor('xterm') {
void unitTest() {
try {
sh './runDevEnvironment.sh'
}
catch (e) {
throw e
}
finally {
publishJUnitReports()
}
}

void publishReports() {
void lint() {
final String ARGS = [
'--user root',
'--env VALIDATE_ALL_CODEBASE=true',
"--volume ${env.WORKSPACE}:/tmp/lint",
'--entrypoint=""',
].join(' ')

try {
docker.image('oxsecurity/megalinter-python:v7').inside(ARGS) {
sh '/entrypoint.sh'
}
}
catch (e) {
throw e
}
finally {
resetFilePermissions()
publishLintReports()
}
}

void publishJUnitReports() {
catchError(buildResult: 'SUCCESS', message: 'Failure in report generation') {
junit testResults: 'build/test-results/test/*.xml', allowEmptyResults: true
publishHTML target: [
Expand All @@ -41,3 +70,12 @@ void publishReports() {
]
}
}

void publishLintReports() {
archiveArtifacts artifacts: 'megalinter-reports/megalinter.log, linters_logs/ERROR-*', allowEmptyArchive: true
}

void resetFilePermissions() {
sh 'sudo find -mindepth 1 -maxdepth 1 -exec chown --preserve-root --recursive jenkins: {} + || true'
sh 'sudo find -mindepth 1 -maxdepth 1 -exec chmod --preserve-root --recursive u+rw {} + || true'
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
Jenkinsfile.run()
Jenkinsfile.node(groovy.lang.Closure)
Jenkinsfile.stage(checkout, groovy.lang.Closure)
Jenkinsfile.checkout({})
Jenkinsfile.stage(unit-test, groovy.lang.Closure)
Jenkinsfile.ansiColor(xterm, groovy.lang.Closure)
Jenkinsfile.sh(./runDevEnvironment.sh)
Jenkinsfile.catchError({buildResult=SUCCESS, message=Failure in report generation}, groovy.lang.Closure)
Jenkinsfile.junit({testResults=build/test-results/test/*.xml, allowEmptyResults=true})
Jenkinsfile.publishHTML({target={allowMissing=true, alwaysLinkToLastBuild=false, keepAll=true, reportDir=build/reports/tests/test, reportFiles=index.html, reportName=Test summary report}})
Jenkinsfile.ansiColor(xterm, groovy.lang.Closure)
Jenkinsfile.stage(checkout, groovy.lang.Closure)
Jenkinsfile.checkout({})
Jenkinsfile.stage(verify, groovy.lang.Closure)
Jenkinsfile.parallel({failFast=false, lint=groovy.lang.Closure, unit-test=groovy.lang.Closure})
DockerMock.image(oxsecurity/megalinter-python:v7)
Image.inside(--user root --env VALIDATE_ALL_CODEBASE=true --volume /workspace:/tmp/lint --entrypoint="", groovy.lang.Closure)
Jenkinsfile.sh(/entrypoint.sh)
Jenkinsfile.sh(sudo find -mindepth 1 -maxdepth 1 -exec chown --preserve-root --recursive jenkins: {} + || true)
Jenkinsfile.sh(sudo find -mindepth 1 -maxdepth 1 -exec chmod --preserve-root --recursive u+rw {} + || true)
Jenkinsfile.archiveArtifacts({artifacts=megalinter-reports/megalinter.log, linters_logs/ERROR-*, allowEmptyArchive=true})
Jenkinsfile.sh(./runDevEnvironment.sh)
Jenkinsfile.catchError({buildResult=SUCCESS, message=Failure in report generation}, groovy.lang.Closure)
Jenkinsfile.junit({testResults=build/test-results/test/*.xml, allowEmptyResults=true})
Jenkinsfile.publishHTML({target={allowMissing=true, alwaysLinkToLastBuild=false, keepAll=true, reportDir=build/reports/tests/test, reportFiles=index.html, reportName=Test summary report}})

0 comments on commit 024bcc4

Please sign in to comment.