-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
186 lines (166 loc) · 6.88 KB
/
Copy pathJenkinsfile
File metadata and controls
186 lines (166 loc) · 6.88 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// ============================================================
// PLAYWRIGHT TESTS - PRODUCTION PIPELINE
// ============================================================
pipeline {
agent {
docker {
image 'mcr.microsoft.com/playwright:v1.48.0-jammy'
args '-u root:root'
}
}
parameters {
choice(name: 'ENVIRONMENT', choices: ['dev', 'staging', 'production'], description: 'Target environment')
choice(name: 'BROWSER', choices: ['chromium', 'firefox', 'webkit', 'all'], description: 'Which browser to test')
booleanParam(name: 'HEADLESS', defaultValue: true, description: 'Run tests in headless mode')
// Leave blank to run ALL tests via playwright.config.ts testDir
string(name: 'TEST_PATTERN', defaultValue: '', description: 'Test file pattern (blank = all tests)')
}
environment {
OPENWEATHER_API_KEY = credentials('openweather-api-key')
WEATHER_BASE_URL = 'https://api.openweathermap.org'
REQRES_BASE_URL = 'https://reqres.in'
DOGS_BASE_URL = 'https://dog.ceo/api'
NODE_ENV = 'test'
CI = 'true'
TEST_ENV = "${params.ENVIRONMENT}"
}
options {
buildDiscarder(logRotator(
numToKeepStr: '30',
daysToKeepStr: '30',
artifactNumToKeepStr: '10'
))
timeout(time: 1, unit: 'HOURS')
disableConcurrentBuilds()
}
triggers {
pollSCM('H/15 * * * *')
cron('0 2 * * *')
}
stages {
// ─────────────────────────────
// INFO
// ─────────────────────────────
stage('Pipeline Info') {
steps {
script {
echo """
============================================
PLAYWRIGHT TEST PIPELINE
============================================
Branch: ${env.BRANCH_NAME ?: 'N/A'}
Environment: ${params.ENVIRONMENT}
Browser: ${params.BROWSER}
Headless: ${params.HEADLESS}
Test Pattern: ${params.TEST_PATTERN ?: '(all tests)'}
Build: #${BUILD_NUMBER}
============================================
"""
}
sh '''
echo "Commit: $GIT_COMMIT"
git log -1 --pretty=format:"%h - %an: %s"
'''
}
}
// ─────────────────────────────
// INSTALL
// ─────────────────────────────
stage('Install Dependencies') {
steps {
sh 'npm ci'
script {
if (params.BROWSER == 'all') {
sh 'npx playwright install --with-deps'
} else {
sh "npx playwright install --with-deps ${params.BROWSER}"
}
}
}
}
// ─────────────────────────────
// CODE QUALITY (SAFE)
// ─────────────────────────────
stage('Code Quality') {
steps {
echo 'Skipping lint & type checks (not configured)'
}
}
// ─────────────────────────────
// RUN TESTS (FIXED)
// ─────────────────────────────
stage('Run Tests') {
steps {
script {
// Empty string = no --project flag needed for 'all',
// or pass the chosen browser as a project filter
def browserArg = params.BROWSER == 'all' ? '' : "--project=${params.BROWSER}"
def headlessArg = params.HEADLESS ? '' : '--headed'
// Blank TEST_PATTERN → Playwright uses testDir from playwright.config.ts
// i.e. runs ALL 121 tests in ./tests/**
def testScope = params.TEST_PATTERN?.trim() ?: ''
sh 'mkdir -p test-results'
sh "npx playwright test ${browserArg} ${headlessArg} ${testScope}".trim()
}
}
}
// ─────────────────────────────
// SECURITY
// ─────────────────────────────
stage('Security Scan') {
when { branch 'main' }
steps {
sh 'npm audit --production || true'
}
}
// ─────────────────────────────
// REPORTS
// ─────────────────────────────
stage('Publish Reports') {
steps {
publishHTML([
reportDir: 'playwright-report',
reportFiles: 'index.html',
reportName: 'Playwright Test Report',
keepAll: true,
alwaysLinkToLastBuild: true,
allowMissing: true
])
junit testResults: 'test-results/*.xml', allowEmptyResults: false
archiveArtifacts artifacts: 'test-results/**/*', allowEmptyArchive: true
}
}
// ─────────────────────────────
// DEPLOY
// ─────────────────────────────
stage('Deploy') {
when {
allOf {
branch 'main'
expression { params.ENVIRONMENT == 'production' }
}
}
steps {
echo 'Deploying to production...'
sh './deploy.sh'
}
}
}
// ─────────────────────────────
// POST
// ─────────────────────────────
post {
success {
script {
def duration = currentBuild.durationString.replace(' and counting', '')
echo "Build #${BUILD_NUMBER} succeeded in ${duration}"
}
}
failure {
echo "Build #${BUILD_NUMBER} failed"
}
always {
deleteDir()
}
}
}