在云初始化中运行 `GO111MODULE=on go install . ./cmd/…`。

huangapple go评论97阅读模式
英文:

run GO111MODULE=on go install . ./cmd/... in cloud init

问题

我有一个使用cloud init部署的bash脚本,我的bash脚本包含以下代码部分:

GO111MODULE=on go install . ./cmd/...

当我直接在部署的服务器终端中运行我的bash脚本时,它按预期工作。但是当我在cloud config中使用runcmd运行它时,脚本的这部分代码:

GO111MODULE=on go install . ./cmd/...

没有被执行,有人知道为什么吗?

    runcmd:
  - [ bash, /usr/local/bin/myscript.sh ]
英文:

I have a bash script which is deployed with cloud init, my bash script contains the following part of code

GO111MODULE=on go install . ./cmd/...

when running my bash script directly in the terminal of the deplyed server, it works as expected. But when i run it with runcmd in the cloud config, this part of the script:

GO111MODULE=on go install . ./cmd/...

does not get executed, anyone knows why?

    runcmd:
  - [ bash, /usr/local/bin/myscript.sh ]

答案1

得分: 2

runcmd中正确的shell执行方式如下(在Cloud config examples中可见):

- [ bash, -c, /usr/local/bin/myscript.sh ]

或者:

- [ /usr/local/bin/myscript.sh ]

假设你的脚本以shebang #!/bin/bash开始。

此外,你需要在脚本中添加任何环境变量,因为Cloud config examples中没有明显的设置环境变量的方法。

#!/bin/bash
export GO111MODULE=on
export ...
英文:

A proper shell execution in runcmd would be (as seen in Cloud config examples):

- [ bash, -c, /usr/local/bin/myscript.sh ]

or:

- [ /usr/local/bin/myscript.sh ]

Assuming your script starts with a shebang #!/bin/bash

Plus, you need to add any environment variable inside the script, as Cloud config examples do not include any obvious way to set them.

#!/bin/bash
export GO111MODULE=on
export ...

答案2

得分: 1

感谢VonC的提示,我成功解决了问题。我在myscript.sh中添加了以下内容:

GOCACHE=/root/.cache/go-build
export GOCACHE
export GO111MODULE=on
go install . ./cmd/...

runcmd:

  • [ bash, -c, /usr/local/bin/myscript.sh ]

现在脚本可以从cloud-init部署和运行了。

英文:

Thanks to the tip from VonC, i was able to fix the issue. i added the following to myscript.sh

GOCACHE=/root/.cache/go-build
export GOCACHE
export GO111MODULE=on
go install . ./cmd/...


runcmd:
- [ bash, -c, /usr/local/bin/myscript.sh ]

the script now deploys and runs from cloud-init.

huangapple
  • 本文由 发表于 2021年11月23日 04:27:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/70072004.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定