Skip to content

Commit

Permalink
feat: stage支持质量红线的准入准出 TencentBlueKing#4732
Browse files Browse the repository at this point in the history
  • Loading branch information
royalhuang committed Aug 20, 2021
1 parent df064e3 commit d8d30db
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,4 @@ data class PipelineBuildStage(
val controlOption: PipelineBuildStageControlOption?,
var checkIn: StagePauseCheck? = null,
var checkOut: StagePauseCheck? = null
) {

/**
* 判断是否已经被质量红线拦截,仅用于 finally 前序 stage 判断,其他地方请勿使用
*/
fun checkQualityFailed(): Boolean {
return if (checkIn?.status == BuildStatus.QUALITY_CHECK_FAIL.name ||
checkOut?.status == BuildStatus.QUALITY_CHECK_FAIL.name) {
// #4732 如果判断是已拦截的状态,则忽略原状态,将 stage 状态刷新为 QUALITY_CHECK_FAIL
status = BuildStatus.QUALITY_CHECK_FAIL
true
} else false
}
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -260,24 +260,6 @@ class PipelineBuildStageDao {
}
}

fun updateOptions(
dslContext: DSLContext,
buildId: String,
stageId: String,
controlOption: PipelineBuildStageControlOption?,
checkIn: StagePauseCheck? = null,
checkOut: StagePauseCheck? = null
): Int {
return with(T_PIPELINE_BUILD_STAGE) {
val option = if (controlOption != null) JsonUtil.toJson(controlOption) else null
val update = dslContext.update(this)
.set(CONDITIONS, option)
if (checkIn != null) update.set(CHECK_IN, JsonUtil.toJson(checkIn))
if (checkOut != null) update.set(CHECK_OUT, JsonUtil.toJson(checkOut))
update.where(BUILD_ID.eq(buildId)).and(STAGE_ID.eq(stageId)).execute()
}
}

fun update(
dslContext: DSLContext,
buildId: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,6 @@ class PipelineStageService @Autowired constructor(
)
}

fun updateStageOptions(
buildId: String,
stageId: String,
stage: PipelineBuildStage
) {
logger.info("[$buildId]|updateStageOptions|stageId=$stageId|controlOption=${stage.controlOption}" +
"|checkIn=${stage.checkIn}|checkOut=${stage.checkOut}")
pipelineBuildStageDao.updateOptions(
dslContext = dslContext, buildId = buildId,
stageId = stageId, controlOption = stage.controlOption,
checkIn = stage.checkIn, checkOut = stage.checkOut
)
}

fun listStages(buildId: String): List<PipelineBuildStage> {
val list = pipelineBuildStageDao.listByBuildId(dslContext, buildId)
val result = mutableListOf<PipelineBuildStage>()
Expand Down Expand Up @@ -170,9 +156,10 @@ class PipelineStageService @Autowired constructor(
)
dslContext.transaction { configuration ->
val context = DSL.using(configuration)
pipelineBuildStageDao.updateOptions(
pipelineBuildStageDao.updateStatus(
dslContext = context, buildId = buildId,
stageId = stageId, controlOption = controlOption!!,
buildStatus = BuildStatus.QUALITY_CHECK_FAIL,
checkIn = checkIn, checkOut = checkOut
)
pipelineBuildDao.updateBuildStageStatus(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,6 @@ class StartContainerStageCmd(
if (stageStatus.isFinish() || stageStatus == BuildStatus.STAGE_SUCCESS) {
commandContext.buildStatus = stageStatus // 已经是结束或者是STAGE_SUCCESS就直接返回
} else {
// 查找最后一个结束状态的Stage (排除Finally)
if (commandContext.stage.controlOption?.finally == true) {
commandContext.previousStageStatus = fetchPreviousStageStatus(commandContext)
}

stageStatus = pickJob(commandContext, actionType = newActionType, userId = event.userId)

if (commandContext.skipContainerNum == commandContext.containers.size) {
Expand Down Expand Up @@ -137,6 +132,14 @@ class StartContainerStageCmd(
var fail: BuildStatus? = null
var cancel: BuildStatus? = null

// 查找最后一个结束状态的Stage (排除Finally)
if (commandContext.stage.controlOption?.finally == true) {
commandContext.previousStageStatus = pipelineStageService.listStages(commandContext.stage.buildId)
.lastOrNull {
it.stageId != commandContext.stage.stageId &&
(it.status.isFinish() || it.status == BuildStatus.STAGE_SUCCESS)
}?.status
}
// 同一Stage下的多个Container是并行
commandContext.containers.forEach { container ->
if (container.status.isCancel()) {
Expand All @@ -163,19 +166,6 @@ class StartContainerStageCmd(
return running ?: fail ?: cancel ?: BuildStatus.SUCCEED
}

/**
* 获取前序已完成的stage状态
* 如果前序stageStatus是完成状态或[BuildStatus.STAGE_SUCCESS]则直接取用
* 如果前序stage是因为质量红线准入准出而被直接终止,则取用[BuildStatus.QUALITY_CHECK_FAIL]
*/
private fun fetchPreviousStageStatus(commandContext: StageContext): BuildStatus? {
return pipelineStageService.listStages(commandContext.stage.buildId)
.lastOrNull {
it.stageId != commandContext.stage.stageId &&
(it.status.isFinish() || it.status == BuildStatus.STAGE_SUCCESS || it.checkQualityFailed())
}?.status
}

private fun sendBuildContainerEvent(
commandContext: StageContext,
container: PipelineBuildContainer,
Expand Down

0 comments on commit d8d30db

Please sign in to comment.