stack.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package actions
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "github.com/google/go-github/v41/github"
  7. "gopkg.in/yaml.v2"
  8. )
  9. type GithubPROpts struct {
  10. Client *github.Client
  11. GitRepoOwner, GitRepoName string
  12. ApplyWorkflowYAML string
  13. StackName string
  14. ProjectID, ClusterID uint
  15. PorterToken string
  16. ServerURL string
  17. DefaultBranch string
  18. }
  19. type GetStackApplyActionYAMLOpts struct {
  20. ServerURL string
  21. StackName string
  22. ProjectID, ClusterID uint
  23. DefaultBranch string
  24. SecretName string
  25. }
  26. func OpenGithubPR(opts *GithubPROpts) error {
  27. // create porter secret
  28. secretName := fmt.Sprintf("PORTER_STACK_%d_%d", opts.ProjectID, opts.ClusterID)
  29. err := createGithubSecret(
  30. opts.Client,
  31. secretName,
  32. opts.PorterToken,
  33. opts.GitRepoOwner,
  34. opts.GitRepoName,
  35. )
  36. if err != nil {
  37. return err
  38. }
  39. applyWorkflowYAML, err := getStackApplyActionYAML(&GetStackApplyActionYAMLOpts{
  40. ServerURL: opts.ServerURL,
  41. ClusterID: opts.ClusterID,
  42. ProjectID: opts.ProjectID,
  43. StackName: opts.StackName,
  44. DefaultBranch: opts.DefaultBranch,
  45. SecretName: secretName,
  46. })
  47. if err != nil {
  48. return err
  49. }
  50. err = createNewBranch(opts.Client,
  51. opts.GitRepoOwner,
  52. opts.GitRepoName,
  53. opts.DefaultBranch,
  54. "porter-stack")
  55. if err != nil {
  56. return fmt.Errorf(
  57. "Unable to create PR to merge workflow files into protected branch: %s.\n"+
  58. "To enable Porter Preview Environment deployments, please create Github workflow "+
  59. "files in this branch with the following contents:\n"+
  60. "--------\n%s--------\nERROR: %w",
  61. opts.DefaultBranch, string(applyWorkflowYAML), ErrCreatePRForProtectedBranch,
  62. )
  63. }
  64. _, err = commitWorkflowFile(
  65. opts.Client,
  66. fmt.Sprintf("porter_stack_%s.yml", strings.ToLower(opts.StackName)),
  67. applyWorkflowYAML, opts.GitRepoOwner,
  68. opts.GitRepoName, "porter-preview", false,
  69. )
  70. if err != nil {
  71. return fmt.Errorf(
  72. "Unable to create PR to merge workflow files into protected branch: %s.\n"+
  73. "To enable Porter Preview Environment deployments, please create Github workflow "+
  74. "files in this branch with the following contents:\n"+
  75. "--------\n%s--------\nERROR: %w",
  76. opts.DefaultBranch, string(applyWorkflowYAML), ErrCreatePRForProtectedBranch,
  77. )
  78. }
  79. _, _, err = opts.Client.PullRequests.Create(
  80. context.Background(), opts.GitRepoOwner, opts.GitRepoName, &github.NewPullRequest{
  81. Title: github.String("Enable Porter Preview Environment deployments"),
  82. Base: github.String(opts.DefaultBranch),
  83. Head: github.String("porter-preview"),
  84. },
  85. )
  86. if err != nil {
  87. return err
  88. }
  89. return nil
  90. }
  91. func getStackApplyActionYAML(opts *GetStackApplyActionYAMLOpts) ([]byte, error) {
  92. gaSteps := []GithubActionYAMLStep{
  93. getCheckoutCodeStep(),
  94. getSetTagStep(),
  95. getDeployStackStep(
  96. opts.ServerURL,
  97. opts.SecretName,
  98. opts.StackName,
  99. "v0.1.0",
  100. opts.ProjectID,
  101. opts.ClusterID,
  102. ),
  103. }
  104. actionYAML := GithubActionYAML{
  105. On: GithubActionYAMLOnPush{
  106. Push: GithubActionYAMLOnPushBranches{
  107. Branches: []string{
  108. opts.DefaultBranch,
  109. },
  110. },
  111. },
  112. Name: "Deploy to Porter",
  113. Jobs: map[string]GithubActionYAMLJob{
  114. "porter-deploy": {
  115. RunsOn: "ubuntu-latest",
  116. Steps: gaSteps,
  117. },
  118. },
  119. }
  120. return yaml.Marshal(actionYAML)
  121. }