mirror of
https://github.com/drone-plugins/drone-docker.git
synced 2026-06-14 14:03:08 +08:00
Use random string as temporary tag
This commit is contained in:
@@ -17,8 +17,8 @@ import (
|
|||||||
"github.com/inhies/go-bytesize"
|
"github.com/inhies/go-bytesize"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (p Plugin) writeCard(buildName string) error {
|
func (p Plugin) writeCard() error {
|
||||||
cmd := exec.Command(dockerExe, "inspect", buildName)
|
cmd := exec.Command(dockerExe, "inspect", p.Build.TempTag)
|
||||||
data, err := cmd.CombinedOutput()
|
data, err := cmd.CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
@@ -14,6 +16,7 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
version = "unknown"
|
version = "unknown"
|
||||||
|
charset = []byte("abcdefghijklmnopqrstuvwxyz")
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -318,6 +321,7 @@ func run(c *cli.Context) error {
|
|||||||
Build: docker.Build{
|
Build: docker.Build{
|
||||||
Remote: c.String("remote.url"),
|
Remote: c.String("remote.url"),
|
||||||
Name: c.String("commit.sha"),
|
Name: c.String("commit.sha"),
|
||||||
|
TempTag: generateTempTag(),
|
||||||
Dockerfile: c.String("dockerfile"),
|
Dockerfile: c.String("dockerfile"),
|
||||||
Context: c.String("context"),
|
Context: c.String("context"),
|
||||||
Tags: c.StringSlice("tags"),
|
Tags: c.StringSlice("tags"),
|
||||||
@@ -383,6 +387,20 @@ func run(c *cli.Context) error {
|
|||||||
return plugin.Exec()
|
return plugin.Exec()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func generateTempTag() string {
|
||||||
|
tagLength := 8
|
||||||
|
return randomString(tagLength)
|
||||||
|
}
|
||||||
|
|
||||||
|
func randomString(n int) string {
|
||||||
|
rand.Seed(time.Now().UTC().UnixNano())
|
||||||
|
b := make([]byte, n)
|
||||||
|
for i := range b {
|
||||||
|
b[i] = charset[rand.Intn(len(charset))]
|
||||||
|
}
|
||||||
|
return string(b)
|
||||||
|
}
|
||||||
|
|
||||||
func GetExecCmd() string {
|
func GetExecCmd() string {
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
return "C:/bin/drone-docker.exe"
|
return "C:/bin/drone-docker.exe"
|
||||||
|
|||||||
@@ -45,6 +45,7 @@ type (
|
|||||||
Build struct {
|
Build struct {
|
||||||
Remote string // Git remote URL
|
Remote string // Git remote URL
|
||||||
Name string // Docker build using default named tag
|
Name string // Docker build using default named tag
|
||||||
|
TempTag string // Temporary tag used during docker build
|
||||||
Dockerfile string // Docker build Dockerfile
|
Dockerfile string // Docker build Dockerfile
|
||||||
Context string // Docker build context
|
Context string // Docker build context
|
||||||
Tags []string // Docker build tags
|
Tags []string // Docker build tags
|
||||||
@@ -195,11 +196,10 @@ func (p Plugin) Exec() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
buildName := getUniqueBuildName(p.Build.Repo, p.Build.Name)
|
cmds = append(cmds, commandBuild(p.Build)) // docker build
|
||||||
cmds = append(cmds, commandBuild(p.Build, buildName)) // docker build
|
|
||||||
|
|
||||||
for _, tag := range p.Build.Tags {
|
for _, tag := range p.Build.Tags {
|
||||||
cmds = append(cmds, commandTag(p.Build, tag, buildName)) // docker tag
|
cmds = append(cmds, commandTag(p.Build, tag)) // docker tag
|
||||||
|
|
||||||
if !p.Dryrun {
|
if !p.Dryrun {
|
||||||
cmds = append(cmds, commandPush(p.Build, tag)) // docker push
|
cmds = append(cmds, commandPush(p.Build, tag)) // docker push
|
||||||
@@ -225,12 +225,12 @@ func (p Plugin) Exec() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// output the adaptive card
|
// output the adaptive card
|
||||||
if err := p.writeCard(buildName); err != nil {
|
if err := p.writeCard(); err != nil {
|
||||||
fmt.Printf("Could not create adaptive card. %s\n", err)
|
fmt.Printf("Could not create adaptive card. %s\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.ArtifactFile != "" {
|
if p.ArtifactFile != "" {
|
||||||
if digest, err := getDigest(buildName); err == nil {
|
if digest, err := getDigest(p.Build.TempTag); err == nil {
|
||||||
if err = drone.WritePluginArtifactFile(p.Daemon.RegistryType, p.ArtifactFile, p.Daemon.Registry, p.Build.Repo, digest, p.Build.Tags); err != nil {
|
if err = drone.WritePluginArtifactFile(p.Daemon.RegistryType, p.ArtifactFile, p.Daemon.Registry, p.Build.Repo, digest, p.Build.Tags); err != nil {
|
||||||
fmt.Printf("failed to write plugin artifact file at path: %s with error: %s\n", p.ArtifactFile, err)
|
fmt.Printf("failed to write plugin artifact file at path: %s with error: %s\n", p.ArtifactFile, err)
|
||||||
}
|
}
|
||||||
@@ -244,8 +244,8 @@ func (p Plugin) Exec() error {
|
|||||||
// clear the slice
|
// clear the slice
|
||||||
cmds = nil
|
cmds = nil
|
||||||
|
|
||||||
cmds = append(cmds, commandRmi(buildName)) // docker rmi
|
cmds = append(cmds, commandRmi(p.Build.TempTag)) // docker rmi
|
||||||
cmds = append(cmds, commandPrune()) // docker system prune -f
|
cmds = append(cmds, commandPrune()) // docker system prune -f
|
||||||
|
|
||||||
for _, cmd := range cmds {
|
for _, cmd := range cmds {
|
||||||
cmd.Stdout = os.Stdout
|
cmd.Stdout = os.Stdout
|
||||||
@@ -300,12 +300,12 @@ func commandInfo() *exec.Cmd {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// helper function to create the docker build command.
|
// helper function to create the docker build command.
|
||||||
func commandBuild(build Build, buildName string) *exec.Cmd {
|
func commandBuild(build Build) *exec.Cmd {
|
||||||
args := []string{
|
args := []string{
|
||||||
"build",
|
"build",
|
||||||
"--rm=true",
|
"--rm=true",
|
||||||
"-f", build.Dockerfile,
|
"-f", build.Dockerfile,
|
||||||
"-t", buildName,
|
"-t", build.TempTag,
|
||||||
}
|
}
|
||||||
|
|
||||||
args = append(args, build.Context)
|
args = append(args, build.Context)
|
||||||
@@ -462,9 +462,9 @@ func hasProxyBuildArg(build *Build, key string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// helper function to create the docker tag command.
|
// helper function to create the docker tag command.
|
||||||
func commandTag(build Build, tag, buildName string) *exec.Cmd {
|
func commandTag(build Build, tag string) *exec.Cmd {
|
||||||
var (
|
var (
|
||||||
source = buildName
|
source = build.TempTag
|
||||||
target = fmt.Sprintf("%s:%s", build.Repo, tag)
|
target = fmt.Sprintf("%s:%s", build.Repo, tag)
|
||||||
)
|
)
|
||||||
return exec.Command(
|
return exec.Command(
|
||||||
|
|||||||
+13
-7
@@ -16,6 +16,7 @@ func TestCommandBuild(t *testing.T) {
|
|||||||
name: "secret from env var",
|
name: "secret from env var",
|
||||||
build: Build{
|
build: Build{
|
||||||
Name: "plugins/drone-docker:latest",
|
Name: "plugins/drone-docker:latest",
|
||||||
|
TempTag: "abcdefgh",
|
||||||
Dockerfile: "Dockerfile",
|
Dockerfile: "Dockerfile",
|
||||||
Context: ".",
|
Context: ".",
|
||||||
SecretEnvs: []string{
|
SecretEnvs: []string{
|
||||||
@@ -29,7 +30,7 @@ func TestCommandBuild(t *testing.T) {
|
|||||||
"-f",
|
"-f",
|
||||||
"Dockerfile",
|
"Dockerfile",
|
||||||
"-t",
|
"-t",
|
||||||
"plugins/drone-docker:latest",
|
"abcdefgh",
|
||||||
".",
|
".",
|
||||||
"--secret id=foo_secret,env=FOO_SECRET_ENV_VAR",
|
"--secret id=foo_secret,env=FOO_SECRET_ENV_VAR",
|
||||||
),
|
),
|
||||||
@@ -38,6 +39,7 @@ func TestCommandBuild(t *testing.T) {
|
|||||||
name: "secret from file",
|
name: "secret from file",
|
||||||
build: Build{
|
build: Build{
|
||||||
Name: "plugins/drone-docker:latest",
|
Name: "plugins/drone-docker:latest",
|
||||||
|
TempTag: "abcdefgh",
|
||||||
Dockerfile: "Dockerfile",
|
Dockerfile: "Dockerfile",
|
||||||
Context: ".",
|
Context: ".",
|
||||||
SecretFiles: []string{
|
SecretFiles: []string{
|
||||||
@@ -51,7 +53,7 @@ func TestCommandBuild(t *testing.T) {
|
|||||||
"-f",
|
"-f",
|
||||||
"Dockerfile",
|
"Dockerfile",
|
||||||
"-t",
|
"-t",
|
||||||
"plugins/drone-docker:latest",
|
"abcdefgh",
|
||||||
".",
|
".",
|
||||||
"--secret id=foo_secret,src=/path/to/foo_secret",
|
"--secret id=foo_secret,src=/path/to/foo_secret",
|
||||||
),
|
),
|
||||||
@@ -60,6 +62,7 @@ func TestCommandBuild(t *testing.T) {
|
|||||||
name: "multiple mixed secrets",
|
name: "multiple mixed secrets",
|
||||||
build: Build{
|
build: Build{
|
||||||
Name: "plugins/drone-docker:latest",
|
Name: "plugins/drone-docker:latest",
|
||||||
|
TempTag: "abcdefgh",
|
||||||
Dockerfile: "Dockerfile",
|
Dockerfile: "Dockerfile",
|
||||||
Context: ".",
|
Context: ".",
|
||||||
SecretEnvs: []string{
|
SecretEnvs: []string{
|
||||||
@@ -78,7 +81,7 @@ func TestCommandBuild(t *testing.T) {
|
|||||||
"-f",
|
"-f",
|
||||||
"Dockerfile",
|
"Dockerfile",
|
||||||
"-t",
|
"-t",
|
||||||
"plugins/drone-docker:latest",
|
"abcdefgh",
|
||||||
".",
|
".",
|
||||||
"--secret id=foo_secret,env=FOO_SECRET_ENV_VAR",
|
"--secret id=foo_secret,env=FOO_SECRET_ENV_VAR",
|
||||||
"--secret id=bar_secret,env=BAR_SECRET_ENV_VAR",
|
"--secret id=bar_secret,env=BAR_SECRET_ENV_VAR",
|
||||||
@@ -90,6 +93,7 @@ func TestCommandBuild(t *testing.T) {
|
|||||||
name: "invalid mixed secrets",
|
name: "invalid mixed secrets",
|
||||||
build: Build{
|
build: Build{
|
||||||
Name: "plugins/drone-docker:latest",
|
Name: "plugins/drone-docker:latest",
|
||||||
|
TempTag: "abcdefgh",
|
||||||
Dockerfile: "Dockerfile",
|
Dockerfile: "Dockerfile",
|
||||||
Context: ".",
|
Context: ".",
|
||||||
SecretEnvs: []string{
|
SecretEnvs: []string{
|
||||||
@@ -110,7 +114,7 @@ func TestCommandBuild(t *testing.T) {
|
|||||||
"-f",
|
"-f",
|
||||||
"Dockerfile",
|
"Dockerfile",
|
||||||
"-t",
|
"-t",
|
||||||
"plugins/drone-docker:latest",
|
"abcdefgh",
|
||||||
".",
|
".",
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
@@ -118,6 +122,7 @@ func TestCommandBuild(t *testing.T) {
|
|||||||
name: "platform argument",
|
name: "platform argument",
|
||||||
build: Build{
|
build: Build{
|
||||||
Name: "plugins/drone-docker:latest",
|
Name: "plugins/drone-docker:latest",
|
||||||
|
TempTag: "abcdefgh",
|
||||||
Dockerfile: "Dockerfile",
|
Dockerfile: "Dockerfile",
|
||||||
Context: ".",
|
Context: ".",
|
||||||
Platform: "test/platform",
|
Platform: "test/platform",
|
||||||
@@ -129,7 +134,7 @@ func TestCommandBuild(t *testing.T) {
|
|||||||
"-f",
|
"-f",
|
||||||
"Dockerfile",
|
"Dockerfile",
|
||||||
"-t",
|
"-t",
|
||||||
"plugins/drone-docker:latest",
|
"abcdefgh",
|
||||||
".",
|
".",
|
||||||
"--platform",
|
"--platform",
|
||||||
"test/platform",
|
"test/platform",
|
||||||
@@ -139,6 +144,7 @@ func TestCommandBuild(t *testing.T) {
|
|||||||
name: "ssh agent",
|
name: "ssh agent",
|
||||||
build: Build{
|
build: Build{
|
||||||
Name: "plugins/drone-docker:latest",
|
Name: "plugins/drone-docker:latest",
|
||||||
|
TempTag: "abcdefgh",
|
||||||
Dockerfile: "Dockerfile",
|
Dockerfile: "Dockerfile",
|
||||||
Context: ".",
|
Context: ".",
|
||||||
SSHKeyPath: "id_rsa=/root/.ssh/id_rsa",
|
SSHKeyPath: "id_rsa=/root/.ssh/id_rsa",
|
||||||
@@ -150,7 +156,7 @@ func TestCommandBuild(t *testing.T) {
|
|||||||
"-f",
|
"-f",
|
||||||
"Dockerfile",
|
"Dockerfile",
|
||||||
"-t",
|
"-t",
|
||||||
"plugins/drone-docker:latest",
|
"abcdefgh",
|
||||||
".",
|
".",
|
||||||
"--ssh id_rsa=/root/.ssh/id_rsa",
|
"--ssh id_rsa=/root/.ssh/id_rsa",
|
||||||
),
|
),
|
||||||
@@ -161,7 +167,7 @@ func TestCommandBuild(t *testing.T) {
|
|||||||
tc := tc
|
tc := tc
|
||||||
|
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
cmd := commandBuild(tc.build, "plugins/drone-docker:latest")
|
cmd := commandBuild(tc.build)
|
||||||
|
|
||||||
if !reflect.DeepEqual(cmd.String(), tc.want.String()) {
|
if !reflect.DeepEqual(cmd.String(), tc.want.String()) {
|
||||||
t.Errorf("Got cmd %v, want %v", cmd, tc.want)
|
t.Errorf("Got cmd %v, want %v", cmd, tc.want)
|
||||||
|
|||||||
Reference in New Issue
Block a user