Skip to content

Commit

Permalink
Save packages in pages branch as part of upload command
Browse files Browse the repository at this point in the history
  • Loading branch information
annabarnes1138 committed May 16, 2021
1 parent 465a2e5 commit c5b06f4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
2 changes: 1 addition & 1 deletion cmd/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,5 @@ func init() {
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)")
flags.String("release-name-template", "{{ .Name }}-{{ .Version }}", "Go template for computing release names, using chart metadata")
flags.Bool("packages-with-index", false, "Save a copy of the package files to the GitHub pages branch and reference them in the index")
flags.Bool("packages-with-index", false, "Override the download url to point to files in the GitHub Pages branch instead of the release assets")
}
1 change: 1 addition & 0 deletions cmd/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ func init() {
uploadCmd.Flags().StringP("commit", "c", "", "Target commit for release")
uploadCmd.Flags().Bool("skip-existing", false, "Skip upload if release exists")
uploadCmd.Flags().String("release-name-template", "{{ .Name }}-{{ .Version }}", "Go template for computing release names, using chart metadata")
uploadCmd.Flags().Bool("packages-with-index", false, "Save a copy of the package files to the GitHub Pages branch")
}
54 changes: 29 additions & 25 deletions pkg/releaser/releaser.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/http"
"net/url"
Expand Down Expand Up @@ -180,13 +179,6 @@ func (r *Releaser) UpdateIndexFile() (bool, error) {
if err := r.addToIndexFile(indexFile, downloadUrl.String()); err != nil {
return false, err
}
if r.config.PackagesWithIndex {
indexBasePath := filepath.Dir(r.config.IndexPath)
chartTargetPath := filepath.Join(indexBasePath, filepath.Base(chartPackage))
if err := copyFile(chartPackage, chartTargetPath); err != nil {
return false, err
}
}
update = true
break
}
Expand Down Expand Up @@ -217,24 +209,14 @@ func (r *Releaser) UpdateIndexFile() (bool, error) {
}
defer r.git.RemoveWorktree("", worktree) // nolint, errcheck

indexBasePath := filepath.Dir(r.config.IndexPath)
files, _ := ioutil.ReadDir(indexBasePath)
for _, file := range files {
oldFilePath := filepath.Join(indexBasePath, file.Name())
newFilePath := filepath.Join(worktree, file.Name())
if err := copyFile(oldFilePath, newFilePath); err != nil {
return false, err
}
if err := r.git.Add(worktree, newFilePath); err != nil {
return false, err
}
indexYamlPath := filepath.Join(worktree, "index.yaml")
if err := copyFile(r.config.IndexPath, indexYamlPath); err != nil {
return false, err
}

commitMsg := "Update index.yaml"
if r.config.PackagesWithIndex {
commitMsg = "Publishing chart"
if err := r.git.Add(worktree, indexYamlPath); err != nil {
return false, err
}
if err := r.git.Commit(worktree, commitMsg); err != nil {
if err := r.git.Commit(worktree, "Update index.yaml"); err != nil {
return false, err
}

Expand Down Expand Up @@ -308,8 +290,9 @@ func (r *Releaser) addToIndexFile(indexFile *repo.IndexFile, url string) error {
s := strings.Split(url, "/")
s = s[:len(s)-1]

// the chart will be stored in the same repo as the index file so let's make the path relative
if r.config.PackagesWithIndex {
// the chart will be stored in the same repo as
// the index file so let's make the path relative
s = s[:0]
}

Expand Down Expand Up @@ -362,6 +345,27 @@ func (r *Releaser) CreateReleases() error {
if err := r.github.CreateRelease(context.TODO(), release); err != nil {
return errors.Wrapf(err, "error creating GitHub release %s", releaseName)
}

if r.config.PackagesWithIndex {
worktree, err := r.git.AddWorktree("", r.config.Remote+"/"+r.config.PagesBranch)
if err != nil {
return err
}
defer r.git.RemoveWorktree("", worktree) // nolint, errcheck

pkgTargetPath := filepath.Join(worktree, filepath.Base(p))
if err := copyFile(p, pkgTargetPath); err != nil {
return err
}

if err := r.git.Add(worktree, pkgTargetPath); err != nil {
return err
}

if err := r.git.Commit(worktree, fmt.Sprintf("Publishing chart package for %s", releaseName)); err != nil {
return err
}
}
}

return nil
Expand Down

0 comments on commit c5b06f4

Please sign in to comment.