Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Job trigger throws error #4296

Merged
merged 6 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading