Skip to content

Commit

Permalink
feat:流水线版本管理机制 TencentBlueKing#8161
Browse files Browse the repository at this point in the history
  • Loading branch information
royalhuang committed May 23, 2023
1 parent ad6ff3b commit 15fc3dd
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ package com.tencent.devops.process.engine.dao

import com.tencent.devops.common.api.util.JsonUtil
import com.tencent.devops.common.pipeline.Model
import com.tencent.devops.common.pipeline.container.TriggerContainer
import com.tencent.devops.model.process.Tables.T_PIPELINE_RESOURCE
import com.tencent.devops.model.process.tables.records.TPipelineResourceRecord
import com.tencent.devops.process.pojo.setting.PipelineModelVersion
import org.jooq.Condition
import org.jooq.DSLContext
Expand All @@ -50,29 +52,49 @@ class PipelineResDao {
pipelineId: String,
creator: String,
version: Int,
model: Model
versionName: String = "init",
model: Model,
trigger: TriggerContainer,
modelVersion: Int,
triggerVersion: Int,
settingVersion: Int
) {
logger.info("Create the pipeline model pipelineId=$pipelineId, version=$version")
with(T_PIPELINE_RESOURCE) {
val modelString = JsonUtil.toJson(model, formatted = false)
dslContext.insertInto(
this,
PROJECT_ID,
PIPELINE_ID,
VERSION,
MODEL,
CREATOR,
CREATE_TIME
)
.values(projectId, pipelineId, version, modelString, creator, LocalDateTime.now())
.onDuplicateKeyUpdate()
val triggerString = JsonUtil.toJson(trigger, formatted = false)
dslContext.insertInto(this)
.set(PROJECT_ID, projectId)
.set(PIPELINE_ID, pipelineId)
.set(VERSION, version)
.set(VERSION_NAME, versionName)
.set(MODEL, modelString)
.set(TRIGGER, triggerString)
.set(CREATOR, creator)
.set(CREATE_TIME, LocalDateTime.now())
.set(MODEL_VERSION, modelVersion)
.set(TRIGGER_VERSION, triggerVersion)
.set(SETTING_VERSION, settingVersion)
.onDuplicateKeyUpdate()
.set(MODEL, modelString)
.set(TRIGGER, triggerString)
.set(CREATOR, creator)
.set(VERSION_NAME, versionName)
.set(MODEL_VERSION, modelVersion)
.set(TRIGGER_VERSION, triggerVersion)
.set(SETTING_VERSION, settingVersion)
.execute()
}
}

fun getLatestVersionRecord(dslContext: DSLContext, projectId: String, pipelineId: String): TPipelineResourceRecord? {
return with(T_PIPELINE_RESOURCE) {
dslContext.selectFrom(this)
.where(PIPELINE_ID.eq(pipelineId).and(PROJECT_ID.eq(projectId)))
.fetchAny()
}
}

fun getLatestVersionModelString(dslContext: DSLContext, projectId: String, pipelineId: String) =
getVersionModelString(dslContext = dslContext, projectId = projectId, pipelineId = pipelineId, version = null)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ package com.tencent.devops.process.engine.dao
import com.tencent.devops.common.api.util.JsonUtil
import com.tencent.devops.common.api.util.timestampmilli
import com.tencent.devops.common.pipeline.Model
import com.tencent.devops.common.pipeline.container.TriggerContainer
import com.tencent.devops.model.process.Tables.T_PIPELINE_RESOURCE_VERSION
import com.tencent.devops.process.pojo.setting.PipelineVersionSimple
import org.jooq.DSLContext
Expand All @@ -47,18 +48,25 @@ class PipelineResVersionDao {
pipelineId: String,
creator: String,
version: Int,
versionName: String = "init",
model: Model
versionName: String,
model: Model,
trigger: TriggerContainer,
modelVersion: Int,
triggerVersion: Int,
settingVersion: Int
) {
val modelString = JsonUtil.toJson(model, formatted = false)
create(
dslContext = dslContext,
projectId = projectId,
pipelineId = pipelineId,
creator = creator,
version = version,
versionName = versionName,
modelString = modelString
modelString = JsonUtil.toJson(model, formatted = false),
triggerString = JsonUtil.toJson(trigger, formatted = false),
modelVersion = modelVersion,
triggerVersion = triggerVersion,
settingVersion = settingVersion
)
}

