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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
| pipeline { agent none options { timeout(time: 2, unit: 'HOURS') buildDiscarder(logRotator(numToKeepStr: '10')) disableConcurrentBuilds() }
environment { ARTIFACTORY = 'https://artifactory.example.com' VERSION = sh(script: 'git describe --tags --always', returnStdout: true).trim() BUILD_DATE = new Date().format('yyyyMMdd-HHmmss') }
parameters { choice(name: 'DEPLOY_ENV', choices: ['dev', 'staging', 'prod'], description: 'Select deployment environment') booleanParam(name: 'RUN_INTEGRATION_TESTS', defaultValue: true, description: 'Run integration tests') string(name: 'CUSTOM_TAG', defaultValue: '', description: 'Optional custom tag for deployment') }
stages { stage('Initialize') { agent any steps { script { currentBuild.displayName = "#${BUILD_NUMBER}-${VERSION}-${params.DEPLOY_ENV}" echo "Build Version: ${VERSION}" echo "Build Date: ${BUILD_DATE}" echo "Selected Environment: ${params.DEPLOY_ENV}" def buildMatrix = [:] buildMatrix['Linux'] = { buildForPlatform('linux') } buildMatrix['Windows'] = { buildForPlatform('windows') } buildMatrix['Mac'] = { buildForPlatform('mac') } env.BUILD_MATRIX = writeJSON returnText: true, json: buildMatrix.keySet() as List } } }
stage('Parallel Build & Test') { parallel { stage('Build Multiplatform') { stages { stage('Build') { steps { script { def builds = [:] readJSON(text: env.BUILD_MATRIX).each { platform -> builds["Build ${platform}"] = { node(platform.toLowerCase()) { checkout scm buildForPlatform(platform.toLowerCase()) } } } parallel builds } } }
stage('Unit Tests') { steps { script { def tests = [:] readJSON(text: env.BUILD_MATRIX).each { platform -> tests["Test ${platform}"] = { node(platform.toLowerCase()) { runUnitTests(platform.toLowerCase()) } } } parallel tests } } } } }
stage('Integration Tests') { when { expression { params.RUN_INTEGRATION_TESTS } } agent { label 'integration' } stages { stage('Prepare Environment') { steps { sh 'docker-compose up -d' sleep time: 3, unit: 'MINUTES' } }
stage('Run Tests') { parallel { stage('API Tests') { steps { runIntegrationTests('api') } } stage('UI Tests') { steps { runIntegrationTests('ui') } } stage('Load Tests') { steps { runLoadTests() } } } }
stage('Cleanup') { steps { sh 'docker-compose down' } } } }
stage('Static Analysis') { agent { label 'analysis' } steps { parallel { stage('Code Quality') { steps { runSonarQubeAnalysis() } } stage('Security Scan') { steps { runDependencyCheck() } } } } } } }
stage('Package & Archive') { agent any steps { script { def packages = [:] readJSON(text: env.BUILD_MATRIX).each { platform -> packages["Package ${platform}"] = { node(platform.toLowerCase()) { packageArtifacts(platform.toLowerCase()) } } } parallel packages } archiveArtifacts artifacts: '**/target/*.zip,**/target/*.tar.gz', fingerprint: true stash name: 'deployment-artifacts', includes: 'deploy/**' } }
stage('Approval') { when { expression { params.DEPLOY_ENV in ['staging', 'prod'] } } steps { script { def approvers = getApproversForEnvironment(params.DEPLOY_ENV) input message: "Approve deployment to ${params.DEPLOY_ENV}?", ok: 'Deploy', submitter: approvers.join(',') } } }
stage('Deploy') { parallel { stage('Deploy to Infrastructure') { agent { label params.DEPLOY_ENV } steps { unstash 'deployment-artifacts' script { deployToEnvironment(params.DEPLOY_ENV) } } }
stage('Update CDN') { when { expression { params.DEPLOY_ENV == 'prod' } } agent { label 'cdn' } steps { updateCDN() } }
stage('Notify') { steps { script { notifyDeploymentStatus() } } } } } }
post { always { script { cleanWs() notifyBuildCompletion(currentBuild.result) } } success { updateBuildStatus('SUCCESS') } failure { updateBuildStatus('FAILURE') } unstable { updateBuildStatus('UNSTABLE') } } }
def buildForPlatform(platform) { echo "Building for ${platform}" switch(platform) { case 'linux': sh './gradlew build -Pplatform=linux' break case 'windows': bat 'gradlew build -Pplatform=windows' break case 'mac': sh './gradlew build -Pplatform=mac' break default: error "Unsupported platform: ${platform}" } }
def runUnitTests(platform) { echo "Running unit tests on ${platform}" }
def runIntegrationTests(type) { echo "Running ${type} integration tests" }
def runLoadTests() { echo "Running load tests" }
def packageArtifacts(platform) { echo "Packaging artifacts for ${platform}" }
def deployToEnvironment(env) { echo "Deploying to ${env} environment" }
def notifyDeploymentStatus() { echo "Notifying deployment status" }
def getApproversForEnvironment(env) { return env == 'prod' ? ['prod-admins@example.com'] : ['qa-team@example.com'] }
def updateBuildStatus(status) { echo "Updating external systems with build status: ${status}" }
def notifyBuildCompletion(result) { echo "Build completed with status: ${result}" }
|