Jenkinsfile 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. pipeline {
  2. agent {
  3. node {
  4. label 'jenkins-agent-web'
  5. customWorkspace '/workspace'
  6. }
  7. }
  8. options {
  9. timestamps()
  10. timeout(time: 15, unit: 'MINUTES')
  11. }
  12. environment {
  13. BRANCH_NAME = "${env.CHANGE_BRANCH ? env.CHANGE_BRANCH : env.BRANCH_NAME}"
  14. JENKINS_BUILD_NUMBER = "${BUILD_NUMBER}"
  15. }
  16. stages {
  17. stage('Set Build Variables') {
  18. steps {
  19. script {
  20. def relevantCommitHash
  21. if (env.CHANGE_BRANCH) {
  22. def commitExistsOnRemote = sh(script: "git branch --remotes --contains ${GIT_COMMIT}", returnStdout: true).trim()
  23. if (commitExistsOnRemote) {
  24. echo "PR build detected, but commit exists on remote. Using ${GIT_COMMIT}"
  25. relevantCommitHash = "${GIT_COMMIT}"
  26. } else {
  27. def parentCommits = sh(script: 'git rev-parse HEAD^@', returnStdout: true).trim().split('\n')
  28. if (parentCommits.size() >= 2) {
  29. echo "PR build detected. Jenkins checked out a merge commit: ${GIT_COMMIT} (parents: ${parentCommits.join(', ')})"
  30. relevantCommitHash = parentCommits[0]
  31. echo "Using first parent (branch commit): ${relevantCommitHash}"
  32. } else {
  33. echo "WARN: PR build detected, but ${GIT_COMMIT} is neither a merge commit, nor does it exist on the remote."
  34. relevantCommitHash = "${GIT_COMMIT}"
  35. }
  36. }
  37. } else {
  38. echo "Branch build detected. Using commit: ${GIT_COMMIT}"
  39. relevantCommitHash = "${GIT_COMMIT}"
  40. }
  41. env.COMMIT_SHA = relevantCommitHash
  42. env.SHORT_SHA = relevantCommitHash.take(7)
  43. env.BUILD_NUMBER = "${env.SHORT_SHA}_${env.JENKINS_BUILD_NUMBER}"
  44. }
  45. }
  46. }
  47. stage('Stage 1') {
  48. parallel {
  49. stage('Build') {
  50. steps {
  51. dir('services/git-bridge') {
  52. sh 'make refresh_cache -j2'
  53. retry(count: 3) {
  54. sh 'make docker_build_base'
  55. }
  56. }
  57. }
  58. }
  59. }
  60. }
  61. stage('Stage 2') {
  62. parallel {
  63. stage('Build production and push') {
  64. steps {
  65. dir('services/git-bridge') {
  66. retry(count: 3) {
  67. sh 'make docker_build'
  68. }
  69. sh 'make push_branch'
  70. }
  71. }
  72. }
  73. stage('Format Java') {
  74. steps {
  75. dir('services/git-bridge') {
  76. sh 'make docker_format'
  77. }
  78. }
  79. }
  80. stage('Test') {
  81. steps {
  82. dir('services/git-bridge') {
  83. retry(count: 3) {
  84. sh 'make docker_test'
  85. }
  86. }
  87. }
  88. }
  89. }
  90. }
  91. stage('Push Production') {
  92. steps {
  93. dir('services/git-bridge') {
  94. sh 'make push'
  95. }
  96. }
  97. }
  98. }
  99. post {
  100. // Collect junit test results for both success and failure case.
  101. always {
  102. junit checksName: 'git-bridge test results', testResults: 'services/git-bridge/target/surefire-reports/*.xml'
  103. }
  104. cleanup {
  105. dir('services/git-bridge') {
  106. sh 'make clean_ci'
  107. }
  108. sh 'make clean_jenkins -j10'
  109. }
  110. }
  111. }