diff --git a/README.md b/README.md index 51bcf0fc5..fc57c7294 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# GoReleaser +# GoReleaser goreleaser @@ -38,6 +38,9 @@ For that to work, you need to export a `GITHUB_TOKEN` environment variable with the `repo` scope selected. You can create one [here](https://github.com/settings/tokens/new). +GoReleaser uses the latest [Git tag](https://git-scm.com/book/en/v2/Git-Basics-Tagging) of your repository, +so you need to [create a tag](https://git-scm.com/book/en/v2/Git-Basics-Tagging#Annotated-Tags) first. + This will build `main.go` as `my-binary`, for `Darwin` and `Linux` (`amd64` and `i386`), archive the binary and common files as `.tar.gz`, and finally, publish a new github release in the `user/repo` repository with @@ -52,6 +55,7 @@ repo: user/repo binary_name: my-binary brew: repo: user/homebrew-tap + folder: optional/subfolder/inside/the/repo caveats: "Optional caveats to add to the formulae" ``` diff --git a/config/config.go b/config/config.go index bcdbbf22d..da31b4eb2 100644 --- a/config/config.go +++ b/config/config.go @@ -16,6 +16,7 @@ var filePatterns = []string{"LICENCE*", "LICENSE*", "README*", "CHANGELOG*"} // Homebrew contains the brew section type Homebrew struct { Repo string + Folder string Token string `yaml:"-"` Caveats string } diff --git a/pipeline/brew/brew.go b/pipeline/brew/brew.go index c0d949421..69d329779 100644 --- a/pipeline/brew/brew.go +++ b/pipeline/brew/brew.go @@ -3,9 +3,8 @@ package brew import ( "bytes" "context" - "crypto/sha256" - "fmt" "log" + "path/filepath" "strings" "text/template" @@ -60,41 +59,34 @@ func (Pipe) Run(config config.ProjectConfig) error { client := github.NewClient(tc) owner, repo := split.OnSlash(config.Brew.Repo) - name := config.BinaryName + ".rb" - - log.Println("Updating", name, "on", config.Brew.Repo, "...") + path := filepath.Join(config.Brew.Folder, config.BinaryName+".rb") + log.Println("Updating", path, "on", config.Brew.Repo, "...") out, err := buildFormulae(config, client) if err != nil { return err } - sha, err := formulaeSHA(client, owner, repo, name, out) - if err != nil { + + options := &github.RepositoryContentFileOptions{ + Committer: &github.CommitAuthor{ + Name: github.String("goreleaserbot"), + Email: github.String("bot@goreleaser"), + }, + Content: out.Bytes(), + Message: github.String(config.BinaryName + " version " + config.Git.CurrentTag), + } + + file, _, res, err := client.Repositories.GetContents( + owner, repo, path, &github.RepositoryContentGetOptions{}, + ) + if err != nil && res.StatusCode == 404 { + _, _, err = client.Repositories.CreateFile(owner, repo, path, options) return err } - _, _, err = client.Repositories.UpdateFile( - owner, repo, name, &github.RepositoryContentFileOptions{ - Committer: &github.CommitAuthor{ - Name: github.String("goreleaserbot"), - Email: github.String("bot@goreleaser"), - }, - Content: out.Bytes(), - Message: github.String(config.BinaryName + " version " + config.Git.CurrentTag), - SHA: sha, - }, - ) + options.SHA = file.SHA + _, _, err = client.Repositories.UpdateFile(owner, repo, path, options) return err } -func formulaeSHA(client *github.Client, owner, repo, name string, out bytes.Buffer) (*string, error) { - file, _, _, err := client.Repositories.GetContents( - owner, repo, name, &github.RepositoryContentGetOptions{}, - ) - if err == nil { - return file.SHA, err - } - return github.String(fmt.Sprintf("%s", sha256.Sum256(out.Bytes()))), nil -} - func buildFormulae(config config.ProjectConfig, client *github.Client) (bytes.Buffer, error) { data, err := dataFor(config, client) if err != nil {