Compare commits

...

4 Commits

Author SHA1 Message Date
Aman Singh 6740c1d463 incremented-drone-version 2023-05-17 08:23:19 +05:30
Karl Trygve Kalleberg 0dee97e338 Expose tar_path command line option from Kaniko (#78) 2023-04-03 09:47:53 +01:00
Raghav ed6f3c5bf4 Add output variable support (#77)
* add digest as output variable for kaniko plugin
2023-03-16 10:03:59 +00:00
Raghav 4893b5b945 add support for docker complaint registries for base image (#74) 2023-02-23 11:38:53 +05:30
7 changed files with 102 additions and 16 deletions
+5 -6
View File
@@ -4,7 +4,7 @@ name: default
steps:
- name: build
image: golang:1.18
image: golang:1.19
commands:
- go test ./...
- sh scripts/build.sh
@@ -130,16 +130,15 @@ steps:
- pull_request
---
kind: pipeline
type: docker
type: vm
name: arm
platform:
os: linux
arch: arm64
pool:
use: ubuntu_arm64
steps:
- name: build
image: golang:1.18
image: golang:1.19
commands:
- go test ./...
- sh scripts/build.sh
+14
View File
@@ -176,6 +176,11 @@ func main() {
Usage: "Set this flag if you only want to build the image, without pushing to a registry",
EnvVar: "PLUGIN_NO_PUSH",
},
cli.StringFlag{
Name: "tar-path",
Usage: "Set this flag to save the image as a tarball at path",
EnvVar: "PLUGIN_TAR_PATH",
},
cli.StringFlag{
Name: "verbosity",
Usage: "Set this flag with value as oneof <panic|fatal|error|warn|info|debug|trace> to set the logging level for kaniko. Defaults to info.",
@@ -191,6 +196,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 {
@@ -237,6 +247,7 @@ func run(c *cli.Context) error {
CacheTTL: c.Int("cache-ttl"),
DigestFile: defaultDigestFile,
NoPush: noPush,
TarPath: c.String("tar-path"),
Verbosity: c.String("verbosity"),
Platform: c.String("platform"),
SkipUnusedStages: c.Bool("skip-unused-stages"),
@@ -248,6 +259,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()
}
+12 -2
View File
@@ -64,6 +64,11 @@ func main() {
Value: "Dockerfile",
EnvVar: "PLUGIN_DOCKERFILE",
},
cli.StringFlag{
Name: "docker-registry",
Usage: "docker registry",
EnvVar: "PLUGIN_DOCKER_REGISTRY,DOCKER_REGISTRY",
},
cli.StringFlag{
Name: "docker-username",
Usage: "docker username",
@@ -242,6 +247,7 @@ func run(c *cli.Context) error {
noPush := c.Bool("no-push")
dockerConfig, err := createDockerConfig(
c.String("docker-registry"),
c.String("docker-username"),
c.String("docker-password"),
c.String("access-key"),
@@ -328,12 +334,16 @@ func run(c *cli.Context) error {
return plugin.Exec()
}
func createDockerConfig(dockerUsername, dockerPassword, accessKey, secretKey,
func createDockerConfig(dockerRegistry, dockerUsername, dockerPassword, accessKey, secretKey,
registry, assumeRole, externalId, region string, noPush bool) (*docker.Config, error) {
dockerConfig := docker.NewConfig()
if dockerUsername != "" {
dockerConfig.SetAuth(docker.RegistryV1, dockerUsername, dockerPassword)
// if no docker registry provided, use dockerhub by default
if len(dockerRegistry) == 0 {
dockerRegistry = docker.RegistryV1
}
dockerConfig.SetAuth(dockerRegistry, dockerUsername, dockerPassword)
}
if assumeRole != "" {
+28
View File
@@ -10,6 +10,7 @@ import (
func TestCreateDockerConfig(t *testing.T) {
got, err := createDockerConfig(
"",
"docker-username",
"docker-password",
"access-key",
@@ -34,10 +35,37 @@ func TestCreateDockerConfig(t *testing.T) {
}
}
func TestCreateDockerConfigFromGivenRegistry(t *testing.T) {
got, err := createDockerConfig(
"docker-registry",
"docker-username",
"docker-password",
"access-key",
"secret-key",
"ecr-registry",
"",
"",
"",
false,
)
if err != nil {
t.Error("failed to create docker config")
}
want := docker.NewConfig()
want.SetAuth("docker-registry", "docker-username", "docker-password")
want.SetCredHelper(docker.RegistryECRPublic, "ecr-login")
want.SetCredHelper("ecr-registry", "ecr-login")
if !reflect.DeepEqual(want, got) {
t.Errorf("not equal:\n want: %#v\n got: %#v", want, got)
}
}
func TestCreateDockerConfigKanikoOneDotEight(t *testing.T) {
os.Setenv(kanikoVersionEnv, "1.8.1")
defer os.Setenv(kanikoVersionEnv, "")
got, err := createDockerConfig(
"",
"docker-username",
"docker-password",
"access-key",
+1 -1
View File
@@ -43,4 +43,4 @@ require (
golang.org/x/text v0.3.7 // indirect
)
go 1.18
go 1.19
+30 -7
View File
@@ -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"
)
@@ -38,6 +39,7 @@ type (
Verbosity string // Log level
Platform string // Allows to build with another default platform than the host, similarly to docker build --platform
SkipUnusedStages bool // Build only used stages
TarPath string // Set this flag to save the image as a tarball at path
}
// Artifact defines content of artifact file
@@ -49,10 +51,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
}
)
@@ -148,14 +156,15 @@ func (p Plugin) Exec() error {
fmt.Sprintf("--context=dir://%s", p.Build.Context),
}
// Set the destination repository
if !p.Build.NoPush {
// Set the destination repository only when we push or save to tarball
if !p.Build.NoPush || p.Build.TarPath != "" {
for _, tag := range tags {
for _, label := range p.Build.labelsForTag(tag) {
cmdArgs = append(cmdArgs, fmt.Sprintf("--destination=%s:%s", p.Build.Repo, label))
}
}
}
// Set the build arguments
for _, arg := range p.Build.Args {
cmdArgs = append(cmdArgs, fmt.Sprintf("--build-arg=%s", arg))
@@ -212,6 +221,10 @@ func (p Plugin) Exec() error {
cmdArgs = append(cmdArgs, "--skip-unused-stages")
}
if p.Build.TarPath != "" {
cmdArgs = append(cmdArgs, fmt.Sprintf("--tar-path=%s", p.Build.TarPath))
}
cmd := exec.Command("/kaniko/executor", cmdArgs...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
@@ -223,19 +236,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) {
+12
View File
@@ -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)
}