Repository: goreleaser/goreleaser
Pull request #2859 (2022-01-30 17:47:41)

feat: expose runtime goos and goarch on templates and metadata
Expose `{{ .Runtime.Goos }}` and `{{ .Runtime.Goarch }}`

refs  #2829


Patch:
diff --git a/.goreleaser.yaml b/.goreleaser.yaml
index f781b930..a4d13a06 100644
--- a/.goreleaser.yaml
+++ b/.goreleaser.yaml
@@ -1,7 +1,5 @@
 env:
   - GO111MODULE=on
-  - DESCRIPTION="Deliver Go binaries as fast and easily as possible"
-  - README_URL="https://raw.githubusercontent.com/goreleaser/goreleaser/main/README.md"
 
 before:
   hooks:
@@ -71,8 +69,11 @@ dockers:
   use: buildx
   build_flag_templates:
   - "--pull"
-  - "--label=io.artifacthub.package.readme-url={{.Env.README_URL}}"
-  - "--label=org.opencontainers.image.description={{.Env.DESCRIPTION}}"
+  - "--label=io.artifacthub.package.readme-url=https://raw.githubusercontent.com/goreleaser/goreleaser/main/README.md"
+  - "--label=io.artifacthub.package.logo-url=https://goreleaser.com/static/avatar.png"
+  - "--label=io.artifacthub.package.maintainers=[{\"name\":\"Carlos Alexandro Becker\",\"email\":\"carlos@carlosbecker.dev\"}]"
+  - "--label=io.artifacthub.package.license=MIT"
+  - "--label=org.opencontainers.image.description=Deliver Go binaries as fast and easily as possible"
   - "--label=org.opencontainers.image.created={{.Date}}"
   - "--label=org.opencontainers.image.name={{.ProjectName}}"
   - "--label=org.opencontainers.image.revision={{.FullCommit}}"
@@ -88,8 +89,11 @@ dockers:
   use: buildx
   build_flag_templates:
   - "--pull"
-  - "--label=io.artifacthub.package.readme-url={{.Env.README_URL}}"
-  - "--label=org.opencontainers.image.description={{.Env.DESCRIPTION}}"
+  - "--label=io.artifacthub.package.readme-url=https://raw.githubusercontent.com/goreleaser/goreleaser/main/README.md"
+  - "--label=io.artifacthub.package.logo-url=https://goreleaser.com/static/avatar.png"
+  - "--label=io.artifacthub.package.maintainers=[{\"name\":\"Carlos Alexandro Becker\",\"email\":\"carlos@carlosbecker.dev\"}]"
+  - "--label=io.artifacthub.package.license=MIT"
+  - "--label=org.opencontainers.image.description=Deliver Go binaries as fast and easily as possible"
   - "--label=org.opencontainers.image.created={{.Date}}"
   - "--label=org.opencontainers.image.name={{.ProjectName}}"
   - "--label=org.opencontainers.image.revision={{.FullCommit}}"
diff --git a/internal/pipe/metadata/metadata.go b/internal/pipe/metadata/metadata.go
index 3dfb6de9..3614a6ca 100644
--- a/internal/pipe/metadata/metadata.go
+++ b/internal/pipe/metadata/metadata.go
@@ -33,6 +33,10 @@ func writeMetadata(ctx *context.Context) error {
 		Version:     ctx.Version,
 		Commit:      ctx.Git.Commit,
 		Date:        ctx.Date,
+		Runtime: metaRuntime{
+			Goos:   ctx.Runtime.Goos,
+			Goarch: ctx.Runtime.Goarch,
+		},
 	}, "metadata.json")
 }
 
@@ -51,10 +55,16 @@ func writeJSON(ctx *context.Context, j interface{}, name string) error {
 }
 
 type metadata struct {
-	ProjectName string    `json:"project_name"`
-	Tag         string    `json:"tag"`
-	PreviousTag string    `json:"previous_tag"`
-	Version     string    `json:"version"`
-	Commit      string    `json:"commit"`
-	Date        time.Time `json:"date"`
+	ProjectName string      `json:"project_name"`
+	Tag         string      `json:"tag"`
+	PreviousTag string      `json:"previous_tag"`
+	Version     string      `json:"version"`
+	Commit      string      `json:"commit"`
+	Date        time.Time   `json:"date"`
+	Runtime     metaRuntime `json:"runtime"`
+}
+
+type metaRuntime struct {
+	Goos   string `json:"goos"`
+	Goarch string `json:"goarch"`
 }
diff --git a/internal/tmpl/tmpl.go b/internal/tmpl/tmpl.go
index f5fb44be..6874c1bb 100644
--- a/internal/tmpl/tmpl.go
+++ b/internal/tmpl/tmpl.go
@@ -52,6 +52,7 @@ const (
 	timestamp       = "Timestamp"
 	modulePath      = "ModulePath"
 	releaseNotes    = "ReleaseNotes"
+	runtimeK        = "Runtime"
 
 	// artifact-only keys.
 	osKey        = "Os"
@@ -102,6 +103,7 @@ func New(ctx *context.Context) *Template {
 			prerelease:      ctx.Semver.Prerelease,
 			isSnapshot:      ctx.Snapshot,
 			releaseNotes:    ctx.ReleaseNotes,
+			runtimeK:        ctx.Runtime,
 		},
 	}
 }
