Skip to content

Commit

Permalink
fix(contrib): artifactory promote always update artifacts (#6415)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardlt authored Jan 11, 2023
1 parent 8a4dc7c commit b246692
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
39 changes: 23 additions & 16 deletions contrib/integrations/artifactory/artifactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func CreateArtifactoryClient(ctx context.Context, url, token string) (artifactor
return artifactory.New(serviceConfig)
}

func PromoteFile(artiClient artifact_manager.ArtifactManager, data sdk.WorkflowRunResultArtifactManager, lowMaturity, highMaturity string, props *utils.Properties) error {
func PromoteFile(artiClient artifact_manager.ArtifactManager, data sdk.WorkflowRunResultArtifactManager, lowMaturity, highMaturity string, props *utils.Properties, skipExistingArtifacts bool) error {
srcRepo := fmt.Sprintf("%s-%s", data.RepoName, lowMaturity)
targetRepo := fmt.Sprintf("%s-%s", data.RepoName, highMaturity)
params := services.NewMoveCopyParams()
Expand All @@ -81,12 +81,15 @@ func PromoteFile(artiClient artifact_manager.ArtifactManager, data sdk.WorkflowR
fmt.Printf("%s has been already promoted\n", data.Name)
} else {
// Check if artifact already exist on destination
exist, err := artiClient.CheckArtifactExists(targetRepo, data.Path)
if err != nil {
return err
var skipArtifact bool
if skipExistingArtifacts {
exist, err := artiClient.CheckArtifactExists(targetRepo, data.Path)
if err != nil {
return err
}
skipArtifact = exist
}

if !exist {
if !skipArtifact {
// If source repository is a release repository, we should not move but copy the artifact
// Get the properties of the source reposiytory
maturity, err := artiClient.GetRepositoryMaturity(srcRepo)
Expand Down Expand Up @@ -128,25 +131,29 @@ func PromoteFile(artiClient artifact_manager.ArtifactManager, data sdk.WorkflowR
return nil
}

func PromoteDockerImage(ctx context.Context, artiClient artifact_manager.ArtifactManager, data sdk.WorkflowRunResultArtifactManager, lowMaturity, highMaturity string, props *utils.Properties) error {
func PromoteDockerImage(ctx context.Context, artiClient artifact_manager.ArtifactManager, data sdk.WorkflowRunResultArtifactManager, lowMaturity, highMaturity string, props *utils.Properties, skipExistingArtifacts bool) error {
sourceRepo := fmt.Sprintf("%s-%s", data.RepoName, lowMaturity)
targetRepo := fmt.Sprintf("%s-%s", data.RepoName, highMaturity)
params := services.NewDockerPromoteParams(data.Path, sourceRepo, targetRepo)

if lowMaturity == highMaturity {
fmt.Printf("%s has been already promoted\n", data.Name)
} else {
maturity, err := artiClient.GetRepositoryMaturity(sourceRepo)
if err != nil {
fmt.Printf("Warning: unable to get repository maturity: %v\n", err)
}

// Check if artifact already exist on destination
exist, err := artiClient.CheckArtifactExists(targetRepo, data.Path)
if err != nil {
return err
var skipArtifact bool
if skipExistingArtifacts {
exist, err := artiClient.CheckArtifactExists(targetRepo, data.Path)
if err != nil {
return err
}
skipArtifact = exist
}
if !exist {
if !skipArtifact {
maturity, err := artiClient.GetRepositoryMaturity(sourceRepo)
if err != nil {
fmt.Printf("Warning: unable to get repository maturity: %v\n", err)
}

if maturity == "release" {
fmt.Printf("Copying docker image %s from %s to %s\n", data.Name, params.SourceRepo, params.TargetRepo)
params.Copy = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ func (e *artifactoryPromotePlugin) Run(ctx context.Context, opts *integrationplu
}
switch rData.RepoType {
case "docker":
if err := art.PromoteDockerImage(ctx, artifactClient, rData, latestPromotion.FromMaturity, latestPromotion.ToMaturity, props); err != nil {
if err := art.PromoteDockerImage(ctx, artifactClient, rData, latestPromotion.FromMaturity, latestPromotion.ToMaturity, props, false); err != nil {
return fail("unable to promote docker image: %s: %v", rData.Name+"-"+latestPromotion.ToMaturity, err)
}
default:
if err := art.PromoteFile(artifactClient, rData, latestPromotion.FromMaturity, latestPromotion.ToMaturity, props); err != nil {
if err := art.PromoteFile(artifactClient, rData, latestPromotion.FromMaturity, latestPromotion.ToMaturity, props, false); err != nil {
return fail("unable to promote file: %s: %v", rData.Name, err)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,12 @@ func (e *artifactoryReleasePlugin) Run(ctx context.Context, opts *integrationplu
}
switch rData.RepoType {
case "docker":
if err := art.PromoteDockerImage(ctx, artifactClient, rData, latestPromotion.FromMaturity, latestPromotion.ToMaturity, props); err != nil {
if err := art.PromoteDockerImage(ctx, artifactClient, rData, latestPromotion.FromMaturity, latestPromotion.ToMaturity, props, true); err != nil {
return fail("unable to promote docker image: %s: %v", rData.Name+"-"+latestPromotion.ToMaturity, err)
}
promotedArtifacts = append(promotedArtifacts, fmt.Sprintf("%s-%s/%s/manifest.json", rData.RepoName, latestPromotion.ToMaturity, rData.Path))
default:
if err := art.PromoteFile(artifactClient, rData, latestPromotion.FromMaturity, latestPromotion.ToMaturity, props); err != nil {
if err := art.PromoteFile(artifactClient, rData, latestPromotion.FromMaturity, latestPromotion.ToMaturity, props, true); err != nil {
return fail("unable to promote file: %s: %v", rData.Name, err)
}
promotedArtifacts = append(promotedArtifacts, fmt.Sprintf("%s-%s/%s", rData.RepoName, latestPromotion.ToMaturity, rData.Path))
Expand Down

0 comments on commit b246692

Please sign in to comment.