github.com/jfrog/jfrog-cli-go@v1.22.1-0.20200318093948-4826ef344ffd/artifactory/commands/docker/push.go (about)

     1  package docker
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/jfrog/jfrog-cli-go/artifactory/utils"
     7  	"github.com/jfrog/jfrog-cli-go/artifactory/utils/docker"
     8  	"github.com/jfrog/jfrog-cli-go/utils/config"
     9  	"github.com/jfrog/jfrog-client-go/utils/errorutils"
    10  )
    11  
    12  type DockerPushCommand struct {
    13  	DockerCommand
    14  	threads int
    15  }
    16  
    17  func NewDockerPushCommand() *DockerPushCommand {
    18  	return &DockerPushCommand{}
    19  }
    20  
    21  func (dpc *DockerPushCommand) Threads() int {
    22  	return dpc.threads
    23  }
    24  
    25  func (dpc *DockerPushCommand) SetThreads(threads int) *DockerPushCommand {
    26  	dpc.threads = threads
    27  	return dpc
    28  }
    29  
    30  // Push docker image and create build info if needed
    31  func (dpc *DockerPushCommand) Run() error {
    32  	err := docker.ValidateClientApiVersion()
    33  	if err != nil {
    34  		return err
    35  	}
    36  
    37  	// Perform login
    38  	rtDetails, err := dpc.RtDetails()
    39  	if errorutils.CheckError(err) != nil {
    40  		return err
    41  	}
    42  
    43  	if !dpc.skipLogin {
    44  		loginConfig := &docker.DockerLoginConfig{ArtifactoryDetails: rtDetails}
    45  		err = docker.DockerLogin(dpc.imageTag, loginConfig)
    46  		if err != nil {
    47  			return err
    48  		}
    49  	}
    50  
    51  	// Perform push
    52  	if strings.LastIndex(dpc.imageTag, ":") == -1 {
    53  		dpc.imageTag = dpc.imageTag + ":latest"
    54  	}
    55  	image := docker.New(dpc.imageTag)
    56  	err = image.Push()
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	// Return if no build name and number was provided
    62  	if dpc.buildConfiguration.BuildName == "" || dpc.buildConfiguration.BuildNumber == "" {
    63  		return nil
    64  	}
    65  
    66  	if err := utils.SaveBuildGeneralDetails(dpc.buildConfiguration.BuildName, dpc.buildConfiguration.BuildNumber); err != nil {
    67  		return err
    68  	}
    69  
    70  	serviceManager, err := docker.CreateServiceManager(rtDetails, dpc.threads)
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	builder := docker.BuildInfoBuilder(image, dpc.Repo(), dpc.BuildConfiguration().BuildName, dpc.BuildConfiguration().BuildNumber, serviceManager, docker.Push)
    76  	buildInfo, err := builder.Build(dpc.BuildConfiguration().Module)
    77  	if err != nil {
    78  		return err
    79  	}
    80  	return utils.SaveBuildInfo(dpc.BuildConfiguration().BuildName, dpc.BuildConfiguration().BuildNumber, buildInfo)
    81  }
    82  
    83  func (dpc *DockerPushCommand) CommandName() string {
    84  	return "rt_docker_push"
    85  }
    86  
    87  func (dpc *DockerPushCommand) RtDetails() (*config.ArtifactoryDetails, error) {
    88  	return dpc.rtDetails, nil
    89  }