05.GoReleaser配置 发布

将构建产物发布到包管理器、容器注册表、二进制大对象存储等平台。

本文只保留发布到 GitHub/GitLab/Gitea Release , 其它平台参见: Publish – GoReleaser

Release

GoReleaser 可以使用当前标签创建 GitHub/GitLab/Gitea 发布版本,上传所有构建产物,并根据自上一个标签以来的新提交生成变更日志。

release 部分可以自定义的内容如下:

release:
  # Repository in which the release will be created.
  # Default: extracted from the origin remote URL or empty if its private hosted.
  # You can set only one of either 'github', 'gitlab', or 'gitea'.
  github: # OR gitlab OR gitea
    owner: user
    name: repo

    # Token to use for this release, instead of the default one.
    #
    # Useful when the release repository requires a different token than the
    # one used to build from - for instance, when publishing to a repository
    # in another organization.
    #
    # Since "v2.17" >}}
    # Templates: allowed (environment variables only).
    token: "{{ .Env.RELEASE_GITHUB_TOKEN }}"

  # IDs of the archives to use.
  # Empty means all IDs.
  #
  # Default: [].
  ids:
    - foo
    - bar

  # If set to true, will not auto-publish the release.
  # Note: all GitHub releases start as drafts while artifacts are uploaded.
  # Available only for GitHub and Gitea.
  draft: true

  # Whether to remove existing draft releases with the same name before creating
  # a new one.
  #
  # Only effective if `draft` is set to true.
  # Available only for GitHub.
  replace_existing_draft: true

  # Whether to use an existing draft release as the target release.
  #
  # Available only for GitHub.
  # Since "v2.5"
  use_existing_draft: true

  # Whether to remove an artifact that already exists.
  #
  # Available only for GitHub.
  # This might be a bit expensive (rate-limiting speaking), so it is only done
  # when the upload of an artifact fails with a 422 (which means it already
  # exists in the release).
  # We then grab the list of artifacts from the release, and delete the file
  # that matches the one we're trying to upload.
  # GoReleaser will then retry its upload.
  replace_existing_artifacts: true

  # Useful if you want to delay the creation of the tag in the remote.
  # You can create the tag locally, but not push it, and run GoReleaser.
  # It'll then set the `target_commitish` portion of the GitHub release to the
  # value of this field.
  # Only works on GitHub.
  #
  # Default: ''.
  # Templates: allowed.
  target_commitish: "{{ .Commit }}"

  # This allows to change which tag GitHub will create.
  # Usually you'll use this together with `target_commitish`, or if you want to
  # publish a binary from a monorepo into a public repository somewhere, without
  # the tag prefix.
  #
  # Note: if you change this, you might want to change 'url_template' in the
  # subsequent publishers and announcers.
  #
  # Goreleaser Pro ONLY
  # Default: '{{ .PrefixedCurrentTag }}'.
  # Templates: allowed.
  tag: "{{ .CurrentTag }}"

  # If set, will create a release discussion in the category specified.
  #
  # Warning: do not use categories in the 'Announcement' format.
  #  Check https://github.com/goreleaser/goreleaser/issues/2304 for more info.
  #
  # Default: ''.
  discussion_category_name: General

  # If set to auto, will mark the release as not ready for production
  # in case there is an indicator for this in the tag e.g. v1.0.0-rc1
  # If set to true, will mark the release as not ready for production.
  # Default: false.
  prerelease: auto

  # If set to false, will NOT mark the release as "latest".
  # This prevents it from being shown at the top of the release list,
  # and from being returned when calling https://api.github.com/repos/OWNER/REPO/releases/latest.
  #
  # Available only for GitHub.
  #
  # Default: true.
  # Templates: allowed. Since "v2.6"
  make_latest: true

  # What to do with the release notes in case the release already exists.
  #
  # Valid options are:
  # - `keep-existing`: keep the existing notes
  # - `append`: append the current release notes to the existing notes
  # - `prepend`: prepend the current release notes to the existing notes
  # - `replace`: replace existing notes
  #
  # Default: `keep-existing`.
  mode: append

  # Header for the release body.
  #
  # Templates: allowed.
  header: |
    ## Some title ({{ .Date }})

    Welcome to this new release!    

  # Header for the release body.
  #
  # Goreleaser Pro ONLY
  header:
    # Loads from an URL.
    from_url:
      # Templates: allowed.
      url: https://foo.bar/header.md
      headers:
        x-api-token: "${MYCOMPANY_TOKEN}"

    # Loads from a local file.
    # Overrides `from_url`.
    from_file:
      # Templates: allowed.
      path: ./header.md

  # Footer for the release body.
  #
  # Templates: allowed.
  footer: |
    ## Thanks

    Those were the changes on {{ .Tag }}!    

  # Footer for the release body.
  #
  # Goreleaser Pro ONLY
  footer:
    # Loads from an URL.
    from_url:
      # Templates: allowed.
      url: https://foo.bar/footer.md
      footers:
        x-api-token: "${MYCOMPANY_TOKEN}"

    # Loads from a local file.
    # Overrides `from_url`.
    from_file:
      # Templates: allowed.
      path: ./footer.md

  # You can change the name of the release.
  #
  # Default: '{{.Tag}}' ('{{.PrefixedTag}}' on Pro).
  # Templates: allowed.
  name_template: "{{.ProjectName}}-v{{.Version}} {{.Env.USER}}"

  # You can disable this pipe in order to not create the release on any SCM.
  # Keep in mind that this might also break things that depend on the release
  # URL, for instance, homebrew taps.
  #
  # Templates: allowed.
  disable: true

  # Set this to true if you want to disable just the artifact upload to the SCM.
  # If this is true, GoReleaser will still create the release with the
  # changelog, but won't upload anything to it.
  #
  # Templates: allowed.
  skip_upload: true

  # You can add extra pre-existing files to the release.
  # The filename on the release will be the last part of the path (base).
  # If another file with the same name exists, the last one found will be used.
  #
  # Templates: allowed.
  extra_files:
    - glob: ./path/to/file.txt
    - glob: ./glob/**/to/**/file/**/*
    - glob: ./glob/foo/to/bar/file/foobar/override_from_previous
    - glob: ./single_file.txt
      name_template: file.txt # note that this only works if glob matches 1 file only

  # Additional templated extra files to add to the release.
  # Those files will have their contents pass through the template engine,
  # and its results will be added to the release.
  #
  # Goreleaser Pro ONLY
  # Templates: allowed.
  templated_extra_files:
    - src: LICENSE.tpl
      dst: LICENSE.txt

  # Upload metadata.json and artifacts.json to the release as well.
  include_meta: true

特定平台的配置参见:

自定义 Release 说明

执行Release时可以通过 --release-notes=FILE 标志指定一个自定义 Release 说明文件,GoReleaser 将跳过自身发布说明的生成,转而使用指定的文件中的内容。可以使用 Markdown 格式化文件内容。

在 Unix 系统上,还可以通过 Process substitution - Wikipedia 来内联生成发布说明。若要列出自上一个标签以来的所有提交,但跳过以 Merge 或 docs 开头的提交,可以运行以下命令:

goreleaser release --release-notes <(some_changelog_generator)

可以使用的部分变更日志生成工具::

[NOTE] 如果在运行 GoReleaser 之前就创建了该版本,且该版本的正文中包含某些文本,GoReleaser 不会用其发布说明覆盖这些内容,除非将其配置为这样做(例如,mode: replace)。