Skip to content

Commit 23e78d9

Browse files
authored
Fix binary recording script (#2208)
1 parent c87ea57 commit 23e78d9

4 files changed

Lines changed: 74 additions & 54 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env python3
2+
# Parse `aws s3 ls --recursive` output (piped through awk '{print $3, $4}')
3+
# and produce a DynamoDB item JSON with binary sizes.
4+
#
5+
# Usage: aws s3 ls ... | awk '{print $3, $4}' | python3 parse-s3-binaries.py
6+
#
7+
# Required env: S3_PREFIX, COMMIT_HASH, COMMIT_DATE
8+
# Optional env: TAG
9+
10+
import json
11+
import os
12+
import sys
13+
14+
prefix = os.environ["S3_PREFIX"]
15+
commit_hash = os.environ["COMMIT_HASH"]
16+
commit_date = os.environ["COMMIT_DATE"]
17+
tag = os.environ.get("TAG", "")
18+
19+
SKIP_EXT = (".sig", ".jar", ".rpm", ".deb", ".pkg", ".msi", ".tar.gz", ".gz", ".zip")
20+
SKIP_NAMES = ("CWAGENT_VERSION", "buildMSI.zip")
21+
22+
binaries = {}
23+
for line in sys.stdin:
24+
line = line.strip()
25+
if not line:
26+
continue
27+
parts = line.split(" ", 1)
28+
if len(parts) != 2:
29+
continue
30+
try:
31+
size, key = int(parts[0]), parts[1]
32+
except ValueError:
33+
continue
34+
rel_path = key[len(prefix):] if key.startswith(prefix) else key
35+
segments = rel_path.split("/")
36+
if len(segments) != 2:
37+
continue
38+
filename = segments[1]
39+
if any(filename.endswith(ext) for ext in SKIP_EXT):
40+
continue
41+
if filename in SKIP_NAMES:
42+
continue
43+
binaries[rel_path] = {"N": str(size)}
44+
45+
if not binaries:
46+
sys.exit(1)
47+
48+
record_type = "release" if tag else "commit"
49+
item = {
50+
"CommitHash": {"S": commit_hash},
51+
"CommitDate": {"N": commit_date},
52+
"Branch": {"S": "main"},
53+
"RecordType": {"S": record_type},
54+
"Binaries": {"M": binaries},
55+
}
56+
if tag:
57+
item["Tag"] = {"S": tag}
58+
59+
print(json.dumps(item))

.github/scripts/record-binary-sizes.sh

Lines changed: 13 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8,65 +8,25 @@ set -euo pipefail
88

99
TABLE_NAME="CWABinarySizes"
1010
S3_BUCKET="${S3_BUCKET:?S3_BUCKET is required}"
11-
COMMIT_HASH="${COMMIT_HASH:?COMMIT_HASH is required}"
12-
COMMIT_DATE="${COMMIT_DATE:?COMMIT_DATE is required}"
13-
TAG="${TAG:-}"
11+
export COMMIT_HASH="${COMMIT_HASH:?COMMIT_HASH is required}"
12+
export COMMIT_DATE="${COMMIT_DATE:?COMMIT_DATE is required}"
13+
export TAG="${TAG:-}"
1414
REGION="us-west-2"
1515

16-
S3_PREFIX="integration-test/binary/${COMMIT_HASH}/"
16+
export S3_PREFIX="integration-test/binary/${COMMIT_HASH}/"
1717

18-
echo "Collecting binary sizes from S3 (${COMMIT_HASH:0:12})..."
19-
20-
ITEM_JSON=$(
21-
aws s3 ls "s3://${S3_BUCKET}/${S3_PREFIX}" --recursive --region "$REGION" |
22-
awk '{print $3, $4}' |
23-
python3 - "$S3_PREFIX" "$COMMIT_HASH" "$COMMIT_DATE" "$TAG" <<'PYEOF'
24-
import sys, json
25-
26-
prefix = sys.argv[1]
27-
commit_hash = sys.argv[2]
28-
commit_date = sys.argv[3]
29-
tag = sys.argv[4]
30-
31-
skip_ext = ('.sig', '.jar', '.rpm', '.deb', '.pkg', '.msi', '.tar.gz', '.gz', '.zip')
32-
skip_names = ('CWAGENT_VERSION', 'buildMSI.zip')
33-
34-
binaries = {}
35-
for line in sys.stdin:
36-
line = line.strip()
37-
if not line:
38-
continue
39-
parts = line.split(' ', 1)
40-
if len(parts) != 2:
41-
continue
42-
size, key = int(parts[0]), parts[1]
43-
rel_path = key[len(prefix):] if key.startswith(prefix) else key
44-
segments = rel_path.split('/')
45-
if len(segments) != 2:
46-
continue
47-
filename = segments[1]
48-
if any(filename.endswith(ext) for ext in skip_ext):
49-
continue
50-
if filename in skip_names:
51-
continue
52-
binaries[rel_path] = {"N": str(size)}
18+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
5319

54-
if not binaries:
55-
sys.exit(1)
20+
echo "Collecting binary sizes from S3 (${COMMIT_HASH:0:12})..."
5621

57-
record_type = "release" if tag else "commit"
58-
item = {
59-
"CommitHash": {"S": commit_hash},
60-
"CommitDate": {"N": commit_date},
61-
"Branch": {"S": "main"},
62-
"RecordType": {"S": record_type},
63-
"Binaries": {"M": binaries},
64-
}
65-
if tag:
66-
item["Tag"] = {"S": tag}
22+
S3_LISTING=$(aws s3 ls "s3://${S3_BUCKET}/${S3_PREFIX}" --recursive --region "$REGION" || true)
23+
if [[ -z "$S3_LISTING" ]]; then
24+
echo "No binaries found in S3, skipping."
25+
exit 0
26+
fi
6727

68-
print(json.dumps(item))
69-
PYEOF
28+
ITEM_JSON=$(
29+
echo "$S3_LISTING" | awk '{print $3, $4}' | python3 "$SCRIPT_DIR/parse-s3-binaries.py"
7030
) || {
7131
echo "No binaries found in S3, skipping."
7232
exit 0

.github/workflows/build-test-artifacts.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ jobs:
4949

5050
RecordBinarySizes:
5151
needs: [BuildAndUpload]
52+
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
5253
uses: ./.github/workflows/record-binary-sizes.yml
5354
secrets: inherit
5455
permissions:

.github/workflows/record-binary-sizes.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ on:
99
workflow_call:
1010

1111
jobs:
12-
record:
12+
Record:
1313
runs-on: ubuntu-latest
1414
permissions:
1515
id-token: write

0 commit comments

Comments
 (0)