10

Better conda environments on GitHub actions

 3 years ago
source link: https://ericmjl.github.io/blog/2021/12/30/better-conda-environments-on-github-actions/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Better conda environments on GitHub actions

written by Eric J. Ma on 2021-12-30

| tags: continuous integration github actions til

I recently figured out two tips to make GitHub actions play nicely with conda installations. Here they are.

Ensure bash is in login mode

The first is to use the following block:

    # https://github.com/marketplace/actions/setup-miniconda#use-a-default-shell
    defaults:
      run:
        shell: bash -l {0}

What this does is ensure that every shell command is run in login mode. As detailed in this StackOverflow answer:

  • -l to insure (sic) a login bash, where the environment is correctly set;
  • {0}, a template placeholder, replaced at pipeline execution time by the actual script command to execute.

Counterfactually, I would have had to use the deprecated source activate <env_name>, which always made me a bit nervous. Now, I can instead switch over to using conda activate <env_name> before executing environment-specific commands, thereby providing longevity for the build.

Use mambaforge

The other tip is to use the mambaforge installer to get a conda installation onto GitHub actions. The block I recently used for my causality repo is as follows:

      # See: https://github.com/marketplace/actions/setup-miniconda
      - name: Setup miniconda
        uses: conda-incubator/setup-miniconda@v2
        with:
          auto-update-conda: true
          miniforge-variant: Mambaforge
          channels: conda-forge
          python-version: 3.9
          activate-environment: causality
          environment-file: environment.yml
          use-mamba: true

This configuration guarantees the use of mamba to solve the environment, which means we will have blazingly fast builds. Previously, I used to use a different GitHub action (s-weigand/setup-conda@v1), original conda (rather than mamba), and a convoluted build that involved environment caching. You can take a look at an example that I copied over from my nxviz project repository by expanding the details below.


: Build documentation

By contrast, the new build is much smaller and easier to maintain:


: Build documentation

I send out a monthly newsletter with tips and tools for data scientists. Come check it out at Substack.

If you would like to receive deeper, in-depth content as an early subscriber, come support me on Patreon!


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK