Skip to content
🎉 GoReleaser v2.18 is out! with release summaries, preflight checks, OpenTelemetry traces, and more!

Build Hooks

Both pre and post hooks run for each build target, whether the targets come from a matrix of operating systems and architectures or are listed explicitly in targets, and regardless of the builder.

In addition to simple declarations, you can declare multiple hooks to reuse configuration between different build environments.

.goreleaser.yaml
builds:
  - id: "with-hooks"
    builder: go
    targets:
      - "darwin_amd64"
      - "windows_amd64"
    hooks:
      pre:
        - first-script.sh
        - second-script.sh
      post:
        - upx "{{ .Path }}"
        - codesign -project="{{ .ProjectName }}" "{{ .Path }}"

Each hook can also have its own work directory and environment variables:

.goreleaser.yaml
builds:
  - id: "with-hooks"
    builder: go
    targets:
      - "darwin_amd64"
      - "windows_amd64"
    hooks:
      pre:
        - cmd: first-script.sh
          dir:
            "{{ dir .Dist}}"
            # Always print command output, otherwise only visible in debug mode.
          output: true
          env:
            - HOOK_SPECIFIC_VAR={{ .Env.GLOBAL_VAR }}
        - second-script.sh

All properties of a hook (cmd, dir and env) support templating with post hooks having the binary artifact available (as these run after the build). Additionally the following build details are exposed to both pre and post hooks:

KeyDescription
.NameFilename of the binary, e.g. bin.exe
.ExtExtension, e.g. .exe
.PathAbsolute path to the binary
.TargetBuild target, e.g. darwin_amd64

Environment variables are inherited and overridden in the following order:

  • global (env)
  • build (builds[].env)
  • hook (builds[].hooks.pre[].env and builds[].hooks.post[].env)
Learn more about the template language.
Last updated on