Project

General

Profile

Actions

Kicking off Jenkins from terminal

I have the following awful script in ~/bin/buildit, and a Jenkins API token in ~/.jenkins-token.buildit.
  • leave buildit sync running in a screen window somewhere
  • ~/arvados$ buildit submit pushes the current branch and starts a developer-run-tests job to test it
  • switch to the window where buildit sync is running and wait for it to print a line like
    19563-log-cr-mem @ commit:8860ada0a6afd6adb2b26d5b0ab161bfe66c6019 -- https://​ci.arvados.org/job/developer-run-tests/3339/
    
  • paste that into a note on the relevant issue
#!/bin/bash                                                                                                                                                                                           

set -e

if [[ -z "$1" ]]; then
    echo >&2 "usage: $0 { sync | sync1 | submit }" 
    exit 2
fi

setup() {
    cache=~/.cache/buildit
    mkdir -p "${cache}" 

    user="$(whoami)" 
    if ! read token <~/.jenkins-token.buildit; then
        echo >&2 cannot read token
        exit $?
    fi
}

sync() {
    while : ; do
        sync1 || echo >&2 "sync: error: $?" 
        sleep 30
    done
}

sync1() {
    for cachefile in $(find "${cache}" -type f); do
        read timestamp sha1 abbrev branch < ${cachefile}
        echo "TODO: $timestamp $sha1 $abbrev $branch" 
        curl -fgLsS \
             -X GET \
             --user "$user:$token" \
             "https://ci.arvados.org/api/json?tree=jobs[name,number,builds[number,id,timestamp,result,displayName,description,actions[parameters[name,value]]]]" \
            | tee /tmp/jenkins-builds.json \
            | jq -r '.jobs                                                                                                                                                                            
                     | map(select(.name == "developer-run-tests"))                                                                                                                                    
                     | .[].builds                                                                                                                                                                     
                     | map(select(.actions[].parameters[0].name == "git_hash" and                                                                                                                     
                                  .actions[].parameters[0].value == "'"${sha1}"'" and                                                                                                                 
                                  .timestamp > '"${timestamp}"' - 60000 and                                                                                                                           
                                  .timestamp < '"${timestamp}"' + 3600000))                                                                                                                           
                     | .[]                                                                                                                                                                            
                     | ((.number | tostring) + " " + (.displayName | tostring))                                                                                                                       
                    ' | tee /tmp/jenkins-id-name \
                        | while read b displayName; do
            if [[ "$displayName" != "$abbrev" ]]; then
                echo -e "$cachefile: build $b => $abbrev $branch\n\n$branch @ commit:$sha1 -- https://ci.arvados.org/job/developer-run-tests/$b/\n" 
                curl -fgLsS \
                     -X POST \
                     --user "$user:$token" \
                     --data-urlencode submit=Save \
                     --data-urlencode json='{"displayName":"'$abbrev'","description":"'$branch'"}' \
                     "https://ci.arvados.org/job/developer-run-tests/$b/configSubmit" >/dev/null
            else
                echo "$cachefile: build $b => $abbrev $branch => done" 
                rm -v "${cachefile}" || true
            fi
        done
    done
}

submit() {
    if [[ -n "$(git diff; git diff --cached)" ]]; then
        echo >&2 NOPE, uncommitted stuff
        exit 1
    fi

    git push >&2
    sha1="$(git log -n1 --format=%H)" 
    branch="$(git describe --contains --all HEAD)" 
    abbrev="$(git log -n1 --format=%h)" 
    echo >&2 "$branch $abbrev $sha1 $token" 

    timestamp="$(printf %.03f $(date +%s.%N) | tr -d .)" 
    curl -fgLsS \
         -X POST \
         --user "$user:$token" \
         --data-urlencode json='{"parameter":[{"name":"git_hash","value":"'$sha1'"}]}' \
         "https://ci.arvados.org/job/developer-run-tests/build" 
    echo $timestamp $sha1 $abbrev $branch | tee ${cache}/${timestamp}
}

setup
${@}

Updated by Tom Clegg about 2 years ago · 1 revisions