mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-12-12 08:23:48 +02:00
Merge pull request #617 from floatdrop/docker-section-in-yaml
Add docker section in .drone.yml with net option
This commit is contained in:
commit
38fd589083
@ -330,6 +330,10 @@ func (b *Builder) run() error {
|
||||
Privileged: (b.Privileged && len(b.Repo.PR) == 0),
|
||||
}
|
||||
|
||||
if host.Privileged {
|
||||
host.NetworkMode = script.DockerNetworkMode(b.Build.Docker)
|
||||
}
|
||||
|
||||
// debugging
|
||||
log.Noticef("starting build %s", b.Build.Name)
|
||||
|
||||
|
@ -19,6 +19,7 @@ type KeyValuePair struct {
|
||||
type HostConfig struct {
|
||||
Binds []string
|
||||
ContainerIDFile string
|
||||
NetworkMode string
|
||||
LxcConf []KeyValuePair
|
||||
Privileged bool
|
||||
PortBindings map[Port][]PortBinding
|
||||
|
24
shared/build/script/docker.go
Normal file
24
shared/build/script/docker.go
Normal file
@ -0,0 +1,24 @@
|
||||
package script
|
||||
|
||||
const (
|
||||
DefaultDockerNetworkMode = "bridge"
|
||||
)
|
||||
|
||||
// Docker stores the configuration details for
|
||||
// configuring docker container.
|
||||
type Docker struct {
|
||||
// NetworkMode (also known as `--net` option)
|
||||
// Could be set only if Docker is running in privileged mode
|
||||
NetworkMode *string `yaml:"net,omitempty"`
|
||||
}
|
||||
|
||||
// DockerNetworkMode returns DefaultNetworkMode
|
||||
// when Docker.NetworkMode is empty.
|
||||
// DockerNetworkMode returns Docker.NetworkMode
|
||||
// when it is not empty.
|
||||
func DockerNetworkMode(d *Docker) string {
|
||||
if d == nil || d.NetworkMode == nil {
|
||||
return DefaultDockerNetworkMode
|
||||
}
|
||||
return *d.NetworkMode
|
||||
}
|
40
shared/build/script/docker_test.go
Normal file
40
shared/build/script/docker_test.go
Normal file
@ -0,0 +1,40 @@
|
||||
package script
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDockerNetworkMode(t *testing.T) {
|
||||
var d *Docker
|
||||
var expected string
|
||||
|
||||
expected = DefaultDockerNetworkMode
|
||||
d = nil
|
||||
if actual := DockerNetworkMode(d); actual != expected {
|
||||
t.Errorf("The result is invalid. [expected: %s][actual: %s]", expected, actual)
|
||||
}
|
||||
|
||||
expected = DefaultDockerNetworkMode
|
||||
d = &Docker{}
|
||||
if actual := DockerNetworkMode(d); actual != expected {
|
||||
t.Errorf("The result is invalid. [expected: %s][actual: %s]", expected, actual)
|
||||
}
|
||||
|
||||
expected = DefaultDockerNetworkMode
|
||||
d = &Docker{NetworkMode: nil}
|
||||
if actual := DockerNetworkMode(d); actual != expected {
|
||||
t.Errorf("The result is invalid. [expected: %s][actual: %s]", expected, actual)
|
||||
}
|
||||
|
||||
expected = "bridge"
|
||||
d = &Docker{NetworkMode: &expected}
|
||||
if actual := DockerNetworkMode(d); actual != expected {
|
||||
t.Errorf("The result is invalid. [expected: %s][actual: %s]", expected, actual)
|
||||
}
|
||||
|
||||
expected = "host"
|
||||
d = &Docker{NetworkMode: &expected}
|
||||
if actual := DockerNetworkMode(d); actual != expected {
|
||||
t.Errorf("The result is invalid. [expected: %s][actual: %s]", expected, actual)
|
||||
}
|
||||
}
|
@ -71,6 +71,10 @@ type Build struct {
|
||||
// Git specified git-specific parameters, such as
|
||||
// the clone depth and path
|
||||
Git *git.Git `yaml:"git,omitempty"`
|
||||
|
||||
// Docker container parameters, such as
|
||||
// NetworkMode and UserName
|
||||
Docker *Docker `yaml:"docker,omitempty"`
|
||||
}
|
||||
|
||||
// Write adds all the steps to the build script, including
|
||||
|
Loading…
Reference in New Issue
Block a user