mirror of
https://github.com/drone-plugins/drone-docker.git
synced 2026-06-04 18:24:24 +08:00
Use random string as temporary tag
This commit is contained in:
@@ -17,8 +17,8 @@ import (
|
||||
"github.com/inhies/go-bytesize"
|
||||
)
|
||||
|
||||
func (p Plugin) writeCard(buildName string) error {
|
||||
cmd := exec.Command(dockerExe, "inspect", buildName)
|
||||
func (p Plugin) writeCard() error {
|
||||
cmd := exec.Command(dockerExe, "inspect", p.Build.TempTag)
|
||||
data, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"os"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/sirupsen/logrus"
|
||||
@@ -14,6 +16,7 @@ import (
|
||||
|
||||
var (
|
||||
version = "unknown"
|
||||
charset = []byte("abcdefghijklmnopqrstuvwxyz")
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -318,6 +321,7 @@ func run(c *cli.Context) error {
|
||||
Build: docker.Build{
|
||||
Remote: c.String("remote.url"),
|
||||
Name: c.String("commit.sha"),
|
||||
TempTag: generateTempTag(),
|
||||
Dockerfile: c.String("dockerfile"),
|
||||
Context: c.String("context"),
|
||||
Tags: c.StringSlice("tags"),
|
||||
@@ -383,6 +387,20 @@ func run(c *cli.Context) error {
|
||||
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 {
|
||||
if runtime.GOOS == "windows" {
|
||||
return "C:/bin/drone-docker.exe"
|
||||
|
||||
@@ -45,6 +45,7 @@ type (
|
||||
Build struct {
|
||||
Remote string // Git remote URL
|
||||
Name string // Docker build using default named tag
|
||||
TempTag string // Temporary tag used during docker build
|
||||
Dockerfile string // Docker build Dockerfile
|
||||
Context string // Docker build context
|
||||
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, buildName)) // docker build
|
||||
cmds = append(cmds, commandBuild(p.Build)) // docker build
|
||||
|
||||
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 {
|
||||
cmds = append(cmds, commandPush(p.Build, tag)) // docker push
|
||||
@@ -225,12 +225,12 @@ func (p Plugin) Exec() error {
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
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 {
|
||||
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
|
||||
cmds = nil
|
||||
|
||||
cmds = append(cmds, commandRmi(buildName)) // docker rmi
|
||||
cmds = append(cmds, commandPrune()) // docker system prune -f
|
||||
cmds = append(cmds, commandRmi(p.Build.TempTag)) // docker rmi
|
||||
cmds = append(cmds, commandPrune()) // docker system prune -f
|
||||
|
||||
for _, cmd := range cmds {
|
||||
cmd.Stdout = os.Stdout
|
||||
@@ -300,12 +300,12 @@ func commandInfo() *exec.Cmd {
|
||||
}
|
||||
|
||||
// helper function to create the docker build command.
|
||||
func commandBuild(build Build, buildName string) *exec.Cmd {
|
||||
func commandBuild(build Build) *exec.Cmd {
|
||||
args := []string{
|
||||
"build",
|
||||
"--rm=true",
|
||||
"-f", build.Dockerfile,
|
||||
"-t", buildName,
|
||||
"-t", build.TempTag,
|
||||
}
|
||||
|
||||
args = append(args, build.Context)
|
||||
@@ -462,9 +462,9 @@ func hasProxyBuildArg(build *Build, key string) bool {
|
||||
}
|
||||
|
||||
// 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 (
|
||||
source = buildName
|
||||
source = build.TempTag
|
||||
target = fmt.Sprintf("%s:%s", build.Repo, tag)
|
||||
)
|
||||
return exec.Command(
|
||||
|
||||
+13
-7
@@ -16,6 +16,7 @@ func TestCommandBuild(t *testing.T) {
|
||||
name: "secret from env var",
|
||||
build: Build{
|
||||
Name: "plugins/drone-docker:latest",
|
||||
TempTag: "abcdefgh",
|
||||
Dockerfile: "Dockerfile",
|
||||
Context: ".",
|
||||
SecretEnvs: []string{
|
||||
@@ -29,7 +30,7 @@ func TestCommandBuild(t *testing.T) {
|
||||
"-f",
|
||||
"Dockerfile",
|
||||
"-t",
|
||||
"plugins/drone-docker:latest",
|
||||
"abcdefgh",
|
||||
".",
|
||||
"--secret id=foo_secret,env=FOO_SECRET_ENV_VAR",
|
||||
),
|
||||
@@ -38,6 +39,7 @@ func TestCommandBuild(t *testing.T) {
|
||||
name: "secret from file",
|
||||
build: Build{
|
||||
Name: "plugins/drone-docker:latest",
|
||||
TempTag: "abcdefgh",
|
||||
Dockerfile: "Dockerfile",
|
||||
Context: ".",
|
||||
SecretFiles: []string{
|
||||
@@ -51,7 +53,7 @@ func TestCommandBuild(t *testing.T) {
|
||||
"-f",
|
||||
"Dockerfile",
|
||||
"-t",
|
||||
"plugins/drone-docker:latest",
|
||||
"abcdefgh",
|
||||
".",
|
||||
"--secret id=foo_secret,src=/path/to/foo_secret",
|
||||
),
|
||||
@@ -60,6 +62,7 @@ func TestCommandBuild(t *testing.T) {
|
||||
name: "multiple mixed secrets",
|
||||
build: Build{
|
||||
Name: "plugins/drone-docker:latest",
|
||||
TempTag: "abcdefgh",
|
||||
Dockerfile: "Dockerfile",
|
||||
Context: ".",
|
||||
SecretEnvs: []string{
|
||||
@@ -78,7 +81,7 @@ func TestCommandBuild(t *testing.T) {
|
||||
"-f",
|
||||
"Dockerfile",
|
||||
"-t",
|
||||
"plugins/drone-docker:latest",
|
||||
"abcdefgh",
|
||||
".",
|
||||
"--secret id=foo_secret,env=FOO_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",
|
||||
build: Build{
|
||||
Name: "plugins/drone-docker:latest",
|
||||
TempTag: "abcdefgh",
|
||||
Dockerfile: "Dockerfile",
|
||||
Context: ".",
|
||||
SecretEnvs: []string{
|
||||
@@ -110,7 +114,7 @@ func TestCommandBuild(t *testing.T) {
|
||||
"-f",
|
||||
"Dockerfile",
|
||||
"-t",
|
||||
"plugins/drone-docker:latest",
|
||||
"abcdefgh",
|
||||
".",
|
||||
),
|
||||
},
|
||||
@@ -118,6 +122,7 @@ func TestCommandBuild(t *testing.T) {
|
||||
name: "platform argument",
|
||||
build: Build{
|
||||
Name: "plugins/drone-docker:latest",
|
||||
TempTag: "abcdefgh",
|
||||
Dockerfile: "Dockerfile",
|
||||
Context: ".",
|
||||
Platform: "test/platform",
|
||||
@@ -129,7 +134,7 @@ func TestCommandBuild(t *testing.T) {
|
||||
"-f",
|
||||
"Dockerfile",
|
||||
"-t",
|
||||
"plugins/drone-docker:latest",
|
||||
"abcdefgh",
|
||||
".",
|
||||
"--platform",
|
||||
"test/platform",
|
||||
@@ -139,6 +144,7 @@ func TestCommandBuild(t *testing.T) {
|
||||
name: "ssh agent",
|
||||
build: Build{
|
||||
Name: "plugins/drone-docker:latest",
|
||||
TempTag: "abcdefgh",
|
||||
Dockerfile: "Dockerfile",
|
||||
Context: ".",
|
||||
SSHKeyPath: "id_rsa=/root/.ssh/id_rsa",
|
||||
@@ -150,7 +156,7 @@ func TestCommandBuild(t *testing.T) {
|
||||
"-f",
|
||||
"Dockerfile",
|
||||
"-t",
|
||||
"plugins/drone-docker:latest",
|
||||
"abcdefgh",
|
||||
".",
|
||||
"--ssh id_rsa=/root/.ssh/id_rsa",
|
||||
),
|
||||
@@ -161,7 +167,7 @@ func TestCommandBuild(t *testing.T) {
|
||||
tc := tc
|
||||
|
||||
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()) {
|
||||
t.Errorf("Got cmd %v, want %v", cmd, tc.want)
|
||||
|
||||
Reference in New Issue
Block a user