diff --git a/pkg/context/context.go b/pkg/context/context.go
index f2000c45..37589bc3 100644
--- a/pkg/context/context.go
+++ b/pkg/context/context.go
@@ -9,6 +9,7 @@ package context
 import (
 	ctx "context"
 	"os"
+	"runtime"
 	"strings"
 	"time"
 
@@ -98,6 +99,12 @@ type Context struct {
 	Deprecated         bool
 	Parallelism        int
 	Semver             Semver
+	Runtime            Runtime
+}
+
+type Runtime struct {
+	Goos   string
+	Goarch string
 }
 
 // Semver represents a semantic version.
@@ -129,6 +136,10 @@ func Wrap(ctx ctx.Context, config config.Project) *Context {
 		Parallelism: 4,
 		Artifacts:   artifact.New(),
 		Date:        time.Now(),
+		Runtime: Runtime{
+			Goos:   runtime.GOOS,
+			Goarch: runtime.GOARCH,
+		},
 	}
 }
 
diff --git a/www/docs/customization/gofish.md b/www/docs/customization/gofish.md
index b4a0c7af..b5ef2edd 100644
--- a/www/docs/customization/gofish.md
+++ b/www/docs/customization/gofish.md
@@ -37,11 +37,13 @@ rigs:
     rig:
       owner: repo-owner
       name: gofish-rig
-      # Optionally a branch can be provided. If the branch does not exist, it
-      # will be created. If no branch is listed, the default branch will be used
+
+      # Optionally a branch can be provided.
+      # Defaults to the default repository branch.
       branch: main
+
       # Optionally a token can be provided, if it differs from the token provided to GoReleaser
-      token: "{{ .Env.GOFISH_RIG_GITHUB_TOKEN }}"
+      token: "{{ .Env.HOMEBREW_TAP_GITHUB_TOKEN }}"
 
     # Template for the url which is determined by the given Token (github or gitlab)
     # Default for github is "https://github.com/<repo_owner>/<repo_name>/releases/download/{{ .Tag }}/{{ .ArtifactName }}"
diff --git a/www/docs/customization/homebrew.md b/www/docs/customization/homebrew.md
index 503fdc25..da148fa7 100644
--- a/www/docs/customization/homebrew.md
+++ b/www/docs/customization/homebrew.md
@@ -36,9 +36,11 @@ brews:
     tap:
       owner: repo-owner
       name: homebrew-tap
-      # Optionally a branch can be provided. If the branch does not exist, it
-      # will be created. If no branch is listed, the default branch will be used
+
+      # Optionally a branch can be provided.
+      # Defaults to the default repository branch.
       branch: main
+
       # Optionally a token can be provided, if it differs from the token provided to GoReleaser
       token: "{{ .Env.HOMEBREW_TAP_GITHUB_TOKEN }}"
 
diff --git a/www/docs/customization/krew.md b/www/docs/customization/krew.md
index 4a52c189..6c2d00aa 100644
--- a/www/docs/customization/krew.md
+++ b/www/docs/customization/krew.md
@@ -33,11 +33,13 @@ krews:
     index:
       owner: repo-owner
       name: krew-plugins
-      # Optionally a branch can be provided. If the branch does not exist, it
-      # will be created. If no branch is listed, the default branch will be used
+
+      # Optionally a branch can be provided.
+      # Defaults to the default repository branch.
       branch: main
+
       # Optionally a token can be provided, if it differs from the token provided to GoReleaser
-      token: "{{ .Env.KREW_GITHUB_TOKEN }}"
+      token: "{{ .Env.HOMEBREW_TAP_GITHUB_TOKEN }}"
 
     # Template for the url which is determined by the given Token (github or gitlab)
     # Default for github is "https://github.com/<repo_owner>/<repo_name>/releases/download/{{ .Tag }}/{{ .ArtifactName }}"
diff --git a/www/docs/customization/nfpm.md b/www/docs/customization/nfpm.md
index 551be897..8be0f3c0 100644
--- a/www/docs/customization/nfpm.md
+++ b/www/docs/customization/nfpm.md
@@ -53,7 +53,9 @@ nfpms:
 
     # Template to your app's description.
     # Default is empty.
-    description: Software to create fast and easy drum rolls.
+    description: |-
+      Drum rolls installer package.
+      Software to create fast and easy drum rolls.
 
     # Your app's license.
     # Default is empty.
diff --git a/www/docs/customization/scoop.md b/www/docs/customization/scoop.md
index 08369bd3..b934429d 100644
--- a/www/docs/customization/scoop.md
+++ b/www/docs/customization/scoop.md
@@ -19,11 +19,13 @@ scoop:
   bucket:
     owner: user
     name: scoop-bucket
