Skip to content

Commit 378f527

Browse files
Add option to host the chart package files in the GitHub Pages branch (#123)
* add packages-with-index flag Signed-off-by: Steven Barnes <[email protected]> * Add unit tests Signed-off-by: Steven Barnes <[email protected]> * delete files created by test Signed-off-by: Steven Barnes <[email protected]> * Authenticate to get existing index.yaml Signed-off-by: Steven Barnes <[email protected]> * update docs Signed-off-by: cpanato <[email protected]> * fix lints Signed-off-by: cpanato <[email protected]> * add git pull function Signed-off-by: cpanato <[email protected]> * update help text for upload command Signed-off-by: cpanato <[email protected]> --------- Signed-off-by: Steven Barnes <[email protected]> Signed-off-by: cpanato <[email protected]> Co-authored-by: cpanato <[email protected]>
1 parent 9630f42 commit 378f527

File tree

9 files changed

+267
-91
lines changed

9 files changed

+267
-91
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ Flags:
8686
--skip-existing Skip upload if release exists
8787
-t, --token string GitHub Auth Token
8888
--make-release-latest bool Mark the created GitHub release as 'latest' (default "true")
89+
--packages-with-index Host the package files in the GitHub Pages branch
8990

9091
Global Flags:
9192
--config string Config file (default is $HOME/.cr.yaml)
@@ -118,6 +119,7 @@ Flags:
118119
--release-name-template string Go template for computing release names, using chart metadata (default "{{ .Name }}-{{ .Version }}")
119120
--remote string The Git remote used when creating a local worktree for the GitHub Pages branch (default "origin")
120121
-t, --token string GitHub Auth Token (only needed for private repos)
122+
--packages-with-index Host the package files in the GitHub Pages branch
121123

122124
Global Flags:
123125
--config string Config file (default is $HOME/.cr.yaml)

cr/cmd/index.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,5 @@ func init() {
7676
flags.Bool("push", false, "Push index.yaml to the GitHub Pages branch (must not be set if --pr is set)")
7777
flags.Bool("pr", false, "Create a pull request for index.yaml against the GitHub Pages branch (must not be set if --push is set)")
7878
flags.String("release-name-template", "{{ .Name }}-{{ .Version }}", "Go template for computing release names, using chart metadata")
79+
flags.Bool("packages-with-index", false, "Host the package files in the GitHub Pages branch")
7980
}

cr/cmd/upload.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,9 @@ func init() {
5757
"If it is set to empty string, or the file is not found, the chart description will be used instead. The file is read from the chart package")
5858
uploadCmd.Flags().Bool("generate-release-notes", false, "Whether to automatically generate the name and body for this release. See https://docs.github.com/en/rest/releases/releases")
5959
uploadCmd.Flags().Bool("make-release-latest", true, "Mark the created GitHub release as 'latest'")
60+
uploadCmd.Flags().String("pages-branch", "gh-pages", "The GitHub pages branch")
61+
uploadCmd.Flags().String("remote", "origin", "The Git remote used when creating a local worktree for the GitHub Pages branch")
62+
uploadCmd.Flags().Bool("push", false, "Push the chart package to the GitHub Pages branch (must not be set if --pr is set)")
63+
uploadCmd.Flags().Bool("pr", false, "Create a pull request for the chart package against the GitHub Pages branch (must not be set if --push is set)")
64+
uploadCmd.Flags().Bool("packages-with-index", false, "Host the package files in the GitHub Pages branch")
6065
}

doc/cr_index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ cr index [flags]
2323
-i, --index-path string Path to index file (default ".cr-index/index.yaml")
2424
-o, --owner string GitHub username or organization
2525
-p, --package-path string Path to directory with chart packages (default ".cr-release-packages")
26+
--packages-with-index Host the package files in the GitHub Pages branch
2627
--pages-branch string The GitHub pages branch (default "gh-pages")
2728
--pages-index-path string The GitHub pages index path (default "index.yaml")
2829
--pr Create a pull request for index.yaml against the GitHub Pages branch (must not be set if --push is set)

