Skip to content

Commit 6cf4d8e

Browse files
Write artifacts to input artifact file (#389)
* Write artifacts to input artifact file
1 parent fcf616c commit 6cf4d8e

File tree

7 files changed

+99
-41
lines changed

7 files changed

+99
-41
lines changed

cmd/drone-acr/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func main() {
4141
os.Setenv("PLUGIN_REGISTRY", registry)
4242
os.Setenv("DOCKER_USERNAME", username)
4343
os.Setenv("DOCKER_PASSWORD", password)
44+
os.Setenv("PLUGIN_REGISTRY_TYPE", "ACR")
4445

4546
// invoke the base docker plugin binary
4647
cmd := exec.Command(docker.GetDroneDockerExecCmd())

cmd/drone-docker/main.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/urfave/cli"
1010

1111
docker "github.com/drone-plugins/drone-docker"
12+
"github.com/drone-plugins/drone-plugin-lib/drone"
1213
)
1314

1415
var (
@@ -279,6 +280,16 @@ func main() {
279280
Usage: "ssh agent key to use",
280281
EnvVar: "PLUGIN_SSH_AGENT_KEY",
281282
},
283+
cli.StringFlag{
284+
Name: "artifact-file",
285+
Usage: "Artifact file location that will be generated by the plugin. This file will include information of docker images that are uploaded by the plugin.",
286+
EnvVar: "PLUGIN_ARTIFACT_FILE",
287+
},
288+
cli.StringFlag{
289+
Name: "registry-type",
290+
Usage: "registry type",
291+
EnvVar: "PLUGIN_REGISTRY_TYPE",
292+
},
282293
}
283294

284295
if err := app.Run(os.Args); err != nil {
@@ -287,6 +298,11 @@ func main() {
287298
}
288299

289300
func run(c *cli.Context) error {
301+
registryType := drone.Docker
302+
if c.String("registry-type") != "" {
303+
registryType = drone.RegistryType(c.String("registry-type"))
304+
}
305+
290306
plugin := docker.Plugin{
291307
Dryrun: c.Bool("dry-run"),
292308
Cleanup: c.BoolT("docker.purge"),
@@ -297,7 +313,8 @@ func run(c *cli.Context) error {
297313
Email: c.String("docker.email"),
298314
Config: c.String("docker.config"),
299315
},
300-
CardPath: c.String("drone-card-path"),
316+
CardPath: c.String("drone-card-path"),
317+
ArtifactFile: c.String("artifact-file"),
301318
Build: docker.Build{
302319
Remote: c.String("remote.url"),
303320
Name: c.String("commit.sha"),
@@ -339,6 +356,7 @@ func run(c *cli.Context) error {
339356
DNSSearch: c.StringSlice("daemon.dns-search"),
340357
MTU: c.String("daemon.mtu"),
341358
Experimental: c.Bool("daemon.experimental"),
359+
RegistryType: registryType,
342360
},
343361
}
344362

cmd/drone-ecr/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ func main() {
111111
os.Setenv("PLUGIN_REGISTRY", registry)
112112
os.Setenv("DOCKER_USERNAME", username)
113113
os.Setenv("DOCKER_PASSWORD", password)
114+
os.Setenv("PLUGIN_REGISTRY_TYPE", "ECR")
114115

115116
// invoke the base docker plugin binary
116117
cmd := exec.Command(docker.GetDroneDockerExecCmd())

cmd/drone-gcr/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func main() {
5555
os.Setenv("PLUGIN_REGISTRY", registry)
5656
os.Setenv("DOCKER_USERNAME", username)
5757
os.Setenv("DOCKER_PASSWORD", password)
58+
os.Setenv("PLUGIN_REGISTRY_TYPE", "GCR")
5859

5960
// invoke the base docker plugin binary
6061
cmd := exec.Command(docker.GetDroneDockerExecCmd())

docker.go

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
11
package docker
22

33
import (
4+
"errors"
45
"fmt"
56
"os"
67
"os/exec"
78
"path/filepath"
89
"runtime"
910
"strings"
1011
"time"
12+
13+
"github.com/drone-plugins/drone-plugin-lib/drone"
1114
)
1215

1316
type (
1417
// Daemon defines Docker daemon parameters.
1518
Daemon struct {
16-
Registry string // Docker registry
17-
Mirror string // Docker registry mirror
18-
Insecure bool // Docker daemon enable insecure registries
19-
StorageDriver string // Docker daemon storage driver
20-
StoragePath string // Docker daemon storage path
21-
Disabled bool // DOcker daemon is disabled (already running)
22-
Debug bool // Docker daemon started in debug mode
23-
Bip string // Docker daemon network bridge IP address
24-
DNS []string // Docker daemon dns server
25-
DNSSearch []string // Docker daemon dns search domain
26-
MTU string // Docker daemon mtu setting
27-
IPv6 bool // Docker daemon IPv6 networking
28-
Experimental bool // Docker daemon enable experimental mode
19+
Registry string // Docker registry
20+
Mirror string // Docker registry mirror
21+
Insecure bool // Docker daemon enable insecure registries
22+
StorageDriver string // Docker daemon storage driver
23+
StoragePath string // Docker daemon storage path
24+
Disabled bool // DOcker daemon is disabled (already running)
25+
Debug bool // Docker daemon started in debug mode
26+
Bip string // Docker daemon network bridge IP address
27+
DNS []string // Docker daemon dns server
28+
DNSSearch []string // Docker daemon dns search domain
29+
MTU string // Docker daemon mtu setting
30+
IPv6 bool // Docker daemon IPv6 networking
31+
Experimental bool // Docker daemon enable experimental mode
32+
RegistryType drone.RegistryType // Docker registry type
2933
}
3034

3135
// Login defines Docker login parameters.
@@ -69,12 +73,13 @@ type (
6973

7074
// Plugin defines the Docker plugin parameters.
7175
Plugin struct {
72-
Login Login // Docker login configuration
73-
Build Build // Docker build configuration
74-
Daemon Daemon // Docker daemon configuration
75-
Dryrun bool // Docker push is skipped
76-
Cleanup bool // Docker purge is enabled
77-
CardPath string // Card path to write file to
76+
Login Login // Docker login configuration
77+
Build Build // Docker build configuration
78+
Daemon Daemon // Docker daemon configuration
79+
Dryrun bool // Docker push is skipped
80+
Cleanup bool // Docker purge is enabled
81+
CardPath string // Card path to write file to
82+
ArtifactFile string // Artifact path to write file to
7883
}
7984

8085
Card []struct {
@@ -223,6 +228,16 @@ func (p Plugin) Exec() error {
223228
fmt.Printf("Could not create adaptive card. %s\n", err)
224229
}
225230

231+
if p.ArtifactFile != "" {
232+
if digest, err := getDigest(p.Build.Name); err == nil {
233+
if err = drone.WritePluginArtifactFile(p.Daemon.RegistryType, p.ArtifactFile, p.Daemon.Registry, p.Build.Repo, digest, p.Build.Tags); err != nil {
234+
fmt.Printf("failed to write plugin artifact file at path: %s with error: %s\n", p.ArtifactFile, err)
235+
}
236+
} else {
237+
fmt.Printf("Could not fetch the digest. %s\n", err)
238+
}
239+
}
240+
226241
// execute cleanup routines in batch mode
227242
if p.Cleanup {
228243
// clear the slice
@@ -551,3 +566,19 @@ func GetDroneDockerExecCmd() string {
551566

552567
return "drone-docker"
553568
}
569+
570+
func getDigest(buildName string) (string, error) {
571+
cmd := exec.Command("docker", "inspect", "--format='{{index .RepoDigests 0}}'", buildName)
572+
output, err := cmd.Output()
573+
if err != nil {
574+
return "", err
575+
}
576+
577+
// Parse the output to extract the repo digest.
578+
digest := strings.Trim(string(output), "'\n")
579+
parts := strings.Split(digest, "@")
580+
if len(parts) > 1 {
581+
return parts[1], nil
582+
}
583+
return "", errors.New("unable to fetch digest")
584+
}

go.mod

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,21 @@ module github.com/drone-plugins/drone-docker
33
require (
44
github.com/aws/aws-sdk-go v1.26.7
55
github.com/coreos/go-semver v0.3.0
6+
github.com/drone-plugins/drone-plugin-lib v0.4.1
67
github.com/drone/drone-go v1.7.1
78
github.com/inhies/go-bytesize v0.0.0-20210819104631-275770b98743
89
github.com/joho/godotenv v1.3.0
9-
github.com/sirupsen/logrus v1.3.0
10+
github.com/sirupsen/logrus v1.9.0
1011
github.com/urfave/cli v1.22.2
1112
)
1213

1314
require (
14-
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect
15+
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
1516
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af // indirect
16-
github.com/konsorten/go-windows-terminal-sequences v1.0.1 // indirect
17-
github.com/russross/blackfriday/v2 v2.0.1 // indirect
18-
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
19-
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 // indirect
20-
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33 // indirect
17+
github.com/russross/blackfriday/v2 v2.1.0 // indirect
18+
golang.org/x/sys v0.0.0-20220731174439-a90be440212d // indirect
2119
gopkg.in/yaml.v2 v2.2.8 // indirect
20+
gopkg.in/yaml.v3 v3.0.1 // indirect
2221
)
2322

2423
go 1.17

go.sum

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
github.com/99designs/httpsignatures-go v0.0.0-20170731043157-88528bf4ca7e/go.mod h1:Xa6lInWHNQnuWoF0YPSsx+INFA9qk7/7pTjwb3PInkY=
22
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
3+
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
34
github.com/aws/aws-sdk-go v1.26.7 h1:ObjEnmzvSdYy8KVd3me7v/UMyCn81inLy2SyoIPoBkg=
45
github.com/aws/aws-sdk-go v1.26.7/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
56
github.com/coreos/go-semver v0.3.0 h1:wkHLiw0WNATZnSG7epLsujiMCgPAc9xhjJ4tgnAxmfM=
67
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
7-
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
88
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
9+
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
10+
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
11+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
912
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1013
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
14+
github.com/drone-plugins/drone-plugin-lib v0.4.1 h1:47rZlmcMpr1hSp+6Gl+1Z4t+efi/gMQU3lxukC1Yg64=
15+
github.com/drone-plugins/drone-plugin-lib v0.4.1/go.mod h1:KwCu92jFjHV3xv2hu5Qg/8zBNvGwbhoJDQw/EwnTvoM=
1116
github.com/drone/drone-go v1.7.1 h1:ZX+3Rs8YHUSUQ5mkuMLmm1zr1ttiiE2YGNxF3AnyDKw=
1217
github.com/drone/drone-go v1.7.1/go.mod h1:fxCf9jAnXDZV1yDr0ckTuWd1intvcQwfJmTRpTZ1mXg=
1318
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
@@ -18,26 +23,28 @@ github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5i
1823
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
1924
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
2025
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
21-
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
22-
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
2326
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2427
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
25-
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
2628
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
27-
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
29+
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
30+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
2831
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
29-
github.com/sirupsen/logrus v1.3.0 h1:hI/7Q+DtNZ2kINb6qt/lS+IyXnHQe9e90POfeewL/ME=
30-
github.com/sirupsen/logrus v1.3.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
31-
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
32-
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
33-
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
32+
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
33+
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
34+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
35+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
36+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
3437
github.com/urfave/cli v1.22.2 h1:gsqYFH8bb9ekPA12kRo0hfjngWQjkJPlN9R0N78BoUo=
3538
github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
36-
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 h1:u+LnwYTOOW7Ukr/fppxEb1Nwz0AtPflrblfvUudpo+I=
37-
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
38-
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33 h1:I6FyU15t786LL7oL/hn43zqTuEGr4PN7F4XJ1p4E3Y8=
39-
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
39+
github.com/urfave/cli/v2 v2.23.6/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc=
40+
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
41+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
42+
golang.org/x/sys v0.0.0-20220731174439-a90be440212d h1:Sv5ogFZatcgIMMtBSTTAgMYsicp25MXBubjXNDKwm80=
43+
golang.org/x/sys v0.0.0-20220731174439-a90be440212d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
4044
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
4145
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
4246
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
4347
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
48+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
49+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
50+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)