[英]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.