github.com/szyn/goreleaser@v0.76.1-0.20180517112710-333da09a1297/www/content/build.md (about)

     1  ---
     2  title: Builds
     3  series: customization
     4  hideFromIndex: true
     5  weight: 30
     6  ---
     7  
     8  Builds can be customized in multiple ways. You can specify for which `GOOS` and
     9  `GOARCH` binaries are generated, and you can changed the name of the binary, flags, `ldflags`, hooks, etc.
    10  
    11  Here is a commented `builds` section with all fields specified:
    12  
    13  ```yml
    14  # .goreleaser.yml
    15  builds:
    16    # You can have multiple builds defined as a yaml list
    17    -
    18      # Path to main.go file or main package.
    19      # Default is `.`.
    20      main: ./cmd/main.go
    21  
    22      # Name of the binary.
    23      # This is parsed with the Go template engine and the following variables
    24      # are available:
    25      # - Date
    26      # - Commit
    27      # - Tag
    28      # - Version (Git tag without `v` prefix)
    29      # Date format is `2006-01-02_15:04:05`.
    30      # Default is the name of the project directory.
    31      binary: program
    32  
    33      # Set flags for custom build tags.
    34      # Default is empty.
    35      flags:
    36        - -tags
    37        - dev
    38  
    39      # Custom asmflags templates.
    40      # These are parsed with the Go template engine and the following variables
    41      # are available:
    42      # - Date
    43      # - Commit
    44      # - Tag
    45      # - Version (Git tag without `v` prefix)
    46      # - Env (environment variables)
    47      # Date format is `2006-01-02_15:04:05`.
    48      # You can use the `time` function instead of `Date`, for example:
    49      # `time "2006-01-02"` too if you need custom formats
    50      #
    51      # Default is empty.
    52      asmflags:
    53        - -D mysymbol
    54        - all=-trimpath={{.Env.GOPATH}}
    55  
    56      # Custom gcflags templates.
    57      # These are parsed with the Go template engine and the following variables
    58      # are available:
    59      # - Date
    60      # - Commit
    61      # - Tag
    62      # - Version (Git tag without `v` prefix)
    63      # - Env (environment variables)
    64      # Date format is `2006-01-02_15:04:05`.
    65      # You can use the `time` function instead of `Date`, for example:
    66      # `time "2006-01-02"` too if you need custom formats
    67      #
    68      # Default is empty.
    69      gcflags:
    70        - all=-trimpath={{.Env.GOPATH}}
    71        - ./dontoptimizeme=-N
    72  
    73      # Custom ldflags templates.
    74      # These are parsed with the Go template engine and the following variables
    75      # are available:
    76      # - Date
    77      # - Commit
    78      # - Tag
    79      # - Version (Git tag without `v` prefix)
    80      # - Env (environment variables)
    81      # Date format is `2006-01-02_15:04:05`.
    82      # You can use the `time` function instead of `Date`, for example:
    83      # `time "2006-01-02"` too if you need custom formats
    84      #
    85      # Default is `-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}`.
    86      ldflags:
    87       - -s -w -X main.build={{.Version}}
    88       - ./usemsan=-msan
    89  
    90      # Custom environment variables to be set during the builds.
    91      # Default is empty.
    92      env:
    93        - CGO_ENABLED=0
    94  
    95      # GOOS list to build for.
    96      # For more info refer to: https://golang.org/doc/install/source#environment
    97      # Defaults are darwin and linux.
    98      goos:
    99        - freebsd
   100        - windows
   101  
   102      # GOARCH to build for.
   103      # For more info refer to: https://golang.org/doc/install/source#environment
   104      # Defaults are 386 and amd64.
   105      goarch:
   106        - amd64
   107        - arm
   108        - arm64
   109  
   110      # GOARM to build for when GOARCH is arm.
   111      # For more info refer to: https://golang.org/doc/install/source#environment
   112      # Default is only 6.
   113      goarm:
   114        - 6
   115        - 7
   116  
   117      # List of combinations of GOOS + GOARCH + GOARM to ignore.
   118      # Default is empty.
   119      ignore:
   120        - goos: darwin
   121          goarch: 386
   122        - goos: linux
   123          goarch: arm
   124          goarm: 7
   125  
   126      # Hooks can be used to customize the final binary,
   127      # for example, to run generators.
   128      # Default is both hooks empty.
   129      hooks:
   130        pre: rice embed-go
   131        post: ./script.sh
   132  ```
   133  
   134  ## Passing environment variables to ldflags
   135  
   136  You can do that by using `{{ .Env.VARIABLE_NAME }}` in the template, for
   137  example:
   138  
   139  ```yaml
   140  builds:
   141    - ldflags:
   142     - -s -w -X "main.goversion={{.Env.GOVERSION}}"
   143  ```
   144  
   145  Then you can run:
   146  
   147  ```console
   148  GOVERSION=$(go version) goreleaser
   149  ```