|
| 1 | +name: 'Build Contracts' |
| 2 | +description: > |
| 3 | + Modular Soroban WASM build with per-crate targeting, feature-flag matrix, |
| 4 | + binary size reporting, and optional wasm-opt post-processing. |
| 5 | +
|
| 6 | +inputs: |
| 7 | + contracts: |
| 8 | + description: > |
| 9 | + Space-separated list of contract names to build. |
| 10 | + Use "all" to build the full workspace. |
| 11 | + Default: "storage proxy subscription" |
| 12 | + required: false |
| 13 | + default: 'storage proxy subscription' |
| 14 | + features: |
| 15 | + description: > |
| 16 | + Cargo feature flags to pass to subscription crate. |
| 17 | + Examples: "" (core only), "oracle", "extended", "full" |
| 18 | + required: false |
| 19 | + default: '' |
| 20 | + run-wasm-opt: |
| 21 | + description: 'Run wasm-opt post-processor for additional size reduction' |
| 22 | + required: false |
| 23 | + default: 'false' |
| 24 | + upload-artifacts: |
| 25 | + description: 'Upload WASM artifacts' |
| 26 | + required: false |
| 27 | + default: 'true' |
| 28 | + |
| 29 | +outputs: |
| 30 | + wasm-dir: |
| 31 | + description: 'Path to directory containing built WASM files' |
| 32 | + value: ${{ steps.set-output.outputs.wasm-dir }} |
| 33 | + size-report: |
| 34 | + description: 'Multiline string with binary sizes (name: KB)' |
| 35 | + value: ${{ steps.size-report.outputs.report }} |
| 36 | + |
| 37 | +runs: |
| 38 | + using: 'composite' |
| 39 | + steps: |
| 40 | + # ── Rust toolchain ──────────────────────────────────────────────────────── |
| 41 | + - name: Install Rust stable toolchain |
| 42 | + uses: actions-rust-lang/setup-rust-toolchain@v1 |
| 43 | + with: |
| 44 | + toolchain: stable |
| 45 | + components: clippy, rustfmt |
| 46 | + |
| 47 | + - name: Add WASM target |
| 48 | + run: rustup target add wasm32-unknown-unknown |
| 49 | + shell: bash |
| 50 | + |
| 51 | + # ── Cargo cache ─────────────────────────────────────────────────────────── |
| 52 | + - name: Restore Cargo cache |
| 53 | + uses: actions/cache@v4 |
| 54 | + with: |
| 55 | + path: | |
| 56 | + ~/.cargo/registry |
| 57 | + ~/.cargo/git |
| 58 | + contracts/target |
| 59 | + key: cargo-wasm-${{ runner.os }}-${{ hashFiles('contracts/Cargo.lock') }} |
| 60 | + restore-keys: | |
| 61 | + cargo-wasm-${{ runner.os }}- |
| 62 | +
|
| 63 | + # ── Modular build ───────────────────────────────────────────────────────── |
| 64 | + - name: Build contracts (modular) |
| 65 | + shell: bash |
| 66 | + working-directory: contracts |
| 67 | + run: | |
| 68 | + set -euo pipefail |
| 69 | +
|
| 70 | + CONTRACTS="${{ inputs.contracts }}" |
| 71 | + FEATURES="${{ inputs.features }}" |
| 72 | + TARGET="wasm32-unknown-unknown" |
| 73 | +
|
| 74 | + if [[ "$CONTRACTS" == "all" ]]; then |
| 75 | + echo "Building full workspace..." |
| 76 | + if [[ -n "$FEATURES" ]]; then |
| 77 | + cargo build --target "$TARGET" --release \ |
| 78 | + --features "subtrackr-subscription/$FEATURES" || true |
| 79 | + else |
| 80 | + cargo build --target "$TARGET" --release || true |
| 81 | + fi |
| 82 | + else |
| 83 | + for contract in $CONTRACTS; do |
| 84 | + echo "→ Building $contract..." |
| 85 | + PKG="subtrackr-$contract" |
| 86 | + # Fallback to plain name for crates like 'utils' |
| 87 | + FEATURE_FLAGS="" |
| 88 | + if [[ "$contract" == "subscription" && -n "$FEATURES" ]]; then |
| 89 | + FEATURE_FLAGS="--features $PKG/$FEATURES" |
| 90 | + fi |
| 91 | + cargo build --target "$TARGET" --release -p "$PKG" $FEATURE_FLAGS \ |
| 92 | + || cargo build --target "$TARGET" --release -p "$contract" $FEATURE_FLAGS \ |
| 93 | + || echo "::warning::Failed to build $contract (skipping)" |
| 94 | + done |
| 95 | + fi |
| 96 | +
|
| 97 | + # ── wasm-opt post-processing ────────────────────────────────────────────── |
| 98 | + - name: Install wasm-opt |
| 99 | + if: inputs.run-wasm-opt == 'true' |
| 100 | + shell: bash |
| 101 | + run: | |
| 102 | + if ! command -v wasm-opt &>/dev/null; then |
| 103 | + sudo apt-get install -y binaryen 2>/dev/null \ |
| 104 | + || echo "::warning::wasm-opt not available; skipping optimisation" |
| 105 | + fi |
| 106 | +
|
| 107 | + - name: Run wasm-opt |
| 108 | + if: inputs.run-wasm-opt == 'true' |
| 109 | + shell: bash |
| 110 | + working-directory: contracts |
| 111 | + run: | |
| 112 | + WASM_DIR="target/wasm32-unknown-unknown/release" |
| 113 | + for f in "$WASM_DIR"/*.wasm; do |
| 114 | + [[ -f "$f" ]] || continue |
| 115 | + echo "Optimising $(basename "$f")..." |
| 116 | + wasm-opt -Oz --strip-debug --vacuum "$f" -o "$f.opt" 2>/dev/null \ |
| 117 | + && mv "$f.opt" "$f" \ |
| 118 | + || echo "::warning::wasm-opt failed for $f" |
| 119 | + done |
| 120 | +
|
| 121 | + # ── Binary size report ──────────────────────────────────────────────────── |
| 122 | + - name: Generate binary size report |
| 123 | + id: size-report |
| 124 | + shell: bash |
| 125 | + working-directory: contracts |
| 126 | + run: | |
| 127 | + WASM_DIR="target/wasm32-unknown-unknown/release" |
| 128 | + REPORT="" |
| 129 | + SUMMARY="| Contract | Size |\n|----------|------|\n" |
| 130 | + BUDGET_KB=500 # Fail if any single WASM exceeds this |
| 131 | +
|
| 132 | + EXCEEDED=0 |
| 133 | + if ls "$WASM_DIR"/*.wasm 2>/dev/null | head -1 | grep -q wasm; then |
| 134 | + for f in "$WASM_DIR"/*.wasm; do |
| 135 | + name=$(basename "$f") |
| 136 | + bytes=$(wc -c < "$f") |
| 137 | + kb=$(echo "scale=1; $bytes/1024" | bc 2>/dev/null || echo "?") |
| 138 | + REPORT="$REPORT\n$name: ${kb} KB" |
| 139 | + SUMMARY="$SUMMARY| \`$name\` | ${kb} KB |\n" |
| 140 | + # Budget check |
| 141 | + kb_int=$(echo "$kb" | cut -d. -f1) |
| 142 | + if [[ "$kb_int" -gt "$BUDGET_KB" ]] 2>/dev/null; then |
| 143 | + echo "::warning::$name is ${kb} KB which exceeds budget of ${BUDGET_KB} KB" |
| 144 | + EXCEEDED=1 |
| 145 | + fi |
| 146 | + done |
| 147 | + else |
| 148 | + REPORT="No WASM files found" |
| 149 | + SUMMARY="$SUMMARY| (none built) | — |" |
| 150 | + fi |
| 151 | +
|
| 152 | + echo "report<<EOF" >> "$GITHUB_OUTPUT" |
| 153 | + echo -e "$REPORT" >> "$GITHUB_OUTPUT" |
| 154 | + echo "EOF" >> "$GITHUB_OUTPUT" |
| 155 | +
|
| 156 | + # Write to GitHub Step Summary |
| 157 | + if [[ -n "${GITHUB_STEP_SUMMARY:-}" ]]; then |
| 158 | + echo "## 📦 WASM Binary Sizes" >> "$GITHUB_STEP_SUMMARY" |
| 159 | + echo -e "$SUMMARY" >> "$GITHUB_STEP_SUMMARY" |
| 160 | + echo "" >> "$GITHUB_STEP_SUMMARY" |
| 161 | + echo "_Budget: ${BUDGET_KB} KB per contract_" >> "$GITHUB_STEP_SUMMARY" |
| 162 | + if [[ "$EXCEEDED" -eq 1 ]]; then |
| 163 | + echo "⚠️ One or more contracts exceeded the size budget." >> "$GITHUB_STEP_SUMMARY" |
| 164 | + fi |
| 165 | + fi |
| 166 | +
|
| 167 | + - name: Set WASM output directory |
| 168 | + id: set-output |
| 169 | + shell: bash |
| 170 | + run: | |
| 171 | + echo "wasm-dir=contracts/target/wasm32-unknown-unknown/release" >> "$GITHUB_OUTPUT" |
| 172 | +
|
| 173 | + # ── Upload artefacts ────────────────────────────────────────────────────── |
| 174 | + - name: Upload WASM artifacts |
| 175 | + if: inputs.upload-artifacts == 'true' |
| 176 | + uses: actions/upload-artifact@v4 |
| 177 | + with: |
| 178 | + name: contracts-wasm-${{ github.sha }} |
| 179 | + path: contracts/target/wasm32-unknown-unknown/release/*.wasm |
| 180 | + if-no-files-found: warn |
| 181 | + retention-days: 30 |
0 commit comments