mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-05-18 09:17:49 +08:00
Add NetworkMode to bake target
Allows specification of network mode in a bake target. Fixes #848 Signed-off-by: Zachary Povey <zachary.povey@autotrader.co.uk>
This commit is contained in:
parent
ab73275f58
commit
ae53101e89
23
bake/bake.go
23
bake/bake.go
@ -426,6 +426,7 @@ type Target struct {
|
|||||||
Outputs []string `json:"output,omitempty" hcl:"output,optional"`
|
Outputs []string `json:"output,omitempty" hcl:"output,optional"`
|
||||||
Pull *bool `json:"pull,omitempty" hcl:"pull,optional"`
|
Pull *bool `json:"pull,omitempty" hcl:"pull,optional"`
|
||||||
NoCache *bool `json:"no-cache,omitempty" hcl:"no-cache,optional"`
|
NoCache *bool `json:"no-cache,omitempty" hcl:"no-cache,optional"`
|
||||||
|
NetworkMode *string `json:"network,omitempty" hcl:"network,optional"`
|
||||||
|
|
||||||
// IMPORTANT: if you add more fields here, do not forget to update newOverrides and README.
|
// IMPORTANT: if you add more fields here, do not forget to update newOverrides and README.
|
||||||
}
|
}
|
||||||
@ -492,6 +493,9 @@ func (t *Target) Merge(t2 *Target) {
|
|||||||
if t2.NoCache != nil {
|
if t2.NoCache != nil {
|
||||||
t.NoCache = t2.NoCache
|
t.NoCache = t2.NoCache
|
||||||
}
|
}
|
||||||
|
if t2.NetworkMode != nil {
|
||||||
|
t.NetworkMode = t2.NetworkMode
|
||||||
|
}
|
||||||
t.Inherits = append(t.Inherits, t2.Inherits...)
|
t.Inherits = append(t.Inherits, t2.Inherits...)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -563,6 +567,8 @@ func (t *Target) AddOverrides(overrides map[string]Override) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
case "network":
|
||||||
|
t.NetworkMode = &o.Value
|
||||||
default:
|
default:
|
||||||
return errors.Errorf("unknown key: %s", keys[0])
|
return errors.Errorf("unknown key: %s", keys[0])
|
||||||
}
|
}
|
||||||
@ -632,6 +638,10 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
|
|||||||
if t.Pull != nil {
|
if t.Pull != nil {
|
||||||
pull = *t.Pull
|
pull = *t.Pull
|
||||||
}
|
}
|
||||||
|
networkMode := ""
|
||||||
|
if t.NetworkMode != nil {
|
||||||
|
networkMode = *t.NetworkMode
|
||||||
|
}
|
||||||
|
|
||||||
bi := build.Inputs{
|
bi := build.Inputs{
|
||||||
ContextPath: contextPath,
|
ContextPath: contextPath,
|
||||||
@ -648,12 +658,13 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
|
|||||||
t.Context = &bi.ContextPath
|
t.Context = &bi.ContextPath
|
||||||
|
|
||||||
bo := &build.Options{
|
bo := &build.Options{
|
||||||
Inputs: bi,
|
Inputs: bi,
|
||||||
Tags: t.Tags,
|
Tags: t.Tags,
|
||||||
BuildArgs: t.Args,
|
BuildArgs: t.Args,
|
||||||
Labels: t.Labels,
|
Labels: t.Labels,
|
||||||
NoCache: noCache,
|
NoCache: noCache,
|
||||||
Pull: pull,
|
Pull: pull,
|
||||||
|
NetworkMode: networkMode,
|
||||||
}
|
}
|
||||||
|
|
||||||
platforms, err := platformutil.Parse(t.Platforms)
|
platforms, err := platformutil.Parse(t.Platforms)
|
||||||
|
@ -79,7 +79,8 @@ func ParseCompose(dt []byte) (*Config, error) {
|
|||||||
val, ok := cfg.Environment[val]
|
val, ok := cfg.Environment[val]
|
||||||
return val, ok
|
return val, ok
|
||||||
})),
|
})),
|
||||||
CacheFrom: s.Build.CacheFrom,
|
CacheFrom: s.Build.CacheFrom,
|
||||||
|
NetworkMode: &s.Build.Network,
|
||||||
}
|
}
|
||||||
if err = t.composeExtTarget(s.Build.Extensions); err != nil {
|
if err = t.composeExtTarget(s.Build.Extensions); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -19,6 +19,8 @@ services:
|
|||||||
build:
|
build:
|
||||||
context: ./dir
|
context: ./dir
|
||||||
dockerfile: Dockerfile-alternate
|
dockerfile: Dockerfile-alternate
|
||||||
|
network:
|
||||||
|
none
|
||||||
args:
|
args:
|
||||||
buildno: 123
|
buildno: 123
|
||||||
`)
|
`)
|
||||||
@ -43,6 +45,7 @@ services:
|
|||||||
require.Equal(t, "Dockerfile-alternate", *c.Targets[1].Dockerfile)
|
require.Equal(t, "Dockerfile-alternate", *c.Targets[1].Dockerfile)
|
||||||
require.Equal(t, 1, len(c.Targets[1].Args))
|
require.Equal(t, 1, len(c.Targets[1].Args))
|
||||||
require.Equal(t, "123", c.Targets[1].Args["buildno"])
|
require.Equal(t, "123", c.Targets[1].Args["buildno"])
|
||||||
|
require.Equal(t, "none", *c.Targets[1].NetworkMode)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNoBuildOutOfTreeService(t *testing.T) {
|
func TestNoBuildOutOfTreeService(t *testing.T) {
|
||||||
|
@ -36,6 +36,7 @@ func TestHCLBasic(t *testing.T) {
|
|||||||
|
|
||||||
target "webapp-plus" {
|
target "webapp-plus" {
|
||||||
inherits = ["webapp", "cross"]
|
inherits = ["webapp", "cross"]
|
||||||
|
network = "host"
|
||||||
args = {
|
args = {
|
||||||
IAMCROSS = "true"
|
IAMCROSS = "true"
|
||||||
}
|
}
|
||||||
@ -63,6 +64,7 @@ func TestHCLBasic(t *testing.T) {
|
|||||||
require.Equal(t, c.Targets[3].Name, "webapp-plus")
|
require.Equal(t, c.Targets[3].Name, "webapp-plus")
|
||||||
require.Equal(t, 1, len(c.Targets[3].Args))
|
require.Equal(t, 1, len(c.Targets[3].Args))
|
||||||
require.Equal(t, map[string]string{"IAMCROSS": "true"}, c.Targets[3].Args)
|
require.Equal(t, map[string]string{"IAMCROSS": "true"}, c.Targets[3].Args)
|
||||||
|
require.Equal(t, "host", *c.Targets[3].NetworkMode)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHCLBasicInJSON(t *testing.T) {
|
func TestHCLBasicInJSON(t *testing.T) {
|
||||||
@ -93,6 +95,7 @@ func TestHCLBasicInJSON(t *testing.T) {
|
|||||||
},
|
},
|
||||||
"webapp-plus": {
|
"webapp-plus": {
|
||||||
"inherits": ["webapp", "cross"],
|
"inherits": ["webapp", "cross"],
|
||||||
|
"network": "host",
|
||||||
"args": {
|
"args": {
|
||||||
"IAMCROSS": "true"
|
"IAMCROSS": "true"
|
||||||
}
|
}
|
||||||
@ -123,6 +126,7 @@ func TestHCLBasicInJSON(t *testing.T) {
|
|||||||
require.Equal(t, c.Targets[3].Name, "webapp-plus")
|
require.Equal(t, c.Targets[3].Name, "webapp-plus")
|
||||||
require.Equal(t, 1, len(c.Targets[3].Args))
|
require.Equal(t, 1, len(c.Targets[3].Args))
|
||||||
require.Equal(t, map[string]string{"IAMCROSS": "true"}, c.Targets[3].Args)
|
require.Equal(t, map[string]string{"IAMCROSS": "true"}, c.Targets[3].Args)
|
||||||
|
require.Equal(t, "host", *c.Targets[3].NetworkMode)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHCLWithFunctions(t *testing.T) {
|
func TestHCLWithFunctions(t *testing.T) {
|
||||||
|
@ -291,7 +291,7 @@ $ docker buildx bake --set foo*.no-cache # bypass caching only for
|
|||||||
|
|
||||||
Complete list of overridable fields:
|
Complete list of overridable fields:
|
||||||
`args`, `cache-from`, `cache-to`, `context`, `dockerfile`, `labels`, `no-cache`,
|
`args`, `cache-from`, `cache-to`, `context`, `dockerfile`, `labels`, `no-cache`,
|
||||||
`output`, `platform`, `pull`, `secrets`, `ssh`, `tags`, `target`
|
`output`, `platform`, `pull`, `secrets`, `ssh`, `tags`, `target`, `network`
|
||||||
|
|
||||||
### File definition
|
### File definition
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user