pipeline

  1. def comments = ""
  2. def result = "Great job!"
  3. String appendState(String comments_, String status)
  4. {
  5. return comments_ + status
  6. }
  7. String createState(String state)
  8. {
  9. String shields = "https://img.shields.io/badge/"
  10. return "<img src='" + shields + state + "'/>"
  11. }
  12. pipeline {
  13. agent any
  14. options {
  15. gitLabConnection('gitlab')
  16. }
  17. stages {
  18. stage('Prepare') {
  19. steps {
  20. script {
  21. updateGitlabCommitStatus name: 'Prepare', state: 'running'
  22. try {
  23. echo "Prepare env ..."
  24. } catch(Exception ex){
  25. updateGitlabCommitStatus name: 'Prepare', state: 'failed'
  26. comments = appendState(comments,createState("Prepare-FAILED-red"))
  27. result = ex.getMessage()
  28. throw ex;
  29. } finally {
  30. }
  31. updateGitlabCommitStatus name: 'Prepare', state: 'success'
  32. comments = appendState(comments,createState("Prepare-SUCCESS-green"))
  33. }
  34. }
  35. }
  36. stage('Checkout') {
  37. steps {
  38. script {
  39. updateGitlabCommitStatus name: 'Checkout', state: 'running'
  40. try {
  41. echo "checkout code ..."
  42. checkout([$class: 'GitSCM', branches: [[name: '*/${gitlabSourceBranch}']], extensions: [], userRemoteConfigs: [[credentialsId: 'jenkins', url: '${gitlabSourceRepoHttpUrl}']]])
  43. } catch(Exception ex){
  44. updateGitlabCommitStatus name: 'Checkout', state: 'failed'
  45. comments = appendState(comments,createState("Checkout-FAILED-red"))
  46. result = ex.getMessage()
  47. throw ex;
  48. } finally {
  49. }
  50. updateGitlabCommitStatus name: 'Checkout', state: 'success'
  51. comments = appendState(comments,createState("Checkout-SUCCESS-green"))
  52. }
  53. }
  54. }
  55. stage('Cppcheck') {
  56. steps {
  57. script {
  58. updateGitlabCommitStatus name: 'Cppcheck', state: 'running'
  59. try {
  60. echo "running Cppcheck ..."
  61. sh 'cppcheck ./src --xml 2> cppcheck.xml'
  62. publishCppcheck ignoreBlankFiles: true, pattern: 'cppcheck.xml'
  63. } catch(Exception ex){
  64. updateGitlabCommitStatus name: 'Cppcheck', state: 'failed'
  65. comments = appendState(comments,createState("Cppcheck-FAILED-green"))
  66. result = ex.getMessage()
  67. throw ex;
  68. } finally {
  69. }
  70. updateGitlabCommitStatus name: 'Cppcheck', state: 'success'
  71. comments = appendState(comments,createState("Cppcheck-SUCCESS-green"))
  72. }
  73. }
  74. }
  75. stage('Build') {
  76. steps {
  77. script {
  78. updateGitlabCommitStatus name: 'Build', state: 'running'
  79. try {
  80. echo "start building ..."
  81. sh 'cmake -B./build -DCMAKE_BUILD_TYPE=Debug'
  82. sh 'cmake --build ./build'
  83. } catch(Exception ex){
  84. updateGitlabCommitStatus name: 'Build', state: 'failed'
  85. comments = appendState(comments,createState("Build-FAILED-red"))
  86. result = ex.getMessage()
  87. throw ex;
  88. } finally {
  89. }
  90. updateGitlabCommitStatus name: 'Build', state: 'success'
  91. comments = appendState(comments,createState("Build-SUCCESS-green"))
  92. }
  93. }
  94. }
  95. stage('Test') {
  96. steps {
  97. script {
  98. updateGitlabCommitStatus name: 'Test', state: 'running'
  99. try {
  100. echo "start running UT ..."
  101. sh './build/test/${gitlabSourceRepoName}_test --gtest_output=\"xml:gtest.xml\"'
  102. xunit([GoogleTest(excludesPattern: '', pattern: 'gtest.xml', skipNoTestFiles: true, stopProcessingIfError: true)])
  103. } catch(Exception ex){
  104. updateGitlabCommitStatus name: 'Test', state: 'failed'
  105. comments = appendState(comments,createState("Test-FAILED-red"))
  106. result = ex.getMessage()
  107. throw ex;
  108. } finally {
  109. }
  110. updateGitlabCommitStatus name: 'Test', state: 'success'
  111. comments = appendState(comments,createState("Test-SUCCESS-green"))
  112. }
  113. }
  114. }
  115. stage('coverage') {
  116. steps {
  117. script {
  118. updateGitlabCommitStatus name: 'coverage', state: 'running'
  119. try {
  120. echo "coverage ..."
  121. sh 'gcovr -x -r ./ --output \"coverage.xml\"'
  122. 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
  123. } catch(Exception ex){
  124. updateGitlabCommitStatus name: 'coverage', state: 'failed'
  125. comments = appendState(comments,createState("Coverage-FAILED-red"))
  126. result = ex.getMessage()
  127. throw ex;
  128. } finally {
  129. }
  130. updateGitlabCommitStatus name: 'coverage', state: 'success'
  131. comments = appendState(comments,createState("Coverage-SUCCESS-green"))
  132. }
  133. }
  134. }
  135. }
  136. post {
  137. always {
  138. script {
  139. def buildState = "<strong>Jenkins Pipeline report: </strong> <br>"
  140. comments = appendState(buildState, comments)
  141. comments = appendState(comments, "<br>")
  142. comments = appendState(comments, "<strong>Job</strong>: <a href='${env.BUILD_URL}'>pipeline job ${env.BUILD_NUMBER}</a><br>")
  143. comments = appendState(comments, "<strong>State</strong>: ${currentBuild.result}<br>")
  144. comments = appendState(comments, "<strong>Desctrption</strong>: ${currentBuild.description}<br>")
  145. comments = appendState(comments, "<strong>Result: </strong> ${result}<br>")
  146. addGitLabMRComment comment: "${comments}"
  147. }
  148. }
  149. unsuccessful {
  150. updateGitlabCommitStatus name: 'Complete', state: 'failed'
  151. }
  152. success {
  153. updateGitlabCommitStatus name: 'Complete', state: 'success'
  154. }
  155. }
  156. }

