Skip to content

Commit

Permalink
Merge pull request #6703 from royalhuang/issue_6646
Browse files Browse the repository at this point in the history
bug: 修复重试和任务认领失败的逻辑问题 #6646
  • Loading branch information
irwinsun authored May 9, 2022
2 parents 48ccf38 + 8afc0be commit 3550f81
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const val PIPELINE_BUILD_NUM = "BK_CI_BUILD_NUM" // "pipeline.build.num"
const val PIPELINE_BUILD_LAST_UPDATE = "BK_CI_BUILD_LAST_UPDATE" // "pipeline.build.last.update"
const val PIPELINE_BUILD_SVN_REVISION = "BK_CI_BUILD_SVN_REVISION" // "pipeline.build.svn.revision"
const val PIPELINE_BUILD_NUM_ALIAS = "BK_CI_BUILD_NUM_ALIAS"
const val PIPELINE_BUILD_URL = "BK_CI_BUILD_URL"

const val GIT_MR_NUMBER = "BK_CI_GIT_MR_NUMBER" // git_mr_number
const val GITHUB_PR_NUMBER = "BK_CI_GITHUB_PR_NUMBER" // github_pr_number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ object PipelineVarUtil {
"ci.review_reviewers" to BK_REPO_GIT_WEBHOOK_REVIEW_REVIEWERS,
"ci.note_comment" to PIPELINE_WEBHOOK_NOTE_COMMENT,
"ci.note_id" to PIPELINE_WEBHOOK_NOTE_ID,
"ci.action" to PIPELINE_GIT_ACTION
"ci.action" to PIPELINE_GIT_ACTION,
"ci.build_url" to PIPELINE_BUILD_URL
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class PipelineBuildExtServiceImpl @Autowired constructor(
): Map<String, String> {
return pipelineContextService.buildContext(
projectId = task.projectId,
pipelineId = task.pipelineId,
buildId = task.buildId,
stageId = task.stageId,
containerId = task.containerId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ import com.tencent.devops.common.pipeline.utils.PIPELINE_GIT_TIME_TRIGGER_KIND
import com.tencent.devops.process.bean.PipelineUrlBean
import com.tencent.devops.process.engine.control.ControlUtils
import com.tencent.devops.process.engine.service.PipelineBuildDetailService
import com.tencent.devops.process.utils.PIPELINE_BUILD_URL
import com.tencent.devops.process.utils.PIPELINE_START_TYPE
import com.tencent.devops.process.utils.PipelineVarUtil
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

@Suppress("ComplexMethod", "TooManyFunctions", "NestedBlockDepth", "LongParameterList")
@Suppress("ComplexMethod", "TooManyFunctions", "NestedBlockDepth", "LongParameterList", "ReturnCount")
@Service
class PipelineContextService @Autowired constructor(
private val pipelineBuildDetailService: PipelineBuildDetailService,
Expand All @@ -58,6 +59,7 @@ class PipelineContextService @Autowired constructor(

fun buildContext(
projectId: String,
pipelineId: String,
buildId: String,
stageId: String?,
containerId: String?,
Expand Down Expand Up @@ -113,7 +115,7 @@ class PipelineContextService @Autowired constructor(
)
}
}
buildCiContext(contextMap, variables)
buildCiContext(projectId, pipelineId, buildId, contextMap, variables)
} catch (ignore: Throwable) {
logger.warn("BKSystemErrorMonitor|buildContextFailed|", ignore)
}
Expand All @@ -124,7 +126,8 @@ class PipelineContextService @Autowired constructor(
fun buildFinishContext(
projectId: String,
pipelineId: String,
buildId: String
buildId: String,
variables: Map<String, String>
): Map<String, String> {
val modelDetail = pipelineBuildDetailService.get(projectId, buildId) ?: return emptyMap()
val contextMap = mutableMapOf<String, String>()
Expand Down Expand Up @@ -155,13 +158,7 @@ class PipelineContextService @Autowired constructor(
}
contextMap["ci.build_status"] = previousStageStatus.name
contextMap["ci.build_fail_tasknames"] = failTaskNameList.joinToString(",")
contextMap["ci.build_url"] = pipelineUrlBean.genBuildDetailUrl(
projectCode = projectId,
pipelineId = pipelineId,
buildId = buildId,
position = null,
stageId = null
)
buildCiContext(projectId, pipelineId, buildId, contextMap, variables)
} catch (ignore: Throwable) {
logger.warn("BKSystemErrorMonitor|buildContextToNoticeFailed|", ignore)
}
Expand All @@ -187,12 +184,22 @@ class PipelineContextService @Autowired constructor(
status == BuildStatus.SUCCEED.name || status == BuildStatus.CANCELED.name || status == BuildStatus.FAILED.name

private fun buildCiContext(
projectId: String,
pipelineId: String,
buildId: String,
varMap: MutableMap<String, String>,
buildVar: Map<String, String>
) {
// 生成当前流水线详情URL
varMap[PIPELINE_BUILD_URL] = pipelineUrlBean.genBuildDetailUrl(
projectCode = projectId,
pipelineId = pipelineId,
buildId = buildId,
position = null,
stageId = null
)
// 将流水线变量按预置映射关系做替换
PipelineVarUtil.fillContextVarMap(varMap, buildVar)

// 特殊处理触发类型以免定时触发无法记录
if (buildVar[PIPELINE_START_TYPE] == StartType.TIME_TRIGGER.name) {
varMap["ci.event"] = PIPELINE_GIT_TIME_TRIGGER_KIND
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class CheckConditionalSkipContainerCmd constructor(
} else {
val contextMap = pipelineContextService.buildContext(
projectId = container.projectId,
pipelineId = container.pipelineId,
buildId = container.buildId,
stageId = container.stageId,
containerId = container.containerId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ class StartActionTaskContainerCmd(
val contextMap: Map<String, String> by lazy {
pipelineContextService.buildContext(
projectId = containerContext.container.projectId,
pipelineId = containerContext.container.pipelineId,
buildId = containerContext.container.buildId,
stageId = containerContext.container.stageId,
containerId = containerContext.container.containerId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class CheckConditionalSkipStageCmd constructor(
val conditions = controlOption.customVariables ?: emptyList()
val contextMap = pipelineContextService.buildContext(
projectId = stage.projectId,
pipelineId = stage.pipelineId,
buildId = stage.buildId,
stageId = stage.stageId,
containerId = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class BuildVarResourceImpl @Autowired constructor(
val allContext = pipelineContextService.buildContext(
projectId = projectId,
buildId = buildId,
pipelineId = pipelineId,
stageId = null,
containerId = containerId,
taskId = taskId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,11 @@ class ServiceVarResourceImpl @Autowired constructor(
buildId: String,
contextName: String?
): Result<Map<String, String>> {
val buildVars = buildVariableService.getAllVariable(projectId, buildId)
val variables = buildVariableService.getAllVariable(projectId, buildId)
return if (contextName.isNullOrBlank()) {
val contextVar = pipelineContextService.getAllBuildContext(buildVars).toMutableMap()
Result(
contextVar.plus(pipelineContextService.buildFinishContext(projectId, pipelineId, buildId))
)
Result(pipelineContextService.buildFinishContext(projectId, pipelineId, buildId, variables))
} else {
val context = pipelineContextService.getBuildContext(buildVars, contextName)
val context = pipelineContextService.getBuildContext(variables, contextName)
if (context.isNullOrEmpty()) {
Result(emptyMap())
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,10 @@ class ModelCreate @Autowired constructor(
val varMap = pipelineParams.associate { param -> param.id to param.defaultValue.toString() }
EnvUtils.parseEnv(it, PipelineVarUtil.fillContextVarMap(varMap))
},
// Cancel-In-Progress入口先不放开给用户配置
concurrencyCancelInProgress = true,
// Cancel-In-Progress 配置group后默认为true
concurrencyCancelInProgress = yaml.concurrency?.cancelInProgress
?: yaml.concurrency?.group?.let { true }
?: false,
runLockType = when {
yaml.concurrency?.group != null -> PipelineRunLockType.SINGLE
else -> PipelineRunLockType.MULTIPLE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ const val WORKSPACE_CONTEXT = "ci.workspace"

const val CI_TOKEN_CONTEXT = "ci.token"

const val CI_BUILD_URL = "ci.build_url"

const val JOB_OS_CONTEXT = "job.os"

const val SLAVE_AGENT_START_FILE = "devops.slave.agent.start.file"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import com.tencent.devops.engine.api.pojo.HeartBeatInfo
import com.tencent.devops.process.pojo.BuildTask
import com.tencent.devops.process.pojo.BuildTaskResult
import com.tencent.devops.process.pojo.BuildVariables
import com.tencent.devops.worker.common.CI_BUILD_URL
import com.tencent.devops.worker.common.JOB_OS_CONTEXT
import com.tencent.devops.worker.common.api.ApiFactory
import com.tencent.devops.worker.common.api.engine.EngineBuildSDKApi
Expand Down Expand Up @@ -70,7 +69,6 @@ object EngineService {
// #5277 将Job上下文传入本次agent任务
val jobContext = buildApi.getJobContext().toMutableMap()
jobContext[JOB_OS_CONTEXT] = AgentEnv.getOS().name
jobContext[CI_BUILD_URL] = buildApi.getBuildDetailUrl().data ?: ""
return ret.copy(variables = ret.variables.plus(jobContext))
}

Expand Down

0 comments on commit 3550f81

Please sign in to comment.