pipeline
def comments = ""
def result = "Great job!"
String appendState(String comments_, String status)
{
return comments_ + status
}
String createState(String state)
{
String shields = "https://img.shields.io/badge/"
return "<img src='" + shields + state + "'/>"
}
pipeline {
agent any
options {
gitLabConnection('gitlab')
}
stages {
stage('Prepare') {
steps {
script {
updateGitlabCommitStatus name: 'Prepare', state: 'running'
try {
echo "Prepare env ..."
} catch(Exception ex){
updateGitlabCommitStatus name: 'Prepare', state: 'failed'
comments = appendState(comments,createState("Prepare-FAILED-red"))
result = ex.getMessage()
throw ex;
} finally {
}
updateGitlabCommitStatus name: 'Prepare', state: 'success'
comments = appendState(comments,createState("Prepare-SUCCESS-green"))
}
}
}
stage('Checkout') {
steps {
script {
updateGitlabCommitStatus name: 'Checkout', state: 'running'
try {
echo "checkout code ..."
checkout([$class: 'GitSCM', branches: [[name: '*/${gitlabSourceBranch}']], extensions: [], userRemoteConfigs: [[credentialsId: 'jenkins', url: '${gitlabSourceRepoHttpUrl}']]])
} catch(Exception ex){
updateGitlabCommitStatus name: 'Checkout', state: 'failed'
comments = appendState(comments,createState("Checkout-FAILED-red"))
result = ex.getMessage()
throw ex;
} finally {
}
updateGitlabCommitStatus name: 'Checkout', state: 'success'
comments = appendState(comments,createState("Checkout-SUCCESS-green"))
}
}
}
stage('Cppcheck') {
steps {
script {
updateGitlabCommitStatus name: 'Cppcheck', state: 'running'
try {
echo "running Cppcheck ..."
sh 'cppcheck ./src --xml 2> cppcheck.xml'
publishCppcheck ignoreBlankFiles: true, pattern: 'cppcheck.xml'
} catch(Exception ex){
updateGitlabCommitStatus name: 'Cppcheck', state: 'failed'
comments = appendState(comments,createState("Cppcheck-FAILED-green"))
result = ex.getMessage()
throw ex;
} finally {
}
updateGitlabCommitStatus name: 'Cppcheck', state: 'success'
comments = appendState(comments,createState("Cppcheck-SUCCESS-green"))
}
}
}
stage('Build') {
steps {
script {
updateGitlabCommitStatus name: 'Build', state: 'running'
try {
echo "start building ..."
sh 'cmake -B./build -DCMAKE_BUILD_TYPE=Debug'
sh 'cmake --build ./build'
} catch(Exception ex){
updateGitlabCommitStatus name: 'Build', state: 'failed'
comments = appendState(comments,createState("Build-FAILED-red"))
result = ex.getMessage()
throw ex;
} finally {
}
updateGitlabCommitStatus name: 'Build', state: 'success'
comments = appendState(comments,createState("Build-SUCCESS-green"))
}
}
}
stage('Test') {
steps {
script {
updateGitlabCommitStatus name: 'Test', state: 'running'
try {
echo "start running UT ..."
sh './build/test/${gitlabSourceRepoName}_test --gtest_output=\"xml:gtest.xml\"'
xunit([GoogleTest(excludesPattern: '', pattern: 'gtest.xml', skipNoTestFiles: true, stopProcessingIfError: true)])
} catch(Exception ex){
updateGitlabCommitStatus name: 'Test', state: 'failed'
comments = appendState(comments,createState("Test-FAILED-red"))
result = ex.getMessage()
throw ex;
} finally {
}
updateGitlabCommitStatus name: 'Test', state: 'success'
comments = appendState(comments,createState("Test-SUCCESS-green"))
}
}
}
stage('coverage') {
steps {
script {
updateGitlabCommitStatus name: 'coverage', state: 'running'
try {
echo "coverage ..."
sh 'gcovr -x -r ./ --output \"coverage.xml\"'
cobertura autoUpdateHealth: false, autoUpdateStability: false, coberturaReportFile: 'coverage.xml', conditionalCoverageTargets: '70, 0, 0', failUnhealthy: false, failUnstable: false, lineCoverageTargets: '80, 0, 0', maxNumberOfBuilds: 0, methodCoverageTargets: '80, 0, 0', onlyStable: false, sourceEncoding: 'ASCII', zoomCoverageChart: false
} catch(Exception ex){
updateGitlabCommitStatus name: 'coverage', state: 'failed'
comments = appendState(comments,createState("Coverage-FAILED-red"))
result = ex.getMessage()
throw ex;
} finally {
}
updateGitlabCommitStatus name: 'coverage', state: 'success'
comments = appendState(comments,createState("Coverage-SUCCESS-green"))
}
}
}
}
post {
always {
script {
def buildState = "<strong>Jenkins Pipeline report: </strong> <br>"
comments = appendState(buildState, comments)
comments = appendState(comments, "<br>")
comments = appendState(comments, "<strong>Job</strong>: <a href='${env.BUILD_URL}'>pipeline job ${env.BUILD_NUMBER}</a><br>")
comments = appendState(comments, "<strong>State</strong>: ${currentBuild.result}<br>")
comments = appendState(comments, "<strong>Desctrption</strong>: ${currentBuild.description}<br>")
comments = appendState(comments, "<strong>Result: </strong> ${result}<br>")
addGitLabMRComment comment: "${comments}"
}
}
unsuccessful {
updateGitlabCommitStatus name: 'Complete', state: 'failed'
}
success {
updateGitlabCommitStatus name: 'Complete', state: 'success'
}
}
}
publisher
pipeline {
agent any
options {
gitLabConnection('gitlab')
}
stages {
stage('Prepare') {
steps {
script {
try {
echo "Prepare env ..."
} catch(Exception ex){
throw ex;
} finally {
}
}
}
}
stage('Checkout') {
steps {
script {
try {
echo "checkout code ..."
checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']], extensions: [], userRemoteConfigs: [[credentialsId: 'jenkins', url: '${repo}']]])
} catch(Exception ex){
throw ex;
} finally {
}
}
}
}
stage('Build') {
steps {
script {
try {
echo "start building ..."
sh './setting/install.sh Linux x86_64'
sh 'cmake -B./build -DCMAKE_BUILD_TYPE=Release'
sh 'cmake --build ./build'
sh 'cmake --install ./build'
} catch(Exception ex){
throw ex;
} finally {
}
}
}
}
stage('Publish') {
steps {
script {
try {
echo "publish ..."
repo_name = sh(script: "python3 -c \"import yaml;print(yaml.safe_load(open('./package.yaml'))['project'])\"", returnStdout: true).trim()
echo "${repo_name}"
sh "conan export-pkg ./ ${repo_name}/${version}@netflt/dev --force -s os=Linux -s arch=x86_64 -s build_type=Release"
sh "conan upload ${repo_name}/${version}@netflt/dev --all -r=netflt"
} catch(Exception ex){
throw ex;
} finally {
}
}
}
}
}
}
gitlab
def printEnvDetails() {
// only print environment details if a GitLab action is defined
if (env.gitlabActionType != null) {
echo "-----------------------------------------------------"
echo "GitLab Branch: ${gitlabBranch}"
echo "GitLab Source Branch: ${gitlabSourceBranch}"
echo "GitLab Action Type: ${gitlabActionType}"
echo "GitLab Username: ${gitlabUserName}"
echo "GitLab User Email: ${gitlabUserEmail}"
echo "GitLab Source Repo Homepage: ${gitlabSourceRepoHomepage}"
echo "GitLab Source Repo Name: ${gitlabSourceRepoName}"
echo "GitLab Source Namespace: ${gitlabSourceNamespace}"
echo "GitLab Source Repo URL: ${gitlabSourceRepoURL}"
echo "GitLab Source Repo SSH URL: ${gitlabSourceRepoSshUrl}"
echo "GitLab Source Repo HTTP URL: ${gitlabSourceRepoHttpUrl}"
// only print these variables when a merge action occurs - this prevents a groovy.lang.MissingPropertyException
if (env.gitlabActionType == "MERGE") {
echo "GitLab Merge Request Title: ${gitlabMergeRequestTitle}"
echo "GitLab Merge Request ID: ${gitlabMergeRequestId}"
echo "GitLab Merge Request State: ${env.gitlabMergeRequestState}"
echo "GitLab Merge Request Last Commit: ${gitlabMergeRequestLastCommit}"
echo "GitLab Merge Request Target Project ID: ${gitlabMergeRequestTargetProjectId}"
echo "GitLab Target Branch: ${gitlabTargetBranch}"
echo "GitLab Target Repo Name: ${gitlabTargetRepoName}"
echo "GitLab Target Namespace: ${gitlabTargetNamespace}"
echo "GitLab Target Repo SSH URL: ${gitlabTargetRepoSshUrl}"
echo "GitLab Target Repo HTTP URL: ${gitlabTargetRepoHttpUrl}"
}
echo "-----------------------------------------------------"
} else {
echo "env.gitlabActionType was null!"
}
}