publisher

  1. pipeline {
  2. agent any
  3. options {
  4. gitLabConnection('gitlab')
  5. }
  6. stages {
  7. stage('Prepare') {
  8. steps {
  9. script {
  10. try {
  11. echo "Prepare env ..."
  12. } catch(Exception ex){
  13. throw ex;
  14. } finally {
  15. }
  16. }
  17. }
  18. }
  19. stage('Checkout') {
  20. steps {
  21. script {
  22. try {
  23. echo "checkout code ..."
  24. checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']], extensions: [], userRemoteConfigs: [[credentialsId: 'jenkins', url: '${repo}']]])
  25. } catch(Exception ex){
  26. throw ex;
  27. } finally {
  28. }
  29. }
  30. }
  31. }
  32. stage('Build') {
  33. steps {
  34. script {
  35. try {
  36. echo "start building ..."
  37. sh './setting/install.sh Linux x86_64'
  38. sh 'cmake -B./build -DCMAKE_BUILD_TYPE=Release'
  39. sh 'cmake --build ./build'
  40. sh 'cmake --install ./build'
  41. } catch(Exception ex){
  42. throw ex;
  43. } finally {
  44. }
  45. }
  46. }
  47. }
  48. stage('Publish') {
  49. steps {
  50. script {
  51. try {
  52. echo "publish ..."
  53. repo_name = sh(script: "python3 -c \"import yaml;print(yaml.safe_load(open('./package.yaml'))['project'])\"", returnStdout: true).trim()
  54. echo "${repo_name}"
  55. sh "conan export-pkg ./ ${repo_name}/${version}@netflt/dev --force -s os=Linux -s arch=x86_64 -s build_type=Release"
  56. sh "conan upload ${repo_name}/${version}@netflt/dev --all -r=netflt"
  57. } catch(Exception ex){
  58. throw ex;
  59. } finally {
  60. }
  61. }
  62. }
  63. }
  64. }
  65. }

gitlab

  1. def printEnvDetails() {
  2. // only print environment details if a GitLab action is defined
  3. if (env.gitlabActionType != null) {
  4. echo "-----------------------------------------------------"
  5. echo "GitLab Branch: ${gitlabBranch}"
  6. echo "GitLab Source Branch: ${gitlabSourceBranch}"
  7. echo "GitLab Action Type: ${gitlabActionType}"
  8. echo "GitLab Username: ${gitlabUserName}"
  9. echo "GitLab User Email: ${gitlabUserEmail}"
  10. echo "GitLab Source Repo Homepage: ${gitlabSourceRepoHomepage}"
  11. echo "GitLab Source Repo Name: ${gitlabSourceRepoName}"
  12. echo "GitLab Source Namespace: ${gitlabSourceNamespace}"
  13. echo "GitLab Source Repo URL: ${gitlabSourceRepoURL}"
  14. echo "GitLab Source Repo SSH URL: ${gitlabSourceRepoSshUrl}"
  15. echo "GitLab Source Repo HTTP URL: ${gitlabSourceRepoHttpUrl}"
  16. // only print these variables when a merge action occurs - this prevents a groovy.lang.MissingPropertyException
  17. if (env.gitlabActionType == "MERGE") {
  18. echo "GitLab Merge Request Title: ${gitlabMergeRequestTitle}"
  19. echo "GitLab Merge Request ID: ${gitlabMergeRequestId}"
  20. echo "GitLab Merge Request State: ${env.gitlabMergeRequestState}"
  21. echo "GitLab Merge Request Last Commit: ${gitlabMergeRequestLastCommit}"
  22. echo "GitLab Merge Request Target Project ID: ${gitlabMergeRequestTargetProjectId}"
  23. echo "GitLab Target Branch: ${gitlabTargetBranch}"
  24. echo "GitLab Target Repo Name: ${gitlabTargetRepoName}"
  25. echo "GitLab Target Namespace: ${gitlabTargetNamespace}"
  26. echo "GitLab Target Repo SSH URL: ${gitlabTargetRepoSshUrl}"
  27. echo "GitLab Target Repo HTTP URL: ${gitlabTargetRepoHttpUrl}"
  28. }
  29. echo "-----------------------------------------------------"
  30. } else {
  31. echo "env.gitlabActionType was null!"
  32. }
  33. }