create_deployment.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package environment
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "github.com/google/go-github/v41/github"
  7. "github.com/porter-dev/porter/api/server/authz"
  8. "github.com/porter-dev/porter/api/server/handlers"
  9. "github.com/porter-dev/porter/api/server/shared"
  10. "github.com/porter-dev/porter/api/server/shared/apierrors"
  11. "github.com/porter-dev/porter/api/server/shared/config"
  12. "github.com/porter-dev/porter/api/types"
  13. "github.com/porter-dev/porter/internal/models"
  14. "github.com/porter-dev/porter/internal/models/integrations"
  15. )
  16. type CreateDeploymentHandler struct {
  17. handlers.PorterHandlerReadWriter
  18. authz.KubernetesAgentGetter
  19. }
  20. func NewCreateDeploymentHandler(
  21. config *config.Config,
  22. decoderValidator shared.RequestDecoderValidator,
  23. writer shared.ResultWriter,
  24. ) *CreateDeploymentHandler {
  25. return &CreateDeploymentHandler{
  26. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  27. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  28. }
  29. }
  30. func (c *CreateDeploymentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  31. ga, _ := r.Context().Value(types.GitInstallationScope).(*integrations.GithubAppInstallation)
  32. project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  33. cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
  34. request := &types.CreateDeploymentRequest{}
  35. if ok := c.DecodeAndValidate(w, r, request); !ok {
  36. return
  37. }
  38. // read the environment to get the environment id
  39. env, err := c.Repo().Environment().ReadEnvironment(project.ID, cluster.ID, uint(ga.InstallationID))
  40. if err != nil {
  41. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  42. return
  43. }
  44. // create deployment on GitHub API
  45. client, err := getGithubClientFromEnvironment(c.Config(), env)
  46. if err != nil {
  47. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  48. return
  49. }
  50. branch := request.Branch
  51. envName := "Preview"
  52. automerge := false
  53. requiredContexts := []string{}
  54. deploymentRequest := github.DeploymentRequest{
  55. Ref: &branch,
  56. Environment: &envName,
  57. AutoMerge: &automerge,
  58. RequiredContexts: &requiredContexts,
  59. }
  60. deployment, _, err := client.Repositories.CreateDeployment(
  61. context.Background(),
  62. env.GitRepoOwner,
  63. env.GitRepoName,
  64. &deploymentRequest,
  65. )
  66. if err != nil {
  67. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  68. return
  69. }
  70. depID := deployment.GetID()
  71. // Create Deployment Status to indicate it's in progress
  72. state := "in_progress"
  73. log_url := fmt.Sprintf("https://github.com/%s/%s/actions/%d", env.GitRepoOwner, env.GitRepoName, request.ActionID)
  74. deploymentStatusRequest := github.DeploymentStatusRequest{
  75. State: &state,
  76. LogURL: &log_url, // link to actions tab
  77. }
  78. _, _, err = client.Repositories.CreateDeploymentStatus(
  79. context.Background(),
  80. env.GitRepoOwner,
  81. env.GitRepoName,
  82. depID,
  83. &deploymentStatusRequest,
  84. )
  85. if err != nil {
  86. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  87. return
  88. }
  89. // create the deployment
  90. depl, err := c.Repo().Environment().CreateDeployment(&models.Deployment{
  91. EnvironmentID: env.ID,
  92. Namespace: request.Namespace,
  93. Status: "creating",
  94. PullRequestID: request.PullRequestID,
  95. GitHubDeploymentID: depID,
  96. })
  97. if err != nil {
  98. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  99. return
  100. }
  101. // create the backing namespace
  102. agent, err := c.GetAgent(r, cluster, "")
  103. if err != nil {
  104. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  105. return
  106. }
  107. _, err = agent.CreateNamespace(depl.Namespace)
  108. if err != nil {
  109. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  110. return
  111. }
  112. c.WriteResult(w, r, depl.ToDeploymentType())
  113. }