upload-rust-binary-action icon indicating copy to clipboard operation
upload-rust-binary-action copied to clipboard

How to cache last `cargo build` result to save ci time

Open Mon-ius opened this issue 10 months ago • 3 comments

The action usually takes about more than 30mins for large project in every ci run.

Do we have cache options to build from last action?

The example actions is here,

name: build

env:
  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  CARGO_TERM_COLOR: always

on:
  workflow_call:
    inputs:
      path:
        default: 'Cargo.toml'
        required: true
        type: string
      target:
        default: 'x86_64-unknown-linux-gnu'
        required: true
        type: string
      platform:
        default: 'ubuntu-latest'
        required: true
        type: string

jobs:
  ready:
    uses: ./.github/workflows/parse-info.yml
    with:
      path: ${{ inputs.path }}

  cross-build:
    name: publish - ${{ inputs.target }}
    runs-on: ${{ inputs.platform }}
    needs:
      - ready
    steps:
      - uses: actions/checkout@v4
      - name: Set LTO to true (Windows)
        if: runner.os == 'Windows'
        run: |
          $fileContent = Get-Content ./Cargo.toml
          $fileContent = $fileContent -replace 'lto = false', 'lto = true'
          Set-Content ./Cargo.toml $fileContent
      - name: Set LTO to true (Linux/macOS)
        if: runner.os != 'Windows'
        run: perl -pi -e 's/lto = false/lto = true/g' ./Cargo.toml
      - uses: taiki-e/github-actions/install-rust@main
        with:
          toolchain: nightly
      - name: Install cross-compilation tools
        uses: taiki-e/[email protected]
        with:
          target: ${{ inputs.target }}
        if: contains(inputs.target, 'linux-musl')
      - run: echo "RUSTFLAGS=${RUSTFLAGS} -C target-feature=+crt-static" >> "${GITHUB_ENV}"
        if: endsWith(inputs.target, 'windows-msvc')
      - uses: taiki-e/[email protected]
        with:
          bin: ${{ needs.ready.outputs.name }}
          target: ${{ inputs.target }}
          ref: refs/tags/${{ needs.ready.outputs.version }}
          tar: all
          zip: windows
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          CARGO_PROFILE_RELEASE_CODEGEN_UNITS: 1
          CARGO_PROFILE_RELEASE_LTO: true

How to add cache strategy to accelerate taiki-e/upload-rust-binary-action 🤗

Mon-ius avatar Feb 24 '25 01:02 Mon-ius

Does Swatinem/rust-cache action not work? I think that is what is often used when using cache for Rust on GitHub Actions.

taiki-e avatar Feb 24 '25 02:02 taiki-e

seems not work for this case,

name: build

env:
  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  CARGO_TERM_COLOR: always

on:
  workflow_call:
    inputs:
      path:
        default: 'Cargo.toml'
        required: true
        type: string
      target:
        default: 'x86_64-unknown-linux-gnu'
        required: true
        type: string
      platform:
        default: 'ubuntu-latest'
        required: true
        type: string

jobs:
  ready:
    uses: ./.github/workflows/parse-info.yml
    with:
      path: ${{ inputs.path }}

  cross-build:
    name: publish - ${{ inputs.target }}
    runs-on: ${{ inputs.platform }}
    needs:
      - ready
    steps:
      - uses: actions/checkout@v4
      - uses: Swatinem/rust-cache@v2
      - name: Set LTO to true (Windows)
        if: runner.os == 'Windows'
        run: |
          $fileContent = Get-Content ./Cargo.toml
          $fileContent = $fileContent -replace 'lto = false', 'lto = true'
          Set-Content ./Cargo.toml $fileContent
      - name: Set LTO to true (Linux/macOS)
        if: runner.os != 'Windows'
        run: perl -pi -e 's/lto = false/lto = true/g' ./Cargo.toml
      - uses: taiki-e/github-actions/install-rust@main
        with:
          toolchain: nightly
      - name: Install cross-compilation tools
        uses: taiki-e/[email protected]
        with:
          target: ${{ inputs.target }}
        if: contains(inputs.target, 'linux-musl')
      - run: echo "RUSTFLAGS=${RUSTFLAGS} -C target-feature=+crt-static" >> "${GITHUB_ENV}"
        if: endsWith(inputs.target, 'windows-msvc')
      - uses: taiki-e/[email protected]
        with:
          bin: ${{ needs.ready.outputs.name }}
          target: ${{ inputs.target }}
          ref: refs/tags/${{ needs.ready.outputs.version }}
          tar: all
          zip: windows
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          CARGO_PROFILE_RELEASE_CODEGEN_UNITS: 1
          CARGO_PROFILE_RELEASE_LTO: true

Mon-ius avatar Feb 24 '25 09:02 Mon-ius

Unfortunately, sharing caches that are not based on the default branch with other branchs/tags does not seem to be supported: https://github.com/actions/cache/blob/main/tips-and-workarounds.md#use-cache-across-feature-branches

I guess this issue can be addressed by doing a build using dry-run mode on the default branch (like this reusable workflow does), caching the results, and having the cache be used for the release job.

taiki-e avatar Mar 14 '25 12:03 taiki-e