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(contrib): artifactory promote always update artifacts #6415

Merged
merged 1 commit into from
Jan 11, 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
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