File: Jenkinsfile

package info (click to toggle)
checkstyle 8.15-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 25,772 kB
  • sloc: java: 200,083; xml: 50,048; sh: 50; makefile: 5; sql: 2
file content (155 lines) | stat: -rw-r--r-- 4,079 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Node label that specifies on which slave(s) the job should run
BUILD_SLAVES_LABEL = 'build-servers'

DEFAULT_MAVEN_OPTS = "-Xss256k -Xmx300m -XX:MaxMetaspaceSize=80m -XX:MaxMetaspaceExpansion=10m " +
                     "-Dmaven.test.failure.ignore=false -XshowSettings:vm " + 
                     "-XX:+TieredCompilation -XX:TieredStopAtLevel=1"

// 'sha1' envvar is injected by Jenkins GHPRB plugin in case if build is started by pull request
IS_TRIGGERED_BY_PR = env.sha1?.trim()

GIT_BRANCH = '<unknown>'

enum HyperSize {
  S4("s4"),
  M1("m1"),
  M2("m2");

  private size;

  HyperSize(String size) {
    this.size = size
  }
  
  public String toString() {
    return size;
  }
}

// Text colours definition
def GREEN( String msg ) { return "\u001B[32m${msg}\u001B[0m" }
def YELLOW( String msg ) { return "\u001B[33m${msg}\u001B[0m" }
def RED( String msg ) { return "\u001B[31m${msg}\u001B[0m" }

def getCause(def build) {
  while(build.previousBuild) {
    build = build.previousBuild
  }

  return build.rawBuild.getCause(hudson.model.Cause$UserIdCause)
}

def getCauseDescription(def build) {
  return getCause(build).shortDescription
}

// See more about Hyper container sizes at https://hyper.sh/pricing.html
def runOnHyper(HyperSize size, String mavenOpts, String command) {
  
  String name = UUID.randomUUID()

  image = 'checkstyle/maven-builder-image:jdk-8u162b12-maven-3.5.3-groovy-2.4.15'
  containerTTL = 1800 // sec

  echo GREEN("To try this build locally, execute the following command: [ $command ]")

  // 1. Start empty Hyper container with timeout and let it put it's id to the file for further use
  retry(3) {
    sh "hyper -l=warn run -d --name '${name}' --size=${size.toString()} --cidfile ${name}.cid " +
       " --noauto-volume --restart=no -v \$(pwd):/usr/local/checkstyle/ " + 
       " -e MAVEN_OPTS='${mavenOpts}' ${image} sleep '${containerTTL}'"
  }

  // 2. Run the build command inside the container
  sh "hyper -l=info exec -i '${name}' bash -c '${command}'"

  // 3. Cleanup the container as soon as after build finish
  sh "hyper rm -fv '${name}' | cat"
}

def runOnHyperS4(String command) {
  runOnHyper(HyperSize.S4, DEFAULT_MAVEN_OPTS, command)
}

def runOnHyperM1(String command) {
  runOnHyper(HyperSize.M1, DEFAULT_MAVEN_OPTS, command)
}

def runOnHyperM2(String command) {
  runOnHyper(HyperSize.M2, DEFAULT_MAVEN_OPTS, command)
}

pipeline {

  agent {
    label "${BUILD_SLAVES_LABEL}"
  }
    
  options {
    ansiColor('xterm')
  }

  stages {

    stage ("Initial") {
      steps {
        echo GREEN("${getCauseDescription(currentBuild)}")

        script {
          // If build is triggered by PR, use PR branch, otherwise use master
          if (IS_TRIGGERED_BY_PR) {
            GIT_BRANCH = env.sha1
          } else {
            GIT_BRANCH = 'master'
          }
        }

        echo GREEN("Branch: $GIT_BRANCH")

        // Debug: print all the build envvars
        // echo sh(returnStdout: true, script: 'env')
      }
    }

    stage ("Prepare (triggered by PR)") {
      when { expression { IS_TRIGGERED_BY_PR } }
      steps {
        echo "${GREEN('PR:')} ${ghprbPullAuthorLoginMention} ${ghprbPullLink} $ghprbPullTitle"
      }
    }

    stage ("Prepare (triggered by hand)") {
      when { not { expression { IS_TRIGGERED_BY_PR } } }
        steps {
        echo GREEN("Triggered by hand, so building for master branch")
        deleteDir() /* clean up workspace */
        git 'git@github.com:checkstyle/checkstyle.git' /* clone the master branch */
      }
    }
 
    stage('Build') {
      parallel {
        stage('Compile') {
          steps {
            runOnHyperS4("mvn -B compile")
          }
        }
        stage('Package') {
          steps {
            runOnHyperM1("mvn -B package")
          }
        }
      }
    }
  }

  post {
    always {
      // Cleanup any Hyper.sh containers left by the build, if any
      sh "ls | grep '.cid' | xargs -I {} bash -c 'cat {}; echo' | xargs -I {} hyper rm -fv {} | cat"

      // Clean up workspace
      deleteDir()
    }
  }
}