ref: d2249c50991ba7b00b092aca6e315ca1a4de75a1
parent: f4bf214137ebd24a0d12f16d3a98d9038e6eabd3
author: Bjørn Erik Pedersen <[email protected]>
date: Sun Sep 10 13:14:02 EDT 2017
Set up Hugo release flow on CircleCI This rewrites the release logic to use CircleCI 2.0 and its approve workflow in combination with the state of the release notes to determine what to do next. Fixes #3779
--- /dev/null
+++ b/.circleci/config.yml
@@ -1,0 +1,49 @@
+defaults: &defaults
+ working_directory: /go/src/github.com/gohugoio
+ docker:
+ - image: bepsays/ci-goreleaser:0.30.5-2
+
+version: 2
+jobs:
+ build:
+ <<: *defaults
+ steps:
+ - checkout:
+ path: hugo
+ - run:
+ command: |
+ git clone [email protected]:gohugoio/hugoDocs.git
+ cd hugo
+ make vendor
+ make check
+ - persist_to_workspace:
+ root: .
+ paths: .
+ release:
+ <<: *defaults
+ steps:
+ - attach_workspace:
+ at: /go/src/github.com/gohugoio
+ - run:
+ command: |
+ cd hugo
+ git config --global user.email "[email protected]"
+ git config --global user.name "hugoreleaser"
+ go run -tags release main.go release -r ${CIRCLE_BRANCH}
+
+workflows:
+ version: 2
+ release:
+ jobs:
+ - build:
+ filters:
+ branches:
+ only: /release-.*/
+ - hold:
+ type: approval
+ requires:
+ - build
+ - release:
+ context: org-global
+ requires:
+ - hold
--- a/commands/release.go
+++ b/commands/release.go
@@ -33,8 +33,6 @@
skipPublish bool
try bool
-
- step int
}
func createReleaser() *releaseCommandeer {
@@ -53,7 +51,6 @@
}
r.cmd.PersistentFlags().StringVarP(&r.version, "rel", "r", "", "new release version, i.e. 0.25.1")
- r.cmd.PersistentFlags().IntVarP(&r.step, "step", "s", -1, "release step, defaults to -1 for all steps.")
r.cmd.PersistentFlags().BoolVarP(&r.skipPublish, "skip-publish", "", false, "skip all publishing pipes of the release")
r.cmd.PersistentFlags().BoolVarP(&r.try, "try", "", false, "simulate a release, i.e. no changes")
@@ -64,5 +61,5 @@
if r.version == "" {
return errors.New("must set the --rel flag to the relevant version number")
}
- return releaser.New(r.version, r.step, r.skipPublish, r.try).Run()
+ return releaser.New(r.version, r.skipPublish, r.try).Run()
}
--- a/helpers/hugo.go
+++ b/helpers/hugo.go
@@ -41,6 +41,10 @@
// ParseHugoVersion parses a version string.
func ParseHugoVersion(s string) (HugoVersion, error) {
var vv HugoVersion
+ if strings.HasSuffix(s, "-test") {
+ vv.Suffix = "-test"
+ s = strings.TrimSuffix(s, "-test")
+ }
if strings.Contains(s, "DEV") {
return vv, errors.New("DEV versions not supported by parse")
--- a/helpers/hugo_test.go
+++ b/helpers/hugo_test.go
@@ -53,7 +53,7 @@
func TestParseHugoVersion(t *testing.T) {
require.Equal(t, "0.25", MustParseHugoVersion("0.25").String())
require.Equal(t, "0.25.2", MustParseHugoVersion("0.25.2").String())
-
+ require.Equal(t, "0.25-test", MustParseHugoVersion("0.25-test").String())
_, err := ParseHugoVersion("0.25-DEV")
require.Error(t, err)
}
--- a/releaser/git.go
+++ b/releaser/git.go
@@ -156,8 +156,8 @@
return string(out), nil
}
-func getGitInfos(tag, repoPath string, remote bool) (gitInfos, error) {
- return getGitInfosBefore("HEAD", tag, repoPath, remote)
+func getGitInfos(tag, repo, repoPath string, remote bool) (gitInfos, error) {
+ return getGitInfosBefore("HEAD", tag, repo, repoPath, remote)
}
type countribCount struct {
@@ -213,8 +213,8 @@
return c
}
-func getGitInfosBefore(ref, tag, repoPath string, remote bool) (gitInfos, error) {
-
+func getGitInfosBefore(ref, tag, repo, repoPath string, remote bool) (gitInfos, error) {
+ client := newGitHubAPI(repo)
var g gitInfos
log, err := gitLogBefore(ref, tag, repoPath)
@@ -234,7 +234,7 @@
Body: items[3],
}
if remote {
- gc, err := fetchCommit(gi.Hash)
+ gc, err := client.fetchCommit(gi.Hash)
if err == nil {
gi.GitHubCommit = &gc
}
--- a/releaser/git_test.go
+++ b/releaser/git_test.go
@@ -14,7 +14,6 @@
package releaser
import (
- "os"
"testing"
"github.com/stretchr/testify/require"
@@ -22,7 +21,7 @@
func TestGitInfos(t *testing.T) {
skipIfCI(t)
- infos, err := getGitInfos("v0.20", "", false)
+ infos, err := getGitInfos("v0.20", "hugo", "", false)
require.NoError(t, err)
require.True(t, len(infos) > 0)
@@ -68,7 +67,7 @@
}
func skipIfCI(t *testing.T) {
- if os.Getenv("CI") != "" {
+ if isCI() {
// Travis has an ancient git with no --invert-grep: https://github.com/travis-ci/travis-ci/issues/6328
// Also Travis clones very shallowly, making some of the tests above shaky.
t.Skip("Skip git test on Linux to make Travis happy.")
--- a/releaser/github.go
+++ b/releaser/github.go
@@ -6,14 +6,29 @@
"io/ioutil"
"net/http"
"os"
+ "strings"
)
var (
- gitHubCommitsApi = "https://api.github.com/repos/gohugoio/hugo/commits/%s"
- gitHubRepoApi = "https://api.github.com/repos/gohugoio/hugo"
- gitHubContributorsApi = "https://api.github.com/repos/gohugoio/hugo/contributors"
+ gitHubCommitsAPI = "https://api.github.com/repos/gohugoio/REPO/commits/%s"
+ gitHubRepoAPI = "https://api.github.com/repos/gohugoio/REPO"
+ gitHubContributorsAPI = "https://api.github.com/repos/gohugoio/REPO/contributors"
)
+type gitHubAPI struct {
+ commitsAPITemplate string
+ repoAPI string
+ contributorsAPITemplate string
+}
+
+func newGitHubAPI(repo string) *gitHubAPI {
+ return &gitHubAPI{
+ commitsAPITemplate: strings.Replace(gitHubCommitsAPI, "REPO", repo, -1),
+ repoAPI: strings.Replace(gitHubRepoAPI, "REPO", repo, -1),
+ contributorsAPITemplate: strings.Replace(gitHubContributorsAPI, "REPO", repo, -1),
+ }
+}
+
type gitHubCommit struct {
Author gitHubAuthor `json:"author"`
HtmlURL string `json:"html_url"`
@@ -42,10 +57,10 @@
Contributions int `json:"contributions"`
}
-func fetchCommit(ref string) (gitHubCommit, error) {
+func (g *gitHubAPI) fetchCommit(ref string) (gitHubCommit, error) {
var commit gitHubCommit
- u := fmt.Sprintf(gitHubCommitsApi, ref)
+ u := fmt.Sprintf(g.commitsAPITemplate, ref)
req, err := http.NewRequest("GET", u, nil)
if err != nil {
@@ -57,10 +72,10 @@
return commit, err
}
-func fetchRepo() (gitHubRepo, error) {
+func (g *gitHubAPI) fetchRepo() (gitHubRepo, error) {
var repo gitHubRepo
- req, err := http.NewRequest("GET", gitHubRepoApi, nil)
+ req, err := http.NewRequest("GET", g.repoAPI, nil)
if err != nil {
return repo, err
}
@@ -75,7 +90,7 @@
for {
page++
var currPage []gitHubContributor
- url := fmt.Sprintf(gitHubContributorsApi+"?page=%d", page)
+ url := fmt.Sprintf(g.contributorsAPITemplate+"?page=%d", page)
req, err = http.NewRequest("GET", url, nil)
if err != nil {
--- a/releaser/github_test.go
+++ b/releaser/github_test.go
@@ -23,7 +23,8 @@
func TestGitHubLookupCommit(t *testing.T) {
skipIfNoToken(t)
- commit, err := fetchCommit("793554108763c0984f1a1b1a6ee5744b560d78d0")
+ client := newGitHubAPI("hugo")
+ commit, err := client.fetchCommit("793554108763c0984f1a1b1a6ee5744b560d78d0")
require.NoError(t, err)
fmt.Println(commit)
}
@@ -30,7 +31,8 @@
func TestFetchRepo(t *testing.T) {
skipIfNoToken(t)
- repo, err := fetchRepo()
+ client := newGitHubAPI("hugo")
+ repo, err := client.fetchRepo()
require.NoError(t, err)
fmt.Println(">>", len(repo.Contributors))
}
--- a/releaser/releasenotes_writer.go
+++ b/releaser/releasenotes_writer.go
@@ -139,9 +139,10 @@
}
func writeReleaseNotes(version string, infosMain, infosDocs gitInfos, to io.Writer) error {
+ client := newGitHubAPI("hugo")
changes := gitInfosToChangeLog(infosMain, infosDocs)
changes.Version = version
- repo, err := fetchRepo()
+ repo, err := client.fetchRepo()
if err == nil {
changes.Repo = &repo
}
@@ -190,17 +191,43 @@
return f.Name(), nil
}
-func getReleaseNotesDocsTempDirAndName(version string) (string, string) {
+func getReleaseNotesDocsTempDirAndName(version string, final bool) (string, string) {
+ if final {
+ return hugoFilepath("temp"), fmt.Sprintf("%s-relnotes-ready.md", version)
+ }
return hugoFilepath("temp"), fmt.Sprintf("%s-relnotes.md", version)
}
-func getReleaseNotesDocsTempFilename(version string) string {
- return filepath.Join(getReleaseNotesDocsTempDirAndName(version))
+func getReleaseNotesDocsTempFilename(version string, final bool) string {
+ return filepath.Join(getReleaseNotesDocsTempDirAndName(version, final))
}
+func (r *ReleaseHandler) releaseNotesState(version string) (releaseNotesState, error) {
+ docsTempPath, name := getReleaseNotesDocsTempDirAndName(version, false)
+ _, err := os.Stat(filepath.Join(docsTempPath, name))
+
+ if err == nil {
+ return releaseNotesCreated, nil
+ }
+
+ docsTempPath, name = getReleaseNotesDocsTempDirAndName(version, true)
+ _, err = os.Stat(filepath.Join(docsTempPath, name))
+
+ if err == nil {
+ return releaseNotesReady, nil
+ }
+
+ if !os.IsNotExist(err) {
+ return releaseNotesNone, err
+ }
+
+ return releaseNotesNone, nil
+
+}
+
func (r *ReleaseHandler) writeReleaseNotesToTemp(version string, infosMain, infosDocs gitInfos) (string, error) {
- docsTempPath, name := getReleaseNotesDocsTempDirAndName(version)
+ docsTempPath, name := getReleaseNotesDocsTempDirAndName(version, false)
var (
w io.WriteCloser
--- a/releaser/releasenotes_writer_test.go
+++ b/releaser/releasenotes_writer_test.go
@@ -34,7 +34,7 @@
var b bytes.Buffer
// TODO(bep) consider to query GitHub directly for the gitlog with author info, probably faster.
- infos, err := getGitInfosBefore("HEAD", "v0.20", "", false)
+ infos, err := getGitInfosBefore("HEAD", "v0.20", "hugo", "", false)
require.NoError(t, err)
require.NoError(t, writeReleaseNotes("0.21", infos, infos, &b))
--- a/releaser/releaser.go
+++ b/releaser/releaser.go
@@ -31,15 +31,18 @@
const commitPrefix = "releaser:"
+type releaseNotesState int
+
+const (
+ releaseNotesNone = iota
+ releaseNotesCreated
+ releaseNotesReady
+)
+
// ReleaseHandler provides functionality to release a new version of Hugo.
type ReleaseHandler struct {
cliVersion string
- // If set, we do the releases in 3 steps:
- // 1: Create and write a draft release note
- // 2: Prepare files for new version
- // 3: Release
- step int
skipPublish bool
// Just simulate, no actual changes.
@@ -48,29 +51,14 @@
git func(args ...string) (string, error)
}
-func (r ReleaseHandler) shouldRelease() bool {
- return r.step < 1 || r.shouldContinue()
-}
-
-func (r ReleaseHandler) shouldContinue() bool {
- return r.step >= 3
-}
-
-func (r ReleaseHandler) shouldPrepareReleasenotes() bool {
- return r.step < 1 || r.step == 1
-}
-
-func (r ReleaseHandler) shouldPrepareVersions() bool {
- return r.step < 1 || r.step == 2 || r.step > 3
-}
-
func (r ReleaseHandler) calculateVersions() (helpers.HugoVersion, helpers.HugoVersion) {
-
newVersion := helpers.MustParseHugoVersion(r.cliVersion)
finalVersion := newVersion
finalVersion.PatchLevel = 0
- newVersion.Suffix = ""
+ if newVersion.Suffix != "-test" {
+ newVersion.Suffix = ""
+ }
if newVersion.PatchLevel == 0 {
finalVersion = finalVersion.Next()
@@ -82,8 +70,11 @@
}
// New initialises a ReleaseHandler.
-func New(version string, step int, skipPublish, try bool) *ReleaseHandler {
- rh := &ReleaseHandler{cliVersion: version, step: step, skipPublish: skipPublish, try: try}
+func New(version string, skipPublish, try bool) *ReleaseHandler {
+ // When triggered from CI release branch
+ version = strings.TrimPrefix(version, "release-")
+ version = strings.TrimPrefix(version, "v")
+ rh := &ReleaseHandler{cliVersion: version, skipPublish: skipPublish, try: try}
if try {
rh.git = func(args ...string) (string, error) {
@@ -133,20 +124,38 @@
var (
gitCommits gitInfos
gitCommitsDocs gitInfos
+ relNotesState releaseNotesState
)
- if r.shouldPrepareReleasenotes() || r.shouldRelease() {
- gitCommits, err = getGitInfos(changeLogFromTag, "", !r.try)
+ relNotesState, err = r.releaseNotesState(version)
+ if err != nil {
+ return err
+ }
+
+ prepareRelaseNotes := relNotesState == releaseNotesNone
+ shouldRelease := relNotesState == releaseNotesReady
+
+ defer r.gitPush() // TODO(bep)
+
+ if prepareRelaseNotes || shouldRelease {
+ gitCommits, err = getGitInfos(changeLogFromTag, "hugo", "", !r.try)
if err != nil {
return err
}
- gitCommitsDocs, err = getGitInfos(changeLogFromTag, "../hugoDocs", !r.try)
+
+ // TODO(bep) explicit tag?
+ gitCommitsDocs, err = getGitInfos("", "hugoDocs", "../hugoDocs", !r.try)
if err != nil {
return err
}
}
- if r.shouldPrepareReleasenotes() {
+ if relNotesState == releaseNotesCreated {
+ fmt.Println("Release notes created, but not ready. Reneame to *-ready.md to continue ...")
+ return nil
+ }
+
+ if prepareRelaseNotes {
releaseNotesFile, err := r.writeReleaseNotesToTemp(version, gitCommits, gitCommitsDocs)
if err != nil {
return err
@@ -155,33 +164,30 @@
if _, err := r.git("add", releaseNotesFile); err != nil {
return err
}
- if _, err := r.git("commit", "-m", fmt.Sprintf("%s Add release notes draft for %s\n\n[ci skip]", commitPrefix, newVersion)); err != nil {
+ if _, err := r.git("commit", "-m", fmt.Sprintf("%s Add release notes draft for %s\n\nRename to *-ready.md to continue. [ci skip]", commitPrefix, newVersion)); err != nil {
return err
}
}
- if r.shouldPrepareVersions() {
+ if !shouldRelease {
+ fmt.Printf("Skip release ... ")
+ return nil
+ }
- // For docs, for now we assume that:
- // The /docs subtree is up to date and ready to go.
- // The hugoDocs/dev and hugoDocs/master must be merged manually after release.
- // TODO(bep) improve this when we see how it works.
+ // For docs, for now we assume that:
+ // The /docs subtree is up to date and ready to go.
+ // The hugoDocs/dev and hugoDocs/master must be merged manually after release.
+ // TODO(bep) improve this when we see how it works.
- if err := r.bumpVersions(newVersion); err != nil {
- return err
- }
-
- if _, err := r.git("commit", "-a", "-m", fmt.Sprintf("%s Bump versions for release of %s\n\n[ci skip]", commitPrefix, newVersion)); err != nil {
- return err
- }
+ if err := r.bumpVersions(newVersion); err != nil {
+ return err
}
- if !r.shouldRelease() {
- fmt.Printf("Skip release ... Use --state=%d for next or --state=4 to finish\n", r.step+1)
- return nil
+ if _, err := r.git("commit", "-a", "-m", fmt.Sprintf("%s Bump versions for release of %s\n\n[ci skip]", commitPrefix, newVersion)); err != nil {
+ return err
}
- releaseNotesFile := getReleaseNotesDocsTempFilename(version)
+ releaseNotesFile := getReleaseNotesDocsTempFilename(version, true)
// Write the release notes to the docs site as well.
docFile, err := r.writeReleaseNotesToDocs(version, releaseNotesFile)
@@ -196,12 +202,14 @@
return err
}
- if _, err := r.git("tag", "-a", tag, "-m", fmt.Sprintf("%s %s [ci deploy]", commitPrefix, newVersion)); err != nil {
+ if _, err := r.git("tag", "-a", tag, "-m", fmt.Sprintf("%s %s [ci skip]", commitPrefix, newVersion)); err != nil {
return err
}
- if _, err := r.git("push", "origin", tag); err != nil {
- return err
+ if !r.skipPublish {
+ if _, err := r.git("push", "origin", tag); err != nil {
+ return err
+ }
}
if err := r.release(releaseNotesFile); err != nil {
@@ -226,6 +234,15 @@
return nil
}
+func (r *ReleaseHandler) gitPush() {
+ if r.skipPublish {
+ return
+ }
+ if _, err := r.git("push", "origin", "HEAD"); err != nil {
+ log.Fatal("push failed:", err)
+ }
+}
+
func (r *ReleaseHandler) release(releaseNotesFile string) error {
if r.try {
fmt.Println("Skip goreleaser...")
@@ -243,19 +260,16 @@
}
func (r *ReleaseHandler) bumpVersions(ver helpers.HugoVersion) error {
- fromDev := ""
toDev := ""
if ver.Suffix != "" {
- toDev = "-DEV"
- } else {
- fromDev = "-DEV"
+ toDev = ver.Suffix
}
if err := r.replaceInFile("helpers/hugo.go",
`Number:(\s{4,})(.*),`, fmt.Sprintf(`Number:${1}%.2f,`, ver.Number),
`PatchLevel:(\s*)(.*),`, fmt.Sprintf(`PatchLevel:${1}%d,`, ver.PatchLevel),
- fmt.Sprintf(`Suffix:(\s{4,})"%s",`, fromDev), fmt.Sprintf(`Suffix:${1}"%s",`, toDev)); err != nil {
+ `Suffix:(\s{4,})".*",`, fmt.Sprintf(`Suffix:${1}"%s",`, toDev)); err != nil {
return err
}
@@ -324,4 +338,8 @@
log.Fatal(err)
}
return filepath.Join(pwd, filename)
+}
+
+func isCI() bool {
+ return os.Getenv("CI") != ""
}