// Package artifactory provides a Pipe that push to artifactory package artifactory import ( "bytes" "encoding/json" "fmt" "html/template" "io" "io/ioutil" "net/http" "net/url" "os" "strings" "github.com/apex/log" "github.com/pkg/errors" "golang.org/x/sync/errgroup" "github.com/goreleaser/goreleaser/config" "github.com/goreleaser/goreleaser/context" "github.com/goreleaser/goreleaser/internal/artifact" "github.com/goreleaser/goreleaser/pipeline" ) // artifactoryResponse reflects the response after an upload request // to Artifactory. type artifactoryResponse struct { Repo string `json:"repo,omitempty"` Path string `json:"path,omitempty"` Created string `json:"created,omitempty"` CreatedBy string `json:"createdBy,omitempty"` DownloadURI string `json:"downloadUri,omitempty"` MimeType string `json:"mimeType,omitempty"` Size string `json:"size,omitempty"` Checksums artifactoryChecksums `json:"checksums,omitempty"` OriginalChecksums artifactoryChecksums `json:"originalChecksums,omitempty"` URI string `json:"uri,omitempty"` } // artifactoryChecksums reflects the checksums generated by // Artifactory type artifactoryChecksums struct { SHA1 string `json:"sha1,omitempty"` MD5 string `json:"md5,omitempty"` SHA256 string `json:"sha256,omitempty"` } const ( modeBinary = "binary" modeArchive = "archive" ) // Pipe for Artifactory type Pipe struct{} // String returns the description of the pipe func (Pipe) String() string { return "releasing to Artifactory" } // Default sets the pipe defaults func (Pipe) Default(ctx *context.Context) error { if len(ctx.Config.Artifactories) == 0 { return nil } // Check if a mode was set for i := range ctx.Config.Artifactories { if ctx.Config.Artifactories[i].Mode == "" { ctx.Config.Artifactories[i].Mode = modeArchive } } return nil } // Run the pipe // // Docs: https://www.jfrog.com/confluence/display/RTF/Artifactory+REST+API#ArtifactoryRESTAPI-Example-DeployinganArtifact func (Pipe) Run(ctx *context.Context) error { if len(ctx.Config.Artifactories) == 0 { return pipeline.Skip("artifactory section is not configured") } // Check requirements for every instance we have configured. // If not fulfilled, we can skip this pipeline for _, instance := range ctx.Config.Artifactories { if instance.Target == "" { return pipeline.Skip("artifactory section is not configured properly (missing target)") } if instance.Username == "" { return pipeline.Skip("artifactory section is not configured properly (missing username)") } if instance.Name == "" { return pipeline.Skip("artifactory section is not configured properly (missing name)") } envName := fmt.Sprintf("ARTIFACTORY_%s_SECRET", strings.ToUpper(instance.Name)) if _, ok := ctx.Env[envName]; !ok { return pipeline.Skip(fmt.Sprintf("missing secret for artifactory instance %s", instance.Name)) } } return doRun(ctx) } func doRun(ctx *context.Context) error { if !ctx.Publish { return pipeline.ErrSkipPublish } // Handle every configured artifactory instance for _, instance := range ctx.Config.Artifactories { // We support two different modes // - "archive": Upload all artifacts // - "binary": Upload only the raw binaries var filter artifact.Filter switch v := strings.ToLower(instance.Mode); v { case modeArchive: filter = artifact.Or( artifact.ByType(artifact.UploadableArchive), artifact.ByType(artifact.LinuxPackage), ) case modeBinary: filter = artifact.ByType(artifact.UploadableBinary) default: err := fmt.Errorf("artifactory: mode \"%s\" not supported", v) log.WithFields(log.Fields{ "instance": instance.Name, "mode": v, }).Error(err.Error()) return err } if err := runPipeByFilter(ctx, instance, filter); err != nil { return err } } return nil } func runPipeByFilter(ctx *context.Context, instance config.Artifactory, filter artifact.Filter) error { sem := make(chan bool, ctx.Parallelism) var g errgroup.Group for _, artifact := range ctx.Artifacts.Filter(filter).List() { sem <- true artifact := artifact g.Go(func() error { defer func() { <-sem }() return uploadAsset(ctx, instance, artifact) }) } return g.Wait() } // uploadAsset uploads file to target and logs all actions func uploadAsset(ctx *context.Context, instance config.Artifactory, artifact artifact.Artifact) error { envName := fmt.Sprintf("ARTIFACTORY_%s_SECRET", strings.ToUpper(instance.Name)) secret := ctx.Env[envName] // Generate the target url targetURL, err := resolveTargetTemplate(ctx, instance, artifact) if err != nil { msg := "artifactory: error while building the target url" log.WithField("instance", instance.Name).WithError(err).Error(msg) return errors.Wrap(err, msg) } // Handle the artifact file, err := os.Open(artifact.Path) if err != nil { return err } defer file.Close() // nolint: errcheck // The target url needs to contain the artifact name if !strings.HasSuffix(targetURL, "/") { targetURL += "/" } targetURL += artifact.Name uploaded, _, err := uploadAssetToArtifactory(ctx, targetURL, instance.Username, secret, file) if err != nil { msg := "artifactory: upload failed" log.WithError(err).WithFields(log.Fields{ "instance": instance.Name, "username": instance.Username, }).Error(msg) return errors.Wrap(err, msg) } log.WithFields(log.Fields{ "instance": instance.Name, "mode": instance.Mode, "uri": uploaded.DownloadURI, }).Info("uploaded successful") return nil } // targetData is used as a template struct for // Artifactory.Target type targetData struct { Version string Tag string ProjectName string // Only supported in mode binary Os string Arch string Arm string } // resolveTargetTemplate returns the resolved target template with replaced variables // Those variables can be replaced by the given context, goos, goarch, goarm and more func resolveTargetTemplate(ctx *context.Context, artifactory config.Artifactory, artifact artifact.Artifact) (string, error) { data := targetData{ Version: ctx.Version, Tag: ctx.Git.CurrentTag, ProjectName: ctx.Config.ProjectName, } if artifactory.Mode == modeBinary { data.Os = replace(ctx.Config.Archive.Replacements, artifact.Goos) data.Arch = replace(ctx.Config.Archive.Replacements, artifact.Goarch) data.Arm = replace(ctx.Config.Archive.Replacements, artifact.Goarm) } var out bytes.Buffer t, err := template.New(ctx.Config.ProjectName).Parse(artifactory.Target) if err != nil { return "", err } err = t.Execute(&out, data) return out.String(), err } func replace(replacements map[string]string, original string) string { result := replacements[original] if result == "" { return original } return result } // uploadAssetToArtifactory uploads the asset file to target func uploadAssetToArtifactory(ctx *context.Context, target, username, secret string, file *os.File) (*artifactoryResponse, *http.Response, error) { stat, err := file.Stat() if err != nil { return nil, nil, err } if stat.IsDir() { return nil, nil, errors.New("the asset to upload can't be a directory") } req, err := newUploadRequest(target, username, secret, file, stat.Size()) if err != nil { return nil, nil, err } asset := new(artifactoryResponse) resp, err := executeHTTPRequest(ctx, req, asset) if err != nil { return nil, resp, err } return asset, resp, nil } // newUploadRequest creates a new http.Request for uploading func newUploadRequest(target, username, secret string, reader io.Reader, size int64) (*http.Request, error) { u, err := url.Parse(target) if err != nil { return nil, err } req, err := http.NewRequest("PUT", u.String(), reader) if err != nil { return nil, err } req.ContentLength = size req.SetBasicAuth(username, secret) return req, err } // executeHTTPRequest processes the http call with respect of context ctx func executeHTTPRequest(ctx *context.Context, req *http.Request, v interface{}) (*http.Response, error) { resp, err := http.DefaultClient.Do(req) if err != nil { // If we got an error, and the context has been canceled, // the context's error is probably more useful. select { case <-ctx.Done(): return nil, ctx.Err() default: } return nil, err } defer resp.Body.Close() // nolint: errcheck err = checkResponse(resp) if err != nil { // even though there was an error, we still return the response // in case the caller wants to inspect it further return resp, err } err = json.NewDecoder(resp.Body).Decode(v) return resp, err } // An ErrorResponse reports one or more errors caused by an API request. type errorResponse struct { Response *http.Response // HTTP response that caused this error Errors []Error `json:"errors"` // more detail on individual errors } func (r *errorResponse) Error() string { return fmt.Sprintf("%v %v: %d %+v", r.Response.Request.Method, r.Response.Request.URL, r.Response.StatusCode, r.Errors) } // An Error reports more details on an individual error in an ErrorResponse. type Error struct { Status int `json:"status"` // Error code Message string `json:"message"` // Message describing the error. } // checkResponse checks the API response for errors, and returns them if // present. A response is considered an error if it has a status code outside // the 200 range. // API error responses are expected to have either no response // body, or a JSON response body that maps to ErrorResponse. Any other // response body will be silently ignored. func checkResponse(r *http.Response) error { if c := r.StatusCode; 200 <= c && c <= 299 { return nil } errorResponse := &errorResponse{Response: r} data, err := ioutil.ReadAll(r.Body) if err == nil && data != nil { err := json.Unmarshal(data, errorResponse) if err != nil { return err } } return errorResponse }