【问题标题】:Problem with Installing python requirements in GCP cloudbuild在 GCP cloudbuild 中安装 python 要求的问题
【发布时间】:2021-04-30 12:33:12
【问题描述】:

我正在尝试在 cloudbuild 中使用 DirectRunner 运行 apache 光束管道,通过这样做我需要安装 python 脚本的要求,但我遇到了一些错误。
这是我的 cloudbuild.yaml 的一部分

    steps:
- name: gcr.io/cloud-builders/gcloud
  entrypoint: 'bash'
  args: [ '-c', "gcloud secrets versions access latest --secret=env --format='get(payload.data)' | tr '_-' '/+' | base64 -d > .env" ]
  id: GetSecretEnv
# - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
#   entrypoint: 'bash'
#   args: ['-c', 'gcloud config set app/cloud_build_timeout 1600 && gcloud app deploy --quiet tweepy-to-pubsub/app.yaml']

- name: gcr.io/cloud-builders/gcloud
  id: Access id_github
  entrypoint: 'bash'
  args: [ '-c', 'gcloud secrets versions access latest --secret=id_github> /root/.ssh/id_github' ]
  volumes:
  - name: 'ssh'
    path: /root/.ssh
# Set up git with key and domain
- name: 'gcr.io/cloud-builders/git'
  id: Set up git with key and domain
  entrypoint: 'bash'
  args:
  - '-c'
  - |
    chmod 600 /root/.ssh/id_github
    cat <<EOF >/root/.ssh/config
    Hostname github.com
    IdentityFile /root/.ssh/id_github
    EOF
    ssh-keyscan -t rsa github.com > /root/.ssh/known_hosts
  volumes:
  - name: 'ssh'
    path: /root/.ssh
- name: 'gcr.io/cloud-builders/git'
# Connect to the repository
  id: Connect and clone repository
  dir: workspace
  args:
  - clone
  - --recurse-submodules
  - git@github.com:x/repo.git
  volumes:
  - name: 'ssh'
    path: /root/.ssh

- name: 'gcr.io/$PROJECT_ID/dataflow-python3'
  entrypoint: '/bin/bash'
  args: [ '-c',
          'source /venv/bin/activate' ]
- name: 'gcr.io/$PROJECT_ID/dataflow-python3'  
  entrypoint: '/bin/bash' 
  dir: workspace
  args: ['pip', 'install','-r', '/dir1/dir2/requirements.txt']
- name: 'gcr.io/$PROJECT_ID/dataflow-python3'
  entrypoint: 'python'
  dir: workspace
  args: [ 'dir1/dir2/script.py', 
         '--runner=DirectRunner' ]
timeout: "1600s"

没有安装要求的步骤,但我需要库,因为我有缺少库的 python 错误,并且在第二步(实际上是云构建的原始形式的第 5 步),云构建失败了错误

Step #5: Already have image (with digest): gcr.io/x/dataflow-python3
Step #5: import-im6.q16: unable to open X server `' @ error/import.c/ImportImageCommand/360.
Step #5: import-im6.q16: unable to open X server `' @ error/import.c/ImportImageCommand/360.
Step #5: /usr/local/bin/pip: line 5: from: command not found
Step #5: /usr/local/bin/pip: pip: line 7: syntax error near unexpected token `('
Step #5: /usr/local/bin/pip: pip: line 7: `    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])'

我该如何解决这个问题?我也试过网上的一些例子,还是不行

编辑:首先我在应用引擎上部署,然后我在云构建 vm 中下载 repo,安装要求并尝试运行 python 脚本

【问题讨论】:

  • 根据1,安装要求后您尝试运行的 script.py 文件可能存在问题。您确定它以与 #!/usr/bin/env python 类似的方式以正确的 shebang 字符开头吗?
  • 您能否分享完整的 cloudbuild.yaml,或者在您的代码示例中指出第 5 步是什么?
  • @guillaumeblaquiere 我编辑了这个问题,现在它是完整的 cloudbuild.yaml 文件

标签: python linux google-cloud-platform google-cloud-build


【解决方案1】:

我认为问题出在您的路径定义

'source /venv/bin/activate'

'pip', 'install','-r', '/dir1/dir2/requirements.txt'

您使用的是完整路径定义,但它不适用于 Cloud Build。当前工作目录是/workspace/。如果您使用相对路径,只需在路径前添加一个点.,它应该会更好。

或者不...确实,您可以在一个步骤中激活 venv,并在以下步骤中安装 pip。从一个步骤到另一个步骤,运行时环境被卸载并与另一个容器一起重新加载。因此,您设置环境变量的source 命令会在pip 步骤中消失。


此外,您的云构建环境是为构建和销毁而构建的。在这种情况下,您不需要使用 venv,您可以像这样简化最后 3 个步骤


- name: 'gcr.io/$PROJECT_ID/dataflow-python3'
  entrypoint: '/bin/bash'
  args:
   - '-c'
   - |
       pip install -r ./dir1/dir2/requirements.txt
       python ./dir1/dir2/script.py --runner=DirectRunner

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-30
    • 1970-01-01
    • 2022-06-25
    相关资源
    最近更新 更多