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