From 6df0c249beed5823dbb84c7a316ab5b3cb449629 Mon Sep 17 00:00:00 2001 From: Ashish-devtron <123808329+Ashish-devtron@users.noreply.github.com> Date: Tue, 28 Nov 2023 19:53:09 +0530 Subject: [PATCH] fix: Job trigger throws error (#4296) * check for isJob * send docker registry of override if applicable * check for Docker config overridden * custom tag docker repo * trim message size * undo changes --- pkg/pipeline/AppArtifactManager.go | 16 ++++- pkg/pipeline/CiService.go | 107 +++++++++++++++-------------- 2 files changed, 70 insertions(+), 53 deletions(-) diff --git a/pkg/pipeline/AppArtifactManager.go b/pkg/pipeline/AppArtifactManager.go index 39835ec00ed..b709eb721f4 100644 --- a/pkg/pipeline/AppArtifactManager.go +++ b/pkg/pipeline/AppArtifactManager.go @@ -59,6 +59,7 @@ type AppArtifactManagerImpl struct { cdPipelineConfigService CdPipelineConfigService dockerArtifactRegistry dockerArtifactStoreRegistry.DockerArtifactStoreRepository CiPipelineRepository pipelineConfig.CiPipelineRepository + ciTemplateService CiTemplateService } func NewAppArtifactManagerImpl( @@ -71,7 +72,8 @@ func NewAppArtifactManagerImpl( pipelineStageService PipelineStageService, cdPipelineConfigService CdPipelineConfigService, dockerArtifactRegistry dockerArtifactStoreRegistry.DockerArtifactStoreRepository, - CiPipelineRepository pipelineConfig.CiPipelineRepository) *AppArtifactManagerImpl { + CiPipelineRepository pipelineConfig.CiPipelineRepository, + ciTemplateService CiTemplateService) *AppArtifactManagerImpl { return &AppArtifactManagerImpl{ logger: logger, @@ -84,6 +86,7 @@ func NewAppArtifactManagerImpl( pipelineStageService: pipelineStageService, dockerArtifactRegistry: dockerArtifactRegistry, CiPipelineRepository: CiPipelineRepository, + ciTemplateService: ciTemplateService, } } @@ -679,7 +682,16 @@ func (impl *AppArtifactManagerImpl) setAdditionalDataInArtifacts(ciArtifacts []b impl.logger.Errorw("error in fetching ciPipeline", "ciPipelineId", ciPipeline.Id, "error", err) return nil, err } - dockerRegistryId = *ciPipeline.CiTemplate.DockerRegistryId + if !ciPipeline.IsExternal && ciPipeline.IsDockerConfigOverridden { + ciTemplateBean, err := impl.ciTemplateService.FindTemplateOverrideByCiPipelineId(ciPipeline.Id) + if err != nil { + impl.logger.Errorw("error in fetching template override", "pipelineId", ciPipeline.Id, "err", err) + return nil, err + } + dockerRegistryId = ciTemplateBean.CiTemplateOverride.DockerRegistryId + } else { + dockerRegistryId = *ciPipeline.CiTemplate.DockerRegistryId + } } if len(dockerRegistryId) > 0 { dockerArtifact, err := impl.dockerArtifactRegistry.FindOne(dockerRegistryId) diff --git a/pkg/pipeline/CiService.go b/pkg/pipeline/CiService.go index ebb068ef16a..26355d735fd 100644 --- a/pkg/pipeline/CiService.go +++ b/pkg/pipeline/CiService.go @@ -213,7 +213,7 @@ func (impl *CiServiceImpl) TriggerCiPipeline(trigger types.Trigger) (int, error) return 0, err } - workflowRequest, err := impl.buildWfRequestForCiPipeline(pipeline, trigger, ciMaterials, savedCiWf, ciWorkflowConfig, ciPipelineScripts, preCiSteps, postCiSteps, refPluginsData) + workflowRequest, err := impl.buildWfRequestForCiPipeline(pipeline, trigger, ciMaterials, savedCiWf, ciWorkflowConfig, ciPipelineScripts, preCiSteps, postCiSteps, refPluginsData, isJob) if err != nil { impl.Logger.Errorw("make workflow req", "err", err) return 0, err @@ -388,10 +388,7 @@ func (impl *CiServiceImpl) buildDefaultArtifactLocation(ciWorkflowConfig *pipeli return ArtifactLocation } -func (impl *CiServiceImpl) buildWfRequestForCiPipeline(pipeline *pipelineConfig.CiPipeline, trigger types.Trigger, - ciMaterials []*pipelineConfig.CiPipelineMaterial, savedWf *pipelineConfig.CiWorkflow, - ciWorkflowConfig *pipelineConfig.CiWorkflowConfig, ciPipelineScripts []*pipelineConfig.CiPipelineScript, - preCiSteps []*bean2.StepObject, postCiSteps []*bean2.StepObject, refPluginsData []*bean2.RefPluginObject) (*types.WorkflowRequest, error) { +func (impl *CiServiceImpl) buildWfRequestForCiPipeline(pipeline *pipelineConfig.CiPipeline, trigger types.Trigger, ciMaterials []*pipelineConfig.CiPipelineMaterial, savedWf *pipelineConfig.CiWorkflow, ciWorkflowConfig *pipelineConfig.CiWorkflowConfig, ciPipelineScripts []*pipelineConfig.CiPipelineScript, preCiSteps []*bean2.StepObject, postCiSteps []*bean2.StepObject, refPluginsData []*bean2.RefPluginObject, isJob bool) (*types.WorkflowRequest, error) { var ciProjectDetails []bean2.CiProjectDetails commitHashes := trigger.CommitHashes for _, ciMaterial := range ciMaterials { @@ -460,52 +457,6 @@ func (impl *CiServiceImpl) buildWfRequestForCiPipeline(pipeline *pipelineConfig. refPluginsData = []*bean2.RefPluginObject{} } - var dockerImageTag string - customTag, err := impl.customTagService.GetActiveCustomTagByEntityKeyAndValue(bean2.EntityTypeCiPipelineId, strconv.Itoa(pipeline.Id)) - if err != nil && err != pg.ErrNoRows { - return nil, err - } - if customTag.Id != 0 && customTag.Enabled == true { - imagePathReservation, err := impl.customTagService.GenerateImagePath(bean2.EntityTypeCiPipelineId, strconv.Itoa(pipeline.Id), pipeline.CiTemplate.DockerRegistry.RegistryURL, pipeline.CiTemplate.DockerRepository) - if err != nil { - if errors.Is(err, bean2.ErrImagePathInUse) { - savedWf.Status = pipelineConfig.WorkflowFailed - savedWf.Message = bean2.ImageTagUnavailableMessage - err1 := impl.ciWorkflowRepository.UpdateWorkFlow(savedWf) - if err1 != nil { - impl.Logger.Errorw("could not save workflow, after failing due to conflicting image tag") - } - return nil, err - } - return nil, err - } - savedWf.ImagePathReservationIds = []int{imagePathReservation.Id} - //imagePath = docker.io/avd0/dashboard:fd23414b - imagePathSplit := strings.Split(imagePathReservation.ImagePath, ":") - if len(imagePathSplit) >= 1 { - dockerImageTag = imagePathSplit[len(imagePathSplit)-1] - } - } else { - dockerImageTag = impl.buildImageTag(commitHashes, pipeline.Id, savedWf.Id) - } - - // copyContainerImage plugin specific logic - registryDestinationImageMap, registryCredentialMap, pluginArtifactStage, imageReservationIds, err := impl.GetWorkflowRequestVariablesForCopyContainerImagePlugin( - preCiSteps, postCiSteps, dockerImageTag, customTag.Id, - fmt.Sprintf(bean2.ImagePathPattern, pipeline.CiTemplate.DockerRegistry.RegistryURL, pipeline.CiTemplate.DockerRepository, dockerImageTag), pipeline.CiTemplate.DockerRegistry.Id) - if err != nil { - impl.Logger.Errorw("error in getting env variables for copyContainerImage plugin") - savedWf.Status = pipelineConfig.WorkflowFailed - savedWf.Message = err.Error() - err1 := impl.ciWorkflowRepository.UpdateWorkFlow(savedWf) - if err1 != nil { - impl.Logger.Errorw("could not save workflow, after failing due to conflicting image tag") - } - return nil, err - } - - savedWf.ImagePathReservationIds = append(savedWf.ImagePathReservationIds, imageReservationIds...) - if ciWorkflowConfig.CiCacheBucket == "" { ciWorkflowConfig.CiCacheBucket = impl.config.DefaultCacheBucket } @@ -570,6 +521,60 @@ func (impl *CiServiceImpl) buildWfRequestForCiPipeline(pipeline *pipelineConfig. if checkoutPath == "" { checkoutPath = "./" } + var dockerImageTag string + customTag, err := impl.customTagService.GetActiveCustomTagByEntityKeyAndValue(bean2.EntityTypeCiPipelineId, strconv.Itoa(pipeline.Id)) + if err != nil && err != pg.ErrNoRows { + return nil, err + } + if customTag.Id != 0 && customTag.Enabled == true { + imagePathReservation, err := impl.customTagService.GenerateImagePath(bean2.EntityTypeCiPipelineId, strconv.Itoa(pipeline.Id), dockerRegistry.RegistryURL, dockerRepository) + if err != nil { + if errors.Is(err, bean2.ErrImagePathInUse) { + savedWf.Status = pipelineConfig.WorkflowFailed + savedWf.Message = bean2.ImageTagUnavailableMessage + err1 := impl.ciWorkflowRepository.UpdateWorkFlow(savedWf) + if err1 != nil { + impl.Logger.Errorw("could not save workflow, after failing due to conflicting image tag") + } + return nil, err + } + return nil, err + } + savedWf.ImagePathReservationIds = []int{imagePathReservation.Id} + //imagePath = docker.io/avd0/dashboard:fd23414b + imagePathSplit := strings.Split(imagePathReservation.ImagePath, ":") + if len(imagePathSplit) >= 1 { + dockerImageTag = imagePathSplit[len(imagePathSplit)-1] + } + } else { + dockerImageTag = impl.buildImageTag(commitHashes, pipeline.Id, savedWf.Id) + } + + // copyContainerImage plugin specific logic + var registryDestinationImageMap map[string][]string + var registryCredentialMap map[string]plugin.RegistryCredentials + var pluginArtifactStage string + var imageReservationIds []int + if !isJob { + registryDestinationImageMap, registryCredentialMap, pluginArtifactStage, imageReservationIds, err = impl.GetWorkflowRequestVariablesForCopyContainerImagePlugin(preCiSteps, postCiSteps, dockerImageTag, customTag.Id, + fmt.Sprintf(bean2.ImagePathPattern, + dockerRegistry.RegistryURL, + dockerRepository, + dockerImageTag), + dockerRegistry.Id) + if err != nil { + impl.Logger.Errorw("error in getting env variables for copyContainerImage plugin") + savedWf.Status = pipelineConfig.WorkflowFailed + savedWf.Message = err.Error() + err1 := impl.ciWorkflowRepository.UpdateWorkFlow(savedWf) + if err1 != nil { + impl.Logger.Errorw("could not save workflow, after failing due to conflicting image tag") + } + return nil, err + } + + savedWf.ImagePathReservationIds = append(savedWf.ImagePathReservationIds, imageReservationIds...) + } //mergedArgs := string(merged) oldArgs := ciTemplate.Args ciBuildConfigBean, err = bean2.OverrideCiBuildConfig(dockerfilePath, oldArgs, ciLevelArgs, ciTemplate.DockerBuildOptions, ciTemplate.TargetPlatform, ciBuildConfigBean)