gpt4 book ai didi

python-3.x - 为什么 `exec bash` 在 CI 管道中不起作用?

转载 作者:行者123 更新时间:2023-12-05 05:56:06 27 4
gpt4 key购买 nike

我已经写了一个 github 工作流文件。我想在 github 操作中运行一个 python 程序来验证一些更改。我有一个 environment.yml 文件,其中包含该程序所需的所有 conda 环境依赖项。问题是,实际程序根本没有运行,我的工作流程已成功完成。

以下是workflow.yml文件的作业部分

jobs:
build-linux:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v2
with:
ref: refs/pull/${{ github.event.pull_request.number }}/merge

- name: Set up Python 3.8
uses: actions/setup-python@v2
with:
python-version: 3.8

- name: Cache conda
uses: actions/cache@v2
env:
# Increase this value to reset cache if etc/example-environment.yml has not changed
CACHE_NUMBER: 0
with:
path: ~/conda_pkgs_dir
key:
${{ runner.os }}-conda-${{ env.CACHE_NUMBER }}-${{hashFiles('**/environment.yml') }}

- uses: conda-incubator/setup-miniconda@v2
with:
activate-environment: test-env
environment-file: environment.yml
use-only-tar-bz2: true # IMPORTANT: This needs to be set for caching to work properly!

- name: Test
run: |
export PATH="./:$PATH"
conda init bash
exec bash
conda activate test-env
echo "Conda prefix: $CONDA_PREFIX"
python test.py
shell: bash

我也尝试在最后一步中删除 shell:bash ,但这也给了我同样的结果。最后一步中的日志如下所示:

Run export PATH="./:$PATH"
export PATH="./:$PATH"
conda init bash
exec bash
conda activate test-env
echo "Conda prefix: $CONDA_PREFIX"
python test.py
shell: /usr/bin/bash --noprofile --norc -e -o pipefail {0}
env:
pythonLocation: /opt/hostedtoolcache/Python/3.8.11/x64
LD_LIBRARY_PATH: /opt/hostedtoolcache/Python/3.8.11/x64/lib
CONDA_PKGS_DIR: /home/runner/conda_pkgs_dir
no change /usr/share/miniconda/condabin/conda
no change /usr/share/miniconda/bin/conda
no change /usr/share/miniconda/bin/conda-env
no change /usr/share/miniconda/bin/activate
no change /usr/share/miniconda/bin/deactivate
no change /usr/share/miniconda/etc/profile.d/conda.sh
no change /usr/share/miniconda/etc/fish/conf.d/conda.fish
no change /usr/share/miniconda/shell/condabin/Conda.psm1
no change /usr/share/miniconda/shell/condabin/conda-hook.ps1
no change /usr/share/miniconda/lib/python3.9/site-packages/xontrib/conda.xsh
no change /usr/share/miniconda/etc/profile.d/conda.csh
modified /home/runner/.bashrc

==> For changes to take effect, close and re-open your current shell. <==

我们可以清楚地看到,行 echo "Conda prefix: $CONDA_PREFIX" 根本没有被执行,工作流成功终止。我们应该期望它要么运行要么失败,但什么也没有发生。工作流只是忽略这些命令并将工作流标记为成功。

最佳答案

您的 CI 脚本包含以下行:

exec bash

当这一行被执行时,shell进程被一个新进程替换,新的shell进程不知道它应该继续执行前一个进程的脚本:所有的执行状态都丢失了。 GitHub Actions 将要执行的脚本作为命令行参数传递给初始 shell 进程,并将标准输入设置为 /dev/null;由于新的 shell 进程以空命令行和标准输入上的空文件启动,因此它会立即退出。这在交互式 shell 中运行良好这一事实是一个幸运的巧合。

安装程序指示您重新启动 shell 的原因是应用添加到 shell 初始化文件的环境变量更改。因此,将 exec bash 行替换为

可能就足够了
source "$HOME/.bashrc"

然而,即使使用这一行,环境修改也不会应用于后续步骤,因为 the documentation of the setup-miniconda action warns :

  • Bash shells do not use ~/.profile or ~/.bashrc so these shells need to beexplicitely declared as shell: bash -l {0} on steps that need to be properlyactivated (or use a default shell). This is because bash shells are executedwith bash --noprofile --norc -eo pipefail {0} thus ignoring updated on bashprofile files made by conda init bash. SeeGithub Actions Documentationandthread.

根据这个建议,我认为最好的做法是在放置 exec bash 的位置结束操作步骤,然后应用 shell:设置所有进一步的步骤(或至少那些实际需要它的步骤):

      - name: Set up Conda environment
run: |
export PATH="./:$PATH"
conda init bash
- name: Perform Conda tests
shell: bash -l {0}
run: |
export PATH="./:$PATH"
conda activate test-env
echo "Conda prefix: $CONDA_PREFIX"
python test.py

关于python-3.x - 为什么 `exec bash` 在 CI 管道中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69294127/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com