ci: add tmate job triggered on compile failure when enable-tmate inpu… #2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Build Rust Binary | ||
|
Check failure on line 1 in .github/workflows/rust-build.yml
|
||
| on: | ||
| workflow_call: | ||
| inputs: | ||
| rust-toolchain: | ||
| description: 'Rust toolchain to use' | ||
| required: false | ||
| type: string | ||
| default: 'stable' | ||
| packages: | ||
| description: 'Comma-separated list of package names' | ||
| required: true | ||
| type: string | ||
| target-config-file: | ||
| description: 'Path to the TOML file containing target configurations' | ||
| required: false | ||
| type: string | ||
| default: '.github/target.toml' | ||
| run-tests: | ||
| description: 'Whether to run tests (can be overridden by matrix config)' | ||
| required: false | ||
| type: boolean | ||
| default: true | ||
| cache-key-prefix: | ||
| description: 'Prefix for cache key to avoid collisions' | ||
| required: false | ||
| type: string | ||
| default: 'rust-build' | ||
| rustflags: | ||
| description: 'Additional RUSTFLAGS to pass to the compiler' | ||
| required: false | ||
| type: string | ||
| default: '' | ||
| enable-tmate: | ||
| description: 'Enable tmate session on failure for debugging' | ||
| required: false | ||
| type: boolean | ||
| default: false | ||
| defaults: | ||
| run: | ||
| shell: bash | ||
| env: | ||
| RUSTC_BOOTSTRAP: "1" | ||
| jobs: | ||
| prepare: | ||
| runs-on: ubuntu-latest | ||
| outputs: | ||
| target_config: ${{ steps.parse_config.outputs.targets }} | ||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v6 | ||
| - name: Install yj tool | ||
| run: | | ||
| # Install yj for TOML to JSON conversion | ||
| curl -fsSL https://github.com/sclevine/yj/releases/latest/download/yj-linux-amd64 -o /usr/local/bin/yj | ||
| chmod +x /usr/local/bin/yj | ||
| - name: Parse TOML configuration | ||
| id: parse_config | ||
| run: | | ||
| if [ ! -f "${{ inputs.target-config-file }}" ]; then | ||
| echo "Error: Target configuration file not found at ${{ inputs.target-config-file }}" | ||
| exit 1 | ||
| fi | ||
| TARGETS_JSON=$(cat ${{ inputs.target-config-file }} | yj -tj | jq -c '.target') | ||
| echo "targets=$TARGETS_JSON" >> $GITHUB_OUTPUT | ||
| - name: Setup Rust toolchain | ||
| uses: dtolnay/rust-toolchain@master | ||
| with: | ||
| toolchain: nightly | ||
| components: rustfmt | ||
| - name: Check formatting | ||
| run: | | ||
| cargo +nightly fmt --all -- --check | ||
| - name: Spell Check Repo | ||
| uses: crate-ci/typos@v1.44.0 | ||
| env: | ||
| CLICOLOR: 1 | ||
| compile: | ||
| needs: prepare | ||
| name: ${{ matrix.release-name || matrix.target || 'Unknown' }} | ||
| permissions: | ||
| contents: write | ||
| runs-on: ${{ matrix.os || 'ubuntu-latest' }} | ||
| strategy: | ||
| fail-fast: false | ||
| matrix: | ||
| include: ${{ fromJSON(needs.prepare.outputs.target_config) }} | ||
| steps: | ||
| #-------------------------------------------------- | ||
| # Setup Steps | ||
| #-------------------------------------------------- | ||
| - name: Checkout code | ||
| uses: actions/checkout@v6 | ||
| with: | ||
| submodules: true | ||
| fetch-depth: 1 | ||
| - name: Run sccache-cache | ||
| uses: mozilla-actions/sccache-action@v0.0.9 | ||
| - name: Setup SCCache | ||
| run: | | ||
| echo "SCCACHE_GHA_ENABLED=true" >> $GITHUB_ENV | ||
| echo "RUSTC_WRAPPER=sccache" >> $GITHUB_ENV | ||
| - name: Setup Rust cache | ||
| uses: Swatinem/rust-cache@v2 | ||
| with: | ||
| shared-key: ${{ inputs.cache-key-prefix }}-${{ matrix.release-name || matrix.target }}-${{ hashFiles('**/Cargo.lock') }} | ||
| cache-directories: | | ||
| ~/.cargo/registry/index/ | ||
| ~/.cargo/registry/cache/ | ||
| ~/.cargo/git/db/ | ||
| target/ | ||
| - name: Setup Rust toolchain | ||
| uses: dtolnay/rust-toolchain@master | ||
| with: | ||
| toolchain: ${{ matrix.toolchain || inputs.rust-toolchain }} | ||
| targets: ${{ matrix.build-std && 'x86_64-unknown-linux-gnu' || matrix.target }} | ||
| components: ${{ matrix.components || matrix.build-std && 'rustfmt,clippy,rust-src' || 'rustfmt, clippy' }} | ||
| - name: Install cross-compilation tools | ||
| if: matrix.tool == 'cross' | ||
| run: | | ||
| if [ "${{ matrix.build-std }}" != "true" ]; then | ||
| rustup target add ${TARGET} | ||
| fi | ||
| cargo install cross --git https://github.com/cross-rs/cross --force | ||
| env: | ||
| TARGET: ${{ matrix.target }} | ||
| - name: Install NASM | ||
| if: matrix.nasm == true | ||
| uses: ilammy/setup-nasm@v1 | ||
| - name: Print build information | ||
| run: | | ||
| echo "Building for target: ${{ matrix.target }}" | ||
| echo "Using toolchain: ${{ matrix.toolchain || inputs.rust-toolchain }}" | ||
| echo "Building packages: ${{ inputs.packages }}" | ||
| #-------------------------------------------------- | ||
| # Code Quality Steps | ||
| #-------------------------------------------------- | ||
| - name: Run linter | ||
| uses: clechasseur/rs-cargo@v4 | ||
| with: | ||
| tool: ${{ matrix.tool }} | ||
| command: clippy | ||
| args: --all --target ${{ matrix.target }} ${{ matrix.build-std && '-Z build-std=std,panic_abort' || '' }} ${{ matrix.extra-args }} -- -D warnings | ||
| env: | ||
| RUSTFLAGS: ${{ matrix.rustflags }} ${{ inputs.rustflags }} | ||
| #-------------------------------------------------- | ||
| # Build & Test Steps | ||
| #-------------------------------------------------- | ||
| - name: Run tests | ||
| uses: clechasseur/rs-cargo@v4 | ||
| if: ${{ inputs.run-tests != false && !matrix.skip-test }} | ||
| with: | ||
| tool: ${{ matrix.tool }} | ||
| command: test | ||
| args: --all --target ${{ matrix.target }} ${{ matrix.build-std && '-Z build-std=std,panic_abort' || '' }} ${{ matrix.extra-args }} -- --nocapture | ||
| env: | ||
| CROSS_CONTAINER_OPTS: "--network host" | ||
| RUSTFLAGS: ${{ matrix.rustflags }} ${{ inputs.rustflags }} | ||
| - name: Build release binaries | ||
| id: build | ||
| uses: clechasseur/rs-cargo@v4 | ||
| with: | ||
| tool: ${{ matrix.tool }} | ||
| command: build | ||
| args: --workspace --release --target ${{ matrix.target }} ${{ matrix.build-std && '-Z build-std=std,panic_abort' || '' }} ${{ matrix.extra-args }} | ||
| env: | ||
| RUSTFLAGS: ${{ matrix.rustflags }} ${{ inputs.rustflags }} | ||
| CROSS_BUILD_ZIG: ${{ matrix.zig }} | ||
| #-------------------------------------------------- | ||
| # Artifact Steps | ||
| #-------------------------------------------------- | ||
| - name: Prepare binary artifacts | ||
| shell: bash | ||
| id: prepare-artifacts | ||
| run: | | ||
| mkdir -p artifacts | ||
| IFS=',' read -ra PACKAGES <<< "${{ inputs.packages }}" | ||
| for package in "${PACKAGES[@]}"; do | ||
| source_file="target/${{ matrix.target }}/release/${package}${{ matrix.postfix }}" | ||
| target_file="artifacts/${package}-${{ matrix.release-name || matrix.target }}${{ matrix.postfix }}" | ||
| cp "$source_file" "$target_file" | ||
| done | ||
| - name: Upload binary artifacts | ||
| if: ${{ matrix.skip-upload != true }} | ||
| uses: actions/upload-artifact@v7 | ||
| with: | ||
| name: ${{ matrix.release-name || matrix.target }}-binaries | ||
| path: artifacts/ | ||
| if-no-files-found: error | ||
| retention-days: ${{ matrix.retention-days || 3 }} | ||
| tmate: | ||
| needs: [compile] | ||
| if: ${{ inputs.enable-tmate && needs.compile.result == "failure" }} | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Setup tmate session | ||
| uses: mxschmitt/action-tmate@v3 | ||
| with: | ||
| detached: true | ||
| timeout-minutes: 15 | ||
| limit-access-to-actor: true | ||