Skip to content

Commit 5eb6ee5

Browse files
authored
Merge pull request #6836 from devtron-labs/ci-artifact-linked-copy
feat: linked ci should have artifacts of parent ci
2 parents 4bb5045 + 0c4d09c commit 5eb6ee5

File tree

6 files changed

+97
-3
lines changed

6 files changed

+97
-3
lines changed

env_gen.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

env_gen.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@
187187
| ECR_REPO_NAME_PREFIX | string |test/ | Prefix for ECR repo to be created in does not exist | | false |
188188
| ENABLE_ASYNC_ARGO_CD_INSTALL_DEVTRON_CHART | bool |false | To enable async installation of gitops application | | false |
189189
| ENABLE_ASYNC_INSTALL_DEVTRON_CHART | bool |false | To enable async installation of no-gitops application | | false |
190+
| ENABLE_LINKED_CI_ARTIFACT_COPY | bool |false | Enable copying artifacts from parent CI pipeline to linked CI pipeline during creation | | false |
190191
| EPHEMERAL_SERVER_VERSION_REGEX | string |v[1-9]\.\b(2[3-9]\|[3-9][0-9])\b.* | ephemeral containers support version regex that is compared with k8sServerVersion | | false |
191192
| EVENT_URL | string |http://localhost:3000/notify | Notifier service url | | false |
192193
| EXECUTE_WIRE_NIL_CHECKER | bool |false | checks for any nil pointer in wire.go | | false |
@@ -225,6 +226,7 @@
225226
| LENS_URL | string |http://lens-milandevtron-service:80 | Lens micro-service URL | | false |
226227
| LIMIT_CI_CPU | string |0.5 | | | false |
227228
| LIMIT_CI_MEM | string |3G | | | false |
229+
| LINKED_CI_ARTIFACT_COPY_LIMIT | int |10 | Maximum number of artifacts to copy from parent CI pipeline to linked CI pipeline | | false |
228230
| LOGGER_DEV_MODE | bool |false | Enables a different logger theme. | | false |
229231
| LOG_LEVEL | int |-1 | | | false |
230232
| MAX_SESSION_PER_USER | int |5 | max no of cluster terminal pods can be created by an user | | false |

