【问题标题】:curl custom slack notifications from jenkins来自 jenkins 的 curl 自定义松弛通知
【发布时间】:2018-04-13 17:57:00
【问题描述】:

我正在尝试使用 curl 向我的 Slack 频道发送自定义 Slack 通知。在我的有效载荷下方。使用 curl 将通知发布到 Slack 频道,以便团队成员能够看到 Verison、s3 链接,并且他们可以直接访问。任何建议或意见将不胜感激。

version=1.2.4
bundleversion=1.3.4.5
SLACK_MSG="Version=$version bundleversion=$bundleversion s3link:Random "

 curl -H "Content-type: application/json" -X POST --data-urlencode -d 
"payload='{
"username": "Kalyan",
"attachments": [
    {
        "color": "danger",
        "fields": [
            {
                "title": "Danger Event",
                "text": "$SLACK_MSG",
                "short": false
            }
        ]
    },
    {
        "color": "warning",
        "fields": [
            {
                "title": "Warning Event",
                "value": "This is a warning",
                "short": false
            }
        ]
    },
    {
        "color": "good",
        "fields": [
            {
                "title": "Good Event",
                "value": "This is good",
                "short": false
            }
        ]
    }
]
}'" https://hooks.slack.com/services/XXXXXX/XXXXXXX/XXXXXXXXXX 

 Below Jenkins Error 

 curl: (3) [globbing] nested brace in column 51
 curl: (3) Illegal characters found in URL    
curl: (6) Could not resolve host: bundleversion=1.3.4.5
curl: (3) Port number ended with 'R'
curl: (3) [globbing] unmatched close brace/bracket in column 52
curl: (3) Illegal characters found in URL
curl: (6) Could not resolve host: is
curl: (6) Could not resolve host: a
curl: (3) Illegal characters found in URL
curl: (6) Could not resolve host: is
curl: (3) [globbing] unmatched close brace/bracket in column 56

非常感谢任何输入。

【问题讨论】:

  • 我认为您的attachments 部分是错误的。请查看您的 "' 字符。

标签: json webhooks slack


【解决方案1】:

您的第一个 danger 附件有一个 field,其中指定了名为 text 的无效属性 - 将此属性更改为 value 以使其成为有效的 Slack field

您可以通过 Slack 消息格式页面对 Slack 消息负载进行故障排除,以查看它们是否有效:here's the corrected message payload

【讨论】:

  • 感谢您的回复。我已将文本值更改为值并对语法进行了更正。但我无法检索 SLACK_MSG 变量中的值并将其发布到松弛通道上。它只是打印“SLACK_MSG”。
  • 您可能需要在 Jenkins 内部执行某种变量替换以使其正常工作。
  • SLACK_MSG 变量包含值。你能多解释一点吗?非常感谢。
【解决方案2】:

我也会让它变得简单: 请注意:在“content-type:”和“application/json”之后不应该有空格的人会犯这个错误。 并注意反斜杠。您也可以在终端/cmd 上尝试此操作。我应该工作

curl -X POST -H "Content-type:application/json" --data '{\"text\":\"here_is_your_message\"}' YOUR_WEBHOOK_URL

【讨论】:

    【解决方案3】:

    试试这个:payload 和 curl 用于自定义 slack 通知。 我在 Gitlab-CI.yml 文件中使用它来向 Slack 发送作业工件和报告。它工作正常。 您可以修改下面的代码,因为我写的代码就像作业通过或失败时显示为绿色/红色一样。

    - 'curl -H "Content-Type:application/json" -X POST --data "{
        \"attachments\": [
            {
                \"mrkdwn_in\": [\"text\"],
                \"color\": \"#36a64f\",
                \"author_name\": \"<https://$Gitlab_Home_URL/${GITLAB_USER_LOGIN}|${GITLAB_USER_NAME}>($GITLAB_USER_LOGIN)\",
                \"text\": \"*Job <https://$REPO_URL/-/jobs/${CI_JOB_ID}|TESTING> was SUCCESSFUL in pipeline <https://$REPO_URL/pipelines/${CI_PIPELINE_ID}|${CI_PIPELINE_ID}>*\",
                \"fields\": [
                    {
                        \"title\": \"Trigger source\",
                        \"value\": \"$CI_PIPELINE_SOURCE\",
                         \"short\": true
                    },
                    {
                        \"title\": \"Branch\",
                        \"value\": \"<https://$REPO_URL/tree/$CI_COMMIT_REF_NAME|$CI_COMMIT_REF_NAME>\",
                        \"short\": true
                    },
                    {
                        \"title\": \"Commit message\",
                        \"value\": \"<https://$REPO_URL/commit/${CI_COMMIT_SHA}|$CI_COMMIT_TITLE>\",
                        \"short\": true
                    }
                ],
                \"footer\": \"<https://$REPO_URL|$CI_PROJECT_NAME>\",
                \"footer_icon\": \"https://www.stickpng.com/assets/images/5847f997cef1014c0b5e48c1.png\",
            }
        ]
    }" YOUR_SLACK_WEBHOOK'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多