Skip to content

Commit 246dfb3

Browse files
authored
(feat) publish docker data to create drone card (#347)
* plugin logic to write card data to publish drone card
1 parent 3593c41 commit 246dfb3

File tree

7 files changed

+244
-10
lines changed

7 files changed

+244
-10
lines changed

card.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package docker
2+
3+
import (
4+
"encoding/base64"
5+
"encoding/json"
6+
"fmt"
7+
"io"
8+
"io/ioutil"
9+
"os"
10+
"os/exec"
11+
"time"
12+
13+
"github.com/drone/drone-go/drone"
14+
15+
"github.com/inhies/go-bytesize"
16+
)
17+
18+
func (p Plugin) writeCard() error {
19+
cmd := exec.Command("docker", "inspect", p.Build.Name)
20+
data, err := cmd.CombinedOutput()
21+
if err != nil {
22+
return err
23+
}
24+
25+
out := Inspect{}
26+
if err := json.Unmarshal(data, &out); err != nil {
27+
return err
28+
}
29+
30+
inspect := out[0]
31+
inspect.SizeString = fmt.Sprint(bytesize.New(float64(inspect.Size)))
32+
inspect.VirtualSizeString = fmt.Sprint(bytesize.New(float64(inspect.VirtualSize)))
33+
inspect.Time = fmt.Sprint(inspect.Metadata.LastTagTime.Format(time.RFC3339))
34+
cardData, _ := json.Marshal(inspect)
35+
36+
card := drone.CardInput{
37+
Schema: "https://drone-plugins.github.io/drone-docker/card.json",
38+
Data: cardData,
39+
}
40+
41+
writeCard(p.CardPath, &card)
42+
return nil
43+
}
44+
45+
func writeCard(path string, card interface{}) {
46+
data, _ := json.Marshal(card)
47+
switch {
48+
case path == "/dev/stdout":
49+
writeCardTo(os.Stdout, data)
50+
case path == "/dev/stderr":
51+
writeCardTo(os.Stderr, data)
52+
case path != "":
53+
ioutil.WriteFile(path, data, 0644)
54+
}
55+
}
56+
57+
func writeCardTo(out io.Writer, data []byte) {
58+
encoded := base64.StdEncoding.EncodeToString(data)
59+
io.WriteString(out, "\u001B]1338;")
60+
io.WriteString(out, encoded)
61+
io.WriteString(out, "\u001B]0m")
62+
io.WriteString(out, "\n")
63+
}

cmd/drone-docker/main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,11 @@ func main() {
249249
Usage: "additional host:IP mapping",
250250
EnvVar: "PLUGIN_ADD_HOST",
251251
},
252+
cli.StringFlag{
253+
Name: "drone-card-path",
254+
Usage: "card path location to write to",
255+
EnvVar: "DRONE_CARD_PATH",
256+
},
252257
}
253258

254259
if err := app.Run(os.Args); err != nil {
@@ -267,6 +272,7 @@ func run(c *cli.Context) error {
267272
Email: c.String("docker.email"),
268273
Config: c.String("docker.config"),
269274
},
275+
CardPath: c.String("drone-card-path"),
270276
Build: docker.Build{
271277
Remote: c.String("remote.url"),
272278
Name: c.String("commit.sha"),

docker.go

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,34 @@ type (
6464

6565
// Plugin defines the Docker plugin parameters.
6666
Plugin struct {
67-
Login Login // Docker login configuration
68-
Build Build // Docker build configuration
69-
Daemon Daemon // Docker daemon configuration
70-
Dryrun bool // Docker push is skipped
71-
Cleanup bool // Docker purge is enabled
67+
Login Login // Docker login configuration
68+
Build Build // Docker build configuration
69+
Daemon Daemon // Docker daemon configuration
70+
Dryrun bool // Docker push is skipped
71+
Cleanup bool // Docker purge is enabled
72+
CardPath string // Card path to write file to
73+
}
74+
75+
Inspect []struct {
76+
ID string `json:"Id"`
77+
RepoTags []string `json:"RepoTags"`
78+
RepoDigests []interface{} `json:"RepoDigests"`
79+
Parent string `json:"Parent"`
80+
Comment string `json:"Comment"`
81+
Created time.Time `json:"Created"`
82+
Container string `json:"Container"`
83+
DockerVersion string `json:"DockerVersion"`
84+
Author string `json:"Author"`
85+
Architecture string `json:"Architecture"`
86+
Os string `json:"Os"`
87+
Size int `json:"Size"`
88+
VirtualSize int `json:"VirtualSize"`
89+
Metadata struct {
90+
LastTagTime time.Time `json:"LastTagTime"`
91+
} `json:"Metadata"`
92+
SizeString string
93+
VirtualSizeString string
94+
Time string
7295
}
7396
)
7497

@@ -157,11 +180,6 @@ func (p Plugin) Exec() error {
157180
}
158181
}
159182

160-
if p.Cleanup {
161-
cmds = append(cmds, commandRmi(p.Build.Name)) // docker rmi
162-
cmds = append(cmds, commandPrune()) // docker system prune -f
163-
}
164-
165183
// execute all commands in batch mode.
166184
for _, cmd := range cmds {
167185
cmd.Stdout = os.Stdout
@@ -180,6 +198,26 @@ func (p Plugin) Exec() error {
180198
}
181199
}
182200

201+
// output the adaptive card
202+
if err := p.writeCard(); err != nil {
203+
fmt.Printf("Could not create adaptive card. %s\n", err)
204+
}
205+
206+
// execute cleanup routines in batch mode
207+
if p.Cleanup {
208+
// clear the slice
209+
cmds = nil
210+
211+
cmds = append(cmds, commandRmi(p.Build.Name)) // docker rmi
212+
cmds = append(cmds, commandPrune()) // docker system prune -f
213+
214+
for _, cmd := range cmds {
215+
cmd.Stdout = os.Stdout
216+
cmd.Stderr = os.Stderr
217+
trace(cmd)
218+
}
219+
}
220+
183221
return nil
184222
}
185223

