app_push.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package v2
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. api "github.com/porter-dev/porter/api/client"
  7. "github.com/porter-dev/porter/cli/cmd/config"
  8. )
  9. // AppPushInput is the input to the AppPush function
  10. type AppPushInput struct {
  11. // CLIConfig is the CLI configuration
  12. CLIConfig config.CLIConfig
  13. // Client is the Porter API client
  14. Client api.Client
  15. // AppName is the name of the app
  16. AppName string
  17. // DeploymentTargetName is the name of the deployment target, if provided
  18. DeploymentTargetName string
  19. // ImageTag is the image tag to use for the app build
  20. ImageTag string
  21. }
  22. // AppPush pushes an app to a remote registry
  23. func AppPush(ctx context.Context, inp AppPushInput) error {
  24. cliConf := inp.CLIConfig
  25. client := inp.Client
  26. if cliConf.Project == 0 {
  27. return errors.New("project must be set")
  28. }
  29. if cliConf.Cluster == 0 {
  30. return errors.New("cluster must be set")
  31. }
  32. latest, err := client.CurrentAppRevision(ctx, api.CurrentAppRevisionInput{
  33. ProjectID: cliConf.Project,
  34. ClusterID: cliConf.Cluster,
  35. AppName: inp.AppName,
  36. DeploymentTargetName: inp.DeploymentTargetName,
  37. })
  38. if err != nil {
  39. return fmt.Errorf("error getting latest app revision: %s", err)
  40. }
  41. settings, err := client.GetBuildFromRevision(ctx, api.GetBuildFromRevisionInput{
  42. ProjectID: cliConf.Project,
  43. ClusterID: cliConf.Cluster,
  44. AppName: inp.AppName,
  45. AppRevisionID: latest.AppRevision.ID,
  46. })
  47. if err != nil {
  48. return fmt.Errorf("error getting build from revision: %w", err)
  49. }
  50. tagForPush, err := tagFromCommitSHAOrFlag(inp.ImageTag)
  51. if err != nil {
  52. return fmt.Errorf("error getting tag for build: %w", err)
  53. }
  54. // push the image to the remote registry
  55. err = push(ctx, client, pushInput{
  56. ProjectID: cliConf.Project,
  57. ImageTag: tagForPush,
  58. RepositoryURL: settings.Image.Repository,
  59. })
  60. if err != nil {
  61. return fmt.Errorf("error pushing image: %w", err)
  62. }
  63. return nil
  64. }