You can cache entire conda environments to avoid re-solving and re-installing dependencies.
Workflow Pattern:
- Initial Setup: Run
setup-miniconda without an environment-file to set up the base Miniconda/Miniforge installation. - Cache Check: Use
actions/cache with a key that includes a hash of your environment file (e.g., environment.yml) and a timestamp/date to ensure freshness. - Environment Update: If a cache hit is not found, use
mamba env update or conda env update to build the environment from your file.
Warning: Hashing an environment.yml file is not the same as hashing a resolved environment. Because packages in channels change over time, the same YAML file can result in different environments. To ensure absolute reproducibility, use a resolved environment file (e.g., from conda list --explicit).
- name: Setup Miniforge
uses: conda-incubator/setup-miniconda@v4
with:
miniforge-version: latest
activate-environment: anaconda-client-env
- name: Get Date
id: get-date
run: echo "today=$(/bin/date -u '+%Y%m%d')" >> $GITHUB_OUTPUT
shell: bash
- name: Cache Conda env
uses: actions/cache@v5
with:
path: ${{ env.CONDA }}/envs
key: conda-${{ runner.os }}--${{ runner.arch }}--${{ steps.get-date.outputs.today }}-${{ hashFiles('etc/example-environment-caching.yml') }}-${{ env.CACHE_NUMBER }}
env:
CACHE_NUMBER: 0
id: cache
- name: Update environment
run: |
mamba env update -n anaconda-client-env -f etc/example-environment-caching.yml
if: steps.cache.outputs.cache-hit != 'true'