简体   繁体   English

在 Google Cloud Build 中使用 gcloud compute scp 时出错

[英]Error using gcloud compute scp with Google Cloud Build

I've been trying to run a gcloud compute scp command using Google Cloud Build trigger.我一直在尝试使用 Google Cloud Build 触发器运行gcloud compute scp命令。 I want my code to be copied to the respective directories in listed servers.我希望将我的代码复制到列出的服务器中的相应目录。

However, it's kinda strange that when I do a gcloud compute scp * username@instance-name:/home/directory --zone=us-central1-a --recurse in Cloud Shell, it works.但是,当我在 Cloud Shell 中执行gcloud compute scp * username@instance-name:/home/directory --zone=us-central1-a --recurse时,它起作用了,这有点奇怪。

But when I write the equivalent in the cloudbuild.yaml file, it fails with error: *: No such file or directory .但是当我在cloudbuild.yaml文件中写入等效项时,它失败并出现错误: *: No such file or directory

Here are the contents for my cloudbuild.yaml :以下是我的cloudbuild.yaml的内容:

- name: 'gcr.io/cloud-builders/gcloud'
  args: ['compute' , 'scp' , '*', 'username@instance-name:/home/directory' , '--zone=us-central1-a', '--recurse']

I've also tried using the remote-builder and that fails as well with error:我也尝试过使用remote-builder ,但也失败并出现错误:

ssh: connect to host <ip_address> port 22: Connection timed out
lost connection
ERROR: (gcloud.compute.scp) [/usr/bin/scp] exited with return code [1].

I've also tried other combinations by replacing the source * with /workspace/* , ./* etc. but none of them worked.我还通过将源*替换为/workspace/*./*等来尝试其他组合,但它们都不起作用。 Executing with source as /workspace/ works but that copies the entire workspace directory instead of the contents of the directory as desired.使用 source as /workspace/执行是可行的,但会复制整个workspace目录,而不是根据需要复制目录的内容。

I've tried running the following set as well but that failed too.我也试过运行以下设置,但也失败了。

cloudbuild.yaml : cloudbuild.yaml

- name: 'ubuntu'
  args: ['bash', '-c' , 'scp', '*', 'username@instance-name:/home/directory']

Error:错误:

Already have image: ubuntu
scp: command not found

Again, all I want is to use Cloud Build to copy my code over to the VMs.同样,我只想使用 Cloud Build 将我的代码复制到 VM。 Please help.请帮忙。

For the 'gcr.io/cloud-builders/gcloud' container image option:对于“gcr.io/cloud-builders/gcloud”容器镜像选项:

The asterisk, "*", is interpreted by bash and expanded before passing the referents to the command, and, as gcloud in this container image is used directly and not through bash as an intermediary, "*" is not expanded, but passed literally to the gcloud command, which doesn't find any file named "*".星号“*”由bash解释并在将引用对象传递给命令之前展开,并且由于直接使用此容器映像中的gcloud而不是通过bash作为中介,“*”未展开,而是按字面意思传递到gcloud命令,它没有找到任何名为“*”的文件。 If you need to copy all the code (directories recursively and their files) in the repository, use the same command but writing the root symbol, "/" (which, by the way, is a literal -not expandable- to bash ), instead of "*".如果您需要复制存储库中的所有代码(递归目录及其文件),请使用相同的命令,但写入根符号“/”(顺便说一下,它是一个文字 - 不可扩展 - 到bash ),代替 ”*”。

For the OS container image option with bash:对于 bash 的 OS 容器映像选项:

The container image must have scp installed by default to work that way, and the "ubuntu" image doesn't have it by default.容器镜像必须默认安装scp才能以这种方式工作,而“ubuntu”镜像默认没有。 You could either use an image with the openssh-client package installed by default, or install it in place and then run the command, something like this (for the step syntax, check this ):您可以使用默认安装了openssh-client package 的图像,或者将其安装到位然后运行命令,如下所示(对于步骤语法,请检查):

steps:
- name: 'ubuntu'
  entrypoint: 'bash'
  args:
  - '-c'
  - |
    sudo apt update && sudo apt install openssh-client -y && scp * username@instance-name:/home/directory

I had the same issue.我遇到过同样的问题。 I have ended up creating two steps instead:我最终创建了两个步骤:

  - name: 'google/cloud-sdk:slim'
    args:
      - compute
      - scp
      - '--recurse'
      - '--compress'
      - /workspace/dist
      - 'build@instance-1:/var/www/'
      - '--zone'
      - us-west2-a
    id: copy-file-to-server
    entrypoint: gcloud
  - name: 'google/cloud-sdk:slim'
    args:
      - compute
      - ssh
      - '--zone'
      - us-west2-a
      - build@instance-1
      - '--'
      - 'mv /var/www/dist /var/www/$_SHORT_SHA '
    id: rename-dist
    entrypoint: gcloud

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 谷歌云 SDK:gcloud auth login 给我一个错误 - Google Cloud SDK: gcloud auth login gives me an error gcloud.app.deploy 错误响应:[13] 无法创建云构建:无效存储桶 - 如何修复? - gcloud.app.deploy Error Response: [13] Failed to create cloud build: invalid bucket - how to fix? 将 Kaniko 缓存与用于 Google Cloud 的 Google Cloud Build Kubernetes 部署一起使用 - Using Kaniko cache with Google Cloud Build for Google Cloud Kubernetes Deployments Google Cloud Build 错误没有项目活动 - Google Cloud Build error no project active 是什么导致 Google gcloud 错误消息“错误:(gcloud)无效选择:'gcloud'”以及如何解决这个问题? - What causes Google gcloud error message "ERROR: (gcloud) Invalid choice: 'gcloud'" and how to resolve this? 谷歌云计算是否存在数据使用限制 - Are there data usage limitations with Google Cloud Compute 无法访问谷歌云计算引擎上的 Jenkins - Unable to access Jenkins on compute engine on google cloud 如何使用Python的Google Cloud Client Library配置gcloud项目 - How to use Google Cloud Client Library for Python to configure the gcloud project Google Cloud:构建总是失败,MANIFEST_UNKNOWN 错误 - Google Cloud: Build always fails, MANIFEST_UNKNOWN error 错误:(gcloud.compute.instances.stop)无法获取资源 - ERROR: (gcloud.compute.instances.stop) Could not fetch resource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM