setup-miniconda

repository·main·Indexed 19 days ago

https://github.com/conda-incubator/setup-miniconda

A GitHub Action for setting up and managing Conda/Miniconda environments in CI workflows. It supports Miniconda, Miniforge, and custom constructor-based installers, providing features such as automatic environment activation, shell integration across platforms, and support for specific conda or mamba versions. It allows configuration via environment files, .condarc files, and custom channels, and provides guidance on caching conda packages and environments using actions/cache.

Tokens
9K
Snippets
30
Records
38
Agent score
65%

What's inside setup-miniconda

  1. What is setup-miniconda?

    main

    The conda-incubator/setup-miniconda GitHub Action sets up a base conda environment. It can locate a bundled conda installation on available runners or install specific versions of Miniconda3, Miniforge, or any constructor-based installer via URL or filesystem path.

    Key features include:

    • Adding the condabin/ folder to $PATH.
    • Initializing shell integration across all platforms.
    • Creating and activating a test environment by default.
    • Supporting specific versions of conda or mamba via conda-build-version or mamba-version inputs.
  2. Important shell compatibility and activation notes

    main

    Conda activation behavior varies significantly depending on the shell used in GitHub Actions:

    • sh: Conda activation does not work correctly in sh. Use bash instead.
    • bash: Standard GitHub Actions bash steps run with --noprofile --norc, which ignores conda init changes in ~/.bashrc. To ensure activation works, use shell: bash -el {0} or set it as a job default.
    • sh (alternative): If you must use sh, use shell: sh -l {0} to ensure profile files are loaded.
    • cmd (Windows): cmd shells do not run Autorun commands. To ensure activation works, use shell: cmd /C call {0}.

    General Constraints:

    • use-only-tar-bz2: Must be set to true for caching to work.
    • Channel Conflicts: If providing channels via the action, ensure they do not conflict with channels defined in your environment.yaml, as this can cause solver failures or extremely long install times.
  3. How environment activation works

    main

    By default, this action activates an environment named test and does not activate the base environment. This follows the best practice of keeping the base environment minimal (containing only conda or mamba) and installing workflow-specific packages into separate environments.

    To use a different environment name or path, use the activate-environment input. If the value contains POSIX or Windows slashes, it is treated as a path (a prefix in conda terminology).

  4. Activate the `base` environment

    main

    If your workflow requires using the default base environment instead of a separate test environment, you must perform two steps: set activate-environment to an empty string and set auto-activate to true.

    - uses: conda-incubator/setup-miniconda@v4
      with:
        auto-activate: true
        activate-environment: ""
  5. Use a different environment name or path

    main

    To change the default test environment to a custom name or a specific directory path, set the activate-environment input. Using a path can help avoid "path too long" errors on Windows.

    - uses: conda-incubator/setup-miniconda@v4
      with:
        activate-environment: whatever
  6. Cache conda environments

    main

    You can cache entire conda environments to avoid re-solving and re-installing dependencies.

    Workflow Pattern:

    1. Initial Setup: Run setup-miniconda without an environment-file to set up the base Miniconda/Miniforge installation.
    2. 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.
    3. 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'
  7. Best practices for security and reproducibility

    main

    To ensure secure and reproducible workflows, follow these three practices:

    1. Pin the Action by SHA: Instead of using mutable tags like v2, pin the action to a specific commit SHA. Example: uses: conda-incubator/setup-miniconda@9f54435e0e72c53962ee863144e47a4b094bfd35 # v2.3.0
    2. Automate Updates: Use tools like renovate or dependabot to keep your pinned SHAs updated with recent fixes.
    3. Use Lock Files: Use conda-lock files to ensure the exact same dependency versions are used across all environments.
  8. Cache conda packages

    main

    To speed up workflows, you can cache conda packages using actions/cache. By default, conda uses ~/conda_pkgs_dir as the package directory.

    Important Requirements:

    • You must set use-only-tar-bz2: true in the setup-miniconda configuration for caching to work properly.
    • If you change the package directory using the pkgs-dirs option, ensure your cache path matches that directory.
    • If using pip within your conda environment, those dependencies are not included in the conda package cache and must be cached separately.

    On Windows runners, using the D: drive for package directories and enabling enableCrossOsArchive in the cache configuration can improve decompression speeds.

    jobs:
      caching-example:
        name: Caching
        runs-on: "ubuntu-latest"
        steps:
          - uses: actions/checkout@v6
          - name: Cache conda
            uses: actions/cache@v5
            env:
              CACHE_NUMBER: 0
            with:
              path: ~/conda_pkgs_dir
              key: ${{ runner.os }}-conda-${{ env.CACHE_NUMBER }}-${{ hashFiles('etc/example-environment.yml') }}
          - uses: conda-incubator/setup-miniconda@v4
            with:
              activate-environment: anaconda-client-env
              channel-priority: strict
              environment-file: etc/example-environment.yml
              use-only-tar-bz2: true # IMPORTANT: This needs to be set for caching to work properly!
  9. Setup conda in restricted environments

    main

    If your environment (e.g., corporate self-hosted runners) prevents modifications to ~/.profile or ~/.bashrc, you must prevent setup-miniconda from attempting to modify these files and manually source conda in your steps.

    Configuration:

    • Set run-init: "false"
    • Set remove-profiles: "false"

    Usage: Manually source the conda profile script in your shell steps using source "$CONDA/etc/profile.d/conda.sh" before calling conda activate.

    jobs:
      restricted-env:
        name: Restricted environment
        runs-on: "ubuntu-latest"
        steps:
          - uses: actions/checkout@v6
          - uses: conda-incubator/setup-miniconda@v4
            with:
              activate-environment: myenv
              environment-file: environment.yml
              remove-profiles: "false"
              run-init: "false"
          - name: Run with conda
            shell: bash
            run: |
              source "$CONDA/etc/profile.d/conda.sh"
              conda activate myenv
              python my_script.py
  10. Configure a default shell for GitHub Actions jobs

    main

    To avoid repeating shell directives (like shell: bash -el {0}) in every step, you can define a defaults section at the job level. This is particularly useful for ensuring conda environments are properly activated in subsequent steps.

    jobs:
      default-shell:
        name: Default shell
        runs-on: "ubuntu-latest"
        defaults:
          run:
            shell: bash -el {0}
        steps:
          - uses: actions/checkout@v6
          - uses: conda-incubator/setup-miniconda@v4
            with:
              activate-environment: anaconda-client-env
              environment-file: etc/example-environment-caching.yml
          - run: conda info
          - run: conda list
          - run: conda config --show
  11. Use different shells for different operating systems

    main

    Depending on the OS, you may need to use specific shells to interact with Conda.

    • Linux (Ubuntu): Use bash -el {0} or pwsh.
    • macOS: Use sh -l {0}, bash -el {0}, or pwsh.
    • Windows: Use bash -el {0}, powershell, pwsh, or cmd /C CALL {0}.
    # Example for Windows shell usage
      example-2-win:
        name: Ex2 Windows
        runs-on: "windows-latest"
        steps:
          - uses: conda-incubator/setup-miniconda@v4
            with:
              miniconda-version: "latest"
              activate-environment: foo
          - name: PowerShell
            shell: powershell
            run: |
              conda info
              conda list
          - name: Cmd.exe
            shell: cmd /C CALL {0}
            run: >-
              conda info && conda list
  12. Configure Conda channels and priority

    main

    Use the channels option to specify a list of channels. The priority is determined by the order in the list (highest to lowest). Additional options include:

    • channel-priority: e.g., flexible.
    • allow-softlinks: boolean.
    • show-channel-urls: boolean.
    • use-only-tar-bz2: boolean.
          - uses: conda-incubator/setup-miniconda@v4
            with:
              activate-environment: foo
              python-version: 3.6
              channels: conda-forge,spyder-ide
              allow-softlinks: true
              channel-priority: flexible
              show-channel-urls: true
              use-only-tar-bz2: true