@@ -18,6 +18,7 @@ package compose
1818
1919import (
2020 "context"
21+ "fmt"
2122 "os"
2223
2324 "github.com/compose-spec/compose-go/v2/types"
@@ -35,7 +36,11 @@ func (s *composeService) Publish(ctx context.Context, project *types.Project, re
3536}
3637
3738func (s * composeService ) publish (ctx context.Context , project * types.Project , repository string , options api.PublishOptions ) error {
38- err := s .Push (ctx , project , api.PushOptions {IgnoreFailures : true , ImageMandatory : true })
39+ err := preChecks (project , options )
40+ if err != nil {
41+ return err
42+ }
43+ err = s .Push (ctx , project , api.PushOptions {IgnoreFailures : true , ImageMandatory : true })
3944 if err != nil {
4045 return err
4146 }
@@ -63,6 +68,10 @@ func (s *composeService) publish(ctx context.Context, project *types.Project, re
6368 })
6469 }
6570
71+ if options .WithEnvironment {
72+ layers = append (layers , envFileLayers (project )... )
73+ }
74+
6675 if options .ResolveImageDigests {
6776 yaml , err := s .generateImageDigestsOverride (ctx , project )
6877 if err != nil {
@@ -120,3 +129,49 @@ func (s *composeService) generateImageDigestsOverride(ctx context.Context, proje
120129 }
121130 return override .MarshalYAML ()
122131}
132+
133+ func preChecks (project * types.Project , options api.PublishOptions ) error {
134+ if ! options .WithEnvironment {
135+ for _ , service := range project .Services {
136+ if len (service .EnvFiles ) > 0 {
137+ return fmt .Errorf ("service %q has env_file declared. To avoid leaking sensitive data, " +
138+ "you must either explicitly allow the sending of environment variables by using the --with-env flag," +
139+ " or remove sensitive data from your Compose configuration" , service .Name )
140+ }
141+ if len (service .Environment ) > 0 {
142+ return fmt .Errorf ("service %q has environment variable(s) declared. To avoid leaking sensitive data, " +
143+ "you must either explicitly allow the sending of environment variables by using the --with-env flag," +
144+ " or remove sensitive data from your Compose configuration" , service .Name )
145+ }
146+ }
147+
148+ for _ , config := range project .Configs {
149+ if config .Environment != "" {
150+ return fmt .Errorf ("config %q is declare as an environment variable. To avoid leaking sensitive data, " +
151+ "you must either explicitly allow the sending of environment variables by using the --with-env flag," +
152+ " or remove sensitive data from your Compose configuration" , config .Name )
153+ }
154+ }
155+ }
156+
157+ return nil
158+ }
159+
160+ func envFileLayers (project * types.Project ) []ocipush.Pushable {
161+ var layers []ocipush.Pushable
162+ for _ , service := range project .Services {
163+ for _ , envFile := range service .EnvFiles {
164+ f , err := os .ReadFile (envFile .Path )
165+ if err != nil {
166+ // if we can't read the file, skip to the next one
167+ continue
168+ }
169+ layerDescriptor := ocipush .DescriptorForEnvFile (envFile .Path , f )
170+ layers = append (layers , ocipush.Pushable {
171+ Descriptor : layerDescriptor ,
172+ Data : f ,
173+ })
174+ }
175+ }
176+ return layers
177+ }
0 commit comments