-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
154 lines (144 loc) · 5.21 KB
/
Jenkinsfile
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
pipeline {
agent any
parameters {
string(name: 'CI_BUILD_ID', defaultValue: 'none', description: 'Set this value if you want to execute only the failed tests from a specific run')
booleanParam(name: 'IS_ORCHESTRATION', defaultValue: false, description: 'Set this value if you want to execute an orchestrated run')
}
environment {
CURRENTS_PROJECT_ID = credentials('CURRENTS_PROJECT_ID')
CURRENTS_RECORD_KEY = credentials('CURRENTS_RECORD_KEY')
CURRENTS_CI_BUILD_ID = "reporter-${JOB_NAME}-${BUILD_ID}-${BUILD_NUMBER}"
CURRENTS_API_KEY = credentials('CURRENTS_API_KEY')
TOTAL_SHARDS = 3
PARALLEL_JOBS = 4
}
options {
timeout(time: 60, unit: 'MINUTES')
}
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Install Dependencies') {
steps {
sh 'npm ci'
sh 'npx playwright install'
sh 'rm -rf test-results'
sh 'rm -rf .last-run.json'
}
}
stage('Set params CI Build ID') {
steps {
script {
env.CI_BUILD_ID = "${params.CI_BUILD_ID}"
echo "CI_BUILD_ID is set to: ${params.CI_BUILD_ID}"
}
echo "Verify values: ${env.CI_BUILD_ID} ${params.IS_ORCHESTRATION}"
}
}
stage('Run Tests decision') {
steps {
runTestsDecision(env.CI_BUILD_ID, params.IS_ORCHESTRATION)
}
}
}
}
def runTestsDecision(ciBuildId, isOrchestration) {
if (ciBuildId && ciBuildId != 'none') {
stage('Run Tests with last failed') {
script {
echo "Running tests with last failed: ${ciBuildId} ${env.TOTAL_SHARDS}"
script {
sh "npx currents api get-run --api-key ${env.CURRENTS_API_KEY} --project-id ${env.CURRENTS_PROJECT_ID} --ci-build-id ${env.CI_BUILD_ID} --pw-last-run --output .last-run.json"
sh 'cat .last-run.json'
}
if (isOrchestration && isOrchestration == true) {
runPlaywrightOrchestration(env.PARALLEL_JOBS.toInteger(), true)
} else {
runPlaywrightSharded(env.TOTAL_SHARDS.toInteger(), true)
}
}
}
} else {
stage('Run Tests') {
script {
echo 'Running tests'
if (isOrchestration && isOrchestration == true) {
runPlaywrightOrchestration(env.PARALLEL_JOBS.toInteger(), false)
} else {
runPlaywrightSharded(env.TOTAL_SHARDS.toInteger(), false)
}
}
}
}
}
def runPlaywrightSharded(shardTotal, lastFailed) {
def parallelStages = [:]
for (int i = 1; i <= shardTotal; i++) {
def shardIndex = i
parallelStages["shard${shardIndex}"] = {
if (lastFailed) {
sh "mkdir -p test-results/shard-${shardIndex}"
sh "cp .last-run.json test-results/shard-${shardIndex}/.last-run.json"
runPlaywrightTestsLastFailed(shardIndex, shardTotal)
} else {
runPlaywrightTests(shardIndex, shardTotal)
}
}
}
parallel parallelStages
}
def runPlaywrightTests(shardIndex, shardTotal) {
stage("Run Playwright Tests - Shard ${shardIndex}") {
script {
def command = "npx pwc --shard=${shardIndex}/${shardTotal}"
echo "Running command: ${command}"
sh "${command}"
}
}
}
def runPlaywrightTestsLastFailed(shardIndex, shardTotal) {
stage("Run Playwright Tests - Shard ${shardIndex}") {
script {
def command = "npx pwc --shard=${shardIndex}/${shardTotal} --last-failed --output test-results/shard-${shardIndex}"
echo "Running command: ${command}"
sh "${command}"
}
}
}
def runPlaywrightOrchestration(parallelTotal, lastFailed) {
def parallelStages = [:]
for (int i = 1; i <= parallelTotal; i++) {
def parallelIndex = i
parallelStages["parallel${parallelIndex}"] = {
if (lastFailed) {
sh "mkdir -p test-results/parallel-${parallelIndex}"
sh "cp .last-run.json test-results/parallel-${parallelIndex}/.last-run.json"
runPlaywrightTestsLastFailedOrchestration(parallelIndex)
} else {
runPlaywrightTestsOrchestration(parallelIndex)
}
}
}
parallel parallelStages
}
def runPlaywrightTestsOrchestration(parallelIndex) {
stage("Run Playwright Tests - Orchestration ${parallelIndex}") {
script {
def command = 'npx pwc-p'
echo "Running command: ${command}"
sh "${command}"
}
}
}
def runPlaywrightTestsLastFailedOrchestration(parallelIndex) {
stage("Run Playwright Tests - Orchestration ${parallelIndex}") {
script {
def command = "npx pwc-p --last-failed --output test-results/parallel-${parallelIndex}"
echo "Running command: ${command}"
sh "${command}"
}
}
}