doc/cr_upload.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,13 @@ cr upload [flags]
2222
--make-release-latest Mark the created GitHub release as 'latest' (default true)
2323
-o, --owner string GitHub username or organization
2424
-p, --package-path string Path to directory with chart packages (default ".cr-release-packages")
25+
--packages-with-index Host the package files in the GitHub Pages branch
26+
--pages-branch string The GitHub pages branch (default "gh-pages")
27+
--pr Create a pull request for the chart package against the GitHub Pages branch (must not be set if --push is set)
28+
--push Push the chart package to the GitHub Pages branch (must not be set if --pr is set)
2529
--release-name-template string Go template for computing release names, using chart metadata (default "{{ .Name }}-{{ .Version }}")
2630
--release-notes-file string Markdown file with chart release notes. If it is set to empty string, or the file is not found, the chart description will be used instead. The file is read from the chart package
31+
--remote string The Git remote used when creating a local worktree for the GitHub Pages branch (default "origin")
2732
--skip-existing Skip upload if release exists
2833
-t, --token string GitHub Auth Token
2934
```

pkg/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ type Options struct {
6161
ReleaseNotesFile string `mapstructure:"release-notes-file"`
6262
GenerateReleaseNotes bool `mapstructure:"generate-release-notes"`
6363
MakeReleaseLatest bool `mapstructure:"make-release-latest"`
64+
PackagesWithIndex bool `mapstructure:"packages-with-index"`
6465
}
6566

6667
func LoadConfiguration(cfgFile string, cmd *cobra.Command, requiredFlags []string) (*Options, error) {

pkg/git/git.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ func (g *Git) Commit(workingDir string, message string) error {
6060
return runCommand(workingDir, command)
6161
}
6262

63+
// UpdateBranch runs 'git pull' with the given args.
64+
func (g *Git) Pull(workingDir string, args ...string) error {
65+
pullArgs := []string{"pull"}
66+
pullArgs = append(pullArgs, args...)
67+
command := exec.Command("git", pullArgs...)
68+
return runCommand(workingDir, command)
69+
}
70+
6371
// Push runs 'git push' with the given args.
6472
func (g *Git) Push(workingDir string, args ...string) error {
6573
pushArgs := []string{"push"}

pkg/releaser/releaser.go

Lines changed: 69 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ type GitHub interface {
5555

5656
type HTTPClient interface {
5757
Get(url string) (*http.Response, error)
58+
GetWithToken(url string, token string) (*http.Response, error)
5859
}
5960

6061
type Git interface {
@@ -63,6 +64,7 @@ type Git interface {
6364
Add(workingDir string, args ...string) error
6465
Commit(workingDir string, message string) error
6566
Push(workingDir string, args ...string) error
67+
Pull(workingDir string, args ...string) error
6668
GetPushURL(remote string, token string) (string, error)
6769
}
6870

@@ -213,36 +215,21 @@ func (r *Releaser) UpdateIndexFile() (bool, error) {
213215
if err := copyFile(r.config.IndexPath, indexYamlPath); err != nil {
214216
return false, err
215217
}
216-
if err := r.git.Add(worktree, indexYamlPath); err != nil {
218+
219+
if err := r.git.Pull(worktree, r.config.Remote, r.config.PagesBranch); err != nil {
217220
return false, err
218221
}
219-
if err := r.git.Commit(worktree, fmt.Sprintf("Update %s", r.config.PagesIndexPath)); err != nil {
222+
223+
if err := r.git.Add(worktree, indexYamlPath); err != nil {
220224
return false, err
221225
}
222226

223-
pushURL, err := r.git.GetPushURL(r.config.Remote, r.config.Token)
224-
if err != nil {
227+
if err := r.git.Commit(worktree, fmt.Sprintf("Update %s", r.config.PagesIndexPath)); err != nil {
225228
return false, err
226229
}
227230

228-
if r.config.Push {
229-
fmt.Printf("Pushing to branch %q\n", r.config.PagesBranch)
230-
if err := r.git.Push(worktree, pushURL, "HEAD:refs/heads/"+r.config.PagesBranch); err != nil {
231-
return false, err
232-
}
233-
} else if r.config.PR {
234-
branch := fmt.Sprintf("chart-releaser-%s", randomString(16))
235-
236-
fmt.Printf("Pushing to branch %q\n", branch)
237-
if err := r.git.Push(worktree, pushURL, "HEAD:refs/heads/"+branch); err != nil {
238-
return false, err
239-
}
240-
fmt.Printf("Creating pull request against branch %q\n", r.config.PagesBranch)
241-
prURL, err := r.github.CreatePullRequest(r.config.Owner, r.config.GitRepo, "Update index.yaml", branch, r.config.PagesBranch)
242-
if err != nil {
243-
return false, err
244-
}
245-
fmt.Println("Pull request created:", prURL)
231+
if err := r.pushToPagesBranch(worktree); err != nil {
232+
return false, err
246233
}
247234

248235
return true, nil
@@ -302,6 +289,12 @@ func (r *Releaser) addToIndexFile(indexFile *repo.IndexFile, url string) error {
302289
s := strings.Split(url, "/")
303290
s = s[:len(s)-1]
304291

292+
if r.config.PackagesWithIndex {
293+
// the chart will be stored in the same repo as
294+
// the index file so let's make the path relative
295+
s = s[:0]
296+
}
297+
305298
// Add to index
306299
if err := indexFile.MustAdd(c.Metadata, filepath.Base(arch), strings.Join(s, "/"), hash); err != nil {
307300
return err
@@ -354,6 +347,31 @@ func (r *Releaser) CreateReleases() error {
354347
if err := r.github.CreateRelease(context.TODO(), release); err != nil {
355348
return errors.Wrapf(err, "error creating GitHub release %s", releaseName)
356349
}
350+
351+
if r.config.PackagesWithIndex {
352+
worktree, err := r.git.AddWorktree("", r.config.Remote+"/"+r.config.PagesBranch)
353+
if err != nil {
354+
return err
355+
}
356+
defer r.git.RemoveWorktree("", worktree) //nolint: errcheck
357+
358+
pkgTargetPath := filepath.Join(worktree, filepath.Base(p))
359+
if err := copyFile(p, pkgTargetPath); err != nil {
360+
return err
361+
}
362+
363+
if err := r.git.Add(worktree, pkgTargetPath); err != nil {
364+
return err
365+
}
366+
367+
if err := r.git.Commit(worktree, fmt.Sprintf("Publishing chart package for %s", releaseName)); err != nil {
368+
return err
369+
}
370+
371+
if err := r.pushToPagesBranch(worktree); err != nil {
372+
return err
373+
}
374+
}
357375
}
358376

359377
return nil
@@ -363,6 +381,35 @@ func (r *Releaser) getListOfPackages(dir string) ([]string, error) {
363381
return filepath.Glob(filepath.Join(dir, "*.tgz"))
364382
}
365383

384+
func (r *Releaser) pushToPagesBranch(worktree string) error {
385+
pushURL, err := r.git.GetPushURL(r.config.Remote, r.config.Token)
386+
if err != nil {
387+
return err
388+
}
389+
390+
if r.config.Push {
391+
fmt.Printf("Pushing to branch %q\n", r.config.PagesBranch)
392+
if err := r.git.Push(worktree, pushURL, "HEAD:refs/heads/"+r.config.PagesBranch); err != nil {
393+
return err
394+
}
395+
} else if r.config.PR {
396+
branch := fmt.Sprintf("chart-releaser-%s", randomString(16))
397+
398+
fmt.Printf("Pushing to branch %q\n", branch)
399+
if err := r.git.Push(worktree, pushURL, "HEAD:refs/heads/"+branch); err != nil {
400+
return err
401+
}
402+
fmt.Printf("Creating pull request against branch %q\n", r.config.PagesBranch)
403+
prURL, err := r.github.CreatePullRequest(r.config.Owner, r.config.GitRepo, "Update index.yaml", branch, r.config.PagesBranch)
404+
if err != nil {
405+
return err
406+
}
407+
fmt.Println("Pull request created:", prURL)
408+
}
409+
410+
return nil
411+
}
412+
366413
func copyFile(srcFile string, dstFile string) error {
367414
source, err := os.Open(srcFile)
368415
if err != nil {

0 commit comments

Comments
 (0)