Skip to content

Commit 2536ad1

Browse files
Merge pull request #41 from drone-plugins/ci-7218
added utility to write artifact file
2 parents e4261f9 + 4b515c3 commit 2536ad1

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

drone/artifact.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
// RegistryType is the type of registry
17+
type RegistryType string
18+
19+
// Registries const
20+
const (
21+
Docker RegistryType = "Docker"
22+
ECR RegistryType = "ECR"
23+
GCR RegistryType = "GCR"
24+
ACR RegistryType = "ACR"
25+
)
26+
27+
const (
28+
dockerArtifactV1 string = "docker/v1"
29+
)
30+
31+
type (
32+
// Image stores the image data
33+
Image struct {
34+
Image string `json:"image"`
35+
Digest string `json:"digest"`
36+
}
37+
// Data stores the registry data
38+
Data struct {
39+
RegistryType RegistryType `json:"registryType"`
40+
RegistryURL string `json:"registryUrl"`
41+
Images []Image `json:"images"`
42+
}
43+
// DockerArtifact is the current artifact
44+
DockerArtifact struct {
45+
Kind string `json:"kind"`
46+
Data Data `json:"data"`
47+
}
48+
)
49+
50+
// WritePluginArtifactFile writes the docker artifact data to the provided artifact file
51+
func WritePluginArtifactFile(registryType RegistryType, artifactFilePath, registryURL, imageName, digest string, tags []string) error {
52+
var images []Image
53+
for _, tag := range tags {
54+
images = append(images, Image{
55+
Image: fmt.Sprintf("%s:%s", imageName, tag),
56+
Digest: digest,
57+
})
58+
}
59+
data := Data{
60+
RegistryType: registryType,
61+
RegistryURL: registryURL,
62+
Images: images,
63+
}
64+
65+
dockerArtifact := DockerArtifact{
66+
Kind: dockerArtifactV1,
67+
Data: data,
68+
}
69+
70+
b, err := json.MarshalIndent(dockerArtifact, "", "\t")
71+
if err != nil {
72+
return fmt.Errorf("failed with err %s to marshal output %+v", err, dockerArtifact)
73+
}
74+
75+
dir := filepath.Dir(artifactFilePath)
76+
err = os.MkdirAll(dir, 0644)
77+
if err != nil {
78+
return fmt.Errorf("failed with err %s to create %s directory for artifact file", err, dir)
79+
}
80+
81+
err = os.WriteFile(artifactFilePath, b, 0644)
82+
if err != nil {
83+
return fmt.Errorf("failed to write artifact to artifact file %s", artifactFilePath)
84+
}
85+
return nil
86+
}

0 commit comments

Comments
 (0)