symfonyAppPipeline: add actionable pre/post hooks

This commit is contained in:
wpetit 2022-09-20 16:24:05 +02:00
parent 3272427766
commit 12bed86b97
1 changed files with 41 additions and 28 deletions

View File

@ -1,11 +1,17 @@
import org.jenkinsci.plugins.pipeline.modeldefinition.Utils
def call(String baseImage = "ubuntu:22.04") {
def call(String baseImage = 'ubuntu:22.04') {
node {
stage("Checkout project") {
stage('Checkout project') {
checkout(scm)
}
stage('Run pre hooks') {
steps {
script {
hook('pre-symfony-app')
}
}
}
stage('Run in Symfony image') {
def symfonyImage = buildDockerImage(baseImage)
symfonyImage.inside() {
@ -14,7 +20,7 @@ def call(String baseImage = "ubuntu:22.04") {
repo = env.JOB_NAME - "/${env.JOB_BASE_NAME}"
}
stage("Install composer dependencies") {
stage('Install composer dependencies') {
sh '''
composer install
'''
@ -22,24 +28,24 @@ def call(String baseImage = "ubuntu:22.04") {
parallel([
'php-security-check': {
stage("Check PHP security issues") {
stage('Check PHP security issues') {
catchError(buildResult: 'UNSTABLE', stageResult: 'FAILURE') {
def auditReport = sh(script: "local-php-security-checker --format=markdown || true", returnStdout: true)
if (auditReport.trim() != "") {
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)
} else {
print auditReport
}
}
if (!auditReport.contains("No packages have known vulnerabilities.")) {
throw new Exception("Dependencies check failed !")
if (!auditReport.contains('No packages have known vulnerabilities.')) {
throw new Exception('Dependencies check failed !')
}
}
}
}
},
'php-cs-fixer': {
stage("Run PHP-CS-Fixer on modified code") {
stage('Run PHP-CS-Fixer on modified code') {
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
if ( !fileExists('.php-cs-fixer.dist.php') ) {
def phpCsFixerConfig = libraryResource 'com/cadoles/symfony/.php-cs-fixer.dist.php'
@ -51,7 +57,7 @@ def call(String baseImage = "ubuntu:22.04") {
if ! echo "${CHANGED_FILES}" | grep -qE "^(\\.php-cs-fixer(\\.dist)\\.php?|composer\\.lock)$"; then EXTRA_ARGS=$(printf -- '--path-mode=intersection -- %s' "${CHANGED_FILES}"); else EXTRA_ARGS=''; fi
php-cs-fixer fix --config=.php-cs-fixer.dist.php -v --dry-run --using-cache=no --format junit ${EXTRA_ARGS} > php-cs-fixer.xml || true
'''
def report = sh(script: "junit2md php-cs-fixer.xml", returnStdout: true)
def report = sh(script: 'junit2md php-cs-fixer.xml', returnStdout: true)
if (env.CHANGE_ID) {
gitea.commentPullRequest(repo, env.CHANGE_ID, report, 1)
} else {
@ -61,7 +67,7 @@ def call(String baseImage = "ubuntu:22.04") {
}
},
'phpstan': {
stage("Run phpstan") {
stage('Run phpstan') {
catchError(buildResult: 'UNSTABLE', stageResult: 'FAILURE') {
if ( !fileExists('phpstan.neon') ) {
def phpStanConfig = libraryResource 'com/cadoles/symfony/phpstan.neon'
@ -70,9 +76,9 @@ def call(String baseImage = "ubuntu:22.04") {
sh '''
phpstan analyze -l 1 --error-format=table src > phpstan.txt || true
'''
def report = sh(script: "cat phpstan.txt", returnStdout: true)
report = "## Rapport PHPStan\n\n```\n" + report
report = report + "\n```\n"
def report = sh(script: 'cat phpstan.txt', returnStdout: true)
report = '## Rapport PHPStan\n\n```\n' + report
report = report + '\n```\n'
if (env.CHANGE_ID) {
gitea.commentPullRequest(repo, env.CHANGE_ID, report, 2)
} else {
@ -84,32 +90,39 @@ def call(String baseImage = "ubuntu:22.04") {
])
}
}
stage('Run post hooks') {
steps {
script {
hook('post-symfony-app')
}
}
}
}
}
def buildDockerImage(String baseImage) {
def imageName = "cadoles-symfony-ci"
dir (".${imageName}") {
def imageName = 'cadoles-symfony-ci'
dir(".${imageName}") {
def dockerfile = libraryResource 'com/cadoles/symfony/Dockerfile'
writeFile file:'Dockerfile', text: "FROM ${baseImage}\n\n" + dockerfile
def addLetsEncryptCA = libraryResource 'com/cadoles/common/add-letsencrypt-ca.sh'
writeFile file:'add-letsencrypt-ca.sh', text:addLetsEncryptCA
def safeJobName = URLDecoder.decode(env.JOB_NAME).toLowerCase().replace('/', '-').replace(' ', '-')
def imageTag = "${safeJobName}-${env.BUILD_ID}"
return docker.build("${imageName}:${imageTag}", ".")
return docker.build("${imageName}:${imageTag}", '.')
}
}
def when(boolean condition, body) {
def config = [:]
body.resolveStrategy = Closure.OWNER_FIRST
body.delegate = config
def config = [:]
body.resolveStrategy = Closure.OWNER_FIRST
body.delegate = config
if (condition) {
body()
if (condition) {
body()
} else {
Utils.markStageSkippedForConditional(STAGE_NAME)
}
}
Utils.markStageSkippedForConditional(STAGE_NAME)
}
}