Skip to content

Commit d32d3f3

Browse files
committed
added utility to write artifact file
1 parent e4261f9 commit d32d3f3

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

drone/artifact.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright (c) 2019, the Drone Plugins project authors.
2+
// Please see the AUTHORS file for details. All rights reserved.
3+
// Use of this source code is governed by an Apache 2.0 license that can be
4+
// found in the LICENSE file.
5+
6+
package drone
7+
8+
import (
9+
"encoding/json"
10+
"fmt"
11+
12+
"os"
13+
"path/filepath"
14+
)
15+
16+
type RegistryType string
17+
18+
const (
19+
Docker RegistryType = "Docker"
20+
ECR RegistryType = "ECR"
21+
GCR RegistryType = "GCR"
22+
ACR RegistryType = "ACR"
23+
)
24+
25+
const (
26+
dockerArtifactV1 string = "docker/v1"
27+
)
28+
29+
type (
30+
Image struct {
31+
Image string `json:"image"`
32+
Digest string `json:"digest"`
33+
}
34+
Data struct {
35+
RegistryType RegistryType `json:"registryType"`
36+
RegistryUrl string `json:"registryUrl"`
37+
Images []Image `json:"images"`
38+
}
39+
DockerArtifact struct {
40+
Kind string `json:"kind"`
41+
Data Data `json:"data"`
42+
}
43+
)
44+
45+
func WritePluginArtifactFile(registryType RegistryType, artifactFilePath, registryUrl, imageName, digest string, tags []string) error {
46+
var images []Image
47+
for _, tag := range tags {
48+
images = append(images, Image{
49+
Image: fmt.Sprintf("%s:%s", imageName, tag),
50+
Digest: digest,
51+
})
52+
}
53+
data := Data{
54+
RegistryType: registryType,
55+
RegistryUrl: registryUrl,
56+
Images: images,
57+
}
58+
59+
dockerArtifact := DockerArtifact{
60+
Kind: dockerArtifactV1,
61+
Data: data,
62+
}
63+
64+
b, err := json.MarshalIndent(dockerArtifact, "", "\t")
65+
if err != nil {
66+
return fmt.Errorf("failed with err %s to marshal output %+v", err, dockerArtifact)
67+
}
68+
69+
dir := filepath.Dir(artifactFilePath)
70+
err = os.MkdirAll(dir, 0644)
71+
if err != nil {
72+
return fmt.Errorf("failed with err %s to create %s directory for artifact file", err, dir)
73+
}
74+
75+
err = os.WriteFile(artifactFilePath, b, 0644)
76+
if err != nil {
77+
return fmt.Errorf("failed to write artifact to artifact file %s", artifactFilePath)
78+
}
79+
return nil
80+
}

0 commit comments

Comments
 (0)