您如何在 GitHub 操作中使用 pipenv?

sgo*_*lez 5 python docker pipenv github-actions

我正在使用 pipenv 为 GitHub 操作安装 python 依赖项。这是我的 pipfile 的样子:

name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]
pylint = "*"

[packages]
pandas = "*"
pygithub = "*"
matplotlib = "*"

[requires]
python_version = "3.8"
Run Code Online (Sandbox Code Playgroud)

这是 Dockerfile 的样子:

FROM python:3

COPY main.py /
COPY Pipfile /
COPY Pipfile.lock /
COPY views.csv /

# https://github.com/pypa/pipenv/issues/4273
RUN pip install 'pipenv==2018.11.26'
RUN pipenv install --deploy --ignore-pipfile

ENTRYPOINT ["pipenv", "run", "python", "./main.py"
Run Code Online (Sandbox Code Playgroud)

我可以在我的机器上本地运行 docker 镜像,一切都按预期工作,但是当我将它推送到 GitHub 时,它失败了:

Virtualenv location: /github/home/.local/share/virtualenvs/workspace-0SZQOxG8
Traceback (most recent call last):
  File "./main.py", line 1, in <module>
    from github import Github
ModuleNotFoundError: No module named 'github'
Run Code Online (Sandbox Code Playgroud)

出于某种原因,当 docker 镜像在 GitHub 虚拟机上运行时,它不会获取任何依赖项。

2020 年 7 月 28 日编辑:

这是触发操作的工作流的 main.yml。

on: 
  push: 
    branches: 
      - master


# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "insights_job"
  insights_job:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
    # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
    - uses: actions/checkout@v2
      
    - name: GitHub insights 
      id: insights
      uses: ./
Run Code Online (Sandbox Code Playgroud)

Ome*_*mer 5

精简版

只需将其添加到 Dockerfile 的顶部:

# Tells pipenv to create virtualenvs in /root rather than $HOME/.local/share.
# We do this because GitHub modifies the HOME variable between `docker build` and
# `docker run`
ENV WORKON_HOME /root

# Tells pipenv to use this specific Pipfile rather than the Pipfile in the 
# current working directory (the working directory changes between `docker build` 
# and `docker run`, this ensures we always use the same Pipfile)
ENV PIPENV_PIPFILE /Pipfile
Run Code Online (Sandbox Code Playgroud)

长版

这里可以看出,Github actions 将HOME环境变量定义为/github/home.

我们可以看到,这个变量随后通过run操作步骤日志中可见的命令传递给 docker 容器:

/usr/bin/docker run --name c201c6ad6ba986775dbb96d3c072d294f3c8_a057c6 --label 87c201 --workdir /github/workspace --rm -e HOME -e GITHUB_JOB -e GITHUB_REF -e GITHUB_SHA -e GITHUB_REPOSITORY -e GITHUB_REPOSITORY_OWNER -e GITHUB_RUN_ID -e GITHUB_RUN_NUMBER -e GITHUB_ACTOR -e GITHUB_WORKFLOW -e GITHUB_HEAD_REF -e GITHUB_BASE_REF -e GITHUB_EVENT_NAME -e GITHUB_SERVER_URL -e GITHUB_API_URL -e GITHUB_GRAPHQL_URL -e GITHUB_WORKSPACE -e GITHUB_ACTION -e GITHUB_EVENT_PATH -e RUNNER_OS -e RUNNER_TOOL_CACHE -e RUNNER_TEMP -e RUNNER_WORKSPACE -e ACTIONS_RUNTIME_URL -e ACTIONS_RUNTIME_TOKEN -e ACTIONS_CACHE_URL -e GITHUB_ACTIONS=true -e CI=true -v "/var/run/docker.sock":"/var/run/docker.sock" -v "/home/runner/work/_temp/_github_home":"/github/home" -v "/home/runner/work/_temp/_github_workflow":"/github/workflow" -v "/home/runner/work/j/j":"/github/workspace" 87c201:c6ad6ba986775dbb96d3c072d294f3c8
Run Code Online (Sandbox Code Playgroud)

(注意-e home国旗)

pipenv install在其中创建一个 virtualenv$HOME/.local/share/virtualenvs并在该目录中安装所有依赖项。pipenv run ...然后使用相同的路径找到 virtualenv 来运行所需的命令。

在 Docker 映像 ( docker build)的构建阶段,$HOME具有默认值/root(因为这是 root 用户的主目录),因此在/root/.local/share/virtualenvs.

但是,在docker run阶段期间,HOME设置为/github/home(如上所述)。因此,virtualenv 在/github/home/.local/share/virtualenvs.

总结一下——

pipenv install 用途 /root/.local/share/virtualenvs

pipenv run 用途 /github/home/.local/share/virtualenvs

这意味着依赖项安装在一个位置,但在实际运行脚本时预计会在不同的位置。

为了克服这个问题,我们可以使用语句WORKON_HOME在 Dockerfile 中设置ENV

WORKON_HOME变量被尊重pipenv,它会更倾向于将其虚拟环境中定义的路径WORKON_HOME,而不是在定义的路径HOME

所以解决方案是添加

ENV WORKON_HOME /root

Dockerfile.

然而,这还不够,因为 pipenv 根据pipenv文件的位置选择 virtualenv 。将--workdir被设置为/github/workspace当我们通过Github上,然后整个项目的源代码安装到该文件夹,所以pipenv run我们实际上使用Pipenv的文件/github/workspace。这与Pipenvdocker build阶段期间复制到/并用于pipenv install. 由于使用了两个不同的 Pipenv 位置,因此将使用两个不同的 virtualenv。

为了解决这个问题,我们可以使用PIPENV_PIPEFILE环境变量。此变量告诉在该变量指定的位置而不是在当前工作目录中pipenv查找Pipfile

让我们也将它添加到我们的 Dockerfile 中:

ENV PIPENV_PIPFILE /Pipfile
Run Code Online (Sandbox Code Playgroud)