您身边的移动安全专家
提供安全检测、安全加密、安全监测等一站式的移动安全服务
免费咨询首页 / 新闻资讯 / DevSecOps流水线中的程序加固卡点,自动化加固与发布节...
// ========== Build + 编译级加固 ========== stage('Build & Harden') { steps { script { // 仅生产分支启用全量加固 if (env.BRANCH_NAME == 'main') { sh './gradlew build -PfullHardening=true' } else { sh './gradlew build -PfastHardening=true' // 快速模式 } } } } // ========== 安全门禁(分级执行) ========== stage('Security Gate') { steps { script { // SCA依赖扫描 + 门禁判断 def scanResult = dependencyCheck( failBuildOnCVSS: (env.GATE_LEVEL == 'strict') ? '7' : '9', suppressionFile: 'security/suppressions.xml', cveValidForHours: 24 ) // 调用分级决策函数 def gateAction = evaluateGate(scanResult, env.GATE_LEVEL) if (gateAction == 'block') { error("安全门禁阻断:存在不可忽略的高危漏洞") } else if (gateAction == 'warn') { createJiraIssue(scanResult.criticalFindings) slackSend(color: 'warning', message: "⚠️ 发现${scanResult.criticalFindings.size()}个高危漏洞,已创建工单") } } } } // ========== 镜像扫描(异步执行,不阻塞) ========== stage('Image Scan Async') { steps { script { // 异步扫描,不阻塞后续发布 def scanJob = build job: 'Trivy-Scan-Pipeline', parameters: [string(name: 'IMAGE_TAG', value: env.IMAGE_TAG)], wait: false echo "镜像扫描已提交,任务ID: ${scanJob.id}" } } }}post { // 失败时输出可操作的修复建议 failure { script { if (currentBuild.result == 'FAILURE') { sh ''' # 从扫描结果中提取可操作的修复命令 cat sast-result.json | jq '.results[0].fix_advice' ''' } } }}}

// 分级决策函数def evaluateGate(scanResult, gateLevel) { def criticalExploitable = scanResult.findings.findAll { it.cvssScore >= 7.0 && it.exploitExists == true }
if (gateLevel == 'strict') { return criticalExploitable.size() > 0 ? 'block' : 'pass'} else { // warning模式:仅在生产环境阻断,测试环境只警告 if (env.DEPLOY_ENV == 'prod' && criticalExploitable.size() > 0) { return 'block' } return criticalExploitable.size() > 0 ? 'warn' : 'pass'}}