Skip to content

Commit

Permalink
fix: Job trigger throws error (#4296)
Browse files Browse the repository at this point in the history
* check for isJob

* send docker registry of override if applicable

* check for Docker config overridden

* custom tag docker repo

* trim message size

* undo changes
  • Loading branch information
Ashish-devtron committed Nov 28, 2023
1 parent 4bb931d commit 6df0c24
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 53 deletions.
16 changes: 14 additions & 2 deletions pkg/pipeline/AppArtifactManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type AppArtifactManagerImpl struct {
cdPipelineConfigService CdPipelineConfigService
dockerArtifactRegistry dockerArtifactStoreRegistry.DockerArtifactStoreRepository
CiPipelineRepository pipelineConfig.CiPipelineRepository
ciTemplateService CiTemplateService
}

func NewAppArtifactManagerImpl(
Expand All @@ -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,
Expand All @@ -84,6 +86,7 @@ func NewAppArtifactManagerImpl(
pipelineStageService: pipelineStageService,
dockerArtifactRegistry: dockerArtifactRegistry,
CiPipelineRepository: CiPipelineRepository,
ciTemplateService: ciTemplateService,
}
}

Expand Down Expand Up @@ -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)
Expand Down
107 changes: 56 additions & 51 deletions pkg/pipeline/CiService.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 6df0c24

Please sign in to comment.