publish.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "context"
  16. "os"
  17. "github.com/compose-spec/compose-go/v2/types"
  18. "github.com/distribution/reference"
  19. "github.com/docker/buildx/util/imagetools"
  20. "github.com/docker/compose/v2/internal/ocipush"
  21. "github.com/docker/compose/v2/pkg/api"
  22. "github.com/docker/compose/v2/pkg/progress"
  23. )
  24. func (s *composeService) Publish(ctx context.Context, project *types.Project, repository string, options api.PublishOptions) error {
  25. return progress.RunWithTitle(ctx, func(ctx context.Context) error {
  26. return s.publish(ctx, project, repository, options)
  27. }, s.stdinfo(), "Publishing")
  28. }
  29. func (s *composeService) publish(ctx context.Context, project *types.Project, repository string, options api.PublishOptions) error {
  30. err := s.Push(ctx, project, api.PushOptions{})
  31. if err != nil {
  32. return err
  33. }
  34. named, err := reference.ParseDockerRef(repository)
  35. if err != nil {
  36. return err
  37. }
  38. resolver := imagetools.New(imagetools.Opt{
  39. Auth: s.configFile(),
  40. })
  41. var layers []ocipush.Pushable
  42. for _, file := range project.ComposeFiles {
  43. f, err := os.ReadFile(file)
  44. if err != nil {
  45. return err
  46. }
  47. layerDescriptor := ocipush.DescriptorForComposeFile(file, f)
  48. layers = append(layers, ocipush.Pushable{
  49. Descriptor: layerDescriptor,
  50. Data: f,
  51. })
  52. }
  53. if options.ResolveImageDigests {
  54. yaml, err := s.generateImageDigestsOverride(ctx, project)
  55. if err != nil {
  56. return err
  57. }
  58. layerDescriptor := ocipush.DescriptorForComposeFile("image-digests.yaml", yaml)
  59. layers = append(layers, ocipush.Pushable{
  60. Descriptor: layerDescriptor,
  61. Data: yaml,
  62. })
  63. }
  64. w := progress.ContextWriter(ctx)
  65. w.Event(progress.Event{
  66. ID: repository,
  67. Text: "publishing",
  68. Status: progress.Working,
  69. })
  70. if !s.dryRun {
  71. err = ocipush.PushManifest(ctx, resolver, named, layers, options.OCIVersion)
  72. if err != nil {
  73. w.Event(progress.Event{
  74. ID: repository,
  75. Text: "publishing",
  76. Status: progress.Error,
  77. })
  78. return err
  79. }
  80. }
  81. w.Event(progress.Event{
  82. ID: repository,
  83. Text: "published",
  84. Status: progress.Done,
  85. })
  86. return nil
  87. }
  88. func (s *composeService) generateImageDigestsOverride(ctx context.Context, project *types.Project) ([]byte, error) {
  89. project, err := project.WithProfiles([]string{"*"})
  90. if err != nil {
  91. return nil, err
  92. }
  93. project, err = project.WithImagesResolved(ImageDigestResolver(ctx, s.configFile(), s.apiClient()))
  94. if err != nil {
  95. return nil, err
  96. }
  97. override := types.Project{
  98. Services: types.Services{},
  99. }
  100. for name, service := range project.Services {
  101. override.Services[name] = types.ServiceConfig{
  102. Image: service.Image,
  103. }
  104. }
  105. return override.MarshalYAML()
  106. }