internal/sql/repository/CiArtifactRepository.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ type CiArtifactRepository interface {
124124
GetArtifactsByCDPipelineAndRunnerType(cdPipelineId int, runnerType bean.WorkflowType) ([]CiArtifact, error)
125125
SaveAll(artifacts []*CiArtifact) ([]*CiArtifact, error)
126126
GetArtifactsByCiPipelineId(ciPipelineId int) ([]CiArtifact, error)
127+
GetLatestArtifactsByCiPipelineId(ciPipelineId, limit int) ([]CiArtifact, error)
127128
GetArtifactsByCiPipelineIds(ciPipelineIds []int) ([]CiArtifact, error)
128129
FinDByParentCiArtifactAndCiId(parentCiArtifact int, ciPipelineIds []int) ([]*CiArtifact, error)
129130
GetLatest(cdPipelineId int) (int, error)
@@ -680,6 +681,19 @@ func (impl CiArtifactRepositoryImpl) GetArtifactsByCiPipelineId(ciPipelineId int
680681
return artifacts, err
681682
}
682683

684+
func (impl CiArtifactRepositoryImpl) GetLatestArtifactsByCiPipelineId(ciPipelineId, limit int) ([]CiArtifact, error) {
685+
var artifacts []CiArtifact
686+
err := impl.dbConnection.
687+
Model(&artifacts).
688+
Column("ci_artifact.*").
689+
Join("INNER JOIN ci_pipeline cp on cp.id=ci_artifact.pipeline_id").
690+
Where("ci_artifact.pipeline_id = ?", ciPipelineId).
691+
Where("cp.deleted = ?", false).
692+
Order("ci_artifact.id DESC").Limit(limit).
693+
Select()
694+
return artifacts, err
695+
}
696+
683697
func (impl CiArtifactRepositoryImpl) GetArtifactsByCiPipelineIds(ciPipelineIds []int) ([]CiArtifact, error) {
684698
var artifacts []CiArtifact
685699
if len(ciPipelineIds) == 0 {

pkg/pipeline/CiCdPipelineOrchestrator.go

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ type CiCdPipelineOrchestratorImpl struct {
154154
deploymentConfigReadService read.DeploymentConfigReadService
155155
workflowCacheConfig types.WorkflowCacheConfig
156156
chartReadService read2.ChartReadService
157+
globalEnvVariables *util2.GlobalEnvVariables
157158
}
158159

159160
func NewCiCdPipelineOrchestrator(
@@ -185,7 +186,8 @@ func NewCiCdPipelineOrchestrator(
185186
gitOpsConfigReadService config.GitOpsConfigReadService,
186187
deploymentConfigService common.DeploymentConfigService,
187188
deploymentConfigReadService read.DeploymentConfigReadService,
188-
chartReadService read2.ChartReadService) *CiCdPipelineOrchestratorImpl {
189+
chartReadService read2.ChartReadService,
190+
envVariables *util2.EnvironmentVariables) *CiCdPipelineOrchestratorImpl {
189191
_, workflowCacheConfig, err := types.GetCiConfigWithWorkflowCacheConfig()
190192
if err != nil {
191193
logger.Errorw("Error in getting workflow cache config, continuing with default values", "err", err)
@@ -223,6 +225,7 @@ func NewCiCdPipelineOrchestrator(
223225
deploymentConfigReadService: deploymentConfigReadService,
224226
workflowCacheConfig: workflowCacheConfig,
225227
chartReadService: chartReadService,
228+
globalEnvVariables: envVariables.GlobalEnvVariables,
226229
}
227230
}
228231

@@ -1160,6 +1163,19 @@ func (impl CiCdPipelineOrchestratorImpl) CreateCiConf(createRequest *bean.CiConf
11601163
r.GitMaterialName = ciMaterial.GitMaterial.Name[strings.Index(ciMaterial.GitMaterial.Name, "-")+1:]
11611164
}
11621165
}
1166+
1167+
// Copy artifacts from parent CI pipeline if this is a linked CI pipeline
1168+
if ciPipeline.ParentCiPipeline > 0 && ciPipeline.PipelineType == buildCommonBean.LINKED {
1169+
go func() {
1170+
err = impl.copyArtifactsFromParentCiPipeline(ciPipeline.ParentCiPipeline, ciPipeline.Id, createRequest.UserId)
1171+
if err != nil {
1172+
impl.logger.Errorw("error in copying artifacts from parent CI pipeline",
1173+
"parentCiPipelineId", ciPipeline.ParentCiPipeline,
1174+
"childCiPipelineId", ciPipeline.Id,
1175+
"err", err)
1176+
}
1177+
}()
1178+
}
11631179
}
11641180
return createRequest, nil
11651181
}
@@ -2511,3 +2527,63 @@ func (impl *CiCdPipelineOrchestratorImpl) GetWorkflowCacheConfig(appType helper.
25112527
}
25122528
}
25132529
}
2530+
2531+
// copyArtifactsFromParentCiPipeline copies existing artifacts from parent CI pipeline to linked CI pipeline during pipeline creation
2532+
func (impl *CiCdPipelineOrchestratorImpl) copyArtifactsFromParentCiPipeline(parentCiPipelineId, childCiPipelineId int, userId int32) error {
2533+
if !impl.globalEnvVariables.EnableLinkedCiArtifactCopy {
2534+
impl.logger.Debugw("Linked CI artifact copying is disabled", "parentCiPipelineId", parentCiPipelineId, "childCiPipelineId", childCiPipelineId)
2535+
return nil
2536+
}
2537+
limit := impl.globalEnvVariables.LinkedCiArtifactCopyLimit
2538+
if limit <= 0 {
2539+
return nil
2540+
}
2541+
2542+
impl.logger.Infow("Starting, copyArtifactsFromParentCiPipeline", "parentCiPipelineId", parentCiPipelineId,
2543+
"childCiPipelineId", childCiPipelineId, "limit", limit)
2544+
2545+
parentArtifacts, err := impl.CiArtifactRepository.GetLatestArtifactsByCiPipelineId(parentCiPipelineId, limit)
2546+
if err != nil && !util.IsErrNoRows(err) {
2547+
impl.logger.Errorw("error in fetching artifacts from parent CI pipeline", "parentCiPipelineId", parentCiPipelineId, "err", err)
2548+
return err
2549+
}
2550+
2551+
if len(parentArtifacts) == 0 {
2552+
impl.logger.Infow("No artifacts found in parent CI pipeline", "parentCiPipelineId", parentCiPipelineId)
2553+
return nil
2554+
}
2555+
2556+
var childArtifacts []*repository.CiArtifact
2557+
for i := len(parentArtifacts); i > 0; i-- {
2558+
parentArtifact := parentArtifacts[i-1] //reversing order into new array because postgres will save the array as it is thus causing our actual order to reverse
2559+
childArtifact := &repository.CiArtifact{
2560+
Image: parentArtifact.Image,
2561+
ImageDigest: parentArtifact.ImageDigest,
2562+
MaterialInfo: parentArtifact.MaterialInfo,
2563+
DataSource: parentArtifact.DataSource,
2564+
PipelineId: childCiPipelineId,
2565+
ParentCiArtifact: parentArtifact.Id,
2566+
IsArtifactUploaded: parentArtifact.IsArtifactUploaded, // for backward compatibility
2567+
ScanEnabled: parentArtifact.ScanEnabled,
2568+
TargetPlatforms: parentArtifact.TargetPlatforms,
2569+
AuditLog: sql.AuditLog{
2570+
CreatedOn: time.Now(),
2571+
CreatedBy: userId,
2572+
UpdatedOn: time.Now(),
2573+
UpdatedBy: userId,
2574+
},
2575+
}
2576+
if parentArtifact.ScanEnabled {
2577+
childArtifact.Scanned = parentArtifact.Scanned
2578+
}
2579+
childArtifacts = append(childArtifacts, childArtifact)
2580+
}
2581+
if len(childArtifacts) > 0 {
2582+
_, err = impl.CiArtifactRepository.SaveAll(childArtifacts)
2583+
if err != nil {
2584+
impl.logger.Errorw("error in saving copied artifacts for child ci pipeline", "childCiPipelineId", childCiPipelineId, "err", err)
2585+
return err
2586+
}
2587+
}
2588+
return nil
2589+
}

util/GlobalConfig.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ type GlobalEnvVariables struct {
6565
ExposeCiMetrics bool `env:"EXPOSE_CI_METRICS" envDefault:"false" description:"To expose CI metrics"`
6666
ExecuteWireNilChecker bool `env:"EXECUTE_WIRE_NIL_CHECKER" envDefault:"false" description:"checks for any nil pointer in wire.go"`
6767
IsAirGapEnvironment bool `json:"isAirGapEnvironment" env:"IS_AIR_GAP_ENVIRONMENT" envDefault:"false"`
68+
EnableLinkedCiArtifactCopy bool `env:"ENABLE_LINKED_CI_ARTIFACT_COPY" envDefault:"false" description:"Enable copying artifacts from parent CI pipeline to linked CI pipeline during creation"`
69+
LinkedCiArtifactCopyLimit int `env:"LINKED_CI_ARTIFACT_COPY_LIMIT" envDefault:"10" description:"Maximum number of artifacts to copy from parent CI pipeline to linked CI pipeline"`
6870
}
6971

7072
type GlobalClusterConfig struct {

wire_gen.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)