Skip to content

Commit

Permalink
fix CopyImage
Browse files Browse the repository at this point in the history
  • Loading branch information
Egor Balakin committed Nov 15, 2023
1 parent 58b1f90 commit d577ea8
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 29 deletions.
29 changes: 24 additions & 5 deletions cmd/imagePushToRegistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"net/url"

"github.com/SAP/jenkins-library/pkg/command"
"github.com/SAP/jenkins-library/pkg/docker"
Expand Down Expand Up @@ -77,13 +78,18 @@ func runImagePushToRegistry(config *imagePushToRegistryOptions, telemetryData *t
return fmt.Errorf("failed to handle registry credentials for target registry: %w", err)
}

targetImage := config.TargetImage
if targetImage == "" {
targetImage = config.SourceImage
}

if len(config.LocalDockerImagePath) > 0 {
err = pushLocalImageToTargetRegistry(config.LocalDockerImagePath, config.TargetRegistryURL)
err = pushLocalImageToTargetRegistry(config.LocalDockerImagePath, config.TargetRegistryURL, targetImage)
if err != nil {
return fmt.Errorf("failed to push to local image to registry: %w", err)
}
} else {
err = copyImage(config.SourceRegistryURL, config.TargetRegistryURL)
err = copyImage(config.SourceRegistryURL, config.SourceImage, config.TargetRegistryURL, targetImage)
if err != nil {
return fmt.Errorf("failed to copy image from %v to %v with err: %w", config.SourceRegistryURL, config.TargetRegistryURL, err)
}
Expand Down Expand Up @@ -149,16 +155,29 @@ func handleCredentialsForPrivateRegistries(dockerConfigJsonPath string, registry
return nil
}

func pushLocalImageToTargetRegistry(localDockerImagePath string, targetRegistryURL string) error {
func pushLocalImageToTargetRegistry(localDockerImagePath string, targetRegistryURL string, targetImage string) error {
img, err := docker.LoadImage(localDockerImagePath)
if err != nil {
return err
}
return docker.PushImage(img, targetRegistryURL)
}

func copyImage(sourceRegistry string, targetRegistry string) error {
return docker.CopyImage(sourceRegistry, targetRegistry)
func copyImage(sourceRegistry string, sourceImage string, targetRegistry string, targetImage string) error {
// needed without URL scheme
if sourceRegistryURL, err := url.Parse(sourceRegistry); err == nil {
sourceRegistry = sourceRegistry[len(sourceRegistryURL.Scheme+"://"):]
}
srcRef := fmt.Sprintf("%s/%s", sourceRegistry, sourceImage)

// needed without URL scheme
if targetRegistryURL, err := url.Parse(targetRegistry); err == nil {
targetRegistry = targetRegistry[len(targetRegistryURL.Scheme+"://"):]

}
dstRef := fmt.Sprintf("%s/%s", targetRegistry, targetImage)

return docker.CopyImage(srcRef, dstRef)
}

func skopeoMoveImage(sourceImageFullName string, sourceRegistryUser string, sourceRegistryPassword string, targetImageFullName string, targetRegistryUser string, targetRegistryPassword string, utils imagePushToRegistryUtils) error {
Expand Down
58 changes: 37 additions & 21 deletions cmd/imagePushToRegistry_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions pkg/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/google/go-containerregistry/pkg/logs"
"os"
"path"
"path/filepath"
Expand All @@ -31,6 +32,11 @@ type AuthEntry struct {
Auth string `json:"auth,omitempty"`
}

func init() {
logs.Warn.SetOutput(os.Stderr)
logs.Progress.SetOutput(os.Stderr)
}

// MergeDockerConfigJSON merges two docker config.json files.
func MergeDockerConfigJSON(sourcePath, targetPath string, utils piperutils.FileUtils) error {
if exists, _ := utils.FileExists(sourcePath); !exists {
Expand Down Expand Up @@ -125,7 +131,7 @@ func CreateDockerConfigJSON(registryURL, username, password, targetPath, configP
return "", fmt.Errorf("failed to marshal Docker config.json: %w", err)
}

//always create the target path directories if any before writing
// always create the target path directories if any before writing
err = utils.MkdirAll(filepath.Dir(targetPath), 0777)
if err != nil {
return "", fmt.Errorf("failed to create directory path for the Docker config.json file %v:%w", targetPath, err)
Expand Down Expand Up @@ -289,7 +295,7 @@ func ImageListWithFilePath(imageName string, excludes []string, trimDir string,
for _, dockerfilePath := range matches {
// make sure that the path we have is relative
// ToDo: needs rework
//dockerfilePath = strings.ReplaceAll(dockerfilePath, cwd, ".")
// dockerfilePath = strings.ReplaceAll(dockerfilePath, cwd, ".")

if piperutils.ContainsString(excludes, dockerfilePath) {
log.Entry().Infof("Discard %v since it is in the exclude list %v", dockerfilePath, excludes)
Expand Down
5 changes: 4 additions & 1 deletion resources/metadata/imagePushToRegistry.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ spec:
params:
- name: targetImage
type: string
description: Defines the name (incl. tag) of the target image
description: Defines the name (incl. tag) of the target image. If empty, sourceImage will be used.
scope:
- PARAMETERS
- STAGES
Expand All @@ -41,6 +41,9 @@ spec:
- PARAMETERS
- STAGES
- STEPS
resourceRef:
- name: commonPipelineEnvironment
param: container/imageNameTag
- name: sourceRegistryUrl
description: Defines a registry url from where the image should optionally be pulled from, incl. the protocol like `https://my.registry.com`*"
type: string
Expand Down

0 comments on commit d577ea8

Please sign in to comment.