Skip to content

Commit

Permalink
feat:流水线版本管理机制 TencentBlueKing#8161 默认补充job id
Browse files Browse the repository at this point in the history
  • Loading branch information
royalhuang committed May 31, 2024
1 parent 404f2cf commit 8c07ce1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

package com.tencent.devops.process.engine.common

import com.tencent.devops.common.api.util.timestampmilli
import java.time.LocalDateTime
import kotlin.random.Random

/**
Expand Down Expand Up @@ -62,14 +64,19 @@ object VMUtils {

fun getEndLabel() = "end-"

fun getContainerJobId(containerId: Int): String {
val random = Random(containerId)
fun getContainerJobId(randomSeed: Int, jobIdSet: MutableSet<String>): String {
val random = Random(randomSeed)
val sequence = StringBuilder()
for (i in 0 until 3) {
val randomChar = ('A'..'z').random(random)
sequence.append(randomChar)
}
return "job_$sequence"
val jobId = "job_$sequence"
return if (jobIdSet.contains(jobId)) {
"${jobId}_${LocalDateTime.now().timestampmilli()}"
} else {
jobId
}
}

fun isVMTask(taskId: String) = taskId.startsWith(getStartVmLabel()) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ class VMUtilsTest {
fun testGetRandomJobId() {
val jobIdSet = mutableSetOf<String>()
for (i in 0 until 1000) {
jobIdSet.add(VMUtils.getContainerJobId(i))
val newId = VMUtils.getContainerJobId(i, jobIdSet)
jobIdSet.add(newId)
}
assertEquals(1000, jobIdSet.size)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1277,13 +1277,15 @@ class PipelineRepositoryService constructor(
// 1 返回时将别名name补全为id
// 2 填充所有job没有的job id
// 3 所有插件ENV配置合并历史值,并过滤掉默认值
var randomSeq = 1
var randomSeed = 1
val jobIdSet = mutableSetOf<String>()
resource?.model?.stages?.forEachIndexed { index, s ->
if (index == 0) (s.containers[0] as TriggerContainer).params.forEach { param ->
param.name = param.name ?: param.id
} else {
s.containers.forEach { c ->
if (c.jobId.isNullOrBlank()) c.jobId = VMUtils.getContainerJobId(randomSeq++)
if (c.jobId.isNullOrBlank()) c.jobId = VMUtils.getContainerJobId(randomSeed++, jobIdSet)
c.jobId?.let { jobIdSet.add(it) }
c.elements.forEach { e ->
// 保存时将旧customEnv赋值给新的上一级customEnv
val oldCustomEnv = e.additionalOptions?.customEnv?.filter {
Expand Down

0 comments on commit 8c07ce1

Please sign in to comment.