DEV Community

Evan Lin
Evan Lin

Posted on • Originally published at evanlin.com on

[TIL][Go] Using GoReleaser to Package Multiple Executables

title: [TIL][Golang] Using GoReleaser to Package Multiple Executables and Multiple Executable Types
published: false
date: 2021-05-26 00:00:00 UTC
tags: 
canonical_url: http://www.evanlin.com/til-goreleaser-custom/
---

![](https://goreleaser.com/static/logo.png)

## Preface:

In the February article, I mentioned [using GoReleaser to package your CLI (Command Line Tool) written in Golang, and using Github Actions to prepare Changelogs](https://dev.to/evanlin/til-golang-golang-cli-command-line-tool-github-actions-changelogs-3jc4-temp-slug-3934292). Through [GoReleaser](https://goreleaser.com/), you can integrate with Github Action, and it can also help you write Changelogs, making version control simpler and more convenient.

However, as the application of services and products expands, there will be more customized needs. So how can your Github Action have more diverse custom settings?

## Basic Requirements: GoReleaser with Github Action

You can refer to the previous article, or quickly learn this article.

<script src="https://gist.github.com/kkdai/d32ea8f7f99a7097e429b194d2c58c56.js"></script>

You can create this as a file in `.github/workflows/release_build.yml`. This is actually more suitable for main.go to be placed directly under the github repo, you can refer to the project form of [https://github.com/kkdai/go-cli-template](https://github.com/kkdai/go-cli-template).

## main.go in sub-folder Main program in sub-folder

Usually when developing some packages, sometimes our main Repository will be related development packages (Package) and will put the CLI part in the `cmd/xxx_cli` folder. When you need to install it, run `go install xxx.com/xxx/uuu/cmd/xxx_cli` to install.

If your `main.go` is not in the main directory of the repo, but in the `cmd/test_cli` directory. This time you will get an error.

![](https://user-images.githubusercontent.com/2252691/119249210-4d062100-bbc9-11eb-9ee1-a60ec8dcc820.png)

Enter fullscreen mode Exit fullscreen mode

goreleaser error: dones not contain a main function


How to modify this?

<script src="https://gist.github.com/kkdai/34e2167df032a859945b487593465bdb.js"></script>

According to the above display, the main modification is in `workdir: ./cmd/test_cli` placed in Steps Run GoReleaser's `with:`. This way you can compile the executable file normally and update the changelogs.

## Multiple related executable files need to be compiled / customized compilation options

Because there are related command restrictions in the `.github/workflows` folder, refer to [github action Workflow syntax](https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#About-yaml-syntax-for-workflows).

If you want more customized compilation options as follows:

- Multiple folders under `/cmd/`.
- Let GoReleaser's compilation options be more, the default will compile everything. You can choose only Mac + UNIX, or add x64 options.
- Add some enviironment variable (e.g. `CGO_ENABLED=0`)
- Give LDFLAGS to go build (e.g. `-s -w -X main.build=`)
- Run a compile shell script through hooks (e.g. `post: ./script.sh `)

If you have the above related customization options, you may need to separate the GoReleaser config into a file, the related approach is as follows:

<script src="https://gist.github.com/kkdai/bbc626648c51f37e297237a1ae7867f6.js"></script>

The main modification is to run the settings through `.goreleaser.yml` by `args: release -f .goreleaser.yml --rm-dist`. This is equivalent to `goreleaser release -f .goreleaser.yml --rm-dist`.

The content of the file can refer to the file above the document.

<script src="https://gist.github.com/kkdai/d82791da7e1a4ea853231ad839b4154b.js"></script>

You can also refer to PR [https://github.com/kkdai/disqus-to-github-issues-go/pull/4](https://github.com/kkdai/disqus-to-github-issues-go/pull/4)

## Conclusion and Future

By referencing an external config file, the GoReleaser of Github Action can have more customization options. In fact, it can not only package, but also has more ways to use [upload files to other Artifactory](https://goreleaser.com/customization/artifactory/) or [Docker build](https://goreleaser.com/customization/docker/). This can also make packaging products simpler and more convenient.

## Related Articles:

- GoReleaser Quick Star [https://goreleaser.com/quick-start/](https://goreleaser.com/quick-start/)
- [🚀 GitHub Action for release your Go projects as fast and easily as possible](https://dev.to/koddr/github-action-for-release-your-go-projects-as-fast-and-easily-as-possible-20a2)
- [Golang Github Actions Starter](https://github.com/actions/starter-workflows/blob/c59b62dee0eae1f9f368b7011cf05c2fc42cf084/ci/go.yml)
- [GoReleaser Builds Configuration](https://goreleaser.com/customization/build/)
- [github action Workflow syntax](https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#About-yaml-syntax-for-workflows)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)