0

运行 power shell 脚本

curl -X POST --data-urlencode 'payload={"channel": "#personal", "username": "Jenkins", "text": "Building $env:BUILD_NUMBER", "icon_emoji": ":computer:"}' https://xxxxxx.slack.com/services/hooks/incoming-webhook?token=aseaUO

或者

curl -X POST --data-urlencode 'payload={"channel": "#personal-notification", "username": "Jenkins", "text": "Building ${BUILD_NUMBER}", "icon_emoji": ":computer:"}' https://xxxxxx.slack.com/services/hooks/incoming-webhook?token=aseaUO

{BUILD_NUMBER} 没有被 curl 有效负载中的实际内部版本号替换

--Edit 我注意到我的 sh 脚本正在调用 git\bin\sh.exe。那是问题吗?

在尝试以下评论之一 curl -X POST --data-urlencode $('payload={0}"channel": "#personal", "username": "Jenkins", "text": "Building {1} ", "icon_emoji": ":computer:"{2}' -f "{", $env:rohan) "}" https://xxxxxx.slack.com/services/hooks/incoming-webhook?token=aseaUO我明白了

在此处输入图像描述

在直接尝试在 PS 终端上运行脚本时,我得到

在此处输入图像描述

4

2 回答 2

0

您的代码中存在一些问题,首先是引号( ps 中的变量未在引号中扩展)其次是(愚蠢的)参数...如果不起作用,请尝试将 Build_Number 更改为 ${BUILD_NUMBER}

$param = @()
$param = "-X POST"
$param += "--data-urlencode 'payload={""channel"": ""#personal"", ""username"": ""Jenkins"", ""text"": ""Building ${env:BUILD_NUMBER}", ""icon_emoji"": "":computer:""}'"
$param += "https://xxxxxx.slack.com/services/hooks/incoming-webhook?token=aseaUO"
$curl $param

或者

$param = ''
$param = "-X POST"
$param += "--data-urlencode 'payload={""channel"": ""#personal"", ""username"": ""Jenkins"", ""text"": ""Building ${env:BUILD_NUMBER}", ""icon_emoji"": "":computer:""}'"
$param += "https://xxxxxx.slack.com/services/hooks/incoming-webhook?token=aseaUO"
$curl $param
于 2014-09-27T18:56:11.260 回答
0

问题是整个表达式都用单引号括起来,因此 PowerShell 不会评估您的$env:BUILD_NUMBER表达式。

使用字符串格式化表达式。注意,您还必须使用格式将 { } 字符保留在基本表达式之外,以避免运行时错误。

 curl -X POST --data-urlencode $('payload={0}"channel": "#personal", "username": "Jenkins", "text": "Building {1}", "icon_emoji": ":computer:"{2}' -f "{", $env:BUILD_NUMBER) ,"}") https://xxxxxx.slack.com/services/hooks/incoming-webhook?token=aseaUO
于 2014-09-27T17:48:07.820 回答