Updated docker.go

This commit is contained in:
Ompragash Viswanathan
2025-07-09 12:42:07 +05:30
parent 1367829b3f
commit cd8c2ea2a5
+25 -49
View File
@@ -57,13 +57,13 @@ type (
// Plugin defines the Docker plugin parameters. // Plugin defines the Docker plugin parameters.
Plugin struct { Plugin struct {
Login Login // Docker login configuration Login Login // Docker login configuration
Build Build // Docker build configuration Build Build // Docker build configuration
Dryrun bool // Docker push is skipped Dryrun bool // Docker push is skipped
Cleanup bool // Docker purge is enabled Cleanup bool // Docker purge is enabled
PushOnly bool // Push only mode, skips build process PushOnly bool // Push only mode, skips build process
SourceTarPath string // Path to Docker image tar file to load and push SourceTarPath string // Path to Docker image tar file to load and push
TarPath string // Path to save Docker image as tar file TarPath string // Path to save Docker image as tar file
} }
) )
@@ -130,29 +130,22 @@ func (p Plugin) Exec() error {
for _, tag := range p.Build.Tags { for _, tag := range p.Build.Tags {
cmds = append(cmds, commandTag(p.Build, tag)) // docker tag cmds = append(cmds, commandTag(p.Build, tag)) // docker tag
if p.Dryrun == false { if !p.Dryrun {
cmds = append(cmds, commandPush(p.Build, tag)) // docker push cmds = append(cmds, commandPush(p.Build, tag)) // docker push
} }
} }
// If TarPath is specified and Dryrun is enabled, save the image to a tar file // If TarPath is specified and Dryrun is enabled, save the image to a tar file
if p.TarPath != "" && p.Dryrun { if p.TarPath != "" && p.Dryrun && len(p.Build.Tags) > 0 {
// Create parent directories if they don't exist // Create parent directories if they don't exist
dir := filepath.Dir(p.TarPath) dir := filepath.Dir(p.TarPath)
if err := os.MkdirAll(dir, 0755); err != nil { if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("error creating directories for tar file: %s", err) return fmt.Errorf("error creating directories for tar file: %s", err)
} }
// Save the image as tar imageToSave := fmt.Sprintf("%s:%s", p.Build.Repo, p.Build.Tags[0])
fmt.Println("Saving image to tar:", p.TarPath) fmt.Println("Saving image to tar:", p.TarPath)
saveCmd := commandSaveTar(p.Build.Name, p.TarPath) cmds = append(cmds, commandSaveTar(imageToSave, p.TarPath))
saveCmd.Stdout = os.Stdout
saveCmd.Stderr = os.Stderr
trace(saveCmd)
if err := saveCmd.Run(); err != nil {
return fmt.Errorf("error saving image to tar: %s", err)
}
fmt.Printf("Successfully saved image to %s\n", p.TarPath)
} }
if p.Cleanup { if p.Cleanup {
@@ -440,51 +433,34 @@ func (p Plugin) pushOnly() error {
return fmt.Errorf("source image %s not found, cannot push: %s", sourceImage, err) return fmt.Errorf("source image %s not found, cannot push: %s", sourceImage, err)
} }
var cmds []*exec.Cmd
// For each tag, tag the source image (if needed) and push // For each tag, tag the source image (if needed) and push
for _, tag := range p.Build.Tags { for _, tag := range p.Build.Tags {
targetImage := fmt.Sprintf("%s:%s", p.Build.Repo, tag) targetImage := fmt.Sprintf("%s:%s", p.Build.Repo, tag)
// Skip tagging if source and target are identical // Skip tagging if source and target are identical
if sourceImage != targetImage { if sourceImage != targetImage {
fmt.Printf("Tagging %s as %s\n", sourceImage, targetImage) fmt.Printf("Tagging %s as %s\n", sourceImage, targetImage)
tagCmd := commandTag(p.Build, tag) cmds = append(cmds, commandTag(p.Build, tag))
tagCmd.Stdout = os.Stdout
tagCmd.Stderr = os.Stderr
trace(tagCmd)
if err := tagCmd.Run(); err != nil {
return fmt.Errorf("failed to tag image %s as %s: %s", sourceImage, targetImage, err)
}
} }
// Push the image // Push the image if not in dry-run mode
fmt.Println("Pushing image:", targetImage) if !p.Dryrun {
pushCmd := commandPush(p.Build, tag) cmds = append(cmds, commandPush(p.Build, tag))
pushCmd.Stdout = os.Stdout
pushCmd.Stderr = os.Stderr
trace(pushCmd)
if err := pushCmd.Run(); err != nil {
return fmt.Errorf("failed to push image %s: %s", targetImage, err)
} }
} }
// If TarPath is specified and Dryrun is enabled, save the image to a tar file // Tar saving functionality is only available in the regular execution mode with dry-run enabled
if p.TarPath != "" && p.Dryrun {
// Create parent directories if they don't exist
dir := filepath.Dir(p.TarPath)
if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("error creating directories for tar file: %s", err)
}
// Save the image as tar // Execute all commands
fmt.Println("Saving image to tar:", p.TarPath) for _, cmd := range cmds {
saveCmd := commandSaveTar(sourceImage, p.TarPath) cmd.Stdout = os.Stdout
saveCmd.Stdout = os.Stdout cmd.Stderr = os.Stderr
saveCmd.Stderr = os.Stderr trace(cmd)
trace(saveCmd) if err := cmd.Run(); err != nil {
if err := saveCmd.Run(); err != nil { return fmt.Errorf("command failed: %s", err)
return fmt.Errorf("error saving image to tar: %s", err)
} }
fmt.Printf("Successfully saved image to %s\n", p.TarPath)
} }
return nil return nil