Skip to content

Commit

Permalink
Fix bug where env-file variables were not being exposed to detect
Browse files Browse the repository at this point in the history
  • Loading branch information
ameyer-pivotal committed Dec 18, 2018
1 parent 5ccae5a commit 88a5253
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 25 deletions.
4 changes: 2 additions & 2 deletions acceptance/testdata/mock_buildpacks/printenv/bin/build
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/usr/bin/env bash

echo "---> Print env buildpack"
echo "---> BUILD: Printenv buildpack"

set -o errexit
set -o nounset
set -o pipefail

for file in $(ls /platform/env); do
echo "ENV: $file is $(cat /platform/env/$file);"
echo "BUILD: $file is $(cat /platform/env/$file);"
done
10 changes: 10 additions & 0 deletions acceptance/testdata/mock_buildpacks/printenv/bin/detect
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
#!/usr/bin/env bash

echo "---> DETECT: Printenv buildpack"

set -o errexit
set -o nounset
set -o pipefail

for file in $(ls /platform/env); do
echo "DETECT: $file is $(cat /platform/env/$file);"
done

exit 0
45 changes: 28 additions & 17 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,15 @@ func (bf *BuildFactory) BuildConfigFromFlags(f *BuildFlags) (*BuildConfig, error
}

b := &BuildConfig{
AppDir: appDir,
RepoName: f.RepoName,
Publish: f.Publish,
NoPull: f.NoPull,
Buildpacks: f.Buildpacks,
Cli: bf.Cli,
Logger: bf.Logger,
FS: bf.FS,
Config: bf.Config,
AppDir: appDir,
RepoName: f.RepoName,
Publish: f.Publish,
NoPull: f.NoPull,
Buildpacks: f.Buildpacks,
Cli: bf.Cli,
Logger: bf.Logger,
FS: bf.FS,
Config: bf.Config,
}

if f.EnvFile != "" {
Expand Down Expand Up @@ -378,6 +378,10 @@ func (b *BuildConfig) Detect() error {
}
}

if err := b.copyEnvsToContainer(ctx, ctr.ID); err != nil {
return err
}

if err := b.Cli.RunContainer(
ctx,
ctr.ID,
Expand Down Expand Up @@ -493,14 +497,8 @@ func (b *BuildConfig) Build() error {
}
}

if len(b.EnvFile) > 0 {
platformEnvTar, err := b.tarEnvFile()
if err != nil {
return errors.Wrap(err, "create env files")
}
if err := b.Cli.CopyToContainer(ctx, ctr.ID, "/", platformEnvTar, dockertypes.CopyToContainerOptions{}); err != nil {
return errors.Wrap(err, "create env files")
}
if err := b.copyEnvsToContainer(ctx, ctr.ID); err != nil {
return err
}

if err = b.Cli.RunContainer(
Expand Down Expand Up @@ -556,6 +554,19 @@ func (b *BuildConfig) tarEnvFile() (io.Reader, error) {
return bytes.NewReader(buf.Bytes()), nil
}

func (b *BuildConfig) copyEnvsToContainer(ctx context.Context, containerID string) error {
if len(b.EnvFile) > 0 {
platformEnvTar, err := b.tarEnvFile()
if err != nil {
return errors.Wrap(err, "create env files")
}
if err := b.Cli.CopyToContainer(ctx, containerID, "/", platformEnvTar, dockertypes.CopyToContainerOptions{}); err != nil {
return errors.Wrap(err, "create env files")
}
}
return nil
}

func (b *BuildConfig) Export() error {
ctx := context.Background()
ctrConf := &container.Config{
Expand Down
22 changes: 18 additions & 4 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,22 @@ PATH
})
})
})

when("EnvFile is specified", func() {
it("sets specified env variables in /platform/env/...", func() {
if runtime.GOOS == "windows" {
t.Skip("directory buildpacks are not implemented on windows")
}
subject.EnvFile = map[string]string{
"VAR1": "value1",
"VAR2": "value2 with spaces",
}
subject.Buildpacks = []string{"acceptance/testdata/mock_buildpacks/printenv"}
h.AssertNil(t, subject.Detect())
h.AssertContains(t, outBuf.String(), "DETECT: VAR1 is value1;")
h.AssertContains(t, outBuf.String(), "DETECT: VAR2 is value2 with spaces;")
})
})
})

when("#Analyze", func() {
Expand Down Expand Up @@ -556,11 +572,9 @@ cache = false
}
subject.Buildpacks = []string{"acceptance/testdata/mock_buildpacks/printenv"}
h.AssertNil(t, subject.Detect())

h.AssertNil(t, subject.Build())

h.AssertContains(t, outBuf.String(), "ENV: VAR1 is value1;")
h.AssertContains(t, outBuf.String(), "ENV: VAR2 is value2 with spaces;")
h.AssertContains(t, outBuf.String(), "BUILD: VAR1 is value1;")
h.AssertContains(t, outBuf.String(), "BUILD: VAR2 is value2 with spaces;")
})
})
})
Expand Down
4 changes: 2 additions & 2 deletions cmd/pack/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func buildCommandFlags(cmd *cobra.Command, buildFlags *pack.BuildFlags) {
cmd.Flags().StringVarP(&buildFlags.AppDir, "path", "p", "", "Path to app dir (defaults to current working directory)")
cmd.Flags().StringVar(&buildFlags.Builder, "builder", "", "Builder (defaults to builder configured by 'set-default-builder')")
cmd.Flags().StringVar(&buildFlags.RunImage, "run-image", "", "Run image (defaults to default stack's run image)")
cmd.Flags().StringVar(&buildFlags.EnvFile, "env-file", "", "Build-time environment variables file\nOne variable per line, of the form 'VAR=VALUE'")
cmd.Flags().StringVar(&buildFlags.EnvFile, "env-file", "", "Build-time environment variables file\nOne variable per line, of the form 'VAR=VALUE' or 'VAR'\nWhen using latter value-less form, value will be taken from current\n environment at the time this command is executed")
cmd.Flags().BoolVar(&buildFlags.NoPull, "no-pull", false, "Skip pulling images before use")
cmd.Flags().StringSliceVar(&buildFlags.Buildpacks, "buildpack", nil, "Buildpack ID, path to directory, or path/URL to .tgz file"+multiValueHelp("buildpack"))
}
Expand Down Expand Up @@ -417,5 +417,5 @@ func logError(f func(cmd *cobra.Command, args []string) error) func(*cobra.Comma
}

func multiValueHelp(name string) string {
return fmt.Sprintf("\nRepeat for each %s in order,\nor supply once by comma-separated list", name)
return fmt.Sprintf("\nRepeat for each %s in order,\n or supply once by comma-separated list", name)
}

0 comments on commit 88a5253

Please sign in to comment.