Jenkinsfile 8.49 KB
Newer Older
Martin Raifer's avatar
Martin Raifer committed
1
pipeline {
2
  agent {label 'main'}
3
4
5
  options {
    timeout(time: 30, unit: 'MINUTES')
  }
Martin Raifer's avatar
Martin Raifer committed
6
7

  environment {
8
    REPO_NAME = sh(returnStdout: true, script: 'basename `git remote get-url origin` .git').trim()
9
    VERSION = sh(returnStdout: true, script: 'mvn --batch-mode org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | grep -Ev "(^\\[|Download\\w+)"').trim()
10
11
12
13
14
15
    LATEST_AUTHOR = sh(returnStdout: true, script: 'git show -s --pretty=%an').trim()
    LATEST_COMMIT_ID = sh(returnStdout: true, script: 'git describe --tags --long  --always').trim()

    MAVEN_TEST_OPTIONS = ' '
    INFER_BRANCH_REGEX = /(^master$)/
    SNAPSHOT_BRANCH_REGEX = /(^master$)/
Martin Raifer's avatar
Martin Raifer committed
16
17
18
19
20
21
22
23
24
25
26
    RELEASE_REGEX = /^([0-9]+(\.[0-9]+)*)(-(RC|beta-|alpha-)[0-9]+)?$/
    RELEASE_DEPLOY = false
    SNAPSHOT_DEPLOY = false
  }

  stages {
    stage ('Build and Test') {
      steps {
        script {
          env.MAVEN_HOME = '/usr/share/maven'

27
28
29
          echo REPO_NAME
          echo LATEST_AUTHOR
          echo LATEST_COMMIT_ID
Martin Raifer's avatar
Martin Raifer committed
30
31
32
33
34

          echo env.BRANCH_NAME
          echo env.BUILD_NUMBER
          echo env.TAG_NAME

35
          if (!(VERSION ==~ RELEASE_REGEX || VERSION ==~ /.*-SNAPSHOT$/)) {
Martin Raifer's avatar
Martin Raifer committed
36
37
            echo 'Version:'
            echo VERSION
38
            error 'The version declaration is invalid. It is neither a release nor a snapshot. Maybe an error occured while fetching the parent pom using maven?'
Martin Raifer's avatar
Martin Raifer committed
39
40
41
42
43
44
45
46
47
48
49
          }
        }
        script {
          server = Artifactory.server 'HeiGIT Repo'
          rtMaven = Artifactory.newMavenBuild()

          rtMaven.resolver server: server, releaseRepo: 'main', snapshotRepo: 'main'
          rtMaven.deployer server: server, releaseRepo: 'libs-release-local', snapshotRepo: 'libs-snapshot-local'
          rtMaven.deployer.deployArtifacts = false

          withCredentials([string(credentialsId: 'gpg-signing-key-passphrase', variable: 'PASSPHRASE')]) {
50
            buildInfo = rtMaven.run pom: 'pom.xml', goals: '--batch-mode clean compile javadoc:jar source:jar verify -P jacoco,sign,git -Dmaven.repo.local=.m2 $MAVEN_TEST_OPTIONS -Dgpg.passphrase=$PASSPHRASE'
Martin Raifer's avatar
Martin Raifer committed
51
52
53
54
55
          }
        }
      }
      post {
        failure {
56
          rocketSend channel: 'jenkinsohsome', emoji: ':sob:' , message: "*${REPO_NAME}*-build nr. ${env.BUILD_NUMBER} *failed* on Branch - ${env.BRANCH_NAME}  (<${env.BUILD_URL}|Open Build in Jenkins>). Latest commit from  ${LATEST_AUTHOR}. Review the code!" , rawMessage: true
Martin Raifer's avatar
Martin Raifer committed
57
58
59
60
61
62
63
        }
      }
    }

    stage ('Reports and Statistics') {
      steps {
        script {
64
          withSonarQubeEnv('sonarcloud GIScience/ohsome') {
65
            sh "mvn --batch-mode sonar:sonar -Dsonar.branch.name=${env.BRANCH_NAME}"
66
          }
67
68
          report_basedir = "/srv/reports/${REPO_NAME}/${VERSION}_${env.BRANCH_NAME}/${env.BUILD_NUMBER}_${LATEST_COMMIT_ID}"

Martin Raifer's avatar
Martin Raifer committed
69
          // jacoco
70
          report_dir = report_basedir + "/jacoco/"
Martin Raifer's avatar
Martin Raifer committed
71
72

          jacoco(
73
74
75
              execPattern      : '**/target/jacoco.exec',
              classPattern     : '**/target/classes',
              sourcePattern    : '**/src/main/java',
76
              inclusionPattern : 'org/heigit/**'
Martin Raifer's avatar
Martin Raifer committed
77
          )
78
          sh "mkdir -p ${report_dir} && rm -Rf ${report_dir}* && find . -path '*/target/site/jacoco' -exec cp -R --parents {} ${report_dir} \\; && find ${report_dir} -path '*/target/site/jacoco' | while read line; do echo \$line; neu=\${line/target\\/site\\/jacoco/} ;  mv \$line/* \$neu ; done && find ${report_dir} -type d -empty -delete"
Martin Raifer's avatar
Martin Raifer committed
79
80

          // infer
81
82
          if (env.BRANCH_NAME ==~ INFER_BRANCH_REGEX) {
            report_dir = report_basedir + "/infer/"
83
84
            sh "mvn --batch-mode clean"
            sh "infer run --pmd-xml -r -- mvn --batch-mode compile"
85
            sh "mkdir -p ${report_dir} && rm -Rf ${report_dir}* && cp -R ./infer-out/* ${report_dir}"
Martin Raifer's avatar
Martin Raifer committed
86
87
88
          }

          // warnings plugin
89
          rtMaven.run pom: 'pom.xml', goals: '--batch-mode -V -e compile checkstyle:checkstyle pmd:pmd pmd:cpd spotbugs:spotbugs -Dmaven.repo.local=.m2 $MAVEN_TEST_OPTIONS'
Martin Raifer's avatar
Martin Raifer committed
90
91
92
93
94
95
96
97
98
99
100

          recordIssues enabledForFailure: true, tools: [mavenConsole(),  java(), javaDoc()]
          recordIssues enabledForFailure: true, tool: checkStyle()
          recordIssues enabledForFailure: true, tool: spotBugs()
          recordIssues enabledForFailure: true, tool: cpd(pattern: '**/target/cpd.xml')
          recordIssues enabledForFailure: true, tool: pmdParser(pattern: '**/target/pmd.xml')
          recordIssues enabledForFailure: true, tool: pmdParser(pattern: '**/infer-out/report.xml', id: 'infer')
        }
      }
      post {
        failure {
101
102
103
104
105
          rocketSend channel: 'jenkinsohsome', emoji: ':disappointed:', message: "Reporting of *${REPO_NAME}*-build nr. ${env.BUILD_NUMBER} *failed* on Branch - ${env.BRANCH_NAME}  (<${env.BUILD_URL}|Open Build in Jenkins>). Latest commit from  ${LATEST_AUTHOR}." , rawMessage: true
        }
      }
    }

106
107
108
    // START CUSTOM oshdb-extractor
    // Deployment and Javadoc not necessary here
    // END CUSTOM oshdb-extractor
109
110
111
    stage ('Check Dependencies') {
      when {
        expression {
112
          if ((currentBuild.number > 1) && (env.BRANCH_NAME ==~ SNAPSHOT_BRANCH_REGEX)) {
113
114
115
116
117
118
119
120
121
122
123
            month_pre = new Date(currentBuild.previousBuild.rawBuild.getStartTimeInMillis())[Calendar.MONTH]
            echo month_pre.toString()
            month_now = new Date(currentBuild.rawBuild.getStartTimeInMillis())[Calendar.MONTH]
            echo month_now.toString()
            return month_pre != month_now
          }
          return false
        }
      }
      steps {
        script {
124
125
126
127
128
129
130
          try {
            update_notify = sh(returnStdout: true, script: 'mvn --batch-mode versions:display-dependency-updates | grep -Pzo "(?s)The following dependencies([^\\n]*\\S\\n)*[^\\n]*\\s\\n"').trim()
            echo update_notify
            rocketSend channel: 'jenkinsohsome', emoji: ':wave:' , message: "Check your dependencies in *${REPO_NAME}*. You might have updates: ${update_notify}" , rawMessage: true
          } catch (err) {
            echo "No maven dependency upgrades found."
          }
131
        }
132
        script {
133
134
135
136
137
138
139
          try {
            update_notify = sh(returnStdout: true, script: 'mvn --batch-mode versions:display-plugin-updates | grep -Pzo "(?s)The following plugin update([^\\n]*\\S\\n)*[^\\n]*\\s\\n"').trim()
            echo update_notify
            rocketSend channel: 'jenkinsohsome', emoji: ':wave:' , message: "Check your maven plugins in *${REPO_NAME}*. You might have updates: ${update_notify}" , rawMessage: true
          } catch (err) {
            echo "No maven plugin upgrades found."
          }
Martin Raifer's avatar
Martin Raifer committed
140
141
142
143
144
145
146
        }
      }
    }

    stage ('Encourage') {
      when {
        expression {
147
148
149
150
151
          if (currentBuild.number > 1) {
            date_pre = new Date(currentBuild.previousBuild.rawBuild.getStartTimeInMillis()).clearTime()
            echo date_pre.format( 'yyyyMMdd' )
            date_now = new Date(currentBuild.rawBuild.getStartTimeInMillis()).clearTime()
            echo date_now.format( 'yyyyMMdd' )
152
            return date_pre.numberAwareCompareTo(date_now) < 0
Martin Raifer's avatar
Martin Raifer committed
153
154
155
156
157
          }
          return false
        }
      }
      steps {
158
        rocketSend channel: 'jenkinsohsome', emoji: ':wink:', message: "Hey, this is just your daily notice that Jenkins is still working for you on *${REPO_NAME}* Branch ${env.BRANCH_NAME}! Happy and for free! Keep it up!" , rawMessage: true
Martin Raifer's avatar
Martin Raifer committed
159
160
161
      }
      post {
        failure {
162
          rocketSend channel: 'jenkinsohsome', emoji: ':disappointed:', message: "Reporting of *${REPO_NAME}*-build nr. ${env.BUILD_NUMBER} *failed* on Branch - ${env.BRANCH_NAME}  (<${env.BUILD_URL}|Open Build in Jenkins>). Latest commit from  ${LATEST_AUTHOR}." , rawMessage: true
Martin Raifer's avatar
Martin Raifer committed
163
164
165
166
167
168
169
170
171
172
173
        }
      }
    }

    stage ('Report Status Change') {
      when {
        expression {
          return ((currentBuild.number > 1) && (currentBuild.getPreviousBuild().result == 'FAILURE'))
        }
      }
      steps {
174
        rocketSend channel: 'jenkinsohsome', emoji: ':sunglasses:', message: "We had some problems, but we are BACK TO NORMAL! Nice debugging: *${REPO_NAME}*-build-nr. ${env.BUILD_NUMBER} *succeeded* on Branch - ${env.BRANCH_NAME}  (<${env.BUILD_URL}|Open Build in Jenkins>). Latest commit from  ${LATEST_AUTHOR}." , rawMessage: true
Martin Raifer's avatar
Martin Raifer committed
175
176
177
      }
      post {
        failure {
178
          rocketSend channel: 'jenkinsohsome', emoji: ':disappointed:', message: "Reporting of *${REPO_NAME}*-build nr. ${env.BUILD_NUMBER} *failed* on Branch - ${env.BRANCH_NAME}  (<${env.BUILD_URL}|Open Build in Jenkins>). Latest commit from  ${LATEST_AUTHOR}." , rawMessage: true
Martin Raifer's avatar
Martin Raifer committed
179
180
181
182
183
        }
      }
    }
  }
}