forked from AztecProtocol/aztec-packages
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathci.sh
executable file
·245 lines (236 loc) · 8.43 KB
/
ci.sh
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/bin/bash
source $(git rev-parse --show-toplevel)/ci3/source
source $ci3/source_redis
cmd=${1:-}
arch=${ARCH:-$(arch)}
NO_TERMINATE=${NO_TERMINATE:-0}
BRANCH=${BRANCH:-$(git rev-parse --abbrev-ref HEAD)}
ci3_workflow_id=128853861
function echo_cmd {
local name=$1
shift
printf "${blue}${bold}%12s${reset}: %s\n" $name "$(echo $@ | sed 's/\.\\n/.\n /g')"
}
function print_usage {
echo "usage: $(basename $0) <cmd>"
echo
echo_cmd "ec2" "Launch an ec2 instance and './bootstrap.sh ci' on it.\n" \
"Exactly what Github Action's does, but doesn't touch GA."
echo_cmd "ec2-no-cache" "Same as ec2, but perform a full build and test (disable build and test cache)."
echo_cmd "ec2-test" "Same as ec2, but run all tests (disable test cache)."
echo_cmd "ec2-grind" "Same as ec2-test, but run over N instances."
echo_cmd "ec2-shell" "Launch an ec2 instance, clone the repo and drop into a shell."
echo_cmd "local" "Clone your last commit into a fresh container and bootstrap on local hardware."
echo_cmd "run" "Same as calling trigger, then rlog."
echo_cmd "shell" "Jump into a new shell on the current running build instance.\n" \
"Can provide a command to run instead of dropping into a shell, e.g. 'ci shell ls'."
echo_cmd "trigger" "Trigger the GA workflow on the PR associated with the current branch.\n" \
"Effectively the same as ec2, only the results will be tracked on your PR."
echo_cmd "rlog" "Will tail the logs of the latest GA run, or tail/dump the given GA run id."
echo_cmd "ilog" "Will tail the logs of the current running build instance."
echo_cmd "dlog" "Display the log of the given denoise log id."
echo_cmd "llog" "Tail the live log of a given log id."
echo_cmd "tlog" "Display the last log of the given test command as output by test_cmds."
echo_cmd "tilog" "Tail the live log of a given test command as output by test_cmds."
echo_cmd "shell-host" "Connect to host instance of the current running build."
echo_cmd "draft" "Mark current PR as draft (no automatic CI runs when pushing)."
echo_cmd "ready" "Mark current PR as ready (enable automatic CI runs when pushing)."
}
[ -n "$cmd" ] && shift
instance_name=$(echo -n "$BRANCH" | tr -c 'a-zA-Z0-9-' '_')_${arch}
[ -n "${INSTANCE_POSTFIX:-}" ] && instance_name+="_$INSTANCE_POSTFIX"
function get_ip_for_instance {
ip=$(aws ec2 describe-instances \
--region us-east-2 \
--filters "Name=tag:Name,Values=$instance_name" \
--query "Reservations[].Instances[0].PublicIpAddress" \
--output text)
}
function get_latest_run_id {
gh run list --workflow $ci3_workflow_id -b $BRANCH --limit 1 --json databaseId -q .[0].databaseId
}
function tail_live_instance {
get_ip_for_instance
[ -z "$ip" ] && return 1;
ssh -F $ci3/aws/build_instance_ssh_config -q -t -o ConnectTimeout=5 ubuntu@$ip "
trap 'exit 0' SIGINT
docker ps -a --filter name=aztec_build --format '{{.Names}}' | grep -q '^aztec_build$' || exit 1
docker logs -f aztec_build
"
}
case "$cmd" in
"ec2")
# Spin up ec2 instance and ci bootstrap with shell on failure.
# You can override the bootstrap command with the first arg e.g: ci ec2 full
bootstrap_ec2 "./bootstrap.sh ${1:-ci}"
;;
"ec2-no-cache")
# Same as ec2, but disable the build and test cache.
bootstrap_ec2 "NO_CACHE=1 USE_TEST_CACHE=0 ./bootstrap.sh ${1:-ci}"
;;
"ec2-test")
# Same as ec2, but don't use the test cache.
bootstrap_ec2 "USE_TEST_CACHE=0 ./bootstrap.sh ci"
;;
"ec2-shell")
# Spin up ec2 instance, clone, and drop into shell.
# False triggers the shell on fail.
bootstrap_ec2 "false"
;;
"ec2-grind")
# Same as ec2-test but repeat it over arg1 instances.
export DENOISE=1
num=${1:-5}
seq 0 $((num - 1)) | parallel --tag --line-buffered \
'INSTANCE_POSTFIX={} bootstrap_ec2 "USE_TEST_CACHE=0 ./bootstrap.sh ci" | cache_log "Grind {}"'
;;
"local")
# Create container with clone of local repo and bootstrap.
bootstrap_local "$@"
;;
"run")
# Trigger a GA workflow for current branch PR and tail logs.
$0 trigger
$0 rlog
;;
"shell")
get_ip_for_instance
[ -z "$ip" ] && echo "No instance found: $instance_name" && exit 1
[ "$#" -eq 0 ] && set -- "zsh" || true
ssh -tq -F $ci3/aws/build_instance_ssh_config ubuntu@$ip \
"docker start aztec_build &>/dev/null || true && docker exec -it --user aztec-dev aztec_build $@"
;;
"trigger")
# Trigger workflow.
# We use this label trick because triggering the workflow direct doesn't associate with the PR.
pr_number=$(gh pr list --head "$BRANCH" --json number --jq '.[0].number')
if [ -z "$pr_number" ]; then
echo "No pull request found for branch $BRANCH."
exit 1
fi
gh pr edit "$pr_number" --remove-label "trigger-workflow" &> /dev/null
gh pr edit "$pr_number" --add-label "trigger-workflow" &> /dev/null
sleep 1
gh pr edit "$pr_number" --remove-label "trigger-workflow" &> /dev/null
run_id=$(get_latest_run_id)
echo "In progress..." | redis_setexz $run_id 3600
echo -e "Triggered CI for PR: $pr_number (ci rlog ${yellow}$run_id${reset})"
;;
"rlog")
[ -z "${1:-}" ] && run_id=$(get_latest_run_id) || run_id=$1
output=$(redis_getz $run_id)
if [ -z "$output" ] || [ "$output" == "In progress..." ]; then
# If we're in progress, tail live logs from launched instance.
exec $0 ilog
else
echo "$output" | $PAGER
fi
;;
"ilog")
while ! tail_live_instance; do
echo "Waiting on instance with name: $instance_name"
sleep 10
done
;;
"dlog")
if [ "$CI_REDIS_AVAILABLE" -ne 1 ]; then
echo "No redis available for log query."
exit 1
fi
pager=${PAGER:-less}
[ ! -t 0 ] && pager=cat
redis_getz $1 | $pager
;;
"tlog")
if [ "$CI_REDIS_AVAILABLE" -ne 1 ]; then
echo "No redis available for test query."
exit 1
fi
pager=${PAGER:-less}
key=$(hash_str "$1")
log_key=$(redis_cli --raw GET $key)
if [ -n "$log_key" ]; then
redis_getz $log_key | $pager
else
echo "No test log found for: $key"
exit 1
fi
;;
"tilog")
# Given a test cmd, tail it's a live log.
./ci.sh llog $(hash_str "$1")
;;
"llog")
# If the log file exists locally, tail it, otherwise assume it's remote.
key=$1
if [ -f /tmp/$key ]; then
tail -F -n +1 /tmp/$key
else
./ci.sh shell tail -F -n +1 /tmp/$key
fi
;;
"shell-host")
get_ip_for_instance
[ -z "$ip" ] && echo "No instance found: $instance_name" && exit 1
ssh -t -F $ci3/aws/build_instance_ssh_config ubuntu@$ip
;;
"kill")
existing_instance=$(aws ec2 describe-instances \
--region us-east-2 \
--filters "Name=tag:Name,Values=$instance_name" \
--query "Reservations[].Instances[?State.Name!='terminated'].InstanceId[]" \
--output text)
if [ -n "$existing_instance" ]; then
aws_terminate_instance $existing_instance
fi
;;
"draft")
pr_number=$(gh pr list --head "$BRANCH" --json number --jq '.[0].number')
if [ -n "$pr_number" ]; then
gh pr ready "$pr_number" --undo
echo "Pull request #$pr_number has been set to draft."
else
echo "No pull request found for branch $BRANCH."
fi
;;
"ready")
pr_number=$(gh pr list --head "$BRANCH" --json number --jq '.[0].number')
if [ -n "$pr_number" ]; then
gh pr ready "$pr_number"
echo "Pull request #$pr_number has been set to ready."
else
echo "No pull request found for branch $BRANCH."
fi
;;
"gha-url")
workflow_id=$(gh workflow list --all --json name,id -q '.[] | select(.name == "CI").id')
run_url=$(gh run list --workflow $workflow_id -b $BRANCH --limit 1 --json url -q '.[0].url')
if [ -z "$run_url" ]; then
echo "No workflow runs found for branch '$BRANCH'."
exit 1
fi
echo "$run_url"
;;
"pr-url")
# Fetch the current PR associated with the branch
pr_url=$(gh pr list --head "$BRANCH" --limit 1 --json url -q '.[0].url')
if [ -z "$pr_url" ]; then
echo "No pull request found for branch '$BRANCH'."
exit 1
fi
echo "$pr_url"
;;
"deploy")
VERSION_TAG=$1
;;
"watch")
watch_ci "$@"
;;
"help"|"")
print_usage
;;
*)
echo "Unknown command: $cmd, see ./ci.sh help"
exit 1
;;
esac