Вебхук принимает данные из Jenkins: различия между версиями
Материал из Planfix
Artem (обсуждение | вклад) Нет описания правки |
Artem (обсуждение | вклад) Нет описания правки |
||
| Строка 31: | Строка 31: | ||
} | } | ||
</pre> | </pre> | ||
<pre> | <pre> | ||
| Строка 58: | Строка 59: | ||
</pre> | </pre> | ||
Код, который вставляем после завершения всех операций: | |||
<pre> | |||
def deployedTasks = getChangesByTaskNumber() | |||
deployedTasks.eachWithIndex { key, value, i -> | |||
httpRequest contentType: "APPLICATION_JSON_UTF8", httpMode: "POST", requestBody: "{ \"task\" : ${key}, \"comment\" : \"${value.replaceAll('"','\\\\"')}\", \"project\" : \"project-name\", \"branch\" : \"${env.gitlabBranch}\"}", responseHandle: "NONE", url: "https://myacc.planfix.ru/webhook/json/b7mh-dead-687f-lpwq", consoleLogResponseBody: true | |||
} | |||
</pre> | |||
== Перейти == | == Перейти == | ||
*[[Входящие вебхуки]] | *[[Входящие вебхуки]] | ||
Версия от 13:47, 16 апреля 2021
Вебхуки ПланФикса умеют принимать данные из Jenkins. Это реализовано следующим образом:
Есть Jenkins, который делает build & deploy, настроенные через Jenkins Pipeline. Нам необходимо по завершению действия deploy получать в соответствующих задачах ПланФикса комментарий об этом.
Пошаговый план реализации:
- Коммиты отправляем в таком виде: 123456 - текст коммита, где 123456 - номер задачи в ПланФиксе.
- Создаём вебхук, на который отправляем данные.
- В Jenkins Pipeline допишем код, который будет слать данные на вебхук.
Настройка вебхука
Код в Jenkins
Две вспомогательные функции:
@NonCPS
def getTaskNumber(text) {
def m = (text =~ /\d+/);
def taskNumber = m ? m[0] : "";
return taskNumber
}
@NonCPS
def getChangesByTaskNumber() {
def changes = [:]
def maxMessages = 50;
def changeLogSets = currentBuild.changeSets
for (int i = 0; i < changeLogSets.size(); i++) {
def entries = changeLogSets[i].items
for (int j = 0; j < entries.length && i + j < maxMessages; j++) {
def entry = entries[j]
def taskNumber = getTaskNumber(entry.msg);
if(taskNumber != "") {
if(!changes.containsKey(taskNumber)) {
changes[taskNumber] = "";
}
changes[taskNumber] += entry.msg + "<br>"
}
}
}
return changes
}
Код, который вставляем после завершения всех операций:
def deployedTasks = getChangesByTaskNumber()
deployedTasks.eachWithIndex { key, value, i ->
httpRequest contentType: "APPLICATION_JSON_UTF8", httpMode: "POST", requestBody: "{ \"task\" : ${key}, \"comment\" : \"${value.replaceAll('"','\\\\"')}\", \"project\" : \"project-name\", \"branch\" : \"${env.gitlabBranch}\"}", responseHandle: "NONE", url: "https://myacc.planfix.ru/webhook/json/b7mh-dead-687f-lpwq", consoleLogResponseBody: true
}