deploy.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package client
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "github.com/porter-dev/porter/internal/models"
  8. )
  9. // GetReleaseLatestRevision gets the latest revision of a Helm release
  10. type GetReleaseWebhookResponse models.ReleaseExternal
  11. func (c *Client) GetReleaseWebhook(
  12. ctx context.Context,
  13. projID, clusterID uint,
  14. name, namespace string,
  15. ) (*GetReleaseWebhookResponse, error) {
  16. req, err := http.NewRequest(
  17. "GET",
  18. fmt.Sprintf("%s/projects/%d/releases/%s/webhook_token?"+url.Values{
  19. "cluster_id": []string{fmt.Sprintf("%d", clusterID)},
  20. "namespace": []string{namespace},
  21. "storage": []string{"secret"},
  22. }.Encode(), c.BaseURL, projID, name),
  23. nil,
  24. )
  25. if err != nil {
  26. return nil, err
  27. }
  28. req = req.WithContext(ctx)
  29. bodyResp := &GetReleaseWebhookResponse{}
  30. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  31. if httpErr != nil {
  32. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  33. }
  34. return nil, err
  35. }
  36. return bodyResp, nil
  37. }
  38. // DeployWithWebhook deploys an application with an image tag using a unique webhook URI
  39. func (c *Client) DeployWithWebhook(
  40. ctx context.Context,
  41. webhook, tag string,
  42. ) error {
  43. req, err := http.NewRequest(
  44. "POST",
  45. fmt.Sprintf("%s/webhooks/deploy/%s?commit=%s", c.BaseURL, webhook, tag),
  46. nil,
  47. )
  48. if err != nil {
  49. return err
  50. }
  51. req = req.WithContext(ctx)
  52. if httpErr, err := c.sendRequest(req, nil, true); httpErr != nil || err != nil {
  53. if httpErr != nil {
  54. return fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  55. }
  56. return err
  57. }
  58. return nil
  59. }