Skip to content

Commit

Permalink
groovy step for imagePushToRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
Jk1484 authored and Egor Balakin committed Nov 15, 2023
1 parent 46cb37c commit 4c1fbd2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
10 changes: 6 additions & 4 deletions cmd/imagePushToRegistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,15 @@ func handleCredentialsForPrivateRegistries(dockerConfigJsonPath string, registry
}

func pushLocalImageToTargetRegistry(localDockerImagePath string, targetRegistryURL string) error {

return nil
img, err := docker.LoadImage(localDockerImagePath)
if err != nil {
return err
}
return docker.PushImage(img, targetRegistryURL)
}

func copyImage(sourceRegistry string, targetRegistry string) error {
docker.CopyImage(sourceRegistry, targetRegistry)
return nil
return docker.CopyImage(sourceRegistry, targetRegistry)
}

func skopeoMoveImage(sourceImageFullName string, sourceRegistryUser string, sourceRegistryPassword string, targetImageFullName string, targetRegistryUser string, targetRegistryPassword string, utils imagePushToRegistryUtils) error {
Expand Down
8 changes: 5 additions & 3 deletions cmd/imagePushToRegistry_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package cmd

import (
"testing"

"github.com/SAP/jenkins-library/pkg/mock"
"github.com/SAP/jenkins-library/pkg/piperutils"
"github.com/stretchr/testify/assert"
"testing"
)

type imagePushToRegistryMockUtils struct {
Expand Down Expand Up @@ -31,7 +33,7 @@ func TestRunImagePushToRegistry(t *testing.T) {
utils.AddFile("file.txt", []byte("dummy content"))

// test
err := runImagePushToRegistry(&config, nil, utils)
err := runImagePushToRegistry(&config, nil, utils, &piperutils.Files{})

// assert
assert.NoError(t, err)
Expand All @@ -45,7 +47,7 @@ func TestRunImagePushToRegistry(t *testing.T) {
utils := newImagePushToRegistryTestsUtils()

// test
err := runImagePushToRegistry(&config, nil, utils)
err := runImagePushToRegistry(&config, nil, utils, &piperutils.Files{})

// assert
assert.EqualError(t, err, "cannot run without important file")
Expand Down
15 changes: 8 additions & 7 deletions pkg/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,12 +330,13 @@ func ImageListWithFilePath(imageName string, excludes []string, trimDir string,
}

func CopyImage(src string, dest string) error {
crane.Copy(src, dest)
return nil
return crane.Copy(src, dest)
}

func PushImage(im v1.Image, dest string) error {
return crane.Push(im, dest)
}

// func (c *Client) PushImage(localDockerPath string, dest string) error {
// crane.
// crane.Push(localDockerPath, dest)
// return nil
// }
func LoadImage(src string) (v1.Image, error) {
return crane.Load(src)
}
9 changes: 9 additions & 0 deletions vars/imagePushToRegistry.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import groovy.transform.Field

@Field String STEP_NAME = getClass().getName()
@Field String METADATA_FILE = 'metadata/imagePushToRegistry.yaml'

void call(Map parameters = [:]) {
List credentials = [[type: 'file', id: 'dockerConfigJsonCredentialsId', env: ['PIPER_dockerConfigJSON']]]
piperExecuteBin(parameters, STEP_NAME, METADATA_FILE, credentials)
}

0 comments on commit 4c1fbd2

Please sign in to comment.