Project

General

Profile

Kicking off Jenkins from terminal » History » Version 1

Tom Clegg, 10/26/2022 03:18 PM

1 1 Tom Clegg
h1. Kicking off Jenkins from terminal
2
3
I have the following awful script in @~/bin/buildit@, and a Jenkins API token in @~/.jenkins-token.buildit@.
4
* leave @buildit sync@ running in a screen window somewhere
5
* @~/arvados$ buildit submit@ pushes the current branch and starts a developer-run-tests job to test it
6
* switch to the window where @buildit sync@ is running and wait for it to print a line like <pre>
7
19563-log-cr-mem @ commit:8860ada0a6afd6adb2b26d5b0ab161bfe66c6019 -- https://​ci.arvados.org/job/developer-run-tests/3339/
8
</pre>
9
* paste that into a note on the relevant issue
10
11
<pre><code class="bash">
12
#!/bin/bash                                                                                                                                                                                           
13
14
set -e
15
16
if [[ -z "$1" ]]; then
17
    echo >&2 "usage: $0 { sync | sync1 | submit }"
18
    exit 2
19
fi
20
21
setup() {
22
    cache=~/.cache/buildit
23
    mkdir -p "${cache}"
24
25
    user="$(whoami)"
26
    if ! read token <~/.jenkins-token.buildit; then
27
        echo >&2 cannot read token
28
        exit $?
29
    fi
30
}
31
32
sync() {
33
    while : ; do
34
        sync1 || echo >&2 "sync: error: $?"
35
        sleep 30
36
    done
37
}
38
39
sync1() {
40
    for cachefile in $(find "${cache}" -type f); do
41
        read timestamp sha1 abbrev branch < ${cachefile}
42
        echo "TODO: $timestamp $sha1 $abbrev $branch"
43
        curl -fgLsS \
44
             -X GET \
45
             --user "$user:$token" \
46
             "https://ci.arvados.org/api/json?tree=jobs[name,number,builds[number,id,timestamp,result,displayName,description,actions[parameters[name,value]]]]" \
47
            | tee /tmp/jenkins-builds.json \
48
            | jq -r '.jobs                                                                                                                                                                            
49
                     | map(select(.name == "developer-run-tests"))                                                                                                                                    
50
                     | .[].builds                                                                                                                                                                     
51
                     | map(select(.actions[].parameters[0].name == "git_hash" and                                                                                                                     
52
                                  .actions[].parameters[0].value == "'"${sha1}"'" and                                                                                                                 
53
                                  .timestamp > '"${timestamp}"' - 60000 and                                                                                                                           
54
                                  .timestamp < '"${timestamp}"' + 3600000))                                                                                                                           
55
                     | .[]                                                                                                                                                                            
56
                     | ((.number | tostring) + " " + (.displayName | tostring))                                                                                                                       
57
                    ' | tee /tmp/jenkins-id-name \
58
                        | while read b displayName; do
59
            if [[ "$displayName" != "$abbrev" ]]; then
60
                echo -e "$cachefile: build $b => $abbrev $branch\n\n$branch @ commit:$sha1 -- https://ci.arvados.org/job/developer-run-tests/$b/\n"
61
                curl -fgLsS \
62
                     -X POST \
63
                     --user "$user:$token" \
64
                     --data-urlencode submit=Save \
65
                     --data-urlencode json='{"displayName":"'$abbrev'","description":"'$branch'"}' \
66
                     "https://ci.arvados.org/job/developer-run-tests/$b/configSubmit" >/dev/null
67
            else
68
                echo "$cachefile: build $b => $abbrev $branch => done"
69
                rm -v "${cachefile}" || true
70
            fi
71
        done
72
    done
73
}
74
75
submit() {
76
    if [[ -n "$(git diff; git diff --cached)" ]]; then
77
        echo >&2 NOPE, uncommitted stuff
78
        exit 1
79
    fi
80
81
    git push >&2
82
    sha1="$(git log -n1 --format=%H)"
83
    branch="$(git describe --contains --all HEAD)"
84
    abbrev="$(git log -n1 --format=%h)"
85
    echo >&2 "$branch $abbrev $sha1 $token"
86
87
    timestamp="$(printf %.03f $(date +%s.%N) | tr -d .)"
88
    curl -fgLsS \
89
         -X POST \
90
         --user "$user:$token" \
91
         --data-urlencode json='{"parameter":[{"name":"git_hash","value":"'$sha1'"}]}' \
92
         "https://ci.arvados.org/job/developer-run-tests/build"
93
    echo $timestamp $sha1 $abbrev $branch | tee ${cache}/${timestamp}
94
}
95
96
setup
97
${@}
98
</code></pre>