-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathverify-release-assets.sh
executable file
·79 lines (66 loc) · 2.65 KB
/
verify-release-assets.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
#!/bin/bash
set -e
set -u
set -o pipefail
# Script to verify the release assets by downloading them from the GitHub release
# and checking if they are accessible. If the asset is not accessible, the script
# will try to delete asset and re-upload it to the release.
# https://github.com/gruntwork-io/terragrunt/issues/3220
# Ensure CIRCLE_TAG and GITHUB_OAUTH_TOKEN are set
if [ -z "${CIRCLE_TAG:-}" ]; then
echo "CIRCLE_TAG environment variable is not set. Exiting..."
exit 1
fi
if [ -z "${GITHUB_OAUTH_TOKEN:-}" ]; then
echo "GITHUB_OAUTH_TOKEN environment variable is not set. Exiting..."
exit 1
fi
RELEASE_TAG=$CIRCLE_TAG
REPO_OWNER="gruntwork-io"
REPO_NAME="terragrunt"
MAX_RETRIES=10
RELEASE_RESPONSE=$(curl -s \
-H "Accept: application/vnd.github.v3 json" \
-H "Authorization: token $GITHUB_OAUTH_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/releases/tags/$RELEASE_TAG")
# Check if the release exists
if jq -e '.message == "Not Found"' <<< "$RELEASE_RESPONSE" > /dev/null; then
echo "Release $RELEASE_TAG not found. Exiting..."
exit 1
fi
# Get the release id
RELEASE_ID=$(echo "$RELEASE_RESPONSE" | jq -r '.id')
ASSET_URLS=$(echo "$RELEASE_RESPONSE" | jq -r '.assets[].browser_download_url')
# Loop through each asset URL and attempt to download
for ASSET_URL in $ASSET_URLS; do
ASSET_NAME=$(basename "$ASSET_URL")
for ((i=0; i<MAX_RETRIES; i )); do
if ! curl -sILf "$ASSET_URL" > /dev/null; then
echo "Failed to download the asset $ASSET_NAME. Retrying..."
# Delete the asset
ASSET_ID=$(jq -r --arg asset_name "$ASSET_NAME" '.assets[] | select(.name == $asset_name) | .id' <<< "$RELEASE_RESPONSE")
curl -s -L -XDELETE \
-H "Accept: application/vnd.github.v3 json" \
-H "Authorization: token $GITHUB_OAUTH_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/releases/assets/$ASSET_ID" > /dev/null
# Re-upload the asset
curl -s -L -XPOST \
-H "Accept: application/vnd.github.v3 json" \
-H "Authorization: token $GITHUB_OAUTH_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-H "Content-Type: application/octet-stream" \
--data-binary "@bin/$ASSET_NAME" \
"https://uploads.github.com/repos/$REPO_OWNER/$REPO_NAME/releases/$RELEASE_ID/assets?name=$ASSET_NAME" > /dev/null
else
echo "Successfully checked the asset $ASSET_NAME"
break
fi
done
if (( i == MAX_RETRIES )); then
echo "Failed to download the asset $ASSET_NAME after $MAX_RETRIES retries. Exiting..."
exit 1
fi
done
echo "All assets checks passed."