|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Build and optionally push the enclave distribution tarball. |
| 3 | +# |
| 4 | +# Usage: |
| 5 | +# scripts/ci/build_tarball.sh build # Build and validate only |
| 6 | +# scripts/ci/build_tarball.sh build-push # Build, validate, and push to Quay |
| 7 | + |
| 8 | +set -euo pipefail |
| 9 | + |
| 10 | +ACTION="${1:-build}" |
| 11 | + |
| 12 | +TAG="${TARBALL_TAG:-$(git rev-parse --short HEAD 2>/dev/null || echo dev)}" |
| 13 | +TARBALL="enclave.tar.gz" |
| 14 | +MAX_SIZE=1073741824 # 1GB |
| 15 | + |
| 16 | +# --- Build --- |
| 17 | + |
| 18 | +echo "Building distribution tarball..." |
| 19 | +echo -n "$TAG" > .version |
| 20 | + |
| 21 | +tar --exclude='.git' --exclude='.gitignore' --exclude='.github' --exclude='scripts' \ |
| 22 | + --exclude='Makefile.ci' \ |
| 23 | + -czvf "/tmp/$TARBALL" . |
| 24 | +mv "/tmp/$TARBALL" . |
| 25 | + |
| 26 | +echo "" |
| 27 | +echo "Validating tarball..." |
| 28 | + |
| 29 | +# Check size |
| 30 | +SIZE=$(stat -c%s "$TARBALL") |
| 31 | +echo "Tarball size: $(numfmt --to=iec-i --suffix=B "$SIZE")" |
| 32 | +if [ "$SIZE" -gt "$MAX_SIZE" ]; then |
| 33 | + echo "Error: Tarball exceeds 1GB" |
| 34 | + exit 1 |
| 35 | +fi |
| 36 | + |
| 37 | +# Extract file list |
| 38 | +tar -tzf "$TARBALL" > /tmp/tarball-contents.txt |
| 39 | + |
| 40 | +# Check required files |
| 41 | +REQUIRED_FILES=(".version" "Makefile") |
| 42 | +for file in "${REQUIRED_FILES[@]}"; do |
| 43 | + if ! grep -q "^\./${file}$" /tmp/tarball-contents.txt; then |
| 44 | + echo "Error: Required file '${file}' not found in tarball" |
| 45 | + head -20 /tmp/tarball-contents.txt |
| 46 | + exit 1 |
| 47 | + fi |
| 48 | + echo " Found ${file}" |
| 49 | +done |
| 50 | + |
| 51 | +# Check required directories (only if they exist in source) |
| 52 | +REQUIRED_DIRS=("playbooks" "operators" "configs") |
| 53 | +for dir in "${REQUIRED_DIRS[@]}"; do |
| 54 | + if [ -d "$dir" ]; then |
| 55 | + if ! grep -q "^\./${dir}/" /tmp/tarball-contents.txt; then |
| 56 | + echo "Error: Required directory '${dir}/' not found in tarball" |
| 57 | + head -20 /tmp/tarball-contents.txt |
| 58 | + exit 1 |
| 59 | + fi |
| 60 | + echo " Found ${dir}/" |
| 61 | + fi |
| 62 | +done |
| 63 | + |
| 64 | +# Check excluded paths are absent |
| 65 | +EXCLUDED_PATHS=(".git/" ".github/" "Makefile.ci") |
| 66 | +for path in "${EXCLUDED_PATHS[@]}"; do |
| 67 | + if grep -q "^\./${path}" /tmp/tarball-contents.txt; then |
| 68 | + echo "Error: Excluded path '${path}' found in tarball" |
| 69 | + exit 1 |
| 70 | + fi |
| 71 | + echo " ${path} correctly excluded" |
| 72 | +done |
| 73 | + |
| 74 | +# Validate file counts for critical directories |
| 75 | +echo "Validating file counts..." |
| 76 | +for dir in "${REQUIRED_DIRS[@]}"; do |
| 77 | + if [ -d "$dir" ]; then |
| 78 | + SOURCE_COUNT=$(find "$dir" -type f | wc -l) |
| 79 | + TARBALL_COUNT=$(grep "^\./${dir}/" /tmp/tarball-contents.txt | grep -v '/$' | wc -l) |
| 80 | + echo " ${dir}/: source=${SOURCE_COUNT}, tarball=${TARBALL_COUNT}" |
| 81 | + if [ "$SOURCE_COUNT" -ne "$TARBALL_COUNT" ]; then |
| 82 | + echo "Error: File count mismatch in ${dir}/" |
| 83 | + echo " Expected: ${SOURCE_COUNT} files" |
| 84 | + echo " Found in tarball: ${TARBALL_COUNT} files" |
| 85 | + exit 1 |
| 86 | + fi |
| 87 | + fi |
| 88 | +done |
| 89 | + |
| 90 | +echo "Tarball validation passed" |
| 91 | +rm -f .version |
| 92 | + |
| 93 | +# --- Push (optional) --- |
| 94 | + |
| 95 | +if [ "$ACTION" = "build-push" ]; then |
| 96 | + if [ -z "${QUAY_USER:-}" ] || [ -z "${QUAY_TOKEN:-}" ]; then |
| 97 | + echo "Error: QUAY_USER and QUAY_TOKEN must be set" |
| 98 | + exit 1 |
| 99 | + fi |
| 100 | + |
| 101 | + echo "$QUAY_TOKEN" | podman login quay.io -u "$QUAY_USER" --password-stdin |
| 102 | + |
| 103 | + echo "Pushing tarball with tag: $TAG" |
| 104 | + oras push "quay.io/edge-infrastructure/enclave:${TAG}" \ |
| 105 | + "${TARBALL}:application/vnd.oci.image.layer.v1.tar+gzip" |
| 106 | + |
| 107 | + rm -f "$TARBALL" |
| 108 | + echo "Tarball pushed successfully" |
| 109 | +elif [ "$ACTION" = "build" ]; then |
| 110 | + echo "Tarball built: $TARBALL" |
| 111 | +else |
| 112 | + echo "Unknown action: $ACTION" |
| 113 | + echo "Usage: $0 build|build-push" |
| 114 | + exit 1 |
| 115 | +fi |
0 commit comments