-
Notifications
You must be signed in to change notification settings - Fork 7
/
travis-debug
executable file
·131 lines (118 loc) · 3.26 KB
/
travis-debug
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
#!/usr/bin/env bash
set -euo pipefail
YP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." >/dev/null && pwd)"
source ${YP_DIR}/sh/common.inc.sh
#- travis-debug 1.0
## Usage: travis-debug [OPTION]
## Request debug mode for a Travis CI job.
##
## --job A job url or id
## --token Travis CI token shown on your profile page
## Defaults to ${TRAVIS_API_TOKEN}
## --org Use travis-ci.org (if you have not migrated a public repository yet)
##
## -h, --help Display this help and exit
## -v, --version Output version information and exit
TRAVIS_API_TOKEN=${TRAVIS_API_TOKEN:-}
JOB_ID=
TRAVIS_DOMAIN=travis-ci.com
if { getopt --test >/dev/null 2>&1 && false; } || [[ "$?" = "4" ]] || false; then
ARGS=$(getopt -o hv -l help,version,token:,job:,org,com,pro -n $(basename ${BASH_SOURCE[0]}) -- "$@") || sh_script_usage # editorconfig-checker-disable-line
eval set -- "${ARGS}"
fi
while [[ $# -gt 0 ]]; do
case "$1" in
--token)
TRAVIS_API_TOKEN=$2
shift 2
;;
--job)
JOB_ID=$2
shift 2
;;
--org)
TRAVIS_DOMAIN=travis-ci.org
shift
;;
--com,--pro)
# ignore
shift
;;
-h|--help)
sh_script_usage
;;
-v|--version)
sh_script_version
;;
--)
shift
break
;;
-*)
sh_script_usage
;;
*)
break
;;
esac
done
[[ $# -eq 0 ]] || sh_script_usage
[[ -n ${TRAVIS_API_TOKEN} ]] || {
echo_err "Please provide a --token."
exit 1
}
[[ -n ${JOB_ID} ]] || {
echo_err "Please provide a --job."
exit 1
}
function get_tmate_cmd() {
curl -qfsSL \
-X GET \
-H "accept: application/json" \
-H "travis-api-version: 3" \
-H "authorization: token ${TRAVIS_API_TOKEN}" \
"https://api.${TRAVIS_DOMAIN}/job/${JOB_ID}/log" | \
jq -r ".content" | \
grep -o "ssh .\+\.tmate\.io" || true
}
[[ ! "${JOB_ID}" =~ /builds/ ]] || {
JOB_ID=$(basename "${JOB_ID}")
JOB_ID=$(curl -qfsSL \
-X GET \
-H "accept: application/json" \
-H "travis-api-version: 3" \
-H "authorization: token ${TRAVIS_API_TOKEN}" \
"https://api.${TRAVIS_DOMAIN}/build/${JOB_ID}" | \
jq ".jobs[0].id")
}
JOB_ID=$(basename "${JOB_ID}")
curl -qfsSL \
-X POST \
-H "content-type: application/json" \
-H "accept: application/json" \
-H "travis-api-version: 3" \
-H "authorization: token ${TRAVIS_API_TOKEN}" \
-d '{"quiet": true}' \
"https://api.${TRAVIS_DOMAIN}/job/${JOB_ID}/debug"
echo
echo_do "Waiting for the tmate session..."
sleep 90
while true; do
TMATE_CMD=$(get_tmate_cmd)
[[ -z "${TMATE_CMD}" ]] || break
echo_info "Waiting for the tmate session..."
sleep 30
done
echo_done
echo_info "${TMATE_CMD}"
${TMATE_CMD} || true
echo_do "Deleting the log..."
sleep 5
# FIXME it does the job but return 500 Internal Server Error
curl -qfsSL \
-X DELETE \
-H "travis-api-version: 3" \
-H "authorization: token ${TRAVIS_API_TOKEN}" \
"https://api.${TRAVIS_DOMAIN}/job/${JOB_ID}/log" || true
echo
echo_done