Skip to content

Commit

Permalink
add packages-with-index flag
Browse files Browse the repository at this point in the history
  • Loading branch information
annabarnes1138 committed May 17, 2021
1 parent 6fd1ce2 commit d9f3e57
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 22 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ Flags:
-p, --package-path string Path to directory with chart packages (default ".cr-release-packages")
--release-name-template string Go template for computing release names, using chart metadata (default "{{ .Name }}-{{ .Version }}")
-t, --token string GitHub Auth Token
--packages-with-index Host the package files in the GitHub Pages branch

Global Flags:
--config string Config file (default is $HOME/.cr.yaml)
Expand Down Expand Up @@ -109,6 +110,7 @@ Flags:
-p, --package-path string Path to directory with chart packages (default ".cr-release-packages")
--release-name-template string Go template for computing release names, using chart metadata (default "{{ .Name }}-{{ .Version }}")
-t, --token string GitHub Auth Token (only needed for private repos)
--packages-with-index Host the package files in the GitHub Pages branch

Global Flags:
--config string Config file (default is $HOME/.cr.yaml)
Expand Down
1 change: 1 addition & 0 deletions cr/cmd/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +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, "Host the package files in the GitHub Pages branch")
}
5 changes: 5 additions & 0 deletions cr/cmd/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,9 @@ 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().String("pages-branch", "gh-pages", "The GitHub pages branch")
uploadCmd.Flags().String("remote", "origin", "The Git remote used when creating a local worktree for the GitHub Pages branch")
uploadCmd.Flags().Bool("push", false, "Push index.yaml to the GitHub Pages branch (must not be set if --pr is set)")
uploadCmd.Flags().Bool("pr", false, "Create a pull request for index.yaml against the GitHub Pages branch (must not be set if --push is set)")
uploadCmd.Flags().Bool("packages-with-index", false, "Host the package files in the GitHub Pages branch")
}
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type Options struct {
Remote string `mapstructure:"remote"`
ReleaseNameTemplate string `mapstructure:"release-name-template"`
SkipExisting bool `mapstructure:"skip-existing"`
PackagesWithIndex bool `mapstructure:"packages-with-index"`
}

func LoadConfiguration(cfgFile string, cmd *cobra.Command, requiredFlags []string) (*Options, error) {
Expand Down
83 changes: 61 additions & 22 deletions pkg/releaser/releaser.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,31 +220,10 @@ func (r *Releaser) UpdateIndexFile() (bool, error) {
return false, err
}

pushURL, err := r.git.GetPushURL(r.config.Remote, r.config.Token)
if err != nil {
if err := r.pushToPagesBranch(worktree); err != nil {
return false, err
}

if r.config.Push {
fmt.Printf("Pushing to branch %q\n", r.config.PagesBranch)
if err := r.git.Push(worktree, pushURL, "HEAD:refs/heads/"+r.config.PagesBranch); err != nil {
return false, err
}
} else if r.config.PR {
branch := fmt.Sprintf("chart-releaser-%s", randomString(16))

fmt.Printf("Pushing to branch %q\n", branch)
if err := r.git.Push(worktree, pushURL, "HEAD:refs/heads/"+branch); err != nil {
return false, err
}
fmt.Printf("Creating pull request against branch %q\n", r.config.PagesBranch)
prURL, err := r.github.CreatePullRequest(r.config.Owner, r.config.GitRepo, "Update index.yaml", branch, r.config.PagesBranch)
if err != nil {
return false, err
}
fmt.Println("Pull request created:", prURL)
}

return true, nil
}

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

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]
}

// Add to index
if err := indexFile.MustAdd(c.Metadata, filepath.Base(arch), strings.Join(s, "/"), hash); err != nil {
return err
Expand Down Expand Up @@ -339,6 +324,31 @@ 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
}

if err := r.pushToPagesBranch(worktree); err != nil {
return err
}
}
}

return nil
Expand All @@ -348,6 +358,35 @@ func (r *Releaser) getListOfPackages(dir string) ([]string, error) {
return filepath.Glob(filepath.Join(dir, "*.tgz"))
}

func (r *Releaser) pushToPagesBranch(worktree string) error {
pushURL, err := r.git.GetPushURL(r.config.Remote, r.config.Token)
if err != nil {
return err
}

if r.config.Push {
fmt.Printf("Pushing to branch %q\n", r.config.PagesBranch)
if err := r.git.Push(worktree, pushURL, "HEAD:refs/heads/"+r.config.PagesBranch); err != nil {
return err
}
} else if r.config.PR {
branch := fmt.Sprintf("chart-releaser-%s", randomString(16))

fmt.Printf("Pushing to branch %q\n", branch)
if err := r.git.Push(worktree, pushURL, "HEAD:refs/heads/"+branch); err != nil {
return err
}
fmt.Printf("Creating pull request against branch %q\n", r.config.PagesBranch)
prURL, err := r.github.CreatePullRequest(r.config.Owner, r.config.GitRepo, "Update index.yaml", branch, r.config.PagesBranch)
if err != nil {
return err
}
fmt.Println("Pull request created:", prURL)
}

return nil
}

func copyFile(srcFile string, dstFile string) error {
source, err := os.Open(srcFile)
if err != nil {
Expand Down

0 comments on commit d9f3e57

Please sign in to comment.