1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2024-12-31 01:53:50 +02:00

feat: add support for GitLab Generic Packages (#2006) (#2745)

GitLab's Generic Package Repository allows uploading files
to GitLab, as an alternative to attachments.

Added the flag `use_package_registry` to the `gitlab_urls` config.
When false, the default, it will behave as before.
When true, it will publish the file to the Generic Package Registry.
Either way, the URL is bound to the release the same

Updated `go-gitlab` to version 0.52.2 which has the new
`GenericPackages.PublishPackageFile` function.

Co-authored-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
This commit is contained in:
Evan 2022-01-06 20:11:11 -05:00 committed by GitHub
parent 791de897bb
commit fe79b4b9da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 18 deletions

View File

@ -377,19 +377,41 @@ func (c *gitlabClient) Upload(
projectID = ctx.Config.Release.GitLab.Owner + "/" + projectID
}
log.WithField("file", file.Name()).Debug("uploading file")
projectFile, _, err := c.client.Projects.UploadFile(
projectID,
file.Name(),
nil,
)
if err != nil {
return err
var baseLinkURL string
if ctx.Config.GitLabURLs.UsePackageRegistry {
log.WithField("file", file.Name()).Debug("uploading file as generic package")
_, _, err := c.client.GenericPackages.PublishPackageFile(
projectID,
ctx.Config.ProjectName,
ctx.Version,
artifact.Name,
file,
nil,
)
if err != nil {
return err
}
baseLinkURL, err = c.client.GenericPackages.FormatPackageURL(
projectID, ctx.Config.ProjectName, ctx.Version, artifact.Name)
if err != nil {
return err
}
} else {
log.WithField("file", file.Name()).Debug("uploading file as attachment")
projectFile, _, err := c.client.Projects.UploadFile(
projectID,
file.Name(),
nil,
)
if err != nil {
return err
}
baseLinkURL = projectFile.URL
}
log.WithFields(log.Fields{
"file": file.Name(),
"url": projectFile.URL,
"url": baseLinkURL,
}).Debug("uploaded file")
// search for project details based on projectID
@ -398,14 +420,18 @@ func (c *gitlabClient) Upload(
return err
}
gitlabBaseURL, err := tmpl.New(ctx).Apply(ctx.Config.GitLabURLs.Download)
if err != nil {
return fmt.Errorf("templating GitLab Download URL: %w", err)
}
linkURL := gitlabBaseURL + "/" + projectDetails.PathWithNamespace + projectFile.URL
name := artifact.Name
filename := "/" + name
var linkURL string
if ctx.Config.GitLabURLs.UsePackageRegistry {
linkURL = c.client.BaseURL().String() + baseLinkURL
} else {
gitlabBaseURL, err := tmpl.New(ctx).Apply(ctx.Config.GitLabURLs.Download)
if err != nil {
return fmt.Errorf("templating GitLab Download URL: %w", err)
}
linkURL = gitlabBaseURL + "/" + projectDetails.PathWithNamespace + baseLinkURL
}
releaseLink, _, err := c.client.ReleaseLinks.CreateReleaseLink(
projectID,
releaseID,

View File

@ -25,9 +25,10 @@ type GitHubURLs struct {
// GitLabURLs holds the URLs to be used when using gitlab ce/enterprise.
type GitLabURLs struct {
API string `yaml:"api,omitempty"`
Download string `yaml:"download,omitempty"`
SkipTLSVerify bool `yaml:"skip_tls_verify,omitempty"`
API string `yaml:"api,omitempty"`
Download string `yaml:"download,omitempty"`
SkipTLSVerify bool `yaml:"skip_tls_verify,omitempty"`
UsePackageRegistry bool `yaml:"use_package_registry,omitempty"`
}
// GiteaURLs holds the URLs to be used when using gitea.

View File

@ -28,6 +28,9 @@ gitlab_urls:
download: https://gitlab.company.com
# set to true if you use a self-signed certificate
skip_tls_verify: false
# set to true if you want to upload to the Package Registry rather than attachments
# Only works with GitLab 13.5+
use_package_registry: false
```
If none are set, they default to GitLab's public URLs.
@ -37,6 +40,19 @@ If none are set, they default to GitLab's public URLs.
on [release](https://docs.gitlab.com/ee/user/project/releases/index.html) functionality
and [direct asset linking](https://docs.gitlab.com/ee/user/project/releases/index.html#permanent-links-to-release-assets).
## Generic Package Registry
GitLab introduced the [Generic Package Registry](https://docs.gitlab.com/ee/user/packages/package_registry/index.html) in Gitlab 13.5.
Normally, `goreleaser` uploads release files as "attachments", which may have [administrative limits](https://docs.gitlab.com/ee/user/admin_area/settings/account_and_limit_settings.html). Notably, hosted gitlab.com instances have a 10MB attachment limit which cannot be changed.
Uploading to the Generic Package Registry does not have this restriction. To use it instead, set `use_package_registry` to `true`.
```yaml
# .goreleaser.yml
gitlab_urls:
use_package_registry: true
```
## Example release