update_image.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package v2
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "github.com/fatih/color"
  7. api "github.com/porter-dev/porter/api/client"
  8. )
  9. // UpdateImageInput is the input for the UpdateImage function
  10. type UpdateImageInput struct {
  11. ProjectID uint
  12. ClusterID uint
  13. AppName string
  14. DeploymentTargetName string
  15. Tag string
  16. Client api.Client
  17. WaitForSuccessfulDeployment bool
  18. }
  19. // UpdateImage updates the image of an application
  20. func UpdateImage(ctx context.Context, input UpdateImageInput) error {
  21. if input.DeploymentTargetName == "" {
  22. return errors.New("please provide a deployment target")
  23. }
  24. tag := input.Tag
  25. if tag == "" {
  26. tag = "latest"
  27. }
  28. resp, err := input.Client.UpdateImage(ctx, input.ProjectID, input.ClusterID, input.AppName, input.DeploymentTargetName, tag)
  29. if err != nil {
  30. return fmt.Errorf("unable to update image: %w", err)
  31. }
  32. triggeredBackgroundColor := color.FgGreen
  33. _, _ = color.New(triggeredBackgroundColor).Printf("Updated application %s to use tag \"%s\"\n", input.AppName, tag)
  34. if input.WaitForSuccessfulDeployment {
  35. return waitForAppRevisionStatus(ctx, waitForAppRevisionStatusInput{
  36. ProjectID: input.ProjectID,
  37. ClusterID: input.ClusterID,
  38. AppName: input.AppName,
  39. RevisionID: resp.RevisionID,
  40. Client: input.Client,
  41. })
  42. }
  43. return nil
  44. }