|
| 1 | +name: Build Rust native binaries |
| 2 | + |
| 3 | +# Reusable workflow that compiles the varlock-local-encrypt Rust binary |
| 4 | +# for Linux and Windows targets. |
| 5 | +# |
| 6 | +# Builds are cached by Cargo.lock + source hash. Each platform builds |
| 7 | +# natively on its own runner for maximum compatibility. |
| 8 | +# |
| 9 | +# Output: native binaries uploaded as artifacts, ready to be bundled |
| 10 | +# into the varlock npm package and CLI release archives. |
| 11 | + |
| 12 | +permissions: |
| 13 | + contents: read |
| 14 | + |
| 15 | +on: |
| 16 | + workflow_call: |
| 17 | + inputs: |
| 18 | + artifact-name: |
| 19 | + description: 'Base name for uploaded artifacts (suffixed with platform)' |
| 20 | + type: string |
| 21 | + default: 'native-bin-rust' |
| 22 | + |
| 23 | +jobs: |
| 24 | + build: |
| 25 | + strategy: |
| 26 | + matrix: |
| 27 | + include: |
| 28 | + - os: ubuntu-latest |
| 29 | + target: x86_64-unknown-linux-gnu |
| 30 | + native-bin-subdir: linux-x64 |
| 31 | + binary-name: varlock-local-encrypt |
| 32 | + - os: ubuntu-24.04-arm |
| 33 | + target: aarch64-unknown-linux-gnu |
| 34 | + native-bin-subdir: linux-arm64 |
| 35 | + binary-name: varlock-local-encrypt |
| 36 | + - os: windows-latest |
| 37 | + target: x86_64-pc-windows-msvc |
| 38 | + native-bin-subdir: win32-x64 |
| 39 | + binary-name: varlock-local-encrypt.exe |
| 40 | + |
| 41 | + runs-on: ${{ matrix.os }} |
| 42 | + name: Build ${{ matrix.native-bin-subdir }} |
| 43 | + |
| 44 | + steps: |
| 45 | + - uses: actions/checkout@v6 |
| 46 | + |
| 47 | + - name: Install Rust toolchain |
| 48 | + uses: dtolnay/rust-toolchain@stable |
| 49 | + with: |
| 50 | + targets: ${{ matrix.target }} |
| 51 | + |
| 52 | + # Cache Cargo registry + build artifacts by lockfile hash |
| 53 | + - name: Cache Cargo |
| 54 | + uses: actions/cache@v5 |
| 55 | + with: |
| 56 | + path: | |
| 57 | + ~/.cargo/registry |
| 58 | + ~/.cargo/git |
| 59 | + packages/encryption-binary-rust/target |
| 60 | + key: rust-${{ matrix.target }}-${{ hashFiles('packages/encryption-binary-rust/Cargo.lock') }}-${{ hashFiles('packages/encryption-binary-rust/src/**') }} |
| 61 | + restore-keys: | |
| 62 | + rust-${{ matrix.target }}-${{ hashFiles('packages/encryption-binary-rust/Cargo.lock') }}- |
| 63 | + rust-${{ matrix.target }}- |
| 64 | +
|
| 65 | + - name: Install UPX |
| 66 | + shell: bash |
| 67 | + run: | |
| 68 | + if [[ "${{ matrix.os }}" == *"ubuntu"* ]]; then |
| 69 | + sudo apt-get update && sudo apt-get install -y upx-ucl |
| 70 | + elif [[ "${{ matrix.os }}" == *"windows"* ]]; then |
| 71 | + choco install upx --yes |
| 72 | + fi |
| 73 | +
|
| 74 | + - name: Build release binary |
| 75 | + working-directory: packages/encryption-binary-rust |
| 76 | + run: cargo build --release --target ${{ matrix.target }} |
| 77 | + |
| 78 | + - name: Prepare artifact |
| 79 | + shell: bash |
| 80 | + run: | |
| 81 | + ARTIFACT_DIR="native-bins/${{ matrix.native-bin-subdir }}" |
| 82 | + mkdir -p "$ARTIFACT_DIR" |
| 83 | + cp "packages/encryption-binary-rust/target/${{ matrix.target }}/release/${{ matrix.binary-name }}" "$ARTIFACT_DIR/" |
| 84 | + echo "=== Binary info (before UPX) ===" |
| 85 | + ls -la "$ARTIFACT_DIR/${{ matrix.binary-name }}" |
| 86 | + file "$ARTIFACT_DIR/${{ matrix.binary-name }}" || true |
| 87 | + echo "=== UPX compress ===" |
| 88 | + upx --best "$ARTIFACT_DIR/${{ matrix.binary-name }}" |
| 89 | + echo "=== Binary info (after UPX) ===" |
| 90 | + ls -la "$ARTIFACT_DIR/${{ matrix.binary-name }}" |
| 91 | +
|
| 92 | + # Run Rust unit tests |
| 93 | + - name: Run unit tests |
| 94 | + working-directory: packages/encryption-binary-rust |
| 95 | + run: cargo test --release --target ${{ matrix.target }} |
| 96 | + |
| 97 | + # Test the built binary end-to-end (one-shot commands, no biometric) |
| 98 | + # Uses python (not python3) for Windows compat; printf for reliable base64 input |
| 99 | + - name: Test binary - status |
| 100 | + shell: bash |
| 101 | + run: | |
| 102 | + BIN="native-bins/${{ matrix.native-bin-subdir }}/${{ matrix.binary-name }}" |
| 103 | + PY=$(command -v python3 || command -v python) |
| 104 | + echo "=== status ===" |
| 105 | + $BIN status |
| 106 | + $BIN status | $PY -c "import sys,json; d=json.load(sys.stdin); assert d['ok'], 'status not ok'" |
| 107 | +
|
| 108 | + - name: Test binary - key lifecycle + encrypt/decrypt roundtrip |
| 109 | + shell: bash |
| 110 | + run: | |
| 111 | + BIN="native-bins/${{ matrix.native-bin-subdir }}/${{ matrix.binary-name }}" |
| 112 | + PY=$(command -v python3 || command -v python) |
| 113 | +
|
| 114 | + echo "=== generate-key ===" |
| 115 | + $BIN generate-key --key-id ci-test |
| 116 | + $BIN key-exists --key-id ci-test | $PY -c "import sys,json; d=json.load(sys.stdin); assert d['exists']" |
| 117 | +
|
| 118 | + echo "=== encrypt ===" |
| 119 | + PLAINTEXT=$($PY -c "import base64; print(base64.b64encode(b'hello from CI').decode())") |
| 120 | + CIPHERTEXT=$($BIN encrypt --key-id ci-test --data "$PLAINTEXT" | $PY -c "import sys,json; print(json.load(sys.stdin)['ciphertext'])") |
| 121 | + echo "Ciphertext: ${CIPHERTEXT:0:40}..." |
| 122 | +
|
| 123 | + echo "=== decrypt ===" |
| 124 | + DECRYPTED=$($BIN decrypt --key-id ci-test --data "$CIPHERTEXT" | $PY -c "import sys,json; print(json.load(sys.stdin)['plaintext'])") |
| 125 | + echo "Decrypted: $DECRYPTED" |
| 126 | +
|
| 127 | + if [ "$DECRYPTED" != "hello from CI" ]; then |
| 128 | + echo "::error::Roundtrip failed! Expected 'hello from CI', got '$DECRYPTED'" |
| 129 | + exit 1 |
| 130 | + fi |
| 131 | +
|
| 132 | + echo "=== delete-key ===" |
| 133 | + $BIN delete-key --key-id ci-test |
| 134 | +
|
| 135 | + echo "All binary tests passed" |
| 136 | +
|
| 137 | + - name: Upload artifact |
| 138 | + uses: actions/upload-artifact@v7 |
| 139 | + with: |
| 140 | + name: ${{ inputs.artifact-name }}-${{ matrix.native-bin-subdir }} |
| 141 | + path: native-bins/${{ matrix.native-bin-subdir }}/ |
| 142 | + retention-days: 7 |
| 143 | + |
| 144 | + # Cache the built binary so preview releases on other runners can restore it |
| 145 | + - name: Cache built binary |
| 146 | + uses: actions/cache/save@v5 |
| 147 | + with: |
| 148 | + path: native-bins/${{ matrix.native-bin-subdir }}/ |
| 149 | + key: native-bin-rust-${{ matrix.native-bin-subdir }}-${{ hashFiles('packages/encryption-binary-rust/Cargo.lock', 'packages/encryption-binary-rust/src/**') }} |
| 150 | + |
| 151 | + # Cross-platform interop: encrypt with Rust on Linux, decrypt with JS, and vice versa |
| 152 | + test-interop: |
| 153 | + needs: build |
| 154 | + runs-on: ubuntu-latest |
| 155 | + steps: |
| 156 | + - uses: actions/checkout@v6 |
| 157 | + |
| 158 | + - name: Setup Bun |
| 159 | + uses: oven-sh/setup-bun@v2 |
| 160 | + |
| 161 | + - name: Install deps |
| 162 | + run: bun install |
| 163 | + |
| 164 | + - name: Download Linux x64 binary |
| 165 | + uses: actions/download-artifact@v7 |
| 166 | + with: |
| 167 | + name: ${{ inputs.artifact-name }}-linux-x64 |
| 168 | + path: native-bins/linux-x64 |
| 169 | + |
| 170 | + - name: Fix permissions |
| 171 | + run: chmod +x native-bins/linux-x64/varlock-local-encrypt |
| 172 | + |
| 173 | + - name: Test Rust→JS interop |
| 174 | + run: | |
| 175 | + BIN=native-bins/linux-x64/varlock-local-encrypt |
| 176 | +
|
| 177 | + # Generate a key with Rust |
| 178 | + $BIN generate-key --key-id interop-test |
| 179 | + KEY_JSON=$(cat ~/.config/varlock/local-encrypt/keys/interop-test.json) |
| 180 | + PUBLIC_KEY=$(echo "$KEY_JSON" | python3 -c "import sys,json; print(json.load(sys.stdin)['publicKey'])") |
| 181 | + PRIVATE_KEY=$(echo "$KEY_JSON" | python3 -c "import sys,json; print(json.load(sys.stdin)['protectedPrivateKey'])") |
| 182 | +
|
| 183 | + # Encrypt with Rust |
| 184 | + PLAINTEXT_B64=$(echo -n "rust to javascript" | base64) |
| 185 | + CIPHERTEXT=$($BIN encrypt --key-id interop-test --data "$PLAINTEXT_B64" | python3 -c "import sys,json; print(json.load(sys.stdin)['ciphertext'])") |
| 186 | +
|
| 187 | + # Decrypt with JS |
| 188 | + RESULT=$(bun -e " |
| 189 | + const { decrypt } = await import('./packages/varlock/src/lib/local-encrypt/crypto.ts'); |
| 190 | + const result = await decrypt('$PRIVATE_KEY', '$PUBLIC_KEY', '$CIPHERTEXT'); |
| 191 | + process.stdout.write(result); |
| 192 | + ") |
| 193 | +
|
| 194 | + if [ "$RESULT" != "rust to javascript" ]; then |
| 195 | + echo "::error::Rust→JS interop failed! Got: $RESULT" |
| 196 | + exit 1 |
| 197 | + fi |
| 198 | + echo "✓ Rust→JS: '$RESULT'" |
| 199 | +
|
| 200 | + - name: Test JS→Rust interop |
| 201 | + run: | |
| 202 | + BIN=native-bins/linux-x64/varlock-local-encrypt |
| 203 | + KEY_JSON=$(cat ~/.config/varlock/local-encrypt/keys/interop-test.json) |
| 204 | + PUBLIC_KEY=$(echo "$KEY_JSON" | python3 -c "import sys,json; print(json.load(sys.stdin)['publicKey'])") |
| 205 | +
|
| 206 | + # Encrypt with JS |
| 207 | + CIPHERTEXT=$(bun -e " |
| 208 | + const { encrypt } = await import('./packages/varlock/src/lib/local-encrypt/crypto.ts'); |
| 209 | + const result = await encrypt('$PUBLIC_KEY', 'javascript to rust'); |
| 210 | + process.stdout.write(result); |
| 211 | + ") |
| 212 | +
|
| 213 | + # Decrypt with Rust |
| 214 | + RESULT=$($BIN decrypt --key-id interop-test --data "$CIPHERTEXT" | python3 -c "import sys,json; print(json.load(sys.stdin)['plaintext'])") |
| 215 | +
|
| 216 | + if [ "$RESULT" != "javascript to rust" ]; then |
| 217 | + echo "::error::JS→Rust interop failed! Got: $RESULT" |
| 218 | + exit 1 |
| 219 | + fi |
| 220 | + echo "✓ JS→Rust: '$RESULT'" |
| 221 | +
|
| 222 | + # Cleanup |
| 223 | + $BIN delete-key --key-id interop-test |
| 224 | + echo "✓ All cross-platform interop tests passed" |
0 commit comments