Skip to content
This repository was archived by the owner on Aug 29, 2023. It is now read-only.

Commit a5a561f

Browse files
authored
feat: load buildkit config from string instead of file (#37)
1 parent 8f25682 commit a5a561f

File tree

5 files changed

+19
-21
lines changed

5 files changed

+19
-21
lines changed

cmd/drone-docker-buildx/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func settingsFlags(settings *plugin.Settings) []cli.Flag {
9595
},
9696
&cli.StringFlag{
9797
Name: "daemon.buildkit-config",
98-
Usage: "location of buildkit config file",
98+
Usage: "docker buildkit json config content",
9999
EnvVars: []string{"PLUGIN_BUILDKIT_CONFIG"},
100100
Destination: &settings.Daemon.BuildkitConfig,
101101
},

go.sum

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ github.com/drone-plugins/drone-plugin-lib v0.4.0/go.mod h1:EgqogX38GoJFtckeSQyhB
1212
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
1313
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
1414
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
15-
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
1615
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
1716
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
1817
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -71,7 +70,5 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
7170
gopkg.in/yaml.v2 v2.2.3 h1:fvjTMHxHEw/mxHbtzPi3JCcKXQRAnQTBRo6YCJSVHKI=
7271
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
7372
gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk=
74-
honnef.co/go/tools v0.1.4 h1:SadWOkti5uVN1FAMgxn165+Mw00fuQKyk4Gyn/inxNQ=
75-
honnef.co/go/tools v0.1.4/go.mod h1:NgwopIslSNH47DimFoV78dnkksY2EFtX0ajyb3K/las=
7673
honnef.co/go/tools v0.2.0 h1:ws8AfbgTX3oIczLPNPCu5166oBg9ST2vNs0rcht+mDE=
7774
honnef.co/go/tools v0.2.0/go.mod h1:lPVVZ2BS5TfnjLyizF7o7hv7j9/L+8cZY2hLyjP9cGY=

plugin/daemon.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
package plugin
22

33
import (
4-
"io/ioutil"
4+
"io"
55
"os"
66
)
77

88
const dockerExe = "/usr/local/bin/docker"
99
const dockerdExe = "/usr/local/bin/dockerd"
1010
const dockerHome = "/root/.docker/"
11+
const buildkitConfig = "/tmp/buildkit.json"
1112

1213
func (p Plugin) startDaemon() {
1314
cmd := commandDaemon(p.settings.Daemon)
1415
if p.settings.Daemon.Debug {
1516
cmd.Stdout = os.Stdout
1617
cmd.Stderr = os.Stderr
1718
} else {
18-
cmd.Stdout = ioutil.Discard
19-
cmd.Stderr = ioutil.Discard
19+
cmd.Stdout = io.Discard
20+
cmd.Stderr = io.Discard
2021
}
2122
go func() {
2223
trace(cmd)

plugin/docker.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ func commandInfo() *exec.Cmd {
5454

5555
func commandBuilder(daemon Daemon) *exec.Cmd {
5656
args := []string{
57-
"buildx",
58-
"create",
57+
"buildx",
58+
"create",
5959
"--use",
6060
}
6161

6262
if daemon.BuildkitConfig != "" {
63-
args = append(args, "--config", daemon.BuildkitConfig)
63+
args = append(args, "--config", buildkitConfig)
6464
}
6565

6666
return exec.Command(dockerExe, args...)
@@ -84,7 +84,7 @@ func commandBuild(build Build, dryrun bool) *exec.Cmd {
8484
}
8585

8686
args = append(args, build.Context)
87-
if ! dryrun {
87+
if !dryrun {
8888
args = append(args, "--push")
8989
}
9090
if build.Squash {
@@ -124,7 +124,7 @@ func commandBuild(build Build, dryrun bool) *exec.Cmd {
124124

125125
for _, arg := range build.Tags.Value() {
126126
args = append(args, "-t", fmt.Sprintf("%s:%s", build.Repo, arg))
127-
}
127+
}
128128

129129
return exec.Command(dockerExe, args...)
130130
}

plugin/impl.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package plugin
22

33
import (
44
"fmt"
5-
"io/ioutil"
65
"os"
76
"os/exec"
87
"path/filepath"
@@ -27,7 +26,7 @@ type Daemon struct {
2726
MTU string // Docker daemon mtu setting
2827
IPv6 bool // Docker daemon IPv6 networking
2928
Experimental bool // Docker daemon enable experimental mode
30-
BuildkitConfig string // Docker buildkit config file
29+
BuildkitConfig string // Docker buildkit config
3130
}
3231

3332
// Login defines Docker login parameters.
@@ -78,12 +77,6 @@ func (p *Plugin) Validate() error {
7877
p.settings.Build.Ref = p.pipeline.Commit.Ref
7978
p.settings.Daemon.Registry = p.settings.Login.Registry
8079

81-
if p.settings.Daemon.BuildkitConfig != "" {
82-
if _, err := os.Stat(p.settings.Daemon.BuildkitConfig); err != nil && os.IsNotExist(err) {
83-
return fmt.Errorf("given buildkit config file not found")
84-
}
85-
}
86-
8780
if p.settings.Build.TagsAuto {
8881
// return true if tag event or default branch
8982
if UseDefaultTag(
@@ -131,7 +124,7 @@ func (p *Plugin) Execute() error {
131124
os.MkdirAll(dockerHome, 0600)
132125

133126
path := filepath.Join(dockerHome, "config.json")
134-
err := ioutil.WriteFile(path, []byte(p.settings.Login.Config), 0600)
127+
err := os.WriteFile(path, []byte(p.settings.Login.Config), 0600)
135128
if err != nil {
136129
return fmt.Errorf("error writing config.json: %s", err)
137130
}
@@ -146,6 +139,13 @@ func (p *Plugin) Execute() error {
146139
}
147140
}
148141

142+
if p.settings.Daemon.BuildkitConfig != "" {
143+
err := os.WriteFile(buildkitConfig, []byte(p.settings.Daemon.BuildkitConfig), 0600)
144+
if err != nil {
145+
return fmt.Errorf("error writing buildkit.json: %s", err)
146+
}
147+
}
148+
149149
switch {
150150
case p.settings.Login.Password != "":
151151
fmt.Println("Detected registry credentials")

0 commit comments

Comments
 (0)