aboutsummaryrefslogtreecommitdiff
path: root/openembedded-lkft/submit_build_result.groovy
blob: 732c92b84e6d8540aa2018b69578789e1ab6a27d (plain)
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
/*
    This lovely groovy script is used to send the jenkins build result to
    qa-reports.linaro.org (SQUAD).

    It requires the following variables to be available in the environment:
    DEVICE_TYPE: This becomes the environment in qa-reports
    QA_SERVER: URL for qa-reports (squad) host.
    QA_REPORTS_TOKEN: Auth token for qa-reports (squad) host.
    QA_SERVER_PROJECT: qa-reports project name
    KERNEL_DESCRIBE: This becomes build version in qa-reports (squad).

    Optional variables:
    QA_SERVER_TEAM: This becomes the group in qa-reports. It defaults to 'lkft'
    KERNEL_REPO: metadata - git repo url. Defaults to 'unknown'.
    SRCREV_kernel/KERNEL_COMMIT: metadata - git commit sha. Checks for
        SRCREV_kernel, falls through to KERNEL_COMMIT, and then finally defaults
        to 'unknown'.
    KERNEL_BRANCH: metadata - git branch. Defaults to 'unknown'.
    MAKE_KERNELVERSION: metadata - result of 'make kernelversion'. Defaults to 'unknown'.

    If the jenkins job is successful, a 'build/build_process' test result will
    be set to 'pass' in qa-reports, otherwise it will get a 'fail' result.

    To test changes to this script:
    - Set up a local jenkins container
    - Create a freestyle job
    - Add QA_REPORTS_TOKEN 'secret text' under 'Use secret text(s) or file(s)
    - Untick 'Inject nevironment variables to the build process'
      - Paste the following (for example) into the 'Properties Content':
          DEVICE_TYPE=x15
          QA_SERVER=https://staging-qa-reports.linaro.org
          QA_SERVER_PROJECT=linux-mainline-master
          KERNEL_DESCRIBE=v5.1
          QA_SERVER_TEAM=lkft
          KERNEL_REPO=https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
          SRCREV_kernel=e93c9c99a629c61837d5a7fc2120cd2b6c70dbdd
          KERNEL_BRANCH=master
          MAKE_KERNELVERSION=5.1.0
    - Enable Groovy Postbuild and paste this script in

    A build error log is extracted from the jenkins log using a regular
    expression, and included in the test result.

*/
import hudson.AbortException

def device_type = manager.envVars["DEVICE_TYPE"]
def qa_server_team = 'lkft'
if (manager.envVars.containsKey('QA_SERVER_TEAM')) {
    qa_server_team = manager.envVars["QA_SERVER_TEAM"]
}
def base_url = manager.envVars['QA_SERVER']
def auth_token = manager.envVars["QA_REPORTS_TOKEN"]
def build_process = "fail"
def log_error_pattern = ~'^ERROR: .*$'
def log_fetch_failure = ~'^ERROR: .*Fetcher failure: Unable to find revision.* even from upstream'
if (manager.build.result == hudson.model.Result.SUCCESS) {
    build_process = "pass"
}
def url_path = '/api/submit/' +
               qa_server_team +
               '/' +
               manager.envVars["QA_SERVER_PROJECT"] +
               '/' +
               manager.envVars["KERNEL_DESCRIBE"] +
               '/' +
               device_type
def url_path_sanity = '/api/submit/' +
               qa_server_team +
               '/' +
               manager.envVars["QA_SERVER_PROJECT"] + '-sanity' +
               '/' +
               manager.envVars["KERNEL_DESCRIBE"] +
               '/' +
               device_type

def error_log = ""
manager.build.logFile.eachLine { line ->
    matcher = log_error_pattern.matcher(line)
    if (matcher.matches()) {
        error_log += line+"\n"
    }
}

manager.build.logFile.eachLine { line ->
    matcher = log_fetch_failure.matcher(line)
    if (matcher.matches()) {
        throw new AbortException("Fetcher failure. Stop everything.")
    }
}

@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7' )
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.URLENC

def http = new HTTPBuilder(base_url)
def postBody = [
    tests: '{"build/build_process": "'+ build_process +'"}',
    log: error_log,
    metadata:
        // This is fussy, but I thought it was easier than figuring out how to
        // add JsonOutput
        """{
            "job_id": "${device_type}-${manager.build.number}",
            "git branch": "${manager.envVars.get("KERNEL_BRANCH", "unknown")}",
            "git repo": "${manager.envVars.get("KERNEL_REPO", "unknown")}",
            "git commit": "${manager.envVars.get("KERNEL_COMMIT", "unknown")}",
            "git commit": "${manager.envVars.get("SRCREV_kernel", manager.envVars.get("KERNEL_COMMIT", "unknown"))}",
            "git describe": "${manager.envVars.get("KERNEL_DESCRIBE", "unknown")}",
            "make_kernelversion": "${manager.envVars.get("MAKE_KERNELVERSION", "unknown")}"
        }"""
]
http.headers['Auth-Token'] = auth_token

http.post(path: url_path,
          body: postBody,
          requestContentType: URLENC ) { resp ->
  println "POST Success: ${resp.statusLine}"
  assert resp.statusLine.statusCode == 201
}
http.post(path: url_path_sanity,
          body: postBody,
          requestContentType: URLENC ) { resp ->
  println "POST Success: ${resp.statusLine}"
  assert resp.statusLine.statusCode == 201
}