|
| 1 | +name: Reusable Build Workflow |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + rust-toolchain: |
| 7 | + description: 'Rust toolchain to use' |
| 8 | + required: false |
| 9 | + type: string |
| 10 | + default: 'stable' |
| 11 | + packages: |
| 12 | + description: 'Comma-separated list of package names' |
| 13 | + required: true |
| 14 | + type: string |
| 15 | + target-config-file: |
| 16 | + description: 'Path to the TOML file containing target configurations' |
| 17 | + required: false |
| 18 | + type: string |
| 19 | + default: '.github/target.toml' |
| 20 | + run-tests: |
| 21 | + description: 'Whether to run tests (can be overridden)' |
| 22 | + required: false |
| 23 | + type: boolean |
| 24 | + default: true |
| 25 | + |
| 26 | +on: |
| 27 | + workflow_call: |
| 28 | + inputs: |
| 29 | + rust-toolchain: |
| 30 | + description: 'Rust toolchain to use' |
| 31 | + required: false |
| 32 | + type: string |
| 33 | + default: 'stable' |
| 34 | + packages: |
| 35 | + description: 'Comma-separated list of package names' |
| 36 | + required: true |
| 37 | + type: string |
| 38 | + target-config-file: |
| 39 | + description: 'Path to the TOML file containing target configurations' |
| 40 | + required: false |
| 41 | + type: string |
| 42 | + default: '.github/target.toml' |
| 43 | + run-tests: |
| 44 | + description: 'Whether to run tests (can be overridden by matrix config)' |
| 45 | + required: false |
| 46 | + type: boolean |
| 47 | + default: true |
| 48 | + cache-key-prefix: |
| 49 | + description: 'Prefix for cache key to avoid collisions' |
| 50 | + required: false |
| 51 | + type: string |
| 52 | + default: 'rust-build' |
| 53 | + rustflags: |
| 54 | + description: 'Additional RUSTFLAGS to pass to the compiler' |
| 55 | + required: false |
| 56 | + type: string |
| 57 | + default: '' |
| 58 | + |
| 59 | +defaults: |
| 60 | + run: |
| 61 | + shell: bash |
| 62 | + |
| 63 | +env: |
| 64 | + RUSTC_BOOTSTRAP: "1" |
| 65 | + |
| 66 | +jobs: |
| 67 | + prepare: |
| 68 | + runs-on: ubuntu-latest |
| 69 | + outputs: |
| 70 | + target_config: ${{ steps.parse_config.outputs.targets }} |
| 71 | + steps: |
| 72 | + - name: Checkout code |
| 73 | + uses: actions/checkout@v6 |
| 74 | + |
| 75 | + - name: Install yj tool |
| 76 | + run: | |
| 77 | + # Install yj for TOML to JSON conversion |
| 78 | + curl -fsSL https://github.com/sclevine/yj/releases/latest/download/yj-linux-amd64 -o /usr/local/bin/yj |
| 79 | + chmod +x /usr/local/bin/yj |
| 80 | +
|
| 81 | + - name: Parse TOML configuration |
| 82 | + id: parse_config |
| 83 | + run: | |
| 84 | + if [ ! -f "${{ inputs.target-config-file }}" ]; then |
| 85 | + echo "Error: Target configuration file not found at ${{ inputs.target-config-file }}" |
| 86 | + exit 1 |
| 87 | + fi |
| 88 | + TARGETS_JSON=$(cat ${{ inputs.target-config-file }} | yj -tj | jq -c '.target') |
| 89 | + echo "targets=$TARGETS_JSON" >> $GITHUB_OUTPUT |
| 90 | +
|
| 91 | + - name: Setup Rust toolchain |
| 92 | + uses: dtolnay/rust-toolchain@master |
| 93 | + with: |
| 94 | + toolchain: nightly |
| 95 | + components: rustfmt |
| 96 | + |
| 97 | + - name: Check formatting |
| 98 | + run: | |
| 99 | + cargo +nightly fmt --all -- --check |
| 100 | +
|
| 101 | + - name: Spell Check Repo |
| 102 | + uses: crate-ci/typos@v1.43.4 |
| 103 | + env: |
| 104 | + CLICOLOR: 1 |
| 105 | + |
| 106 | + compile: |
| 107 | + needs: prepare |
| 108 | + name: ${{ matrix.release-name || matrix.target || 'Unknown' }} |
| 109 | + permissions: |
| 110 | + contents: write |
| 111 | + runs-on: ${{ matrix.os || 'ubuntu-latest' }} |
| 112 | + strategy: |
| 113 | + fail-fast: false |
| 114 | + matrix: |
| 115 | + include: ${{ fromJSON(needs.prepare.outputs.target_config) }} |
| 116 | + |
| 117 | + steps: |
| 118 | + #-------------------------------------------------- |
| 119 | + # Setup Steps |
| 120 | + #-------------------------------------------------- |
| 121 | + - name: Checkout code |
| 122 | + uses: actions/checkout@v6 |
| 123 | + with: |
| 124 | + submodules: true |
| 125 | + fetch-depth: 1 |
| 126 | + |
| 127 | + - name: Run sccache-cache |
| 128 | + uses: mozilla-actions/sccache-action@v0.0.9 |
| 129 | + |
| 130 | + - name: Setup SCCache |
| 131 | + run: | |
| 132 | + echo "SCCACHE_GHA_ENABLED=true" >> $GITHUB_ENV |
| 133 | + echo "RUSTC_WRAPPER=sccache" >> $GITHUB_ENV |
| 134 | +
|
| 135 | + - name: Setup Rust cache |
| 136 | + uses: Swatinem/rust-cache@v2 |
| 137 | + with: |
| 138 | + shared-key: ${{ inputs.cache-key-prefix }}-${{ matrix.release-name || matrix.target }}-${{ hashFiles('**/Cargo.lock') }} |
| 139 | + cache-directories: | |
| 140 | + ~/.cargo/registry/index/ |
| 141 | + ~/.cargo/registry/cache/ |
| 142 | + ~/.cargo/git/db/ |
| 143 | + target/ |
| 144 | +
|
| 145 | + - name: Setup Rust toolchain |
| 146 | + uses: dtolnay/rust-toolchain@master |
| 147 | + with: |
| 148 | + toolchain: ${{ matrix.toolchain || inputs.rust-toolchain }} |
| 149 | + targets: ${{ matrix.build-std && 'x86_64-unknown-linux-gnu' || matrix.target }} |
| 150 | + components: ${{ matrix.components || matrix.build-std && 'rustfmt,clippy,rust-src' || 'rustfmt, clippy' }} |
| 151 | + |
| 152 | + - name: Install cross-compilation tools |
| 153 | + if: matrix.tool == 'cross' |
| 154 | + run: | |
| 155 | + if [ "${{ matrix.build-std }}" != "true" ]; then |
| 156 | + rustup target add ${TARGET} |
| 157 | + fi |
| 158 | + cargo install cross --git https://github.com/cross-rs/cross --force |
| 159 | + env: |
| 160 | + TARGET: ${{ matrix.target }} |
| 161 | + |
| 162 | + - name: Install NASM |
| 163 | + if: matrix.nasm == true |
| 164 | + uses: ilammy/setup-nasm@v1 |
| 165 | + |
| 166 | + - name: Print build information |
| 167 | + run: | |
| 168 | + echo "Building for target: ${{ matrix.target }}" |
| 169 | + echo "Using toolchain: ${{ matrix.toolchain || inputs.rust-toolchain }}" |
| 170 | + echo "Building packages: ${{ inputs.packages }}" |
| 171 | +
|
| 172 | + #-------------------------------------------------- |
| 173 | + # Code Quality Steps |
| 174 | + #-------------------------------------------------- |
| 175 | + |
| 176 | + - name: Run linter |
| 177 | + uses: clechasseur/rs-cargo@v4 |
| 178 | + with: |
| 179 | + tool: ${{ matrix.tool }} |
| 180 | + command: clippy |
| 181 | + args: --all --target ${{ matrix.target }} ${{ matrix.build-std && '-Z build-std=std,panic_abort' || '' }} ${{ matrix.extra-args }} -- -D warnings |
| 182 | + env: |
| 183 | + RUSTFLAGS: ${{ matrix.rustflags }} ${{ inputs.rustflags }} |
| 184 | + |
| 185 | + #-------------------------------------------------- |
| 186 | + # Build & Test Steps |
| 187 | + #-------------------------------------------------- |
| 188 | + - name: Run tests |
| 189 | + uses: clechasseur/rs-cargo@v4 |
| 190 | + if: ${{ inputs.run-tests != false && !matrix.skip-test }} |
| 191 | + with: |
| 192 | + tool: ${{ matrix.tool }} |
| 193 | + command: test |
| 194 | + args: --all --target ${{ matrix.target }} ${{ matrix.build-std && '-Z build-std=std,panic_abort' || '' }} ${{ matrix.extra-args }} -- --nocapture |
| 195 | + env: |
| 196 | + CROSS_CONTAINER_OPTS: "--network host" |
| 197 | + RUSTFLAGS: ${{ matrix.rustflags }} ${{ inputs.rustflags }} |
| 198 | + |
| 199 | + - name: Build release binaries |
| 200 | + id: build |
| 201 | + uses: clechasseur/rs-cargo@v4 |
| 202 | + with: |
| 203 | + tool: ${{ matrix.tool }} |
| 204 | + command: build |
| 205 | + args: --workspace --release --target ${{ matrix.target }} ${{ matrix.build-std && '-Z build-std=std,panic_abort' || '' }} ${{ matrix.extra-args }} |
| 206 | + env: |
| 207 | + RUSTFLAGS: ${{ matrix.rustflags }} ${{ inputs.rustflags }} |
| 208 | + CROSS_BUILD_ZIG: ${{ matrix.zig }} |
| 209 | + |
| 210 | + #-------------------------------------------------- |
| 211 | + # Artifact Steps |
| 212 | + #-------------------------------------------------- |
| 213 | + - name: Prepare binary artifacts |
| 214 | + shell: bash |
| 215 | + id: prepare-artifacts |
| 216 | + run: | |
| 217 | + mkdir -p artifacts |
| 218 | + IFS=',' read -ra PACKAGES <<< "${{ inputs.packages }}" |
| 219 | + for package in "${PACKAGES[@]}"; do |
| 220 | + source_file="target/${{ matrix.target }}/release/${package}${{ matrix.postfix }}" |
| 221 | + target_file="artifacts/${package}-${{ matrix.release-name || matrix.target }}${{ matrix.postfix }}" |
| 222 | + cp "$source_file" "$target_file" |
| 223 | + done |
| 224 | +
|
| 225 | + - name: Upload binary artifacts |
| 226 | + uses: actions/upload-artifact@v6 |
| 227 | + with: |
| 228 | + name: ${{ matrix.release-name || matrix.target }}-binaries |
| 229 | + path: artifacts/ |
| 230 | + if-no-files-found: error |
| 231 | + retention-days: ${{ matrix.retention-days || 3 }} |
0 commit comments