From ed6f3c5bf4220b61d9d6d8a9066ca6c59404bbd4 Mon Sep 17 00:00:00 2001 From: Raghav Date: Thu, 16 Mar 2023 15:33:59 +0530 Subject: [PATCH] Add output variable support (#77) * add digest as output variable for kaniko plugin --- .drone.yml | 7 +++---- cmd/kaniko-docker/main.go | 8 ++++++++ kaniko.go | 27 ++++++++++++++++++++++----- pkg/output/output.go | 12 ++++++++++++ 4 files changed, 45 insertions(+), 9 deletions(-) create mode 100644 pkg/output/output.go diff --git a/.drone.yml b/.drone.yml index f5e9f91..8917710 100644 --- a/.drone.yml +++ b/.drone.yml @@ -130,12 +130,11 @@ steps: - pull_request --- kind: pipeline -type: docker +type: vm name: arm -platform: - os: linux - arch: arm64 +pool: + use: ubuntu_arm64 steps: - name: build diff --git a/cmd/kaniko-docker/main.go b/cmd/kaniko-docker/main.go index 712cfa9..e41b2f1 100644 --- a/cmd/kaniko-docker/main.go +++ b/cmd/kaniko-docker/main.go @@ -191,6 +191,11 @@ func main() { Usage: "build only used stages", EnvVar: "PLUGIN_SKIP_UNUSED_STAGES", }, + cli.StringFlag{ + Name: "output-file", + Usage: "Output file location that will be generated by the plugin. This file will include information of the output that are exported by the plugin.", + EnvVar: "DRONE_OUTPUT", + }, } if err := app.Run(os.Args); err != nil { @@ -248,6 +253,9 @@ func run(c *cli.Context) error { ArtifactFile: c.String("artifact-file"), RegistryType: artifact.Docker, }, + Output: kaniko.Output{ + OutputFile: c.String("output-file"), + }, } return plugin.Exec() } diff --git a/kaniko.go b/kaniko.go index 9b222c4..f5df59e 100644 --- a/kaniko.go +++ b/kaniko.go @@ -8,6 +8,7 @@ import ( "strings" "github.com/drone/drone-kaniko/pkg/artifact" + "github.com/drone/drone-kaniko/pkg/output" "github.com/drone/drone-kaniko/pkg/tagger" "golang.org/x/mod/semver" ) @@ -49,10 +50,16 @@ type ( ArtifactFile string // Artifact file location } + // Output defines content of output file + Output struct { + OutputFile string // File where plugin output are saved + } + // Plugin defines the Docker plugin parameters. Plugin struct { Build Build // Docker build configuration Artifact Artifact // Artifact file content + Output Output // Output file content } ) @@ -223,19 +230,29 @@ func (p Plugin) Exec() error { } if p.Build.DigestFile != "" && p.Artifact.ArtifactFile != "" { - content, err := ioutil.ReadFile(p.Build.DigestFile) - if err != nil { - fmt.Fprintf(os.Stderr, "failed to read digest file contents at path: %s with error: %s\n", p.Build.DigestFile, err) - } - err = artifact.WritePluginArtifactFile(p.Artifact.RegistryType, p.Artifact.ArtifactFile, p.Artifact.Registry, p.Artifact.Repo, string(content), p.Artifact.Tags) + err = artifact.WritePluginArtifactFile(p.Artifact.RegistryType, p.Artifact.ArtifactFile, p.Artifact.Registry, p.Artifact.Repo, getDigest(p.Build.DigestFile), p.Artifact.Tags) if err != nil { fmt.Fprintf(os.Stderr, "failed to write plugin artifact file at path: %s with error: %s\n", p.Artifact.ArtifactFile, err) } } + if p.Output.OutputFile != "" { + if err = output.WritePluginOutputFile(p.Output.OutputFile, getDigest(p.Build.DigestFile)); err != nil { + fmt.Fprintf(os.Stderr, "failed to write plugin output file at path: %s with error: %s\n", p.Output.OutputFile, err) + } + } + return nil } +func getDigest(digestFile string) string { + content, err := ioutil.ReadFile(digestFile) + if err != nil { + fmt.Fprintf(os.Stderr, "failed to read digest file contents at path: %s with error: %s\n", digestFile, err) + } + return string(content) +} + // trace writes each command to stdout with the command wrapped in an xml // tag so that it can be extracted and displayed in the logs. func trace(cmd *exec.Cmd) { diff --git a/pkg/output/output.go b/pkg/output/output.go new file mode 100644 index 0000000..de6349d --- /dev/null +++ b/pkg/output/output.go @@ -0,0 +1,12 @@ +package output + +import ( + "github.com/joho/godotenv" +) + +func WritePluginOutputFile(outputFilePath, digest string) error { + output := map[string]string{ + "digest": digest, + } + return godotenv.Write(output, outputFilePath) +}