-    # Optionally a branch can be provided. If the branch does not exist, it
-    # will be created. If no branch is listed, the default branch will be used
+
+    # Optionally a branch can be provided.
+    # Defaults to the default repository branch.
     branch: main
+
     # Optionally a token can be provided, if it differs from the token provided to GoReleaser
-    token: "{{ .Env.SCOOP_BUCKET_GITHUB_TOKEN }}"
+    token: "{{ .Env.HOMEBREW_TAP_GITHUB_TOKEN }}"
 
   # Folder inside the repository to put the scoop.
   # Default is the root folder.
diff --git a/www/docs/customization/templates.md b/www/docs/customization/templates.md
index 3235bb0c..08a0435a 100644
--- a/www/docs/customization/templates.md
+++ b/www/docs/customization/templates.md
@@ -45,6 +45,8 @@ On fields that support templating, these fields are always available:
 | `.PrefixedSummary`     | the git summary prefixed with the monorepo config tag prefix (if any)                                  |
 | `.TagSubject`          | the annotated tag message subject, or the message subject of the commit it points out[^6]              |
 | `.TagContents`         | the annotated tag message, or the message of the commit it points out[^7]                              |
+| `.Runtime.Goos`        | equivalent to `runtime.GOOS`                                                                           |
+| `.Runtime.Goarch`      | equivalent to `runtime.GOARCH`                                                                         |
 
 [^1]: The `v` prefix is stripped and it might be changed in `snapshot` and `nightly` builds.
 [^2]: Assuming `Tag` is a valid a SemVer, otherwise empty/zeroed.


Test patch:
diff --git a/internal/pipe/metadata/metadata_test.go b/internal/pipe/metadata/metadata_test.go
index a9c49337..7fdb041c 100644
--- a/internal/pipe/metadata/metadata_test.go
+++ b/internal/pipe/metadata/metadata_test.go
@@ -27,6 +27,10 @@ func TestRun(t *testing.T) {
 		Dist:        tmp,
 		ProjectName: "name",
 	})
+	ctx.Runtime = context.Runtime{
+		Goos:   "fakeos",
+		Goarch: "fakearch",
+	}
 	ctx.Version = "1.2.3"
 	ctx.Git = context.GitInfo{
 		CurrentTag:  "v1.2.3",
diff --git a/internal/pipe/metadata/testdata/TestRun/metadata.json.golden b/internal/pipe/metadata/testdata/TestRun/metadata.json.golden
index b02aa05f..69405bf9 100644
--- a/internal/pipe/metadata/testdata/TestRun/metadata.json.golden
+++ b/internal/pipe/metadata/testdata/TestRun/metadata.json.golden
@@ -1,1 +1,1 @@
-{"project_name":"name","tag":"v1.2.3","previous_tag":"v1.2.2","version":"1.2.3","commit":"aef34a","date":"2022-01-22T10:12:13Z"}
\ No newline at end of file
+{"project_name":"name","tag":"v1.2.3","previous_tag":"v1.2.2","version":"1.2.3","commit":"aef34a","date":"2022-01-22T10:12:13Z","runtime":{"goos":"fakeos","goarch":"fakearch"}}
\ No newline at end of file
diff --git a/internal/tmpl/tmpl_test.go b/internal/tmpl/tmpl_test.go
index c596d0de..0f5e080f 100644
--- a/internal/tmpl/tmpl_test.go
+++ b/internal/tmpl/tmpl_test.go
@@ -3,6 +3,7 @@ package tmpl
 import (
 	"os"
 	"path/filepath"
+	"runtime"
 	"testing"
 	"text/template"
 
@@ -62,6 +63,8 @@ func TestWithArtifact(t *testing.T) {
 		"v1.2.2":                           "{{ .PreviousTag }}",
 		"awesome release":                  "{{ .TagSubject }}",
 		"awesome release\n\nanother line":  "{{ .TagContents }}",
+		"runtime: " + runtime.GOOS:         "runtime: {{ .Runtime.Goos }}",
+		"runtime: " + runtime.GOARCH:       "runtime: {{ .Runtime.Goarch }}",
 	} {
 		tmpl := tmpl
 		expect := expect
diff --git a/pkg/context/context_test.go b/pkg/context/context_test.go
index 7f674009..db0f47f6 100644
--- a/pkg/context/context_test.go
+++ b/pkg/context/context_test.go
@@ -2,6 +2,7 @@ package context
 
 import (
 	"os"
+	"runtime"
 	"testing"
 	"time"
 
@@ -20,6 +21,8 @@ func TestNew(t *testing.T) {
 	require.Equal(t, "BAR", ctx.Env["FOO"])
 	require.Equal(t, "1", ctx.Env["BAR"])
 	require.Equal(t, 4, ctx.Parallelism)
+	require.Equal(t, runtime.GOOS, ctx.Runtime.Goos)
+	require.Equal(t, runtime.GOARCH, ctx.Runtime.Goarch)
 }
 
 func TestNewWithTimeout(t *testing.T) {