docs/card.json

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
{
2+
"type": "AdaptiveCard",
3+
"body": [
4+
{
5+
"type": "ColumnSet",
6+
"columns": [
7+
{
8+
"type": "Column",
9+
"items": [
10+
{
11+
"type": "Image",
12+
"url": "https://d36jcksde1wxzq.cloudfront.net/be7833db9bddb4494d2a7c3dd659199a.png",
13+
"size": "Medium"
14+
}
15+
],
16+
"width": "auto"
17+
},
18+
{
19+
"type": "Column",
20+
"items": [
21+
{
22+
"type": "TextBlock",
23+
"text": "${RepoTags[0]}",
24+
"size": "Medium"
25+
},
26+
{
27+
"type": "TextBlock",
28+
"text": "DIGEST: ${RepoDigests[0]}",
29+
"wrap": true,
30+
"size": "Small",
31+
"weight": "Lighter",
32+
"isSubtle": true,
33+
"spacing": "Small"
34+
},
35+
{
36+
"type": "ColumnSet",
37+
"columns": [
38+
{
39+
"type": "Column",
40+
"items": [
41+
{
42+
"type": "TextBlock",
43+
"weight": "Lighter",
44+
"text": "OS/ARCH",
45+
"wrap": true,
46+
"size": "Small",
47+
"isSubtle": true,
48+
"spacing": "Medium"
49+
},
50+
{
51+
"type": "TextBlock",
52+
"text": "${OS}/${Architecture}",
53+
"wrap": true,
54+
"size": "Small",
55+
"spacing": "Small"
56+
}
57+
],
58+
"separator": true,
59+
"width": "auto"
60+
},
61+
{
62+
"type": "Column",
63+
"items": [
64+
{
65+
"type": "TextBlock",
66+
"weight": "Lighter",
67+
"text": "SIZE",
68+
"wrap": true,
69+
"size": "Small",
70+
"isSubtle": true
71+
},
72+
{
73+
"type": "TextBlock",
74+
"spacing": "Small",
75+
"text": "${SizeString}",
76+
"wrap": true,
77+
"size": "Small"
78+
}
79+
],
80+
"width": "auto",
81+
"separator": true,
82+
"spacing": "Medium"
83+
},
84+
{
85+
"type": "Column",
86+
"items": [
87+
{
88+
"type": "TextBlock",
89+
"weight": "Lighter",
90+
"text": "LAST PUSHED",
91+
"wrap": true,
92+
"size": "Small",
93+
"isSubtle": true
94+
},
95+
{
96+
"type": "TextBlock",
97+
"spacing": "Small",
98+
"text": "{{DATE(${Time})}} - {{TIME(${Time})}}",
99+
"wrap": true,
100+
"size": "Small"
101+
}
102+
],
103+
"width": "auto",
104+
"separator": true,
105+
"spacing": "Medium"
106+
}
107+
],
108+
"style": "default"
109+
}
110+
],
111+
"width": "stretch"
112+
}
113+
]
114+
}
115+
],
116+
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
117+
"version": "1.0"
118+
}

docs/index.json

Whitespace-only changes.

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ 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/drone-go v1.7.1
7+
github.com/inhies/go-bytesize v0.0.0-20210819104631-275770b98743
68
github.com/joho/godotenv v1.3.0
79
github.com/sirupsen/logrus v1.3.0
810
github.com/urfave/cli v1.22.2

go.sum

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
github.com/99designs/httpsignatures-go v0.0.0-20170731043157-88528bf4ca7e/go.mod h1:Xa6lInWHNQnuWoF0YPSsx+INFA9qk7/7pTjwb3PInkY=
12
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
23
github.com/aws/aws-sdk-go v1.26.7 h1:ObjEnmzvSdYy8KVd3me7v/UMyCn81inLy2SyoIPoBkg=
34
github.com/aws/aws-sdk-go v1.26.7/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
@@ -7,6 +8,12 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSY
78
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
89
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
910
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
11+
github.com/drone/drone-go v1.7.1 h1:ZX+3Rs8YHUSUQ5mkuMLmm1zr1ttiiE2YGNxF3AnyDKw=
12+
github.com/drone/drone-go v1.7.1/go.mod h1:fxCf9jAnXDZV1yDr0ckTuWd1intvcQwfJmTRpTZ1mXg=
13+
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
14+
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
15+
github.com/inhies/go-bytesize v0.0.0-20210819104631-275770b98743 h1:X3Xxno5Ji8idrNiUoFc7QyXpqhSYlDRYQmc7mlpMBzU=
16+
github.com/inhies/go-bytesize v0.0.0-20210819104631-275770b98743/go.mod h1:KrtyD5PFj++GKkFS/7/RRrfnRhAMGQwy75GLCHWrCNs=
1017
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM=
1118
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
1219
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=

0 commit comments

Comments
 (0)