diff --git a/README.md b/README.md index 98f6583a..c83fd225 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,7 @@ Flags: -o, --owner string GitHub username or organization -p, --package-path string Path to directory with chart packages (default ".cr-release-packages") --pages-branch string The GitHub pages branch (default "gh-pages") + --pages-index-path string The GitHub pages index path (default "index.yaml") --pr Create a pull request for index.yaml against the GitHub Pages branch (must not be set if --push is set) --push Push index.yaml to the GitHub Pages branch (must not be set if --pr is set) --release-name-template string Go template for computing release names, using chart metadata (default "{{ .Name }}-{{ .Version }}") diff --git a/cr/cmd/index.go b/cr/cmd/index.go index 1177ede5..61d1bb01 100644 --- a/cr/cmd/index.go +++ b/cr/cmd/index.go @@ -71,6 +71,7 @@ func init() { flags.StringP("git-base-url", "b", "https://api.github.com/", "GitHub Base URL (only needed for private GitHub)") flags.StringP("git-upload-url", "u", "https://uploads.github.com/", "GitHub Upload URL (only needed for private GitHub)") flags.String("pages-branch", "gh-pages", "The GitHub pages branch") + flags.String("pages-index-path", "index.yaml", "The GitHub pages index path") flags.String("remote", "origin", "The Git remote used when creating a local worktree for the GitHub Pages branch") flags.Bool("push", false, "Push index.yaml to the GitHub Pages branch (must not be set if --pr is set)") flags.Bool("pr", false, "Create a pull request for index.yaml against the GitHub Pages branch (must not be set if --push is set)") diff --git a/pkg/config/config.go b/pkg/config/config.go index 982af1cb..be822b57 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -52,6 +52,7 @@ type Options struct { GitUploadURL string `mapstructure:"git-upload-url"` Commit string `mapstructure:"commit"` PagesBranch string `mapstructure:"pages-branch"` + PagesIndexPath string `mapstructure:"pages-index-path"` Push bool `mapstructure:"push"` PR bool `mapstructure:"pr"` Remote string `mapstructure:"remote"` diff --git a/pkg/releaser/releaser.go b/pkg/releaser/releaser.go index 6cea94df..c84916cd 100644 --- a/pkg/releaser/releaser.go +++ b/pkg/releaser/releaser.go @@ -93,14 +93,14 @@ func NewReleaser(config *config.Options, github GitHub, git Git) *Releaser { // UpdateIndexFile updates the index.yaml file for a given Git repo func (r *Releaser) UpdateIndexFile() (bool, error) { - // if path doesn't end with index.yaml we can try and fix it + // if index-path doesn't end with index.yaml we can try and fix it if filepath.Base(r.config.IndexPath) != "index.yaml" { // if path is a directory then add index.yaml if stat, err := os.Stat(r.config.IndexPath); err == nil && stat.IsDir() { r.config.IndexPath = filepath.Join(r.config.IndexPath, "index.yaml") // otherwise error out } else { - fmt.Printf("path (%s) should be a directory or a file called index.yaml\n", r.config.IndexPath) + fmt.Printf("index-path (%s) should be a directory or a file called index.yaml\n", r.config.IndexPath) os.Exit(1) } } @@ -111,7 +111,19 @@ func (r *Releaser) UpdateIndexFile() (bool, error) { return false, err } defer r.git.RemoveWorktree("", worktree) // nolint: errcheck - indexYamlPath := filepath.Join(worktree, "index.yaml") + + // if pages-index-path doesn't end with index.yaml we can try and fix it + if filepath.Base(r.config.PagesIndexPath) != "index.yaml" { + // if path is a directory then add index.yaml + if stat, err := os.Stat(filepath.Join(worktree, r.config.PagesIndexPath)); err == nil && stat.IsDir() { + r.config.PagesIndexPath = filepath.Join(r.config.PagesIndexPath, "index.yaml") + // otherwise error out + } else { + fmt.Printf("pages-index-path (%s) should be a directory or a file called index.yaml\n", r.config.PagesIndexPath) + os.Exit(1) + } + } + indexYamlPath := filepath.Join(worktree, r.config.PagesIndexPath) var indexFile *repo.IndexFile _, err = os.Stat(indexYamlPath) @@ -197,7 +209,7 @@ func (r *Releaser) UpdateIndexFile() (bool, error) { if err := r.git.Add(worktree, indexYamlPath); err != nil { return false, err } - if err := r.git.Commit(worktree, "Update index.yaml"); err != nil { + if err := r.git.Commit(worktree, fmt.Sprintf("Update %s", r.config.PagesIndexPath)); err != nil { return false, err } diff --git a/pkg/releaser/releaser_test.go b/pkg/releaser/releaser_test.go index 6676c6df..e6d0ce2b 100644 --- a/pkg/releaser/releaser_test.go +++ b/pkg/releaser/releaser_test.go @@ -120,6 +120,19 @@ func TestReleaser_UpdateIndexFile(t *testing.T) { git: &FakeGit{"testdata/repo/index.yaml"}, }, }, + { + "index-file-exists-pages-index-path", + true, + &Releaser{ + config: &config.Options{ + IndexPath: "testdata/index/index.yaml", + PackagePath: "testdata/release-packages", + PagesIndexPath: "./", + }, + github: fakeGitHub, + git: &FakeGit{"testdata/repo/index.yaml"}, + }, + }, { "index-file-does-not-exist", false, @@ -132,6 +145,19 @@ func TestReleaser_UpdateIndexFile(t *testing.T) { git: &FakeGit{""}, }, }, + { + "index-file-does-not-exist-pages-index-path", + false, + &Releaser{ + config: &config.Options{ + IndexPath: filepath.Join(indexDir, "index.yaml"), + PackagePath: "testdata/release-packages", + PagesIndexPath: "./", + }, + github: fakeGitHub, + git: &FakeGit{""}, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {