Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

env activation is not persistent throughout the session #12

Open
basic-ph opened this issue May 19, 2020 · 5 comments
Open

env activation is not persistent throughout the session #12

basic-ph opened this issue May 19, 2020 · 5 comments
Labels
bug Something isn't working enhancement New feature or request

Comments

@basic-ph
Copy link

Describe the bug
If the command source activate <env-name> is executed in a previous step respect to one in which the environment is used, the env itself does not result activated and raise for using a command that is not installed in the base env.

To Reproduce
the actual YAML file with the workflow:

name: CI
on: push

jobs:
  test:
    name: Pytest Testing
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Conda setup
        uses: s-weigand/[email protected]
        with:
          update-conda: true
          python-version: 3.7
      # env activation as suggested by @s-weigand
      - name: Conda environment creation
        run: |
          conda env create -f environment.yml
          source activate feat
          pip install -e .
      - name: Testing with pytest
        run: |
          pytest

the following error is reported in the Actions log:

2020-05-19T17:38:50.4263319Z ##[group]Run pytest
2020-05-19T17:38:50.4263646Z �[36;1mpytest�[0m
2020-05-19T17:38:50.4299399Z shell: /bin/bash -e {0}
2020-05-19T17:38:50.4299513Z ##[endgroup]
2020-05-19T17:38:50.4377171Z /home/runner/work/_temp/5ded05d7-e2df-43db-bfca-4721d3e5faea.sh: line 1: pytest: command not found
2020-05-19T17:38:50.4384238Z ##[error]Process completed with exit code 127.

Workaround
At the moment the only way I found to make it work is to run source activate feat also before the usage of the environment (in my case pytest command) like this:

name: CI
on: push

jobs:
  test:
    name: Pytest Testing
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Conda setup
        uses: s-weigand/[email protected]
        with:
          update-conda: true
          python-version: 3.7
      # env activation as suggested by @s-weigand
      - name: Conda environment creation
        run: |
          conda env create -f environment.yml
          source activate feat
          pip install -e .
      - name: Testing with pytest
        run: |
          source activate feat
          pytest
@s-weigand s-weigand added bug Something isn't working enhancement New feature or request labels May 19, 2020
@s-weigand
Copy link
Owner

Thanks for providing the bug as well as the current fix at the same time 😃
Since activation of the base env does work across steps (activate-conda: true), my guess is, that this is due to differences in how the workflow executes run: vs. the internal use of exec.exec.

// write temp shell script to activate conda
temp.track()
const stream = temp.createWriteStream({ suffix: '.sh' })
stream.write('source activate base')
stream.end()
await exec.exec('bash', [stream.path as string])

When I finally got some time to write the unit tests and implement the environment.yml feature, this will hopefully be solved.
IMHO w/o proper testing, new (especially breaking) features would be just collecting technical debt and things could blow with certain os-settings combinations, which would be really ugly, especially in the CI/CD context.

@BtbN
Copy link

BtbN commented Jun 7, 2020

Unless I'm missing something, that code-snipped there spawns a new sub-shell, activate the env in there, and when that sub-shell exits, it's like nothing has ever happened.
You'd need to somehow run the command on the main shell the workflow is running on.
Which I'm not sure is even possible, given that for each step, github seems to invoke a new subshell in itself.

@s-weigand
Copy link
Owner

Just as a future reference, when I finally got time to implement those changes 😞
@BtbN You are correct, and the reason why it still partially works is due to the right paths being added to $PATH.
The correct way to implement this could be to parse the output of conda <shell_name> activate <env_name>, and set the correct paths and env vars with core.addPath('/path/to/mytool'); and core.exportVariable('envVar', 'Val');.

@s-weigand
Copy link
Owner

@all-contributors please add @basic-ph for bug

@allcontributors
Copy link
Contributor

@s-weigand

I've put up a pull request to add @basic-ph! 🎉

s-weigand added a commit that referenced this issue Jun 28, 2021
…e <env_name>'

output instead running a tmp script in a subshell
see #12
s-weigand added a commit that referenced this issue Jun 28, 2021
…e <env_name>'

output instead running a tmp script in a subshell
see #12
s-weigand added a commit that referenced this issue Jun 28, 2021
…e <env_name>'

output instead running a tmp script in a subshell
see #12
s-weigand added a commit that referenced this issue Jun 29, 2021
…e <env_name>'

output instead running a tmp script in a subshell
see #12
s-weigand added a commit that referenced this issue Jun 29, 2021
* 🧹 Fixed typos in readme

* ♻️ Refactored env activation to use 'conda shell.<shell_name> activate <env_name>'

output instead running a tmp script in a subshell
see #12

* 🧪 Added job to check if env activation persists

* 👌 Changed logging to use groups for a cleaner log

* 🧹 Remove env activation test

Env activation will be a feature for v2

* 🧪 Activate env var testing

* 🧹 Removed obsolete warining from readme

* 🧹 Removed insecsure env var logging
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants