在 jenkins 中使用 pip 安装后无法 运行 cookiecutter

Cannot run cookiecutter after installing with pip in jenkins

我正在尝试使用 jenkins 管道安装 cookiecutter 实用程序。我试试运行这个阶段:

        stage('Install CookieCutter') {
            steps{
                script {
                     sh'''
                        pip uninstall cookiecutter -y
                        export PATH=$HOME/.local/bin:$PATH
                        pip install --user cookiecutter
                        cookiecutter -V
                     '''
                }
            }
        }

在这个阶段 运行 之后,我得到以下输出:

  + pip uninstall cookiecutter -y
  Found existing installation: cookiecutter 1.7.3
  Uninstalling cookiecutter-1.7.3:
    Successfully uninstalled cookiecutter-1.7.3
  + export PATH=/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin
  + PATH=/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin
  + pip install --user cookiecutter

  Collecting cookiecutter
    Using cached cookiecutter-1.7.3-py2.py3-none-any.whl (34 kB)
  Requirement already satisfied: click>=7.0 in /root/.local/lib/python2.7/site-packages (from cookiecutter) (7.1.2)
  Requirement already satisfied: six>=1.10 in /usr/local/lib/python2.7/site-packages (from cookiecutter) (1.16.0)

  Installing collected packages: cookiecutter
    WARNING: The script cookiecutter is installed in '/root/.local/bin' which is not on PATH.
    Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  Successfully installed cookiecutter-1.7.3

  + cookiecutter -V
  /workspace/pipeline@tmp/durable-123456/script.sh: line 5: cookiecutter: command not found 

我暂时忽略 pip 版本警告,因为我认为这与未安装没有任何关系。

这里出了什么问题?

WARNING: The script cookiecutter is installed in '/root/.local/bin' which is not on PATH.

因此,出于某种原因,您的导出不起作用。这可能有两个原因:

  1. $HOME 不是 root,我对此表示怀疑
  2. 这个 Jenkins 脚本的行为类似于 GNU Make。每行都在不同的子 shell 中执行,因此您的导出并不重要。尝试像这样将其作为一个衬里执行:
        stage('Install CookieCutter') {
            steps{
                script {
                     sh'''
                        pip uninstall cookiecutter -y && \
                        export PATH=$HOME/.local/bin:$PATH && \
                        pip install --user cookiecutter && \
                        cookiecutter -V
                     '''
                }
            }
        }

或者您可以尝试将此作为更简洁的变体:

        stage('Install CookieCutter') {
            steps{
                script {
                     sh'''
                        pip uninstall cookiecutter -y
                        pip install --user cookiecutter
                        PATH=$HOME/.local/bin:$PATH cookiecutter -V
                     '''
                }
            }
        }

原来主目录没有正确定义,所以用 ~

替换了 $HOME
                script {
                     sh'''
                        pip uninstall cookiecutter -y
                        source ~/.bash_profile
                        export PATH="~/.local/bin:$PATH"
                        pip install --user cookiecutter
                        ls ~/.local/bin
                        cookiecutter -V
                     '''
                }