How to cache last `cargo build` result to save ci time
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 🤗
Does Swatinem/rust-cache action not work? I think that is what is often used when using cache for Rust on GitHub Actions.
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
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.