| 1 |
def REBUILD_IMAGE = false |
| 2 |
|
| 3 |
pipeline { |
| 4 |
agent none |
| 5 |
options { |
| 6 |
buildDiscarder(logRotator(numToKeepStr: '135', daysToKeepStr: '21')) |
| 7 |
gitLabConnection('GitLabConnectionJenkins') |
| 8 |
ansiColor('xterm') |
| 9 |
} |
| 10 |
stages { |
| 11 |
stage('Update Gitlab commit status') { |
| 12 |
agent any |
| 13 |
steps { |
| 14 |
updateGitlabCommitStatus(name: 'clang format', state: 'running') |
| 15 |
script{ |
| 16 |
COMMIT_ID = env.GIT_COMMIT |
| 17 |
println GIT_COMMIT |
| 18 |
} |
| 19 |
} |
| 20 |
} |
| 21 |
stage('Build'){ |
| 22 |
agent { label 'docker' } |
| 23 |
stages { |
| 24 |
stage('Checkout'){ |
| 25 |
steps { |
| 26 |
checkout([ |
| 27 |
$class: 'GitSCM', |
| 28 |
branches: [[name: "origin/${env.gitlabSourceBranch}"]], |
| 29 |
userRemoteConfigs: [[ url: "${env.GIT_URL_SDK}", credentialsId: "12492eb8-0278-4402-98f0-4412abfb65c1" ]], |
| 30 |
extensions: [ |
| 31 |
[$class: "UserIdentity",name: "jenkins", email: "jenkins@jenkins"], |
| 32 |
[$class: 'PreBuildMerge', options: [fastForwardMode: 'FF', mergeRemote: "origin", mergeStrategy: 'DEFAULT', mergeTarget: "${env.gitlabTargetBranch}"]] |
| 33 |
] |
| 34 |
]) |
| 35 |
} |
| 36 |
} |
| 37 |
stage('Get parameters'){ |
| 38 |
steps { |
| 39 |
script{ |
| 40 |
// flag may be `--rebuild-docker` or empty `` |
| 41 |
def flag = sh(script: 'echo "$gitlabTriggerPhrase" | grep -o "\\-\\-rebuild-docker\\$" || :', returnStdout: true).trim() |
| 42 |
REBUILD_IMAGE = flag == "--rebuild-docker" |
| 43 |
echo "REBUILD_IMAGE set to ${REBUILD_IMAGE}" |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
stage('Rebuild docker image') { |
| 48 |
when { |
| 49 |
beforeAgent true |
| 50 |
expression { REBUILD_IMAGE } |
| 51 |
} |
| 52 |
steps { |
| 53 |
dir ("dockerfile") { |
| 54 |
sh """ |
| 55 |
docker build \ |
| 56 |
-f clang-format.dockerfile \ |
| 57 |
-t ${env.MEGA_INTERNAL_DOCKER_REGISTRY}:8443/clang-format-sdk:24.04_v18 \ |
| 58 |
. |
| 59 |
""" |
| 60 |
} |
| 61 |
withCredentials([usernamePassword(credentialsId: 'artifactory-jenkins-docker', usernameVariable: 'ART_USER', passwordVariable: 'ART_PASS')]) { |
| 62 |
sh """ |
| 63 |
echo \$ART_PASS | docker login \ |
| 64 |
-u \$ART_USER \ |
| 65 |
--password-stdin \ |
| 66 |
${env.MEGA_INTERNAL_DOCKER_REGISTRY}:8443 |
| 67 |
docker push ${env.MEGA_INTERNAL_DOCKER_REGISTRY}:8443/clang-format-sdk:24.04_v18 |
| 68 |
""" |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
stage('Clang format'){ |
| 73 |
agent { |
| 74 |
docker { |
| 75 |
image "${env.MEGA_INTERNAL_DOCKER_REGISTRY}:8443/clang-format-sdk:24.04_v18" |
| 76 |
reuseNode true |
| 77 |
} |
| 78 |
} |
| 79 |
steps { |
| 80 |
sh """#!/bin/bash |
| 81 |
TARGET_REF="origin/${env.gitlabTargetBranch}" |
| 82 |
SOURCE_REF="origin/${env.gitlabSourceBranch}" |
| 83 |
|
| 84 |
if ! command -v clang-format-mr-check >/dev/null 2>&1; then |
| 85 |
echo "ERROR: clang-format-mr-check not found in Docker image." >&2 |
| 86 |
echo "This job is configured to run the check only inside the Docker image." >&2 |
| 87 |
echo "Rebuild/push the image (trigger with --rebuild-docker) and retry." >&2 |
| 88 |
exit 2 |
| 89 |
fi |
| 90 |
|
| 91 |
clang-format-mr-check "\${TARGET_REF}" "\${SOURCE_REF}" |
| 92 |
""" |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
post{ |
| 97 |
always { |
| 98 |
deleteDir() |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
post { |
| 104 |
success { |
| 105 |
updateGitlabCommitStatus(name: 'clang format', state: 'success') |
| 106 |
addGitLabMRComment(comment: ":white_check_mark: ${currentBuild.projectName} :page_with_curl: <b>ClangFormat</b> SUCCEEDED :muscle:<br/>Build results: [Jenkins [${currentBuild.displayName}]](${currentBuild.absoluteUrl})<br/>Commit: ${COMMIT_ID}" ) |
| 107 |
} |
| 108 |
unstable { |
| 109 |
updateGitlabCommitStatus(name: 'clang format', state: 'failed') |
| 110 |
addGitLabMRComment(comment: ":interrobang: ${currentBuild.projectName} :page_with_curl: <b>ClangFormat</b> UNSTABLE :confused:<br/>Build results: [Jenkins [${currentBuild.displayName}]](${currentBuild.absoluteUrl})<br/>Commit: ${COMMIT_ID}" ) |
| 111 |
} |
| 112 |
aborted { |
| 113 |
updateGitlabCommitStatus(name: 'clang format', state: 'canceled') |
| 114 |
addGitLabMRComment(comment: ":interrobang: ${currentBuild.projectName} :page_with_curl: <b>ClangFormat</b> ABORTED :confused:<br/>Build results: [Jenkins [${currentBuild.displayName}]](${currentBuild.absoluteUrl})<br/>Commit: ${COMMIT_ID}" ) |
| 115 |
} |
| 116 |
failure { |
| 117 |
updateGitlabCommitStatus(name: 'clang format', state: 'failed') |
| 118 |
addGitLabMRComment(comment: ":red_circle: ${currentBuild.projectName} :page_with_curl: <b>ClangFormat</b> FAILURE :worried:<br/>Build results: [Jenkins [${currentBuild.displayName}]](${currentBuild.absoluteUrl})<br/>Commit: ${COMMIT_ID}" ) |
| 119 |
} |
| 120 |
} |
| 121 |
} |