Skip to content

Commit

Permalink
Merge pull request TencentBlueKing#10836 from yongyiduan/pac_0819
Browse files Browse the repository at this point in the history
feat:流水线版本管理机制 TencentBlueKing#8161 优化
  • Loading branch information
bkci-bot authored Aug 19, 2024
2 parents ef52b5f + 82ec4f7 commit 3ee852f
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ class StageTransfer @Autowired(required = false) constructor(
}
}
return PreStage(
enable = stage.isStageEnable().nullIfDefault(true),
name = stage.name,
label = maskYamlStageLabel(stage.tag).ifEmpty { null },
ifField = when (stage.stageControlOption?.runCondition) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ import com.tencent.devops.common.api.util.PageUtil
import com.tencent.devops.common.client.Client
import com.tencent.devops.common.pipeline.Model
import com.tencent.devops.common.pipeline.enums.ChannelCode
import com.tencent.devops.common.pipeline.pojo.PipelineModelAndSetting
import com.tencent.devops.common.redis.RedisLock
import com.tencent.devops.common.redis.RedisOperation
import com.tencent.devops.model.stream.tables.records.TGitPipelineResourceRecord
import com.tencent.devops.process.api.service.ServicePipelineResource
import com.tencent.devops.common.pipeline.pojo.PipelineModelAndSetting
import com.tencent.devops.process.yaml.v2.utils.ScriptYmlUtils
import com.tencent.devops.project.api.service.ServiceProjectResource
import com.tencent.devops.stream.config.StreamGitConfig
import com.tencent.devops.stream.dao.GitPipelineResourceDao
import com.tencent.devops.stream.dao.GitRequestEventBuildDao
Expand Down Expand Up @@ -391,6 +392,13 @@ class StreamPipelineService @Autowired constructor(
gitProjectId = gitProjectId.toLong(),
scmType = gitConfig.getScmType()
)
val pipelineAsCodeSettings = try {
client.get(ServiceProjectResource::class).get(gitProjectCode)
.data?.properties?.pipelineAsCodeSettings
} catch (ignore: Throwable) {
logger.warn("StreamYamlTrigger get project[$gitProjectCode] as code settings error.", ignore)
null
}
val realPipeline: StreamTriggerPipeline
// 避免出现多个触发拿到空的pipelineId后依次进来创建,所以需要在锁后重新获取pipeline
triggerLock.use {
Expand All @@ -404,7 +412,10 @@ class StreamPipelineService @Autowired constructor(
userId = userId,
gitProjectId = gitProjectId.toLong(),
projectCode = gitProjectCode,
modelAndSetting = StreamPipelineUtils.createEmptyPipelineAndSetting(realPipeline.displayName),
modelAndSetting = StreamPipelineUtils.createEmptyPipelineAndSetting(
realPipeline.displayName,
pipelineAsCodeSettings
),
updateLastModifyUser = true,
branch = branch,
// 空model计算md5没有意义,直接传空
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import com.tencent.devops.common.event.dispatcher.pipeline.PipelineEventDispatch
import com.tencent.devops.common.pipeline.Model
import com.tencent.devops.common.pipeline.enums.ChannelCode
import com.tencent.devops.common.pipeline.pojo.BuildParameters
import com.tencent.devops.common.pipeline.pojo.PipelineModelAndSetting
import com.tencent.devops.common.redis.RedisOperation
import com.tencent.devops.common.web.utils.I18nUtil
import com.tencent.devops.model.stream.tables.records.TGitPipelineResourceRecord
Expand All @@ -45,7 +46,6 @@ import com.tencent.devops.process.pojo.BuildId
import com.tencent.devops.process.pojo.BuildTemplateAcrossInfo
import com.tencent.devops.process.pojo.TemplateAcrossInfoType
import com.tencent.devops.process.pojo.code.PipelineBuildCommit
import com.tencent.devops.common.pipeline.pojo.PipelineModelAndSetting
import com.tencent.devops.process.pojo.webhook.WebhookTriggerParams
import com.tencent.devops.process.utils.PIPELINE_NAME
import com.tencent.devops.process.yaml.v2.enums.TemplateType
Expand Down Expand Up @@ -262,7 +262,10 @@ class StreamYamlBaseBuild @Autowired constructor(
userId = pipeline.creator ?: "",
gitProjectId = gitProjectId.toLong(),
projectCode = projectCode,
modelAndSetting = StreamPipelineUtils.createEmptyPipelineAndSetting(pipeline.displayName),
modelAndSetting = StreamPipelineUtils.createEmptyPipelineAndSetting(
pipeline.displayName,
action.data.context.pipelineAsCodeSettings
),
updateLastModifyUser = true
)
streamPipelineBranchService.saveOrUpdate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,10 @@ class StreamYamlBuild @Autowired constructor(
userId = action.data.getUserId(),
gitProjectId = action.data.eventCommon.gitProjectId.toLong(),
projectCode = action.getProjectCode(),
modelAndSetting = StreamPipelineUtils.createEmptyPipelineAndSetting(realPipeline.displayName),
modelAndSetting = StreamPipelineUtils.createEmptyPipelineAndSetting(
realPipeline.displayName,
action.data.context.pipelineAsCodeSettings
),
updateLastModifyUser = true
)
}
Expand Down Expand Up @@ -235,6 +238,7 @@ class StreamYamlBuild @Autowired constructor(
TriggerReason.PIPELINE_PREPARE_ERROR
)
}

is QualityRulesException -> {
Triple(
false,
Expand All @@ -246,6 +250,7 @@ class StreamYamlBuild @Autowired constructor(
is StreamTriggerBaseException, is ErrorCodeException -> {
throw e
}

else -> {
logger.warn("StreamYamlBuild|gitStartBuild|${action.data.context.requestEventId}|error", e)
Triple(false, e.message, TriggerReason.UNKNOWN_ERROR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,24 @@ class StreamYamlTrigger @Autowired constructor(
)
var pipeline = action.data.context.pipeline!!

// 获取蓝盾流水线的pipelineAsCodeSetting
val projectCode = GitCommonUtils.getCiProjectId(pipeline.gitProjectId.toLong(), streamGitConfig.getScmType())
action.data.context.pipelineAsCodeSettings = try {
if (pipeline.pipelineId.isNotBlank()) {
client.get(ServicePipelineSettingResource::class).getPipelineSetting(
projectId = projectCode,
pipelineId = pipeline.pipelineId,
channelCode = ChannelCode.GIT
).data?.pipelineAsCodeSettings
} else {
client.get(ServiceProjectResource::class).get(projectCode)
.data?.properties?.pipelineAsCodeSettings
}
} catch (ignore: Throwable) {
logger.warn("StreamYamlTrigger get project[$projectCode] as code settings error.", ignore)
null
}

// 提前创建新流水线,保证git提交后 stream上能看到
if (pipeline.pipelineId.isBlank()) {
pipeline = StreamTriggerPipeline(
Expand All @@ -225,24 +243,6 @@ class StreamYamlTrigger @Autowired constructor(
)
}

// 获取蓝盾流水线的pipelineAsCodeSetting
val projectCode = GitCommonUtils.getCiProjectId(pipeline.gitProjectId.toLong(), streamGitConfig.getScmType())
action.data.context.pipelineAsCodeSettings = try {
if (pipeline.pipelineId.isNotBlank()) {
client.get(ServicePipelineSettingResource::class).getPipelineSetting(
projectId = projectCode,
pipelineId = pipeline.pipelineId,
channelCode = ChannelCode.GIT
).data?.pipelineAsCodeSettings
} else {
client.get(ServiceProjectResource::class).get(projectCode)
.data?.properties?.pipelineAsCodeSettings
}
} catch (ignore: Throwable) {
logger.warn("StreamYamlTrigger get project[$projectCode] as code settings error.", ignore)
null
}

// 拼接插件时会需要传入GIT仓库信息需要提前刷新下状态,只有url或者名称不对才更新
val gitProjectInfo = action.api.getGitProjectInfo(
action.getGitCred(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ import com.tencent.devops.common.api.pojo.PipelineAsCodeSettings
import com.tencent.devops.common.pipeline.Model
import com.tencent.devops.common.pipeline.container.Stage
import com.tencent.devops.common.pipeline.container.TriggerContainer
import com.tencent.devops.common.pipeline.pojo.PipelineModelAndSetting
import com.tencent.devops.common.pipeline.pojo.element.trigger.ManualTriggerElement
import com.tencent.devops.common.pipeline.pojo.setting.PipelineSetting
import com.tencent.devops.common.web.utils.I18nUtil
import com.tencent.devops.process.engine.common.VMUtils
import com.tencent.devops.common.pipeline.pojo.PipelineModelAndSetting
import com.tencent.devops.common.pipeline.pojo.setting.PipelineSetting

@Suppress("LongParameterList", "ReturnCount")
object StreamPipelineUtils {
Expand Down Expand Up @@ -66,38 +66,39 @@ object StreamPipelineUtils {
messageId: String
) = "$streamUrl/notifications?id=$messageId#$gitProjectId"

fun createEmptyPipelineAndSetting(displayName: String) = PipelineModelAndSetting(
model = Model(
name = displayName,
desc = "",
stages = listOf(
Stage(
id = VMUtils.genStageId(1),
name = VMUtils.genStageId(1),
containers = listOf(
TriggerContainer(
id = "0",
name = I18nUtil.getCodeLanMessage(
messageCode = BK_BUILD_TRIGGER,
language = I18nUtil.getDefaultLocaleLanguage()
),
elements = listOf(
ManualTriggerElement(
name = I18nUtil.getCodeLanMessage(
messageCode = BK_MANUAL_TRIGGER,
language = I18nUtil.getDefaultLocaleLanguage()
),
id = "T-1-1-1"
fun createEmptyPipelineAndSetting(displayName: String, pipelineAsCodeSettings: PipelineAsCodeSettings?) =
PipelineModelAndSetting(
model = Model(
name = displayName,
desc = "",
stages = listOf(
Stage(
id = VMUtils.genStageId(1),
name = VMUtils.genStageId(1),
containers = listOf(
TriggerContainer(
id = "0",
name = I18nUtil.getCodeLanMessage(
messageCode = BK_BUILD_TRIGGER,
language = I18nUtil.getDefaultLocaleLanguage()
),
elements = listOf(
ManualTriggerElement(
name = I18nUtil.getCodeLanMessage(
messageCode = BK_MANUAL_TRIGGER,
language = I18nUtil.getDefaultLocaleLanguage()
),
id = "T-1-1-1"
)
)
)
)
)
)
),
setting = PipelineSetting(
cleanVariablesWhenRetry = true,
pipelineAsCodeSettings = pipelineAsCodeSettings ?: PipelineAsCodeSettings()
)
),
setting = PipelineSetting(
cleanVariablesWhenRetry = true,
pipelineAsCodeSettings = PipelineAsCodeSettings()
)
)
}

0 comments on commit 3ee852f

Please sign in to comment.