|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +name: Build Rust Binary |
| 5 | +description: Build, verify, package, and upload an auditable Rust binary |
| 6 | + |
| 7 | +inputs: |
| 8 | + package: |
| 9 | + description: Cargo package that owns the binary |
| 10 | + required: true |
| 11 | + binary: |
| 12 | + description: Cargo binary name |
| 13 | + required: true |
| 14 | + triple: |
| 15 | + description: Rust target triple |
| 16 | + required: true |
| 17 | + dev-shell: |
| 18 | + description: Nix development shell used to build the binary |
| 19 | + required: true |
| 20 | + cargo-version: |
| 21 | + description: Cargo package version embedded in the binary |
| 22 | + required: true |
| 23 | + image-tag: |
| 24 | + description: Default supervisor image tag embedded in the binary |
| 25 | + required: false |
| 26 | + default: "" |
| 27 | + artifact-name: |
| 28 | + description: GitHub artifact name |
| 29 | + required: false |
| 30 | + default: "" |
| 31 | + extra-cargo-flags: |
| 32 | + description: Additional flags passed to cargo build |
| 33 | + required: false |
| 34 | + default: "" |
| 35 | + interpreter: |
| 36 | + description: ELF interpreter for a dynamically linked Linux binary |
| 37 | + required: false |
| 38 | + default: "" |
| 39 | + |
| 40 | +runs: |
| 41 | + using: composite |
| 42 | + steps: |
| 43 | + - name: Hash development shell |
| 44 | + id: dev-shell |
| 45 | + shell: bash |
| 46 | + env: |
| 47 | + DEV_SHELL: ${{ inputs.dev-shell }} |
| 48 | + run: echo "hash=$(nix hash file --type sha256 --base16 "$(nix eval --raw "${DEV_SHELL}.drvPath")")" >> "$GITHUB_OUTPUT" |
| 49 | + |
| 50 | + - name: Cache Rust artifacts |
| 51 | + uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 |
| 52 | + with: |
| 53 | + shared-key: binaries-${{ inputs.binary }}-${{ inputs.triple }}-${{ steps.dev-shell.outputs.hash }} |
| 54 | + cache-on-failure: "true" |
| 55 | + cache-workspace-crates: "true" |
| 56 | + cache-bin: "false" |
| 57 | + cmd-format: nix develop ${{ inputs.dev-shell }} -c {0} |
| 58 | + |
| 59 | + - name: Set version |
| 60 | + shell: nix develop ${{ inputs.dev-shell }} -c bash -euo pipefail {0} |
| 61 | + env: |
| 62 | + INPUTS_CARGO_VERSION: ${{ inputs.cargo-version }} |
| 63 | + run: sed -i "s/^version = \"0\\.0\\.0\"$/version = \"${INPUTS_CARGO_VERSION}\"/" Cargo.toml |
| 64 | + |
| 65 | + - name: Build ${{ inputs.binary }} |
| 66 | + shell: nix develop ${{ inputs.dev-shell }} -c bash -euo pipefail {0} |
| 67 | + env: |
| 68 | + OPENSHELL_IMAGE_TAG: ${{ inputs.image-tag }} |
| 69 | + INPUTS_PACKAGE: ${{ inputs.package }} |
| 70 | + INPUTS_BINARY: ${{ inputs.binary }} |
| 71 | + INPUTS_EXTRA_CARGO_FLAGS: ${{ inputs.extra-cargo-flags }} |
| 72 | + run: GIT_DIR=/nonexistent cargo auditable build --release --package "${INPUTS_PACKAGE}" --bin "${INPUTS_BINARY}" ${INPUTS_EXTRA_CARGO_FLAGS} |
| 73 | + |
| 74 | + - name: Verify ${{ inputs.binary }} |
| 75 | + shell: nix develop ${{ inputs.dev-shell }} -c bash -euo pipefail {0} |
| 76 | + env: |
| 77 | + INPUTS_BINARY: ${{ inputs.binary }} |
| 78 | + run: | |
| 79 | + # Confirm the binary runs and reports the expected name. |
| 80 | + target/release/${INPUTS_BINARY} --version | grep -q "^${INPUTS_BINARY} " |
| 81 | + # Confirm Syft can decode the embedded cargo-auditable metadata. |
| 82 | + SYFT_CHECK_FOR_APP_UPDATE=false syft file:target/release/${INPUTS_BINARY} -o cyclonedx-json | grep 'pkg:cargo/' > /dev/null |
| 83 | +
|
| 84 | + - name: Verify static linkage |
| 85 | + if: endsWith(inputs.triple, '-linux-musl') |
| 86 | + shell: nix develop ${{ inputs.dev-shell }} -c bash -euo pipefail {0} |
| 87 | + env: |
| 88 | + INPUTS_BINARY: ${{ inputs.binary }} |
| 89 | + run: tasks/scripts/verify-static-binary.sh target/release/${INPUTS_BINARY} |
| 90 | + |
| 91 | + - name: Normalize Linux dynamic binary |
| 92 | + if: endsWith(inputs.triple, '-linux-gnu') |
| 93 | + shell: nix develop ${{ inputs.dev-shell }} -c bash -euo pipefail {0} |
| 94 | + env: |
| 95 | + INPUTS_BINARY: ${{ inputs.binary }} |
| 96 | + INPUTS_INTERPRETER: ${{ inputs.interpreter }} |
| 97 | + run: | |
| 98 | + binary=target/release/${INPUTS_BINARY} |
| 99 | + # Remove Nix store paths so the binary can run on other distributions. |
| 100 | + patchelf --set-interpreter "${INPUTS_INTERPRETER}" --remove-rpath "$binary" |
| 101 | + # Z3 must be embedded instead of loaded from the target system. |
| 102 | + test -z "$(patchelf --print-needed "$binary" | grep '^libz3')" |
| 103 | + # Reject symbols introduced after glibc 2.28. |
| 104 | + tasks/scripts/verify-glibc-symbols.sh 2.28 "$binary" |
| 105 | +
|
| 106 | + - name: Upload ${{ inputs.binary }} |
| 107 | + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 |
| 108 | + with: |
| 109 | + name: ${{ inputs.artifact-name || format('{0}-{1}', inputs.binary, inputs.triple) }} |
| 110 | + path: target/release/${{ inputs.binary }} |
| 111 | + compression-level: 0 |
| 112 | + retention-days: 5 |
| 113 | + if-no-files-found: error |
0 commit comments