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: switch ci fixes #5358

Merged
merged 11 commits into from
Jul 12, 2024
4 changes: 4 additions & 0 deletions pkg/bean/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ type CiPipeline struct {
EnableCustomTag bool `json:"enableCustomTag"`
}

func (ciPipeline *CiPipeline) IsLinkedCi() bool {
return ciPipeline.IsExternal
}

type DockerConfigOverride struct {
DockerRegistry string `json:"dockerRegistry,omitempty"`
DockerRepository string `json:"dockerRepository,omitempty"`
Expand Down
11 changes: 9 additions & 2 deletions pkg/pipeline/BuildPipelineSwitchService.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package pipeline

import (
"fmt"
"github.com/devtron-labs/devtron/internal/sql/repository/appWorkflow"
"github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig"
"github.com/devtron-labs/devtron/pkg/bean"
Expand Down Expand Up @@ -97,7 +98,7 @@ func (impl *BuildPipelineSwitchServiceImpl) SwitchToExternalCi(tx *pg.Tx, appWor
return err
}

//setting new ci_pipeline_id to 0 because we dont store ci_pipeline_id if the ci_pipeline is external/webhook type.
// setting new ci_pipeline_id to 0 because we dont store ci_pipeline_id if the ci_pipeline is external/webhook type.
err = impl.pipelineRepository.UpdateOldCiPipelineIdToNewCiPipelineId(tx, switchFromCiPipelineId, 0)
if err != nil {
impl.logger.Errorw("error in updating pipelines ci_pipeline_ids with new ci_pipelineId", "oldCiPipelineId", switchFromCiPipelineId)
Expand Down Expand Up @@ -130,11 +131,17 @@ func (impl *BuildPipelineSwitchServiceImpl) SwitchToCiPipelineExceptExternal(req
return nil, err
}

//delete old pipeline and it's appworkflow mapping
// delete old pipeline and it's appworkflow mapping
return impl.createNewPipelineAndReplaceOldPipelineLinks(request.CiPipeline, ciConfig, switchFromPipelineId, switchFromType, request.UserId)
}

func (impl *BuildPipelineSwitchServiceImpl) createNewPipelineAndReplaceOldPipelineLinks(ciPipelineReq *bean.CiPipeline, ciConfig *bean.CiConfigRequest, switchFromPipelineId int, switchFromType pipelineConfigBean.PipelineType, userId int32) (*bean.CiConfigRequest, error) {

isSelfLinkedCiPipeline := switchFromType != pipelineConfigBean.EXTERNAL && ciPipelineReq.IsLinkedCi() && ciPipelineReq.ParentCiPipeline == switchFromPipelineId
if isSelfLinkedCiPipeline {
return nil, fmt.Errorf("cannot create linked ci pipeline from the same source")
}

tx, err := impl.ciPipelineRepository.StartTx()
if err != nil {
impl.logger.Errorw("error in starting transaction", "switchFromPipelineId", switchFromPipelineId, "switchFromType", switchFromType, "userId", userId, "err", err)
Expand Down
28 changes: 20 additions & 8 deletions pkg/pipeline/DeploymentPipelineConfigService.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,18 +385,24 @@ func (impl *CdPipelineConfigServiceImpl) CreateCdPipelines(pipelineCreateRequest
}
pipeline.DeploymentAppType = overrideDeploymentType
}

err = impl.checkIfNsExistsForEnvIds(envIds)
if err != nil {
impl.logger.Errorw("error in checking existence of namespace for env's", "envIds", envIds, "err", err)
return nil, err
}

isGitOpsRequiredForCD := impl.IsGitOpsRequiredForCD(pipelineCreateRequest)
app, err := impl.appRepo.FindById(pipelineCreateRequest.AppId)

if err != nil {
impl.logger.Errorw("app not found", "err", err, "appId", pipelineCreateRequest.AppId)
return nil, err
}

_, err = impl.validateCDPipelineRequest(pipelineCreateRequest)
if err != nil {
impl.logger.Errorw("error in validating cd pipeline create request", "pipelineCreateRequest", pipelineCreateRequest, "err", err)
return nil, err
}

Expand Down Expand Up @@ -1765,18 +1771,20 @@ func (impl *CdPipelineConfigServiceImpl) createCdPipeline(ctx context.Context, a
}

}
}
// save custom tag data
err = impl.CDPipelineCustomTagDBOperations(pipeline)
if err != nil {
return pipelineId, err
}

if pipeline.IsDigestEnforcedForPipeline {
_, err = impl.imageDigestPolicyService.CreatePolicyForPipeline(tx, pipelineId, pipeline.Name, userId)
// save custom tag data
err = impl.CDPipelineCustomTagDBOperations(pipeline)
if err != nil {
return pipelineId, err
}

if pipeline.IsDigestEnforcedForPipeline {
_, err = impl.imageDigestPolicyService.CreatePolicyForPipeline(tx, pipelineId, pipeline.Name, userId)
if err != nil {
return pipelineId, err
}
}

}

err = tx.Commit()
Expand Down Expand Up @@ -2072,6 +2080,10 @@ func (impl *CdPipelineConfigServiceImpl) BulkDeleteCdPipelines(impactedPipelines

}
func (impl *CdPipelineConfigServiceImpl) checkIfNsExistsForEnvIds(envIds []*int) error {

if len(envIds) > 0 {
return nil
}
//fetching environments for the given environment Ids
environmentList, err := impl.environmentRepository.FindByIds(envIds)
if err != nil {
Expand Down
Loading