卷曲来自詹金斯的自定义松弛通知

curl custom slack notifications from jenkins

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

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

非常感谢任何意见。

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

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

试试这个:用于自定义松弛通知的负载和卷曲。 我在 Gitlab-CI.yml 文件中使用它向 Slack 发送作业工件和报告。它工作正常。 您可以修改下面的代码,因为我写的就像它在作业通过或失败时出现 Green/Red。

- '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'

我也会简单一点: 请注意:"content-type:" 和 "application/json" 之后不应该有任何 space 人会犯这个错误。 并注意反斜杠。您也可以在 terminal/cmd 上试试这个。我应该工作

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