// Push an image
// Returns its calculated digest (SHA256 hash of the image)
Push an image
Returns its calculated digest (SHA256 hash of the image)
// Push an image // Returns its calculated digest ( SHA256 hash of the image )
func PushImage(encodedAuth, image string) (digest string, err error) {
    ctx := context.Background()
    cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
    if err != nil {
        return "", err
    }
    defer cli.Close()
    resp, err := cli.ImagePush(ctx, image, types.ImagePushOptions{RegistryAuth:encodedAuth})
    if err != nil {
        return "", err
    }
    defer resp.Close()
    scanner := bufio.NewScanner(resp)
    for scanner.Scan() {
        line := scanner.Text()
        var respObject struct {
            Aux struct {
                Digest string
            }
            Error string
        }
        err = json.Unmarshal([]byte(line), &respObject)
        if err != nil {
            return "", err
        }
        if respObject.Aux.Digest != "" {
            digest = respObject.Aux.Digest
        } else if respObject.Error != "" {
            return "", errors.New(respObject.Error)
        }
    }
    if digest == "" {
        return "", errors.New("docker_driver: Error did not receive digest")
    }
    return digest, nil
}
func PushImage ( encodedAuth , image string ) ( digest string , err error ) { ctx := context . Background ( ) cli , err := client . NewClientWithOpts ( client . FromEnv , client . WithAPIVersionNegotiation ( ) ) if err != nil { return " " , err } defer cli . Close ( ) resp , err := cli . ImagePush ( ctx , image , types . ImagePushOptions { RegistryAuth : encodedAuth } ) if err != nil { return " " , err } defer resp . Close ( ) scanner := bufio . NewScanner ( resp ) for scanner . Scan ( ) { line := scanner . Text ( ) var respObject struct { Aux struct { Digest string } Error string } err = json . Unmarshal ( [ ] byte ( line ) , & respObject ) if err != nil { return " " , err } if respObject . Aux . Digest != " " { digest = respObject . Aux . Digest } else if respObject . Error != " " { return " " , errors . New ( respObject . Error ) } } if digest == " " { return " " , errors . New ( " " ) } return digest , nil }
Push an image
Returns its calculated digest (SHA256 hash of the image)
Push an image Returns its calculated digest ( SHA256 hash of the image )
// Extract image digest from repsonse body // Possible that cli.ImageBuild() does not return an error, but we see an error from the response body // Read until EOF sent to ensure proper transfer of image // fmt.Println(line) // Only fields we care about are "error" or "digest"