在Circle CI环境变量中包含星号(*)会导致环境变量的值出现奇怪的副作用。

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

Including asterisks (*) in Circle CI environment variable causes strange side effects in the value of the environment variable

问题

我在 Circle CI 的环境变量中有一个 cron 调度,让我们称之为 CRON_SCHEDULE,用于在 Go 应用程序中使用。假设所需的调度字符串是 0 15 13 * * *,表示每天下午 1:15:00,即 13:15:00。

cron 没有触发,我进行了调查。当我在 Go 应用程序中打印出 CRON_SCHEDULE 时,前三个数字是正确的,但是在三个 "*" 的位置上,我看到的是应用程序文件本身的内容:

my_app docker-compose.yml Dockerfile go.mod go.sum main.go README.md tests utils

明确一下,这是从 fmt.Print(os.Getenv("CRON_SCHEDULE")) 输出的完整内容:

0 15 13 my_app docker-compose.yml Dockerfile go.mod go.sum main.go README.md tests utils my_app docker-compose.yml Dockerfile go.mod go.sum main.go README.md tests utils my_app docker-compose.yml Dockerfile go.mod go.sum main.go README.md tests utils

非常奇怪!

在 Circle CI 变量中,* 是否有特殊含义?如果有,我该如何正确转义它?或者这是 Docker 的一个怪癖(从 Docker 容器中运行 Go 应用程序),* 被打印为文件?我现在完全不知道发生了什么!

我目前的猜测是,由于 bash shell 转义的问题,当你在环境变量中有 * 字符时,它们被错误地注入到容器/应用程序环境中。

编辑:我可以确认尝试转义这些字符可以解决这个问题。然而,当然,cron 库仍然无法理解 0 15 13 \* \* \*

英文:

I have a cron schedule in an Circle CI environment variable, lets call it CRON_SCHEDULE to be used within a Go application. Let's say the desired schedule string is 0 15 13 * * *, for every day exactly 1:15:00 PM, i.e. 13:15:00

The cron wasn't firing and I went to investigate. When I log out CRON_SCHEDULE from within the Go application, I get the first three numbers correctly, but instead of the three "*" I see instead the contents of the application files itself:

my_app docker-compose.yml Dockerfile go.mod go.sum main.go README.md tests utils

To be clear, this is the full exact output from fmt.Print(os.Getenv("CRON_SCHEDULE")):

0 15 13 my_app docker-compose.yml Dockerfile go.mod go.sum main.go README.md tests utils my_app docker-compose.yml Dockerfile go.mod go.sum main.go README.md tests utils my_app docker-compose.yml Dockerfile go.mod go.sum main.go README.md tests utils

Very strange!

Does * within a Circle CI variable have a special meaning? If so, how can I escape this properly? Or is this instead a Docker quirk (running the Go app from a docker container) and * is being printed as the files? No idea what is going on right now!

My current guess is that due to bash shell escaping fun, that when you have the * character in an environment variable, they're injected incorrectly into the container / application environment.

EDIT: I can confirm trying to escape these characters removes this 'bug'. However then of course the cron library still can't understand 0 15 13 \* \* \*!

答案1

得分: 1

Zeke的问题指引我找到了正确的方向。显然,如果你的Circle CI环境文件中有*字符(或许还有其他字符!),你需要对环境注入进行转义。通过以下方式将内容追加到我的.env文件中解决了这个问题:

echo CRON_SCHEDULE="{$CRON_SCHEDULE}" >> .env
英文:

Zeke's question pointed me in the right direction. Apparently, you need to escape the environment injection if you have * characters (and perhaps there are others!) in your Circle CI environment files. Appending to my .env file with:

echo CRON_SCHEDULE="${CRON_SCHEDULE}" >> .env

Resolved the issue.

huangapple
  • 本文由 发表于 2023年5月23日 18:56:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76313950.html
匿名

发表评论

匿名网友

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

确定