forked from decentraland/catalyst-owner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
receive-sqs.sh
executable file
·57 lines (47 loc) · 1.65 KB
/
receive-sqs.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
#!/bin/bash
# reads a message from an SQS named SQS_QUEUE_NAME and performs the update based on the .version field of the message
if [ -n "${SQS_QUEUE_NAME}" ]; then
SQS_URL=$(aws sqs get-queue-url --queue-name "${SQS_QUEUE_NAME}" | jq -r .QueueUrl)
# Update the version if we have messages in the SQS
if [ -n "${SQS_URL}" ]; then
MSG=$(aws sqs receive-message --queue-url "$SQS_URL");
if [ -n "$MSG" ]; then
export DOCKER_TAG
export TARGETED_REGION
export EXIT_CODE
DOCKER_TAG=$(echo "$MSG" | jq -r '.Messages[0].Body' | jq -r .Message | jq -r .version);
TARGETED_REGION=$(echo "$MSG" | jq -r '.Messages[0].Body' | jq -r .Message | jq -r .region);
if [ -n "$TARGETED_REGION" ] && [ "$DEPLOYMENT_REGION" != "$TARGETED_REGION" ]; then
exit 0
fi
if [ -n "$DOCKER_TAG" ]; then
if ! [ -f ".env" ]; then
echo -n "Error: .env does not exist" >&2
else
escapedDockerTag=$(echo "$DOCKER_TAG" | jq -MR '.')
{
echo "";
echo "# $(date)";
echo "DOCKER_TAG=${escapedDockerTag}";
} >> .env
fi
export SLEEP_TIME=0
bash ./init.sh
EXIT_CODE=$?
else
EXIT_CODE=1
fi
# Only remove the message from the queue if the update was successful
if [ $EXIT_CODE -eq 0 ]; then
echo "$MSG" | jq -r '.Messages[0] | .ReceiptHandle' | (xargs -I {} aws sqs delete-message --queue-url "$SQS_URL" --receipt-handle {});
else
echo "[ERROR] consuming message $MSG"
exit 1
fi
fi
else
echo 'No SQS_URL is set'
fi
else
echo 'No SQS_QUEUE_NAME is set'
fi