Use uniuri to generate temporary tag

This commit is contained in:
Rutvij Mehta
2023-05-16 10:14:18 -07:00
parent 47e09cf885
commit a38298c4f2
4 changed files with 23 additions and 29 deletions
+5 -17
View File
@@ -1,22 +1,20 @@
package main package main
import ( import (
"math/rand"
"os" "os"
"runtime" "runtime"
"time" "strings"
"github.com/dchest/uniuri"
docker "github.com/drone-plugins/drone-docker"
"github.com/drone-plugins/drone-plugin-lib/drone"
"github.com/joho/godotenv" "github.com/joho/godotenv"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/urfave/cli" "github.com/urfave/cli"
docker "github.com/drone-plugins/drone-docker"
"github.com/drone-plugins/drone-plugin-lib/drone"
) )
var ( var (
version = "unknown" version = "unknown"
charset = []byte("abcdefghijklmnopqrstuvwxyz")
) )
func main() { func main() {
@@ -388,17 +386,7 @@ func run(c *cli.Context) error {
} }
func generateTempTag() string { func generateTempTag() string {
tagLength := 8 return strings.ToLower(uniuri.New())
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 {
+15 -12
View File
@@ -1,12 +1,15 @@
package docker package docker
import ( import (
"github.com/dchest/uniuri"
"os/exec" "os/exec"
"reflect" "reflect"
"strings"
"testing" "testing"
) )
func TestCommandBuild(t *testing.T) { func TestCommandBuild(t *testing.T) {
tempTag := strings.ToLower(uniuri.New())
tcs := []struct { tcs := []struct {
name string name string
build Build build Build
@@ -16,7 +19,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", TempTag: tempTag,
Dockerfile: "Dockerfile", Dockerfile: "Dockerfile",
Context: ".", Context: ".",
SecretEnvs: []string{ SecretEnvs: []string{
@@ -30,7 +33,7 @@ func TestCommandBuild(t *testing.T) {
"-f", "-f",
"Dockerfile", "Dockerfile",
"-t", "-t",
"abcdefgh", tempTag,
".", ".",
"--secret id=foo_secret,env=FOO_SECRET_ENV_VAR", "--secret id=foo_secret,env=FOO_SECRET_ENV_VAR",
), ),
@@ -39,7 +42,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", TempTag: tempTag,
Dockerfile: "Dockerfile", Dockerfile: "Dockerfile",
Context: ".", Context: ".",
SecretFiles: []string{ SecretFiles: []string{
@@ -53,7 +56,7 @@ func TestCommandBuild(t *testing.T) {
"-f", "-f",
"Dockerfile", "Dockerfile",
"-t", "-t",
"abcdefgh", tempTag,
".", ".",
"--secret id=foo_secret,src=/path/to/foo_secret", "--secret id=foo_secret,src=/path/to/foo_secret",
), ),
@@ -62,7 +65,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", TempTag: tempTag,
Dockerfile: "Dockerfile", Dockerfile: "Dockerfile",
Context: ".", Context: ".",
SecretEnvs: []string{ SecretEnvs: []string{
@@ -81,7 +84,7 @@ func TestCommandBuild(t *testing.T) {
"-f", "-f",
"Dockerfile", "Dockerfile",
"-t", "-t",
"abcdefgh", tempTag,
".", ".",
"--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",
@@ -93,7 +96,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", TempTag: tempTag,
Dockerfile: "Dockerfile", Dockerfile: "Dockerfile",
Context: ".", Context: ".",
SecretEnvs: []string{ SecretEnvs: []string{
@@ -114,7 +117,7 @@ func TestCommandBuild(t *testing.T) {
"-f", "-f",
"Dockerfile", "Dockerfile",
"-t", "-t",
"abcdefgh", tempTag,
".", ".",
), ),
}, },
@@ -122,7 +125,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", TempTag: tempTag,
Dockerfile: "Dockerfile", Dockerfile: "Dockerfile",
Context: ".", Context: ".",
Platform: "test/platform", Platform: "test/platform",
@@ -134,7 +137,7 @@ func TestCommandBuild(t *testing.T) {
"-f", "-f",
"Dockerfile", "Dockerfile",
"-t", "-t",
"abcdefgh", tempTag,
".", ".",
"--platform", "--platform",
"test/platform", "test/platform",
@@ -144,7 +147,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", TempTag: tempTag,
Dockerfile: "Dockerfile", Dockerfile: "Dockerfile",
Context: ".", Context: ".",
SSHKeyPath: "id_rsa=/root/.ssh/id_rsa", SSHKeyPath: "id_rsa=/root/.ssh/id_rsa",
@@ -156,7 +159,7 @@ func TestCommandBuild(t *testing.T) {
"-f", "-f",
"Dockerfile", "Dockerfile",
"-t", "-t",
"abcdefgh", tempTag,
".", ".",
"--ssh id_rsa=/root/.ssh/id_rsa", "--ssh id_rsa=/root/.ssh/id_rsa",
), ),
+1
View File
@@ -13,6 +13,7 @@ require (
require ( require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/dchest/uniuri v1.2.0 // indirect
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af // indirect github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect
golang.org/x/sys v0.0.0-20220731174439-a90be440212d // indirect golang.org/x/sys v0.0.0-20220731174439-a90be440212d // indirect
+2
View File
@@ -11,6 +11,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dchest/uniuri v1.2.0 h1:koIcOUdrTIivZgSLhHQvKgqdWZq5d7KdMEWF1Ud6+5g=
github.com/dchest/uniuri v1.2.0/go.mod h1:fSzm4SLHzNZvWLvWJew423PhAzkpNQYq+uNLq4kxhkY=
github.com/drone-plugins/drone-plugin-lib v0.4.1 h1:47rZlmcMpr1hSp+6Gl+1Z4t+efi/gMQU3lxukC1Yg64= github.com/drone-plugins/drone-plugin-lib v0.4.1 h1:47rZlmcMpr1hSp+6Gl+1Z4t+efi/gMQU3lxukC1Yg64=
github.com/drone-plugins/drone-plugin-lib v0.4.1/go.mod h1:KwCu92jFjHV3xv2hu5Qg/8zBNvGwbhoJDQw/EwnTvoM= github.com/drone-plugins/drone-plugin-lib v0.4.1/go.mod h1:KwCu92jFjHV3xv2hu5Qg/8zBNvGwbhoJDQw/EwnTvoM=
github.com/drone/drone-go v1.7.1 h1:ZX+3Rs8YHUSUQ5mkuMLmm1zr1ttiiE2YGNxF3AnyDKw= github.com/drone/drone-go v1.7.1 h1:ZX+3Rs8YHUSUQ5mkuMLmm1zr1ttiiE2YGNxF3AnyDKw=