mirror of
https://github.com/drone/drone-kaniko.git
synced 2026-06-14 05:12:26 +08:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 15255d3520 | |||
| 6ba0eb58c3 |
+22
-10
@@ -90,9 +90,9 @@ func main() {
|
|||||||
Usage: "enable auto generation of build tags",
|
Usage: "enable auto generation of build tags",
|
||||||
EnvVar: "PLUGIN_AUTO_TAG",
|
EnvVar: "PLUGIN_AUTO_TAG",
|
||||||
},
|
},
|
||||||
cli.BoolFlag{
|
cli.StringFlag{
|
||||||
Name: "dockerconfig-override",
|
Name: "dockerconfig-override",
|
||||||
Usage: "enable auto generation of build tags",
|
Usage: "use provided docker config override for docker auth",
|
||||||
EnvVar: "PLUGIN_DOCKERCONFIG_OVERRIDE",
|
EnvVar: "PLUGIN_DOCKERCONFIG_OVERRIDE",
|
||||||
},
|
},
|
||||||
cli.StringFlag{
|
cli.StringFlag{
|
||||||
@@ -201,11 +201,15 @@ func main() {
|
|||||||
func run(c *cli.Context) error {
|
func run(c *cli.Context) error {
|
||||||
username := c.String("username")
|
username := c.String("username")
|
||||||
noPush := c.Bool("no-push")
|
noPush := c.Bool("no-push")
|
||||||
// use the dockerconfig present at the path instead of creating one
|
configOverride := c.String("dockerconfig-override")
|
||||||
configOverride := c.Bool("dockerconfig-override")
|
|
||||||
|
|
||||||
// only setup auth when pushing or credentials are defined and docker config override is false
|
// if configOverride is provided, use this for docker auth
|
||||||
if (!noPush || username != "") && !configOverride {
|
if len(configOverride) > 0 {
|
||||||
|
if err := writeDockerCfgFile([]byte(c.String("dockerconfig-override"))); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else if !noPush || username != "" {
|
||||||
|
// setup auth when pushing or credentials are defined and docker config override is false
|
||||||
if err := createDockerCfgFile(username, c.String("password"), c.String("registry")); err != nil {
|
if err := createDockerCfgFile(username, c.String("password"), c.String("registry")); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -266,14 +270,22 @@ func createDockerCfgFile(username, password, registry string) error {
|
|||||||
registry = v1RegistryURL
|
registry = v1RegistryURL
|
||||||
}
|
}
|
||||||
|
|
||||||
|
authBytes := []byte(fmt.Sprintf("%s:%s", username, password))
|
||||||
|
encodedString := base64.StdEncoding.EncodeToString(authBytes)
|
||||||
|
jsonBytes := []byte(fmt.Sprintf(`{"auths": {"%s": {"auth": "%s"}}}`, registry, encodedString))
|
||||||
|
|
||||||
|
if err := writeDockerCfgFile(jsonBytes); err != nil {
|
||||||
|
return errors.Wrap(err, "failed to write docker config file")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write json bytes in the docker config file
|
||||||
|
func writeDockerCfgFile(jsonBytes []byte) error {
|
||||||
err := os.MkdirAll(dockerPath, 0600)
|
err := os.MkdirAll(dockerPath, 0600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, fmt.Sprintf("failed to create %s directory", dockerPath))
|
return errors.Wrap(err, fmt.Sprintf("failed to create %s directory", dockerPath))
|
||||||
}
|
}
|
||||||
|
|
||||||
authBytes := []byte(fmt.Sprintf("%s:%s", username, password))
|
|
||||||
encodedString := base64.StdEncoding.EncodeToString(authBytes)
|
|
||||||
jsonBytes := []byte(fmt.Sprintf(`{"auths": {"%s": {"auth": "%s"}}}`, registry, encodedString))
|
|
||||||
err = ioutil.WriteFile(dockerConfigPath, jsonBytes, 0644)
|
err = ioutil.WriteFile(dockerConfigPath, jsonBytes, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "failed to create docker config file")
|
return errors.Wrap(err, "failed to create docker config file")
|
||||||
|
|||||||
Reference in New Issue
Block a user