Expand All @@ -69,24 +77,33 @@ class PipelineResVersionDao {
creator: String,
version: Int,
versionName: String = "init",
modelString: String
modelString: String,
triggerString: String,
modelVersion: Int,
triggerVersion: Int,
settingVersion: Int
) {
with(T_PIPELINE_RESOURCE_VERSION) {
dslContext.insertInto(
this,
PROJECT_ID,
PIPELINE_ID,
VERSION,
VERSION_NAME,
MODEL,
CREATOR,
CREATE_TIME
).values(projectId, pipelineId, version, versionName, modelString, creator, LocalDateTime.now())
dslContext.insertInto(this)
.set(PROJECT_ID, projectId)
.set(PIPELINE_ID, pipelineId)
.set(VERSION, version)
.set(VERSION_NAME, versionName)
.set(MODEL, modelString)
.set(TRIGGER, triggerString)
.set(CREATOR, creator)
.set(CREATE_TIME, LocalDateTime.now())
.set(MODEL_VERSION, modelVersion)
.set(TRIGGER_VERSION, triggerVersion)
.set(SETTING_VERSION, settingVersion)
.onDuplicateKeyUpdate()
.set(MODEL, modelString)
.set(TRIGGER, triggerString)
.set(CREATOR, creator)
.set(VERSION_NAME, versionName)
.set(CREATE_TIME, LocalDateTime.now())
.set(MODEL_VERSION, modelVersion)
.set(TRIGGER_VERSION, triggerVersion)
.set(SETTING_VERSION, settingVersion)
.execute()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ class PipelineRepositoryService constructor(
private val redisOperation: RedisOperation
) {

companion object {
private const val MAX_LEN_FOR_NAME = 255
private val logger = LoggerFactory.getLogger(PipelineRepositoryService::class.java)
private const val PIPELINE_SETTING_VERSION_BIZ_TAG_NAME = "PIPELINE_SETTING_VERSION"
private fun getVersionName(modelVersion: Int, triggerVersion: Int, settingVersion: Int) =
"P$modelVersion.T$triggerVersion.$settingVersion"
}

fun deployPipeline(
model: Model,
projectId: String,
Expand Down Expand Up @@ -192,7 +200,8 @@ class PipelineRepositoryService constructor(
buildNo = buildNo,
modelTasks = modelTasks,
useTemplateSettings = useTemplateSettings,
templateId = templateId
templateId = templateId,
trigger = triggerContainer
)
}
}
Expand Down Expand Up @@ -466,6 +475,7 @@ class PipelineRepositoryService constructor(
projectId: String,
pipelineId: String,
model: Model,
trigger: TriggerContainer,
userId: String,
channelCode: ChannelCode,
canManualStartup: Boolean,
Expand Down Expand Up @@ -498,21 +508,33 @@ class PipelineRepositoryService constructor(
id = id
)
model.latestVersion = 1
// 最新版本永远是新增逻辑
pipelineResDao.create(
dslContext = transactionContext,
projectId = projectId,
pipelineId = pipelineId,
creator = userId,
version = 1,
model = model
model = model,
trigger = trigger,
versionName = getVersionName(1, 1, 1),
modelVersion = 1,
triggerVersion = 1,
settingVersion = 1
)
// 同步记录到历史版本表
pipelineResVersionDao.create(
dslContext = transactionContext,
projectId = projectId,
pipelineId = pipelineId,
creator = userId,
version = 1,
model = model
model = model,
trigger = trigger,
versionName = getVersionName(1, 1, 1),
modelVersion = 1,
triggerVersion = 1,
settingVersion = 1
)
if (model.instanceFromTemplate != true) {
if (null == pipelineSettingDao.getSetting(transactionContext, projectId, pipelineId)) {
Expand Down Expand Up @@ -616,6 +638,7 @@ class PipelineRepositoryService constructor(
updateLastModifyUser: Boolean? = true
): DeployPipelineResult {
val taskCount: Int = model.taskCount()
val triggerContainer = model.stages[0].containers[0] as TriggerContainer
var version = 0
val lock = PipelineModelLock(redisOperation, pipelineId)
try {
Expand Down Expand Up @@ -656,21 +679,26 @@ class PipelineRepositoryService constructor(
throw ErrorCodeException(errorCode = ProcessMessageCode.ERROR_PIPELINE_IS_NOT_THE_LATEST)
}
model.latestVersion = version
val latestResRecord = pipelineResDao.getLatestVersionRecord(
transactionContext, projectId, pipelineId
)
pipelineResDao.create(
dslContext = transactionContext,
projectId = projectId,
pipelineId = pipelineId,
creator = userId,
version = version,
model = model
model = model,
trigger = latestResRecord?.
)
pipelineResVersionDao.create(
dslContext = transactionContext,
projectId = projectId,
pipelineId = pipelineId,
creator = userId,
version = version,
model = model
model = model,
trigger = triggerContainer
)
if (version > 1 && pipelineResVersionDao.getVersionModelString(
dslContext = transactionContext,
Expand Down Expand Up @@ -1288,10 +1316,4 @@ class PipelineRepositoryService constructor(
maxConRunningQueueSize = maxConRunningQueueSize
)
}

companion object {
private const val MAX_LEN_FOR_NAME = 255
private val logger = LoggerFactory.getLogger(PipelineRepositoryService::class.java)
private const val PIPELINE_SETTING_VERSION_BIZ_TAG_NAME = "PIPELINE_SETTING_VERSION"
}
}

0 comments on commit 15fc3dd

Please sign in to comment.