forked from fescobar/allure-docker-service
-
Notifications
You must be signed in to change notification settings - Fork 1
/
send_results_jenkins_pipeline.groovy
109 lines (95 loc) · 4.46 KB
/
send_results_jenkins_pipeline.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Required Jenkins plugins:
// https://plugins.jenkins.io/http_request/
// https://plugins.jenkins.io/pipeline-utility-steps/
// Documentation:
// https://jenkins.io/doc/pipeline/steps/pipeline-utility-steps/
// https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/
import groovy.json.JsonOutput;
class Result { String file_name; String content_base64 }
// This url is where the Allure container is deployed. We are using localhost as example
allure_server_url = 'http://localhost:5050'
// Project ID according to existent projects in your Allure container - Check endpoint for project creation >> `[POST]/projects`
project_id = 'default'
//project_id = 'my-project-id'
// This directory is where you have all your results, generally named as `allure-results`
// For the example we are using the results located in 'allure-docker-service/allure-docker-api-usage/allure-results-example'
// Finish the pattern just with 1 asterisk. On this way you avoid to include recursive directories and only you are including files from the first directory level.
pattern_allure_results_directory = '**/**/allure-results-example/*'
automation_repository = 'https://github.com/fescobar/allure-docker-service.git'
default_branch = 'master'
String build_allure_results_json(pattern) {
def results = []
def files = findFiles(glob: pattern)
files.each {
def b64_content = readFile file: "${it.path}", encoding: 'Base64'
if (!b64_content.trim().isEmpty()) {
results.add(new Result(file_name: "${it.name}", content_base64: b64_content))
} else {
print("Empty File skipped: ${it.path}")
}
}
JsonOutput.toJson(results: results)
}
Object send_results_to_allure_docker_service(allure_server_url, project_id, results_json) {
httpRequest url: "${allure_server_url}/allure-docker-service/send-results?project_id=${project_id}",
httpMode: 'POST',
contentType: 'APPLICATION_JSON',
requestBody: results_json,
consoleLogResponseBody: true,
validResponseCodes: '200'
}
Object generate_allure_report(allure_server_url, project_id, execution_name, execution_from, execution_type) {
execution_name = URLEncoder.encode(execution_name, 'UTF-8')
execution_from = URLEncoder.encode(execution_from, 'UTF-8')
execution_type = URLEncoder.encode(execution_type, 'UTF-8')
httpRequest url: "${allure_server_url}/allure-docker-service/generate-report?project_id=${project_id}&execution_name=${execution_name}&execution_from=${execution_from}&execution_type=${execution_type}",
httpMode: 'GET',
contentType: 'APPLICATION_JSON',
consoleLogResponseBody: true,
validResponseCodes: '200'
}
pipeline {
agent any
options {
disableConcurrentBuilds()
buildDiscarder(logRotator(numToKeepStr: '10'))
}
stages {
stage('Clone Project Testing Repository') {
steps {
cleanWs()
git(url: automation_repository, branch: default_branch)
}
}
stage('Run Tests') {
steps {
warnError('Unstable Tests') {
print('This stage should be use it to run tests generating allure-results directory')
}
}
}
stage('Post Results to Allure Docker Service Server') {
steps {
script {
def results_json = build_allure_results_json(pattern_allure_results_directory)
send_results_to_allure_docker_service(allure_server_url, project_id, results_json)
}
}
}
/*
stage('Generate Report in Allure Docker Service Server') {
steps {
script {
// If you want to generate reports on demand use the endpoint `GET /generate-report` and disable the Automatic Execution >> `CHECK_RESULTS_EVERY_SECONDS: NONE`
def execution_name = 'execution from my jenkins'
def execution_from = "$BUILD_URL"
def execution_type = 'jenkins'
def response = generate_allure_report(allure_server_url, project_id, execution_name, execution_from, execution_type)
def response_body = readJSON text: response.content
print "ALLURE REPORT URL: $response_body.data.report_url"
}
}
}
*/
}
}