在 GCP cloudbuild 中安装 python 要求时出现问题

Problem with Installing python requirements in GCP cloudbuild

我正在尝试 运行 在 cloudbuild 中使用 DirectRunner 的 apache beam 管道,这样做我需要安装 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])'

我该如何解决这个问题?我也在网上试了一些例子,都不行

编辑:首先我在 App Engine 上部署,然后在云构建虚拟机中下载 repo,安装要求并尝试 运行 它 python 脚本

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

'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