From 2074877a3beb858e4ec03e9c923d626308ef484e Mon Sep 17 00:00:00 2001 From: Nightapes Date: Sun, 22 Sep 2019 15:42:08 +0200 Subject: [PATCH 1/3] build(pages): add jemoji plugin --- _config.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 _config.yml diff --git a/_config.yml b/_config.yml new file mode 100644 index 0000000..3544320 --- /dev/null +++ b/_config.yml @@ -0,0 +1,2 @@ +plugins: + - jemoji \ No newline at end of file From 399a3515f211a3eea22d7bd5a429a86fef1f50e6 Mon Sep 17 00:00:00 2001 From: Nightapes Date: Sun, 22 Sep 2019 15:49:06 +0200 Subject: [PATCH 2/3] feat(docker): add docker image with go-semantic-release --- .dockerignore | 2 ++ .github/workflows/main.yml | 28 ++++++++++++++++++++++++++++ .gitignore | 3 ++- .release.yml | 6 +++++- Dockerfile | 9 +++++++++ 5 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..fe0c3b2 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +* +!build/ \ No newline at end of file diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index e35ca00..5f50468 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -34,6 +34,34 @@ jobs: GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o build/go-semantic-release -ldflags "-w -s --X main.version=`./build/go-semantic-release-temp next`" ./cmd/go-semantic-release/ GOOS=windows GOARCH=386 CGO_ENABLED=0 go build -o build/go-semantic-release.exe -ldflags "-w -s -X main.version=`./build/go-semantic-release-temp next`" ./cmd/go-semantic-release/ + - name: Build Docker image for master + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + if: github.ref == 'refs/heads/master' + run: | + docker build -t nightapes/go-semantic-release:latest . + docker build -t nightapes/go-semantic-release:"$(./go-semantic-release next)" . + + - name: Push Docker image for master + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + if: github.ref == 'refs/heads/master' + run: | + docker login -u nightapes -p ${{ secrets.DOCKER_PASSWORD }} + docker push nightapes/go-semantic-release:latest + docker push nightapes/go-semantic-release:"$(./go-semantic-release next)" + + - name: Build Docker image + if: github.ref != 'refs/heads/master' + run: | + docker build -t nightapes/go-semantic-release:development . + + - name: Push Docker image + if: github.ref != 'refs/heads/master' + run: | + docker login -u nightapes -p ${{ secrets.DOCKER_PASSWORD }} + docker push nightapes/go-semantic-release:development + - name: Release env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore index 45eee87..848da49 100644 --- a/.gitignore +++ b/.gitignore @@ -16,4 +16,5 @@ go-semantic-release .vscode/settings.json CHANGELOG.md cover.html -build/ \ No newline at end of file +build/ +.idea/ diff --git a/.release.yml b/.release.yml index 77a2e85..2f68ae0 100644 --- a/.release.yml +++ b/.release.yml @@ -9,4 +9,8 @@ assets: - name: ./build/go-semantic-release compress: false - name: ./build/go-semantic-release.exe - compress: false \ No newline at end of file + compress: false +changelog: + docker: + latest: true + repository: "nightapes/go-semantic-release" \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..58a16af --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM alpine:3.10.2 + +WORKDIR /code + +COPY ./build/go-semantic-release . + +USER 1000 + +ENTRYPOINT [ "./go-semantic-release" ] \ No newline at end of file From 44f95969bf68879962dde676c1dbd8237e98fe38 Mon Sep 17 00:00:00 2001 From: Nightapes Date: Sun, 22 Sep 2019 15:50:12 +0200 Subject: [PATCH 3/3] feat(template): allow custom changelog template --- README.md | 11 +++++ examples/changelog.tmpl | 16 +++++++ internal/changelog/changelog.go | 76 +++++++++++++++++++++++++-------- pkg/config/config.go | 10 ++--- pkg/config/config_test.go | 23 ++-------- 5 files changed, 93 insertions(+), 43 deletions(-) create mode 100644 examples/changelog.tmpl diff --git a/README.md b/README.md index 648fe85..ed53712 100644 --- a/README.md +++ b/README.md @@ -110,9 +110,20 @@ assets: #### Changelog +Following variables can be used for templates: +* `Commits` string +* `Version` string +* `Now` time.Time +* `Backtick` string +* `HasDocker` bool +* `HasDockerLatest` bool +* `DockerRepository` string + ```yml changelog: printAll: false ## Print all valid commits to changelog + title: "v{{.Version}} ({{.Now.Format "2006-01-02"}})" ## Used for releases (go template) + templatePath: "./examples/changelog.tmpl" ## Path to a template file (go template) ``` ##### Docker diff --git a/examples/changelog.tmpl b/examples/changelog.tmpl new file mode 100644 index 0000000..f8f4486 --- /dev/null +++ b/examples/changelog.tmpl @@ -0,0 +1,16 @@ +# My custom release template v{{$.Version}} ({{.Now.Format "2006-01-02"}}) +{{ .Commits -}} +{{ if .HasDocker}} +## Docker image + +New docker image is released under {{$.Backtick}}{{.DockerRepository}}:{{.Version}}{{$.Backtick}} + +### Usage + +{{$.Backtick}}docker run {{.DockerRepository}}:{{.Version}}{{$.Backtick}} +{{ if .HasDockerLatest}} +or + +{{$.Backtick}}docker run {{.DockerRepository}}:latest{{$.Backtick}} +{{ end -}} +{{ end -}} \ No newline at end of file diff --git a/internal/changelog/changelog.go b/internal/changelog/changelog.go index ba1d87c..af6a6e2 100644 --- a/internal/changelog/changelog.go +++ b/internal/changelog/changelog.go @@ -2,6 +2,7 @@ package changelog import ( "bytes" + "io/ioutil" "strings" "text/template" "time" @@ -13,9 +14,7 @@ import ( log "github.com/sirupsen/logrus" ) -const defaultChangelogTitle string = `v{{.Version}} ({{.Now.Format "2006-01-02"}})` -const defaultChangelog string = `# v{{$.Version}} ({{.Now.Format "2006-01-02"}}) -{{ range $index,$commit := .BreakingChanges -}} +const defaultCommitList string = `{{ range $index,$commit := .BreakingChanges -}} {{ if eq $index 0 }} ## BREAKING CHANGES {{ end}} @@ -30,7 +29,10 @@ introduced by commit: * **{{$.Backtick}}{{$commit.Scope}}{{$.Backtick}}** {{$commit.ParsedMessage}} {{if $.HasURL}} ([{{ printf "%.7s" $commit.Commit.Hash}}]({{ replace $.URL "{{hash}}" $commit.Commit.Hash}})) {{end}} {{ end -}} {{ end -}} -{{ end -}} +{{ end -}}` +const defaultChangelogTitle string = `v{{.Version}} ({{.Now.Format "2006-01-02"}})` +const defaultChangelog string = `# v{{$.Version}} ({{.Now.Format "2006-01-02"}}) +{{ .Commits -}} {{ if .HasDocker}} ## Docker image @@ -48,19 +50,26 @@ or ` type changelogContent struct { - Commits map[string][]shared.AnalyzedCommit - BreakingChanges []shared.AnalyzedCommit - Order []string + Commits string Version string Now time.Time Backtick string - HasURL bool - URL string HasDocker bool HasDockerLatest bool DockerRepository string } +type commitsContent struct { + Commits map[string][]shared.AnalyzedCommit + BreakingChanges []shared.AnalyzedCommit + Order []string + Version string + Now time.Time + Backtick string + HasURL bool + URL string +} + //Changelog struct type Changelog struct { config *config.ReleaseConfig @@ -108,30 +117,61 @@ func (c *Changelog) GenerateChanglog(templateConfig shared.ChangelogTemplateConf } } + commitsContent := commitsContent{ + Version: templateConfig.Version, + Commits: commitsPerScope, + Now: c.releaseTime, + BreakingChanges: commitsBreakingChange, + Backtick: "`", + Order: order, + HasURL: templateConfig.CommitURL != "", + URL: templateConfig.CommitURL, + } + changelogContent := changelogContent{ Version: templateConfig.Version, - Commits: commitsPerScope, Now: c.releaseTime, - BreakingChanges: commitsBreakingChange, Backtick: "`", - Order: order, - HasURL: templateConfig.CommitURL != "", - URL: templateConfig.CommitURL, HasDocker: c.config.Changelog.Docker.Repository != "", HasDockerLatest: c.config.Changelog.Docker.Latest, DockerRepository: c.config.Changelog.Docker.Repository, } + template := defaultChangelog + if c.config.Changelog.TemplatePath != "" { + content, err := ioutil.ReadFile(c.config.Changelog.TemplatePath) + if err != nil { + return nil, err + } + template = string(content) + } - title, err := generateTemplate(defaultChangelogTitle, changelogContent) + templateTitle := defaultChangelogTitle + if c.config.Changelog.TemplateTitle != "" { + templateTitle = c.config.Changelog.TemplateTitle + } + + log.Debugf("Render title") + renderedTitle, err := generateTemplate(templateTitle, changelogContent) if err != nil { return nil, err } - content, err := generateTemplate(defaultChangelog, changelogContent) - return &shared.GeneratedChangelog{Title: title, Content: content}, err + log.Debugf("Render commits") + renderedCommitList, err := generateTemplate(defaultCommitList, commitsContent) + if err != nil { + return nil, err + } + + log.Tracef("Commits %s", renderedCommitList) + + changelogContent.Commits = renderedCommitList + log.Debugf("Render changelog") + renderedContent, err := generateTemplate(template, changelogContent) + + return &shared.GeneratedChangelog{Title: renderedTitle, Content: renderedContent}, err } -func generateTemplate(text string, values changelogContent) (string, error) { +func generateTemplate(text string, values interface{}) (string, error) { funcMap := template.FuncMap{ "replace": replace, diff --git a/pkg/config/config.go b/pkg/config/config.go index 1a036b5..6db6f68 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -10,11 +10,11 @@ import ( // ChangelogConfig struct type ChangelogConfig struct { - PrintAll bool `yaml:"printAll,omitempty"` - Template string `yaml:"template,omitempty"` - TemplatePath string `yaml:"templatePath,omitempty"` - Docker ChangelogDocker `yaml:"docker,omitempty"` - NPM ChangelogNPM `yaml:"npm,omitempty"` + PrintAll bool `yaml:"printAll,omitempty"` + TemplateTitle string `yaml:"title,omitempty"` + TemplatePath string `yaml:"templatePath,omitempty"` + Docker ChangelogDocker `yaml:"docker,omitempty"` + NPM ChangelogNPM `yaml:"npm,omitempty"` } //ChangelogDocker type struct diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 3a3a3ce..b2bb0f1 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -74,9 +74,9 @@ github: CommitFormat: "angular", Branch: map[string]string{"add_git_releases": "alpha", "alpha": "alpha", "beta": "beta", "master": "release", "rc": "rc"}, Changelog: config.ChangelogConfig{ - PrintAll: false, - Template: "", - TemplatePath: ""}, + PrintAll: false, + TemplateTitle: "", + TemplatePath: ""}, Release: "github", GitHubProvider: config.GitHubProvider{ Repo: "go-semantic-release", @@ -92,20 +92,3 @@ github: }, result) } - -// func TestWriteNotFound(t *testing.T) { - -// err := cache.Write("notfound/dir", shared.ReleaseVersion{ -// Last: shared.ReleaseVersionEntry{ -// Commit: "12345", -// Version: createVersion("1.0.0"), -// }, -// Next: shared.ReleaseVersionEntry{ -// Commit: "12346", -// Version: createVersion("1.1.0"), -// }, -// Branch: "master", -// }) -// assert.Errorf(t, err, "Write non exsiting file") - -// }