publish.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. "github.com/opencontainers/go-digest"
  24. )
  25. func (s *composeService) Publish(ctx context.Context, project *types.Project, repository string, options api.PublishOptions) error {
  26. return progress.RunWithTitle(ctx, func(ctx context.Context) error {
  27. return s.publish(ctx, project, repository, options)
  28. }, s.stdinfo(), "Publishing")
  29. }
  30. func (s *composeService) publish(ctx context.Context, project *types.Project, repository string, options api.PublishOptions) error {
  31. err := s.Push(ctx, project, api.PushOptions{})
  32. if err != nil {
  33. return err
  34. }
  35. named, err := reference.ParseDockerRef(repository)
  36. if err != nil {
  37. return err
  38. }
  39. resolver := imagetools.New(imagetools.Opt{
  40. Auth: s.configFile(),
  41. })
  42. var layers []ocipush.Pushable
  43. for _, file := range project.ComposeFiles {
  44. f, err := os.ReadFile(file)
  45. if err != nil {
  46. return err
  47. }
  48. layerDescriptor := ocipush.DescriptorForComposeFile(file, f)
  49. layers = append(layers, ocipush.Pushable{
  50. Descriptor: layerDescriptor,
  51. Data: f,
  52. })
  53. }
  54. if options.ResolveImageDigests {
  55. yaml, err := s.generateImageDigestsOverride(ctx, project)
  56. if err != nil {
  57. return err
  58. }
  59. layerDescriptor := ocipush.DescriptorForComposeFile("image-digests.yaml", yaml)
  60. layers = append(layers, ocipush.Pushable{
  61. Descriptor: layerDescriptor,
  62. Data: yaml,
  63. })
  64. }
  65. w := progress.ContextWriter(ctx)
  66. w.Event(progress.Event{
  67. ID: repository,
  68. Text: "publishing",
  69. Status: progress.Working,
  70. })
  71. if !s.dryRun {
  72. err = ocipush.PushManifest(ctx, resolver, named, layers, options.OCIVersion)
  73. if err != nil {
  74. return err
  75. }
  76. if err != nil {
  77. w.Event(progress.Event{
  78. ID: repository,
  79. Text: "publishing",
  80. Status: progress.Error,
  81. })
  82. return err
  83. }
  84. }
  85. w.Event(progress.Event{
  86. ID: repository,
  87. Text: "published",
  88. Status: progress.Done,
  89. })
  90. return nil
  91. }
  92. func (s *composeService) generateImageDigestsOverride(ctx context.Context, project *types.Project) ([]byte, error) {
  93. project.ApplyProfiles([]string{"*"})
  94. err := project.ResolveImages(func(named reference.Named) (digest.Digest, error) {
  95. auth, err := encodedAuth(named, s.configFile())
  96. if err != nil {
  97. return "", err
  98. }
  99. inspect, err := s.apiClient().DistributionInspect(ctx, named.String(), auth)
  100. if err != nil {
  101. return "", err
  102. }
  103. return inspect.Descriptor.Digest, nil
  104. })
  105. if err != nil {
  106. return nil, err
  107. }
  108. override := types.Project{
  109. Services: types.Services{},
  110. }
  111. for name, service := range project.Services {
  112. override.Services[name] = types.ServiceConfig{
  113. Image: service.Image,
  114. }
  115. }
  116. return override.MarshalYAML()
  117. }