feat(container): add image build validation steps

This commit is contained in:
2022-10-10 11:56:55 +02:00
parent e670fb8bf6
commit 8e1b257144
4 changed files with 220 additions and 56 deletions

View File

@ -1,7 +1,13 @@
import org.jenkinsci.plugins.pipeline.modeldefinition.Utils
def call(String baseImage = 'ubuntu:22.04') {
def call(String baseImage = 'ubuntu:22.04', Map options = [:]) {
Map hooks = options.get('hooks', [:])
String jobHistory = options.get('jobHistory', '10')
node {
properties([
buildDiscarder(logRotator(daysToKeepStr: jobHistory, numToKeepStr: jobHistory)),
])
stage('Cancel older jobs') {
def buildNumber = env.BUILD_NUMBER as int
if (buildNumber > 1) milestone(buildNumber - 1)
@ -11,10 +17,10 @@ def call(String baseImage = 'ubuntu:22.04') {
checkout(scm)
}
stage('Run pre hooks') {
hook('pre-symfony-app')
runHook(hooks, 'preSymfonyAppPipeline')
}
stage('Run in Symfony image') {
def symfonyImage = buildDockerImage(baseImage)
def symfonyImage = buildDockerImage(baseImage, hooks)
symfonyImage.inside() {
def repo = env.JOB_NAME
if (env.BRANCH_NAME ==~ /^PR-.*$/) {
@ -34,7 +40,7 @@ def call(String baseImage = 'ubuntu:22.04') {
def auditReport = sh(script: 'local-php-security-checker --format=markdown || true', returnStdout: true)
if (auditReport.trim() != '') {
if (env.CHANGE_ID) {
gitea.commentPullRequest(repo, env.CHANGE_ID, auditReport, 0)
gitea.commentPullRequest(repo, env.CHANGE_ID, auditReport)
} else {
print auditReport
}
@ -60,7 +66,7 @@ def call(String baseImage = 'ubuntu:22.04') {
'''
def report = sh(script: 'junit2md php-cs-fixer.xml', returnStdout: true)
if (env.CHANGE_ID) {
gitea.commentPullRequest(repo, env.CHANGE_ID, report, 1)
gitea.commentPullRequest(repo, env.CHANGE_ID, report)
} else {
print report
}
@ -81,7 +87,7 @@ def call(String baseImage = 'ubuntu:22.04') {
report = '## Rapport PHPStan\n\n```\n' + report
report = report + '\n```\n'
if (env.CHANGE_ID) {
gitea.commentPullRequest(repo, env.CHANGE_ID, report, 2)
gitea.commentPullRequest(repo, env.CHANGE_ID, report)
} else {
print report
}
@ -92,12 +98,12 @@ def call(String baseImage = 'ubuntu:22.04') {
}
}
stage('Run post hooks') {
hook('post-symfony-app')
runHook(hooks, 'postSymfonyAppPipeline')
}
}
}
def buildDockerImage(String baseImage) {
void buildDockerImage(String baseImage, Map hooks) {
def imageName = 'cadoles-symfony-ci'
dir(".${imageName}") {
def dockerfile = libraryResource 'com/cadoles/symfony/Dockerfile'
@ -106,7 +112,7 @@ def buildDockerImage(String baseImage) {
def addLetsEncryptCA = libraryResource 'com/cadoles/common/add-letsencrypt-ca.sh'
writeFile file:'add-letsencrypt-ca.sh', text:addLetsEncryptCA
hook('build-symfony-image')
runHook(hooks, 'buildSymfonyImage')
def safeJobName = URLDecoder.decode(env.JOB_NAME).toLowerCase().replace('/', '-').replace(' ', '-')
def imageTag = "${safeJobName}-${env.BUILD_ID}"
@ -114,14 +120,15 @@ def buildDockerImage(String baseImage) {
}
}
def when(boolean condition, body) {
def config = [:]
body.resolveStrategy = Closure.OWNER_FIRST
body.delegate = config
void runHook(Map hooks, String name) {
if (!hooks[name]) {
println("No hook '${name}' defined. Skipping.")
return
}
if (condition) {
body()
} else {
Utils.markStageSkippedForConditional(STAGE_NAME)
if (hooks[name] instanceof Closure) {
hooks[name]()
} else {
error("Hook '${name}' seems to be defined but is not a closure !")
}
}