Skip to content

Commit f6e6e65

Browse files
krjakbrjakNikita Vakula
authored andcommitted
Add new attribute to use docker build only
Signed-off-by: Nikita Vakula <programmistov.programmist@gmail.com>
1 parent 55caac0 commit f6e6e65

4 files changed

Lines changed: 68 additions & 34 deletions

File tree

builder/docker/builder.go

Lines changed: 60 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -60,47 +60,64 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
6060
state.Put("config", &b.config)
6161
state.Put("hook", hook)
6262
state.Put("ui", ui)
63-
generatedData := &packerbuilderdata.GeneratedData{State: state}
63+
var generatedData *packerbuilderdata.GeneratedData
64+
if !b.config.BuildOnly {
65+
generatedData = &packerbuilderdata.GeneratedData{State: state}
66+
}
6467

6568
// Setup the driver that will talk to Docker
6669
state.Put("driver", driver)
6770

68-
steps := []multistep.Step{
69-
&StepDefaultGeneratedData{
70-
GeneratedData: generatedData,
71-
},
72-
&StepTempDir{},
73-
&stepBuild{
74-
buildArgs: b.config.BuildConfig,
75-
},
76-
&StepPull{
77-
bootstrapped: !b.config.BuildConfig.IsDefault(),
78-
GeneratedData: generatedData,
79-
},
80-
&StepRun{},
81-
&communicator.StepConnect{
82-
Config: &b.config.Comm,
83-
Host: commHost(b.config.Comm.Host()),
84-
SSHConfig: b.config.Comm.SSHConfigFunc(),
85-
CustomConnect: map[string]multistep.Step{
86-
"docker": &StepConnectDocker{},
87-
"dockerWindowsContainer": &StepConnectDocker{},
71+
var steps []multistep.Step
72+
if !b.config.BuildOnly {
73+
steps = []multistep.Step{
74+
&StepDefaultGeneratedData{
75+
GeneratedData: generatedData,
76+
},
77+
&StepTempDir{},
78+
&stepBuild{
79+
buildArgs: b.config.BuildConfig,
80+
},
81+
&StepPull{
82+
bootstrapped: !b.config.BuildConfig.IsDefault(),
83+
GeneratedData: generatedData,
8884
},
89-
},
90-
&commonsteps.StepProvision{},
91-
&commonsteps.StepCleanupTempKeys{
92-
Comm: &b.config.Comm,
93-
},
85+
&StepRun{},
86+
&communicator.StepConnect{
87+
Config: &b.config.Comm,
88+
Host: commHost(b.config.Comm.Host()),
89+
SSHConfig: b.config.Comm.SSHConfigFunc(),
90+
CustomConnect: map[string]multistep.Step{
91+
"docker": &StepConnectDocker{},
92+
"dockerWindowsContainer": &StepConnectDocker{},
93+
},
94+
},
95+
&commonsteps.StepProvision{},
96+
&commonsteps.StepCleanupTempKeys{
97+
Comm: &b.config.Comm,
98+
},
99+
}
100+
} else {
101+
steps = []multistep.Step{
102+
&stepBuild{
103+
buildArgs: b.config.BuildConfig,
104+
},
105+
&commonsteps.StepCleanupTempKeys{
106+
Comm: &b.config.Comm,
107+
},
108+
}
94109
}
95110

96111
if b.config.Discard {
97112
log.Print("[DEBUG] Container will be discarded")
98113
} else if b.config.Commit {
99114
log.Print("[DEBUG] Container will be committed")
100115
steps = append(steps, &StepSetDefaults{})
101-
steps = append(steps, &StepCommit{
102-
GeneratedData: generatedData,
103-
})
116+
if !b.config.BuildOnly {
117+
steps = append(steps, &StepCommit{
118+
GeneratedData: generatedData,
119+
})
120+
}
104121
} else if b.config.ExportPath != "" {
105122
log.Printf("[DEBUG] Container will be exported to %s", b.config.ExportPath)
106123
steps = append(steps, new(StepExport))
@@ -129,11 +146,20 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
129146

130147
var artifact packersdk.Artifact
131148
if b.config.Commit {
132-
artifact = &ImportArtifact{
133-
IdValue: state.Get("image_id").(string),
134-
BuilderIdValue: BuilderIdImport,
135-
Driver: driver,
136-
StateData: stateData,
149+
if b.config.BuildOnly {
150+
artifact = &ImportArtifact{
151+
IdValue: b.config.Image,
152+
BuilderIdValue: BuilderIdImport,
153+
Driver: driver,
154+
StateData: stateData,
155+
}
156+
} else {
157+
artifact = &ImportArtifact{
158+
IdValue: state.Get("image_id").(string),
159+
BuilderIdValue: BuilderIdImport,
160+
Driver: driver,
161+
StateData: stateData,
162+
}
137163
}
138164
} else {
139165
artifact = &ExportArtifact{

builder/docker/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ type Config struct {
3838
// [Bootstrapping a build with a Dockerfile](#bootstrapping-a-build-with-a-dockerfile)
3939
// section of this documentation.
4040
BuildConfig DockerfileBootstrapConfig `mapstructure:"build"`
41+
// If true, the resulting image will be built only using `build` section,
42+
// which effectively means that the image will be buit using `docker build`
43+
// command only.
44+
BuildOnly bool `mapstructure:"build_only" required:"false"`
4145
// Set the author (e-mail) of a commit.
4246
Author string `mapstructure:"author"`
4347
// Dockerfile instructions to add to the commit. Example of instructions

builder/docker/config.hcl2spec.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs-partials/builder/docker/Config-not-required.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
[Bootstrapping a build with a Dockerfile](#bootstrapping-a-build-with-a-dockerfile)
1111
section of this documentation.
1212

13+
- `build_only` (string) - If true, use `docker build` step only. Defaults to false.
14+
1315
- `author` (string) - Set the author (e-mail) of a commit.
1416

1517
- `changes` ([]string) - Dockerfile instructions to add to the commit. Example of instructions

0 commit comments

Comments
 (0)