From 801c827058cc140412ea910e0df203b86b7c2202 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Thu, 18 Jun 2020 01:25:04 -0700 Subject: [PATCH] create parent dirs of target (#9452) --- builder/file/builder.go | 9 +++++++++ website/pages/docs/builders/file.mdx | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/builder/file/builder.go b/builder/file/builder.go index e2756b141..0f5b78734 100644 --- a/builder/file/builder.go +++ b/builder/file/builder.go @@ -11,6 +11,7 @@ import ( "io" "io/ioutil" "os" + "path/filepath" "github.com/hashicorp/hcl/v2/hcldec" "github.com/hashicorp/packer/helper/multistep" @@ -39,6 +40,14 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) { func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { artifact := new(FileArtifact) + // Create all directories leading to target + dir := filepath.Dir(b.config.Target) + if dir != "." { + if err := os.MkdirAll(dir, 0755); err != nil { + return nil, err + } + } + if b.config.Source != "" { source, err := os.Open(b.config.Source) if err != nil { diff --git a/website/pages/docs/builders/file.mdx b/website/pages/docs/builders/file.mdx index d153fd087..355925b0c 100644 --- a/website/pages/docs/builders/file.mdx +++ b/website/pages/docs/builders/file.mdx @@ -60,8 +60,8 @@ Any [communicator](/docs/templates/communicator) defined is ignored. ### Required: -- `target` (string) - The path for a file which will be copied as the - artifact. +- `target` (string) - The path for the artifact file that will be created. If + the path contains directories that don't exist, Packer will create them, too. ### Optional: