From 316b48b5b5c6c78bc83d34eeeec3135711495ff8 Mon Sep 17 00:00:00 2001 From: Nick Adcock Date: Thu, 20 Feb 2020 15:50:41 +0000 Subject: [PATCH 01/31] Adding github and login args and command Adding functionality to read the common github actions variables and the docker login variables. Also adding the ability to run a `docker login` from the command Signed-off-by: Nick Adcock --- cmd/login.go | 16 +++++++++++++ cmd/main.go | 22 ++++++++++++++++-- go.mod | 7 +++++- go.sum | 26 +++++++++++++++++++++ internal/command/args.go | 11 +++++++++ internal/command/args_test.go | 24 +++++++++++++++++++ internal/command/runner.go | 27 ++++++++++++++++++++++ internal/options/github.go | 25 ++++++++++++++++++++ internal/options/github_test.go | 41 +++++++++++++++++++++++++++++++++ internal/options/login.go | 17 ++++++++++++++ internal/options/login_test.go | 20 ++++++++++++++++ 11 files changed, 233 insertions(+), 3 deletions(-) create mode 100644 cmd/login.go create mode 100644 internal/command/args.go create mode 100644 internal/command/args_test.go create mode 100644 internal/command/runner.go create mode 100644 internal/options/github.go create mode 100644 internal/options/github_test.go create mode 100644 internal/options/login.go create mode 100644 internal/options/login_test.go diff --git a/cmd/login.go b/cmd/login.go new file mode 100644 index 0000000..1d06b2e --- /dev/null +++ b/cmd/login.go @@ -0,0 +1,16 @@ +package main + +import ( + "github.com/docker/github-actions/internal/command" + "github.com/docker/github-actions/internal/options" +) + +func login(cmd command.Runner) error { + o, err := options.GetLoginOptions() + if err != nil { + return err + } + + args := command.LoginArgs(o) + return cmd.Run("docker", args...) +} diff --git a/cmd/main.go b/cmd/main.go index b325279..c14a99a 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -4,17 +4,35 @@ import ( "fmt" "os" + "github.com/docker/github-actions/internal/command" + "github.com/docker/github-actions/internal/options" commandLine "github.com/urfave/cli/v2" ) func main() { + _, err := options.GetGitHubOptions() + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + cmd := command.NewRunner() + app := &commandLine.App{ Name: "docker github actions", Usage: "Used in GitHub Actions to run Docker workflows", + Commands: []*commandLine.Command{ + { + Name: "login", + Description: "Logs into a docker server", + Action: func(c *commandLine.Context) error { + return login(cmd) + }, + }, + }, } - err := app.Run(os.Args) - if err != nil { + if err = app.Run(os.Args); err != nil { fmt.Println(err) os.Exit(1) } diff --git a/go.mod b/go.mod index 14fe100..cccc0fa 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,9 @@ module github.com/docker/github-actions go 1.13 -require github.com/urfave/cli/v2 v2.1.1 +require ( + github.com/caarlos0/env/v6 v6.1.0 + github.com/magiconair/properties v1.8.1 // indirect + github.com/urfave/cli/v2 v2.1.1 + gotest.tools/v3 v3.0.2 +) diff --git a/go.sum b/go.sum index 7731077..dba47e6 100644 --- a/go.sum +++ b/go.sum @@ -1,12 +1,38 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/caarlos0/env v3.5.0+incompatible h1:Yy0UN8o9Wtr/jGHZDpCBLpNrzcFLLM2yixi/rBrKyJs= +github.com/caarlos0/env/v6 v6.1.0 h1:4FbM+HmZA/Q5wdSrH2kj0KQXm7xnhuO8y3TuOTnOvqc= +github.com/caarlos0/env/v6 v6.1.0/go.mod h1:iUA6X3VCAOwDhoqvgKlTGjjwJzQseIJaFYApUqQkt+8= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4= +github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= +github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/urfave/cli/v2 v2.1.1 h1:Qt8FeAtxE/vfdrLmR3rxR6JRE0RoVmbXu8+6kZtYU4k= github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/tools v0.0.0-20190624222133-a101b041ded4/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo= +gotest.tools/v3 v3.0.2 h1:kG1BFyqVHuQoVQiR1bWGnfz/fmHvvuiSPIV7rvl360E= +gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk= diff --git a/internal/command/args.go b/internal/command/args.go new file mode 100644 index 0000000..70a4e13 --- /dev/null +++ b/internal/command/args.go @@ -0,0 +1,11 @@ +package command + +import "github.com/docker/github-actions/internal/options" + +func LoginArgs(o options.Login) []string { + args := []string{"login", "--username", o.Username, "--password", o.Password} + if o.Server != "" { + args = append(args, o.Server) + } + return args +} diff --git a/internal/command/args_test.go b/internal/command/args_test.go new file mode 100644 index 0000000..dc978af --- /dev/null +++ b/internal/command/args_test.go @@ -0,0 +1,24 @@ +package command + +import ( + "testing" + + "github.com/docker/github-actions/internal/options" + "gotest.tools/v3/assert" +) + +func TestLoginArgs(t *testing.T) { + expected := []string{"login", "--username", "username", "--password", "password"} + o := options.Login{ + Username: "username", + Password: "password", + } + args := LoginArgs(o) + + assert.DeepEqual(t, expected, args) + + o.Server = "server" + args = LoginArgs(o) + expected = append(expected, "server") + assert.DeepEqual(t, expected, args) +} diff --git a/internal/command/runner.go b/internal/command/runner.go new file mode 100644 index 0000000..fe64947 --- /dev/null +++ b/internal/command/runner.go @@ -0,0 +1,27 @@ +package command + +import ( + "os" + "os/exec" +) + +// Runner executes standard commands +type Runner interface { + // Run executes the given command with arguments + Run(name string, args ...string) error +} + +type execRunner struct{} + +// NewRunner returns a new Runner with the stdout and stderr defaulting to os +func NewRunner() Runner { + return execRunner{} +} + +// Run executes the given command with arguments using exec.Command +func (runner execRunner) Run(name string, args ...string) error { + cmd := exec.Command(name, args...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + return cmd.Run() +} diff --git a/internal/options/github.go b/internal/options/github.go new file mode 100644 index 0000000..1182456 --- /dev/null +++ b/internal/options/github.go @@ -0,0 +1,25 @@ +package options + +import "github.com/caarlos0/env/v6" + +// GitHub contains the parsed common github actions environment variables +// See https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables +type GitHub struct { + RunInActions bool `env:"GITHUB_ACTIONS"` + Workflow string `env:"GITHUB_WORKFLOW"` + RunID string `env:"GITHUB_RUN_ID"` + RunNumber string `env:"GITHUB_RUN_NUMBER"` + Action string `env:"GITHUB_ACTION"` + Actor string `env:"GITHUB_ACTOR"` + Repository string `env:"GITHUB_REPOSITORY"` + EventName string `env:"GITHUB_EVENT_NAME"` + Sha string `env:"GITHUB_SHA"` + Ref string `env:"GITHUB_REF"` +} + +// GetGitHubOptions gets the common github actions environment variables +func GetGitHubOptions() (GitHub, error) { + var github GitHub + err := env.Parse(&github) + return github, err +} diff --git a/internal/options/github_test.go b/internal/options/github_test.go new file mode 100644 index 0000000..b3cbfe6 --- /dev/null +++ b/internal/options/github_test.go @@ -0,0 +1,41 @@ +package options + +import ( + "os" + "testing" + + "gotest.tools/v3/assert" +) + +func TestGetGitHubOptions(t *testing.T) { + _ = os.Setenv("GITHUB_ACTIONS", "true") + _ = os.Setenv("GITHUB_WORKFLOW", "workflow") + _ = os.Setenv("GITHUB_RUN_ID", "run-id") + _ = os.Setenv("GITHUB_RUN_NUMBER", "run-number") + _ = os.Setenv("GITHUB_ACTION", "action") + _ = os.Setenv("GITHUB_ACTOR", "actor") + _ = os.Setenv("GITHUB_REPOSITORY", "repository") + _ = os.Setenv("GITHUB_EVENT_NAME", "event-name") + _ = os.Setenv("GITHUB_SHA", "sha") + _ = os.Setenv("GITHUB_REF", "ref") + + github, err := GetGitHubOptions() + assert.NilError(t, err) + assert.Equal(t, true, github.RunInActions) + assert.Equal(t, "workflow", github.Workflow) + assert.Equal(t, "run-id", github.RunID) + assert.Equal(t, "run-number", github.RunNumber) + assert.Equal(t, "action", github.Action) + assert.Equal(t, "actor", github.Actor) + assert.Equal(t, "repository", github.Repository) + assert.Equal(t, "event-name", github.EventName) + assert.Equal(t, "sha", github.Sha) + assert.Equal(t, "ref", github.Ref) +} + +func TestGetGitHubOptionsNotInActions(t *testing.T) { + _ = os.Unsetenv("GITHUB_ACTIONS") + github, err := GetGitHubOptions() + assert.NilError(t, err) + assert.Equal(t, false, github.RunInActions) +} diff --git a/internal/options/login.go b/internal/options/login.go new file mode 100644 index 0000000..bd13d68 --- /dev/null +++ b/internal/options/login.go @@ -0,0 +1,17 @@ +package options + +import "github.com/caarlos0/env/v6" + +// Login contains the parsed login action environment variables +type Login struct { + Username string `env:"INPUT_USERNAME"` + Password string `env:"INPUT_PASSWORD"` + Server string `env:"INPUT_SERVER"` +} + +// GetLoginOptions gets the login action environment variables +func GetLoginOptions() (Login, error) { + var login Login + err := env.Parse(&login) + return login, err +} diff --git a/internal/options/login_test.go b/internal/options/login_test.go new file mode 100644 index 0000000..ae36955 --- /dev/null +++ b/internal/options/login_test.go @@ -0,0 +1,20 @@ +package options + +import ( + "os" + "testing" + + "gotest.tools/v3/assert" +) + +func TestGetLoginOptions(t *testing.T) { + _ = os.Setenv("INPUT_USERNAME", "username") + _ = os.Setenv("INPUT_PASSWORD", "password") + _ = os.Setenv("INPUT_SERVER", "server") + + o, err := GetLoginOptions() + assert.NilError(t, err) + assert.Equal(t, "username", o.Username) + assert.Equal(t, "password", o.Password) + assert.Equal(t, "server", o.Server) +} From d062807e45db1c70b70876e3219aeb5f19773542 Mon Sep 17 00:00:00 2001 From: Nick Adcock Date: Fri, 21 Feb 2020 11:28:56 +0000 Subject: [PATCH 02/31] Adding build opts and default tags/labels Adding build options along with generation of default tags and labels based on git settings Signed-off-by: Nick Adcock --- .github/workflows/main.yml | 3 + internal/options/build.go | 100 +++++++++++++++++++++ internal/options/build_test.go | 151 ++++++++++++++++++++++++++++++++ internal/options/github.go | 47 +++++++++- internal/options/github_test.go | 68 +++++++++++--- 5 files changed, 355 insertions(+), 14 deletions(-) create mode 100644 internal/options/build.go create mode 100644 internal/options/build_test.go diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d18d21a..c771b7e 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -9,5 +9,8 @@ jobs: - name: Checkout uses: actions/checkout@v2 + - name: Print env + run: env + - name: Build run: make -f docker.Makefile diff --git a/internal/options/build.go b/internal/options/build.go new file mode 100644 index 0000000..5916497 --- /dev/null +++ b/internal/options/build.go @@ -0,0 +1,100 @@ +package options + +import ( + "fmt" + "os" + "strings" + + "github.com/caarlos0/env/v6" +) + +const githubLabelPrefix = "com.docker.github-actions" + +// Build contains the parsed build action environment variables +type Build struct { + Dockerfile string `env:"INPUT_DOCKERFILE"` + Server string `env:"INPUT_SERVER"` + Repository string `env:"INPUT_REPOSITORY"` + SetDefaultTags bool `env:"INPUT_SET_DEFAULT_TAGS"` + SetDefaultLabels bool `env:"INPUT_SET_DEFAULT_LABELS"` + Target string `env:"INPUT_TARGET"` + AlwaysPull bool `env:"INPUT_ALWAYS_PULL"` + BuildArgs []string + Labels []string + Tags []string +} + +// GetBuildOptions gets the login action environment variables +func GetBuildOptions() (Build, error) { + var build Build + if err := env.Parse(&build); err != nil { + return build, err + } + + if buildArgs := os.Getenv("INPUT_BUILD_ARGS"); buildArgs != "" { + build.BuildArgs = strings.Split(buildArgs, ",") + } + + if labels := os.Getenv("INPUT_LABELS"); labels != "" { + build.Labels = strings.Split(labels, ",") + } + + if tags := os.Getenv("INPUT_TAGS"); tags != "" { + build.Tags = strings.Split(tags, ",") + } + + return build, nil +} + +// GetTags gets a list of all tags to build the image with including automatic tags created from github vars when SetDefaultTags is true +func GetTags(build Build, github GitHub) []string { + tags := []string{} + if build.Tags != nil { + tags = build.Tags + } + + if !build.SetDefaultTags { + return tags + } + + switch github.Reference.Type { + case GitRefHead: + if github.Reference.Name == "master" { + tags = append(tags, "latest") + } else { + tags = append(tags, github.Reference.Name) + } + case GitRefPullRequest: + tags = append(tags, fmt.Sprintf("pr-%s", github.Reference.Name)) + case GitRefTag: + tags = append(tags, github.Reference.Name) + } + + return tags +} + +// GetLabels gets a list of all labels to build the image with including automatic labels created from github vars when SetDefaultLabels is true +func GetLabels(build Build, github GitHub) []string { + labels := []string{} + if build.Labels != nil { + labels = build.Labels + } + + if !build.SetDefaultLabels { + return labels + } + + if github.Actor != "" { + labels = append(labels, fmt.Sprintf("%s-actor=%s", githubLabelPrefix, github.Actor)) + } + + if github.Sha != "" { + labels = append(labels, fmt.Sprintf("%s-sha=%s", githubLabelPrefix, github.Sha)) + } + + if github.RunID != "" && github.RunNumber != "" { + labels = append(labels, fmt.Sprintf("%s-run=%s-%s", githubLabelPrefix, github.RunID, github.RunNumber)) + } + + return labels +} diff --git a/internal/options/build_test.go b/internal/options/build_test.go new file mode 100644 index 0000000..b15bc05 --- /dev/null +++ b/internal/options/build_test.go @@ -0,0 +1,151 @@ +package options + +import ( + "os" + "testing" + + "gotest.tools/v3/assert" +) + +func TestGetBuildOptions(t *testing.T) { + _ = os.Setenv("INPUT_DOCKERFILE", "dockerfile") + _ = os.Setenv("INPUT_SERVER", "server") + _ = os.Setenv("INPUT_REPOSITORY", "repository") + _ = os.Setenv("INPUT_BUILD_ARGS", "buildarg1=b1,buildarg2=b2") + _ = os.Setenv("INPUT_LABELS", "label1=l1,label2=l2") + _ = os.Setenv("INPUT_SET_DEFAULT_TAGS", "false") + _ = os.Setenv("INPUT_SET_DEFAULT_LABELS", "false") + _ = os.Setenv("INPUT_TARGET", "target") + _ = os.Setenv("INPUT_ALWAYS_PULL", "true") + _ = os.Setenv("INPUT_TAGS", "tag1,tag2") + + o, err := GetBuildOptions() + + assert.NilError(t, err) + assert.DeepEqual(t, Build{ + Dockerfile: "dockerfile", + Server: "server", + Repository: "repository", + SetDefaultTags: false, + SetDefaultLabels: false, + Target: "target", + AlwaysPull: true, + BuildArgs: []string{"buildarg1=b1", "buildarg2=b2"}, + Labels: []string{"label1=l1", "label2=l2"}, + Tags: []string{"tag1", "tag2"}, + }, o) +} + +func TestGetTags(t *testing.T) { + testCases := []struct { + name string + setDefault bool + tags []string + ref GitReference + expected []string + }{ + { + name: "no-defaults", + tags: []string{"tag1", "tag2"}, + expected: []string{"tag1", "tag2"}, + ref: GitReference{GitRefHead, "master"}, + }, + { + name: "unknown-ref-type", + tags: []string{"tag1", "tag2"}, + expected: []string{"tag1", "tag2"}, + setDefault: true, + ref: GitReference{GitRefUnknown, "master"}, + }, + { + name: "master-branch", + setDefault: true, + tags: []string{"tag1", "tag2"}, + expected: []string{"tag1", "tag2", "latest"}, + ref: GitReference{GitRefHead, "master"}, + }, + { + name: "different-branch", + setDefault: true, + tags: []string{"tag1", "tag2"}, + expected: []string{"tag1", "tag2", "branch-name"}, + ref: GitReference{GitRefHead, "branch-name"}, + }, + { + name: "pull-request", + setDefault: true, + tags: []string{"tag1", "tag2"}, + expected: []string{"tag1", "tag2", "pr-name"}, + ref: GitReference{GitRefPullRequest, "name"}, + }, + { + name: "tag", + setDefault: true, + tags: []string{"tag1", "tag2"}, + expected: []string{"tag1", "tag2", "v1.0"}, + ref: GitReference{GitRefTag, "v1.0"}, + }, + } + + for _, tc := range testCases { + tc := tc + t.Run(tc.name, func(t *testing.T) { + tags := GetTags( + Build{ + SetDefaultTags: tc.setDefault, + Tags: tc.tags, + }, + GitHub{Reference: tc.ref}, + ) + assert.DeepEqual(t, tc.expected, tags) + }) + } +} + +func TestGetLabels(t *testing.T) { + testCases := []struct { + name string + setDefault bool + labels []string + github GitHub + expected []string + }{ + { + name: "no-defaults", + labels: []string{"label1", "label2"}, + expected: []string{"label1", "label2"}, + }, + { + name: "with-defaults", + labels: []string{"label1", "label2"}, + setDefault: true, + github: GitHub{ + Actor: "actor", + Sha: "sha", + RunID: "1", + RunNumber: "2", + }, + expected: []string{ + "label1", + "label2", + "com.docker.github-actions-actor=actor", + "com.docker.github-actions-sha=sha", + "com.docker.github-actions-run=1-2", + }, + }, + } + + for _, tc := range testCases { + tc := tc + t.Run(tc.name, func(t *testing.T) { + labels := GetLabels( + Build{ + SetDefaultLabels: tc.setDefault, + Labels: tc.labels, + }, + tc.github, + ) + assert.DeepEqual(t, tc.expected, labels) + }) + } +} diff --git a/internal/options/github.go b/internal/options/github.go index 1182456..6d7b677 100644 --- a/internal/options/github.go +++ b/internal/options/github.go @@ -1,6 +1,43 @@ package options -import "github.com/caarlos0/env/v6" +import ( + "os" + "strings" + + "github.com/caarlos0/env/v6" +) + +// GitReferenceType is the type of the git ref +type GitReferenceType int + +// Get reference types +const ( + GitRefUnknown GitReferenceType = iota + GitRefHead + GitRefPullRequest + GitRefTag +) + +// GitReference contains the type and name of the git ref +type GitReference struct { + Type GitReferenceType + Name string +} + +func parseGitRef(ref string) GitReference { + if split := strings.SplitN(ref, "/", 3); len(split) == 3 { + name := split[2] + switch split[1] { + case "heads": + return GitReference{GitRefHead, name} + case "pulls": + return GitReference{GitRefPullRequest, name} + case "tags": + return GitReference{GitRefTag, name} + } + } + return GitReference{GitRefUnknown, ""} +} // GitHub contains the parsed common github actions environment variables // See https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables @@ -14,12 +51,16 @@ type GitHub struct { Repository string `env:"GITHUB_REPOSITORY"` EventName string `env:"GITHUB_EVENT_NAME"` Sha string `env:"GITHUB_SHA"` - Ref string `env:"GITHUB_REF"` + Reference GitReference } // GetGitHubOptions gets the common github actions environment variables func GetGitHubOptions() (GitHub, error) { var github GitHub err := env.Parse(&github) - return github, err + if err != nil { + return github, err + } + github.Reference = parseGitRef(os.Getenv("GITHUB_REF")) + return github, nil } diff --git a/internal/options/github_test.go b/internal/options/github_test.go index b3cbfe6..12d2af8 100644 --- a/internal/options/github_test.go +++ b/internal/options/github_test.go @@ -17,20 +17,66 @@ func TestGetGitHubOptions(t *testing.T) { _ = os.Setenv("GITHUB_REPOSITORY", "repository") _ = os.Setenv("GITHUB_EVENT_NAME", "event-name") _ = os.Setenv("GITHUB_SHA", "sha") - _ = os.Setenv("GITHUB_REF", "ref") + _ = os.Setenv("GITHUB_REF", "refs/heads/master") github, err := GetGitHubOptions() assert.NilError(t, err) - assert.Equal(t, true, github.RunInActions) - assert.Equal(t, "workflow", github.Workflow) - assert.Equal(t, "run-id", github.RunID) - assert.Equal(t, "run-number", github.RunNumber) - assert.Equal(t, "action", github.Action) - assert.Equal(t, "actor", github.Actor) - assert.Equal(t, "repository", github.Repository) - assert.Equal(t, "event-name", github.EventName) - assert.Equal(t, "sha", github.Sha) - assert.Equal(t, "ref", github.Ref) + assert.DeepEqual(t, GitHub{ + RunInActions: true, + Workflow: "workflow", + RunID: "run-id", + RunNumber: "run-number", + Action: "action", + Actor: "actor", + Repository: "repository", + EventName: "event-name", + Sha: "sha", + Reference: GitReference{ + Type: GitRefHead, + Name: "master", + }, + }, github) +} + +func TestParseGitRef(t *testing.T) { + testCases := []struct { + name string + ref string + expectedType GitReferenceType + expectedName string + }{ + { + name: "master-branch", + ref: "refs/heads/master", + expectedType: GitRefHead, + expectedName: "master", + }, + { + name: "different-branch", + ref: "refs/heads/different", + expectedType: GitRefHead, + expectedName: "different", + }, + { + name: "pull-request", + ref: "refs/pulls/pr1", + expectedType: GitRefPullRequest, + expectedName: "pr1", + }, + { + name: "tag", + ref: "refs/tags/tag1", + expectedType: GitRefTag, + expectedName: "tag1", + }, + } + for _, tc := range testCases { + tc := tc + t.Run(tc.name, func(t *testing.T) { + ref := parseGitRef(tc.ref) + assert.DeepEqual(t, GitReference{Type: tc.expectedType, Name: tc.expectedName}, ref) + }) + } } func TestGetGitHubOptionsNotInActions(t *testing.T) { From 2f380ada43efa46f599b3ba6a24424ba0681193b Mon Sep 17 00:00:00 2001 From: Nick Adcock Date: Fri, 21 Feb 2020 11:38:14 +0000 Subject: [PATCH 03/31] Removing run ID and number Removing the GITHUB_RUN_ID and GITHUB_RUN_NUMBER as they do not allow linking to the actual actions run easily Signed-off-by: Nick Adcock --- .github/workflows/main.yml | 3 +++ internal/options/build.go | 4 ---- internal/options/build_test.go | 7 ++----- internal/options/github.go | 2 -- internal/options/github_test.go | 4 ---- 5 files changed, 5 insertions(+), 15 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index c771b7e..bc23799 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -12,5 +12,8 @@ jobs: - name: Print env run: env + - name: Read event json + run: cat $GITHUB_EVENT_PATH + - name: Build run: make -f docker.Makefile diff --git a/internal/options/build.go b/internal/options/build.go index 5916497..08fd254 100644 --- a/internal/options/build.go +++ b/internal/options/build.go @@ -92,9 +92,5 @@ func GetLabels(build Build, github GitHub) []string { labels = append(labels, fmt.Sprintf("%s-sha=%s", githubLabelPrefix, github.Sha)) } - if github.RunID != "" && github.RunNumber != "" { - labels = append(labels, fmt.Sprintf("%s-run=%s-%s", githubLabelPrefix, github.RunID, github.RunNumber)) - } - return labels } diff --git a/internal/options/build_test.go b/internal/options/build_test.go index b15bc05..90a8d42 100644 --- a/internal/options/build_test.go +++ b/internal/options/build_test.go @@ -120,17 +120,14 @@ func TestGetLabels(t *testing.T) { labels: []string{"label1", "label2"}, setDefault: true, github: GitHub{ - Actor: "actor", - Sha: "sha", - RunID: "1", - RunNumber: "2", + Actor: "actor", + Sha: "sha", }, expected: []string{ "label1", "label2", "com.docker.github-actions-actor=actor", "com.docker.github-actions-sha=sha", - "com.docker.github-actions-run=1-2", }, }, } diff --git a/internal/options/github.go b/internal/options/github.go index 6d7b677..a984d33 100644 --- a/internal/options/github.go +++ b/internal/options/github.go @@ -44,8 +44,6 @@ func parseGitRef(ref string) GitReference { type GitHub struct { RunInActions bool `env:"GITHUB_ACTIONS"` Workflow string `env:"GITHUB_WORKFLOW"` - RunID string `env:"GITHUB_RUN_ID"` - RunNumber string `env:"GITHUB_RUN_NUMBER"` Action string `env:"GITHUB_ACTION"` Actor string `env:"GITHUB_ACTOR"` Repository string `env:"GITHUB_REPOSITORY"` diff --git a/internal/options/github_test.go b/internal/options/github_test.go index 12d2af8..9f10acc 100644 --- a/internal/options/github_test.go +++ b/internal/options/github_test.go @@ -10,8 +10,6 @@ import ( func TestGetGitHubOptions(t *testing.T) { _ = os.Setenv("GITHUB_ACTIONS", "true") _ = os.Setenv("GITHUB_WORKFLOW", "workflow") - _ = os.Setenv("GITHUB_RUN_ID", "run-id") - _ = os.Setenv("GITHUB_RUN_NUMBER", "run-number") _ = os.Setenv("GITHUB_ACTION", "action") _ = os.Setenv("GITHUB_ACTOR", "actor") _ = os.Setenv("GITHUB_REPOSITORY", "repository") @@ -24,8 +22,6 @@ func TestGetGitHubOptions(t *testing.T) { assert.DeepEqual(t, GitHub{ RunInActions: true, Workflow: "workflow", - RunID: "run-id", - RunNumber: "run-number", Action: "action", Actor: "actor", Repository: "repository", From 421f37ef1b65604f1ea43f5d4dc74b1701d313a4 Mon Sep 17 00:00:00 2001 From: Nick Adcock Date: Fri, 21 Feb 2020 12:05:19 +0000 Subject: [PATCH 04/31] Adding build command Adding build command to the binary. The command works by calling out to the docker cli binary Signed-off-by: Nick Adcock --- cmd/build.go | 21 ++++++++ cmd/main.go | 7 +++ internal/command/args.go | 43 ++++++++++++++- internal/command/args_test.go | 98 +++++++++++++++++++++++++++++++++++ 4 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 cmd/build.go diff --git a/cmd/build.go b/cmd/build.go new file mode 100644 index 0000000..5d3826b --- /dev/null +++ b/cmd/build.go @@ -0,0 +1,21 @@ +package main + +import ( + "github.com/docker/github-actions/internal/command" + "github.com/docker/github-actions/internal/options" +) + +func build(cmd command.Runner) error { + o, err := options.GetBuildOptions() + if err != nil { + return err + } + + github, err := options.GetGitHubOptions() + if err != nil { + return err + } + + args := command.BuildArgs(o, github) + return cmd.Run("docker", args...) +} diff --git a/cmd/main.go b/cmd/main.go index c14a99a..5053d21 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -29,6 +29,13 @@ func main() { return login(cmd) }, }, + { + Name: "build", + Description: "Builds a docker image", + Action: func(c *commandLine.Context) error { + return build(cmd) + }, + }, }, } diff --git a/internal/command/args.go b/internal/command/args.go index 70a4e13..5de2fa2 100644 --- a/internal/command/args.go +++ b/internal/command/args.go @@ -1,7 +1,12 @@ package command -import "github.com/docker/github-actions/internal/options" +import ( + "fmt" + "github.com/docker/github-actions/internal/options" +) + +// LoginArgs converts login options into the cli arguments used to call `docker login` func LoginArgs(o options.Login) []string { args := []string{"login", "--username", o.Username, "--password", o.Password} if o.Server != "" { @@ -9,3 +14,39 @@ func LoginArgs(o options.Login) []string { } return args } + +// BuildArgs converts build options into the cli arguments used to call `docker build` +func BuildArgs(o options.Build, github options.GitHub) []string { + args := []string{"build"} + + for _, tag := range options.GetTags(o, github) { + t := fmt.Sprintf("%s:%s", o.Repository, tag) + if o.Server != "" { + t = fmt.Sprintf("%s/%s", o.Server, t) + } + args = append(args, "-t", t) + } + + for _, label := range options.GetLabels(o, github) { + args = append(args, "--label", label) + } + + if o.Target != "" { + args = append(args, "--target", o.Target) + } + + if o.AlwaysPull { + args = append(args, "--pull") + } + + for _, buildArg := range o.BuildArgs { + args = append(args, "--build-arg", buildArg) + } + + if o.Dockerfile == "" { + args = append(args, ".") + } else { + args = append(args, o.Dockerfile) + } + return args +} diff --git a/internal/command/args_test.go b/internal/command/args_test.go index dc978af..d68d694 100644 --- a/internal/command/args_test.go +++ b/internal/command/args_test.go @@ -22,3 +22,101 @@ func TestLoginArgs(t *testing.T) { expected = append(expected, "server") assert.DeepEqual(t, expected, args) } + +func TestBuildArgs(t *testing.T) { + testCases := []struct { + name string + build options.Build + github options.GitHub + expected []string + }{ + { + name: "basic", + expected: []string{"build", "."}, + }, + { + name: "with-dockerfile", + build: options.Build{Dockerfile: "dockerfile"}, + expected: []string{"build", "dockerfile"}, + }, + { + name: "with-static-tags", + build: options.Build{ + Repository: "repository", + Tags: []string{"tag1", "tag2"}, + }, + expected: []string{"build", "-t", "repository:tag1", "-t", "repository:tag2", "."}, + }, + { + name: "with-static-tags-and-server", + build: options.Build{ + Server: "server", + Repository: "repository", + Tags: []string{"tag1", "tag2"}, + }, + expected: []string{"build", "-t", "server/repository:tag1", "-t", "server/repository:tag2", "."}, + }, + { + name: "with-default-tags", + build: options.Build{ + SetDefaultTags: true, + Repository: "repository", + Tags: []string{"tag1"}, + }, + github: options.GitHub{ + Reference: options.GitReference{ + Type: options.GitRefHead, + Name: "branch", + }, + }, + expected: []string{"build", "-t", "repository:tag1", "-t", "repository:branch", "."}, + }, + { + name: "with-static-labels", + build: options.Build{ + Labels: []string{"label1", "label2"}, + }, + expected: []string{"build", "--label", "label1", "--label", "label2", "."}, + }, + { + name: "with-default-labels", + build: options.Build{ + SetDefaultLabels: true, + Labels: []string{"label1"}, + }, + github: options.GitHub{ + Actor: "actor", + Sha: "sha", + }, + expected: []string{"build", "--label", "label1", "--label", "com.docker.github-actions-actor=actor", "--label", "com.docker.github-actions-sha=sha", "."}, + }, + { + name: "with-target", + build: options.Build{ + Target: "target", + }, + expected: []string{"build", "--target", "target", "."}, + }, + { + name: "with-always-pull", + build: options.Build{ + AlwaysPull: true, + }, + expected: []string{"build", "--pull", "."}, + }, + { + name: "with-build-args", + build: options.Build{ + BuildArgs: []string{"build-arg-1", "build-arg-2"}, + }, + expected: []string{"build", "--build-arg", "build-arg-1", "--build-arg", "build-arg-2", "."}, + }, + } + for _, tc := range testCases { + tc := tc + t.Run(tc.name, func(t *testing.T) { + args := BuildArgs(tc.build, tc.github) + assert.DeepEqual(t, tc.expected, args) + }) + } +} From 5605e2cb9c877c9c8246c387756be4d5ed763591 Mon Sep 17 00:00:00 2001 From: Nick Adcock Date: Mon, 24 Feb 2020 10:27:36 +0000 Subject: [PATCH 05/31] Add ability to override both dockerfile and path Add ability to override both the path to the dockerfile and the path to run the docker build from Signed-off-by: Nick Adcock --- internal/command/args.go | 8 ++++++-- internal/command/args_test.go | 7 ++++++- internal/options/build.go | 1 + internal/options/build_test.go | 2 ++ 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/internal/command/args.go b/internal/command/args.go index 5de2fa2..d072b5c 100644 --- a/internal/command/args.go +++ b/internal/command/args.go @@ -31,6 +31,10 @@ func BuildArgs(o options.Build, github options.GitHub) []string { args = append(args, "--label", label) } + if o.Dockerfile != "" { + args = append(args, "--file", o.Dockerfile) + } + if o.Target != "" { args = append(args, "--target", o.Target) } @@ -43,10 +47,10 @@ func BuildArgs(o options.Build, github options.GitHub) []string { args = append(args, "--build-arg", buildArg) } - if o.Dockerfile == "" { + if o.Path == "" { args = append(args, ".") } else { - args = append(args, o.Dockerfile) + args = append(args, o.Path) } return args } diff --git a/internal/command/args_test.go b/internal/command/args_test.go index d68d694..88005fb 100644 --- a/internal/command/args_test.go +++ b/internal/command/args_test.go @@ -34,10 +34,15 @@ func TestBuildArgs(t *testing.T) { name: "basic", expected: []string{"build", "."}, }, + { + name: "with-path", + build: options.Build{Path: "path"}, + expected: []string{"build", "path"}, + }, { name: "with-dockerfile", build: options.Build{Dockerfile: "dockerfile"}, - expected: []string{"build", "dockerfile"}, + expected: []string{"build", "--file", "dockerfile", "."}, }, { name: "with-static-tags", diff --git a/internal/options/build.go b/internal/options/build.go index 08fd254..35c5c1e 100644 --- a/internal/options/build.go +++ b/internal/options/build.go @@ -12,6 +12,7 @@ const githubLabelPrefix = "com.docker.github-actions" // Build contains the parsed build action environment variables type Build struct { + Path string `env:"INPUT_PATH"` Dockerfile string `env:"INPUT_DOCKERFILE"` Server string `env:"INPUT_SERVER"` Repository string `env:"INPUT_REPOSITORY"` diff --git a/internal/options/build_test.go b/internal/options/build_test.go index 90a8d42..15f509c 100644 --- a/internal/options/build_test.go +++ b/internal/options/build_test.go @@ -8,6 +8,7 @@ import ( ) func TestGetBuildOptions(t *testing.T) { + _ = os.Setenv("INPUT_PATH", "path") _ = os.Setenv("INPUT_DOCKERFILE", "dockerfile") _ = os.Setenv("INPUT_SERVER", "server") _ = os.Setenv("INPUT_REPOSITORY", "repository") @@ -23,6 +24,7 @@ func TestGetBuildOptions(t *testing.T) { assert.NilError(t, err) assert.DeepEqual(t, Build{ + Path: "path", Dockerfile: "dockerfile", Server: "server", Repository: "repository", From 0f77e60dfcba990039e42e0f7d27bf9ff077e33f Mon Sep 17 00:00:00 2001 From: Nick Adcock Date: Mon, 24 Feb 2020 10:28:29 +0000 Subject: [PATCH 06/31] Add initial e2e framework Adds initial e2e framework including: * Makefile test-e2e command * Running in docker * Login test with automated local registry setup and teardown Signed-off-by: Nick Adcock --- .github/workflows/main.yml | 3 + Dockerfile | 28 ++++++-- Makefile | 13 +++- docker.Makefile | 14 +++- e2e/helper_test.go | 126 ++++++++++++++++++++++++++++++++++++ e2e/login_test.go | 16 +++++ e2e/testdata/auth/htpasswd | 2 + e2e/testdata/login_test.env | 3 + 8 files changed, 194 insertions(+), 11 deletions(-) create mode 100644 e2e/helper_test.go create mode 100644 e2e/login_test.go create mode 100644 e2e/testdata/auth/htpasswd create mode 100644 e2e/testdata/login_test.env diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index bc23799..88cbe63 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -17,3 +17,6 @@ jobs: - name: Build run: make -f docker.Makefile + + - name: E2E + run: make -f docker.Makefile test-e2e diff --git a/Dockerfile b/Dockerfile index 6e7386c..6e972a6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,14 @@ ARG GO_VERSION=1.13.7 ARG GOLANGCI_LINT_VERSION=v1.23.6 -ARG ALPINE_VERSION=3.11.3 - +ARG DND_VERSION=19.03 +# Builds the github-actions binary, checks linting, and runs unit level tests FROM golang:${GO_VERSION} AS builder RUN curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin ${GOLANGCI_LINT_VERSION} -ARG MAKE_TARGET=all +ARG MAKE_TARGET=default ENV CGO_ENABLED=0 WORKDIR /src @@ -17,13 +17,31 @@ COPY . . RUN make ${MAKE_TARGET} +# Used to run e2e tests for github-actions +# This image must be run as a container to run the tests +FROM golang:${GO_VERSION} AS e2e +ARG CLI_CHANNEL=stable +ARG CLI_VERSION=19.03.5 + +RUN apt-get install -y -q --no-install-recommends coreutils util-linux + +ENV CGO_ENABLED=0 +ENV GITHUB_ACTIONS_BINARY=/github-actions +WORKDIR /tests + +RUN curl -fL https://download.docker.com/linux/static/${CLI_CHANNEL}/x86_64/docker-${CLI_VERSION}.tgz | tar xzO docker/docker > /usr/bin/docker && chmod +x /usr/bin/docker + +COPY . . +COPY --from=builder /src/bin/github-actions /github-actions + +# Used to extract the github-actions binary FROM scratch AS cli COPY --from=builder /src/bin/github-actions github-actions - -FROM alpine:${ALPINE_VERSION} +# The github-actions image that is used by published docker github actions +FROM docker:${DND_VERSION} COPY --from=builder /src/bin/github-actions /github-actions diff --git a/Makefile b/Makefile index 5fc9b09..4d1494c 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,6 @@ -all: build lint test +default: build lint test-unit + +all: default test-e2e build: @$(call mkdir,bin) @@ -7,5 +9,10 @@ build: lint: golangci-lint run --config golangci.yml ./... -test: - go test ./... +test: test-unit test-e2e + +test-unit: + go test $(go list ./... | grep -v /e2e) + +test-e2e: build + go test ./e2e/... diff --git a/docker.Makefile b/docker.Makefile index 7a67273..bb77515 100644 --- a/docker.Makefile +++ b/docker.Makefile @@ -2,6 +2,8 @@ TAG ?= latest STATIC_FLAGS = BUILDKIT_PROGRESS=plain DOCKER_BUILD = $(STATIC_FLAGS) docker build +ROOT_DIR = $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST)))) + all: $(DOCKER_BUILD) -t docker/github-actions:$(TAG) . @@ -13,7 +15,13 @@ cli: $(DOCKER_BUILD) -t github-actions-cli --target=cli --output type=local,dest=./bin/ --build-arg MAKE_TARGET=build . lint: - $(DOCKER_BUILD) -t github-actions-lint --build-arg MAKE_TARGET=lint . + $(DOCKER_BUILD) -t github-actions-lint --target=builder --build-arg MAKE_TARGET=lint . + +test: test-unit test-e2e + +test-unit: + $(DOCKER_BUILD) -t github-actions-test-unit --target=builder --build-arg MAKE_TARGET=test-unit . -test: - $(DOCKER_BUILD) -t github-actions-test --build-arg MAKE_TARGET=test . +test-e2e: + $(DOCKER_BUILD) -t github-actions-test-e2e --target e2e --build-arg MAKE_TARGET=build . + docker run --rm --network="host" -e "E2E_HOST_PATH=$(ROOT_DIR)/e2e" -v /var/run/docker.sock:/var/run/docker.sock github-actions-test-e2e make test-e2e \ No newline at end of file diff --git a/e2e/helper_test.go b/e2e/helper_test.go new file mode 100644 index 0000000..bbd66a7 --- /dev/null +++ b/e2e/helper_test.go @@ -0,0 +1,126 @@ +package e2e + +import ( + "bufio" + "fmt" + "os" + "os/exec" + "path" + "strings" +) + +const ( + registryContainerName = "github-actions-registry" + githubActionsImage = "github-actions-e2e" + e2eHostPath = "E2E_HOST_PATH" +) + +type envVar struct { + key string + value string +} + +func parseEnvFile(envFile string) ([]envVar, error) { + vars := []envVar{} + + wd, err := os.Getwd() + if err != nil { + return vars, err + } + + file, err := os.Open(path.Join(wd, envFile)) + if err != nil { + return vars, err + } + defer file.Close() + + scanner := bufio.NewScanner(file) + for scanner.Scan() { + split := strings.Split(scanner.Text(), "=") + vars = append(vars, envVar{split[0], split[1]}) + } + + return vars, scanner.Err() +} + +func setupEnvVars(vars []envVar) error { + for _, v := range vars { + if err := os.Setenv(v.key, v.value); err != nil { + return err + } + } + return nil +} + +func removeEnvVars(vars []envVar) error { + for _, v := range vars { + if err := os.Unsetenv(v.key); err != nil { + return err + } + } + return nil +} + +func getE2eHostPath() (string, error) { + path := os.Getenv(e2eHostPath) + if path != "" { + return path, nil + } + + return os.Getwd() +} + +func setupLocalRegistry() error { + _ = removeLocalRegistry() + + path, err := getE2eHostPath() + if err != nil { + return err + } + + authMount := fmt.Sprintf("%s/testdata/auth:/auth", path) + fmt.Printf("authMount = %s\n", authMount) + cmd := exec.Command("docker", "run", "-d", "-p", "5000:5000", "--name", registryContainerName, "-v", authMount, "-e", "REGISTRY_AUTH=htpasswd", "-e", "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm", "-e", "REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd", "registry:2") + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + return cmd.Run() +} + +func removeLocalRegistry() error { + return exec.Command("docker", "rm", "-f", registryContainerName).Run() +} + +func runActionsCommand(command, envFile string) error { + vars, err := parseEnvFile(envFile) + if err != nil { + return err + } + + if err = setupEnvVars(vars); err != nil { + return err + } + defer removeEnvVars(vars) + + bin, err := getActionsBinaryPath() + if err != nil { + return err + } + + cmd := exec.Command(bin, command) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + return cmd.Run() +} + +func getActionsBinaryPath() (string, error) { + if path := os.Getenv("GITHUB_ACTIONS_BINARY"); path != "" { + return path, nil + } + + wd, err := os.Getwd() + if err != nil { + return "", err + } + + return path.Join(wd, "../bin/github-actions"), nil +} diff --git a/e2e/login_test.go b/e2e/login_test.go new file mode 100644 index 0000000..53730ab --- /dev/null +++ b/e2e/login_test.go @@ -0,0 +1,16 @@ +package e2e + +import ( + "testing" + + "gotest.tools/v3/assert" +) + +func TestLogin(t *testing.T) { + err := setupLocalRegistry() + assert.NilError(t, err) + defer removeLocalRegistry() + + err = runActionsCommand("login", "testdata/login_test.env") + assert.NilError(t, err) +} diff --git a/e2e/testdata/auth/htpasswd b/e2e/testdata/auth/htpasswd new file mode 100644 index 0000000..30c69ff --- /dev/null +++ b/e2e/testdata/auth/htpasswd @@ -0,0 +1,2 @@ +my_user:$2y$05$aj/uKGxLnExU9dMOYtDrOOTp6UfLWIt1r9Y3vz9MnkhK/8z8mwUse + diff --git a/e2e/testdata/login_test.env b/e2e/testdata/login_test.env new file mode 100644 index 0000000..8139314 --- /dev/null +++ b/e2e/testdata/login_test.env @@ -0,0 +1,3 @@ +INPUT_SERVER=localhost:5000 +INPUT_USERNAME=my_user +INPUT_PASSWORD=my_password From d9614cd76ddc2ba08482ff93c6227d56ac9ad148 Mon Sep 17 00:00:00 2001 From: Nick Adcock Date: Mon, 24 Feb 2020 13:57:08 +0000 Subject: [PATCH 07/31] Create registry from Dockerfile for e2e Creates the registry image to be used for e2e tests from a local Dockerfile.registry so the path to the auth/htpasswd doesn't need to be figured out at runtime Signed-off-by: Nick Adcock --- Makefile | 3 ++- docker.Makefile | 11 +++++------ e2e/Dockerfile.registry | 7 +++++++ e2e/helper_test.go | 20 +------------------- 4 files changed, 15 insertions(+), 26 deletions(-) create mode 100644 e2e/Dockerfile.registry diff --git a/Makefile b/Makefile index 4d1494c..fbf8798 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,8 @@ lint: test: test-unit test-e2e test-unit: - go test $(go list ./... | grep -v /e2e) + go test ./cmd/... ./internal/... test-e2e: build + docker build --file ./e2e/Dockerfile.registry -t github-actions-registry ./e2e go test ./e2e/... diff --git a/docker.Makefile b/docker.Makefile index bb77515..e22b9c7 100644 --- a/docker.Makefile +++ b/docker.Makefile @@ -1,11 +1,10 @@ TAG ?= latest -STATIC_FLAGS = BUILDKIT_PROGRESS=plain -DOCKER_BUILD = $(STATIC_FLAGS) docker build +DOCKER_BUILD = docker build --progress=plain -ROOT_DIR = $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST)))) +ROOT_DIR = $(dir $(realpath $(firstword $(MAKEFILE_LIST)))) +ROOT_DIR := $(subst .\,,$(ROOT_DIR)) -all: - $(DOCKER_BUILD) -t docker/github-actions:$(TAG) . +all: image lint test image: $(DOCKER_BUILD) -t docker/github-actions:$(TAG) --build-arg MAKE_TARGET=build . @@ -24,4 +23,4 @@ test-unit: test-e2e: $(DOCKER_BUILD) -t github-actions-test-e2e --target e2e --build-arg MAKE_TARGET=build . - docker run --rm --network="host" -e "E2E_HOST_PATH=$(ROOT_DIR)/e2e" -v /var/run/docker.sock:/var/run/docker.sock github-actions-test-e2e make test-e2e \ No newline at end of file + docker run --rm --network="host" -v /var/run/docker.sock:/var/run/docker.sock github-actions-test-e2e make test-e2e diff --git a/e2e/Dockerfile.registry b/e2e/Dockerfile.registry new file mode 100644 index 0000000..1a2c62c --- /dev/null +++ b/e2e/Dockerfile.registry @@ -0,0 +1,7 @@ +FROM registry:2 + +ENV REGISTRY_AUTH=htpasswd +ENV REGISTRY_AUTH_HTPASSWD_REALM="Registry Realm" +ENV REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd + +COPY ./testdata/auth/htpasswd /auth/htpasswd diff --git a/e2e/helper_test.go b/e2e/helper_test.go index bbd66a7..d34a288 100644 --- a/e2e/helper_test.go +++ b/e2e/helper_test.go @@ -2,7 +2,6 @@ package e2e import ( "bufio" - "fmt" "os" "os/exec" "path" @@ -12,7 +11,6 @@ import ( const ( registryContainerName = "github-actions-registry" githubActionsImage = "github-actions-e2e" - e2eHostPath = "E2E_HOST_PATH" ) type envVar struct { @@ -61,26 +59,10 @@ func removeEnvVars(vars []envVar) error { return nil } -func getE2eHostPath() (string, error) { - path := os.Getenv(e2eHostPath) - if path != "" { - return path, nil - } - - return os.Getwd() -} - func setupLocalRegistry() error { _ = removeLocalRegistry() - path, err := getE2eHostPath() - if err != nil { - return err - } - - authMount := fmt.Sprintf("%s/testdata/auth:/auth", path) - fmt.Printf("authMount = %s\n", authMount) - cmd := exec.Command("docker", "run", "-d", "-p", "5000:5000", "--name", registryContainerName, "-v", authMount, "-e", "REGISTRY_AUTH=htpasswd", "-e", "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm", "-e", "REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd", "registry:2") + cmd := exec.Command("docker", "run", "-d", "-p", "5000:5000", "--name", registryContainerName, registryContainerName) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr return cmd.Run() From a0909469b3f802323377f8df2eb534de1824b02e Mon Sep 17 00:00:00 2001 From: Nick Adcock Date: Mon, 24 Feb 2020 14:07:15 +0000 Subject: [PATCH 08/31] Add poll to e2e login Signed-off-by: Nick Adcock --- .github/workflows/main.yml | 2 +- e2e/login_test.go | 11 +++++- go.mod | 1 + go.sum | 79 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 88cbe63..be19a2a 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -16,7 +16,7 @@ jobs: run: cat $GITHUB_EVENT_PATH - name: Build - run: make -f docker.Makefile + run: make -f docker.Makefile image lint test-unit - name: E2E run: make -f docker.Makefile test-e2e diff --git a/e2e/login_test.go b/e2e/login_test.go index 53730ab..8cc7bf5 100644 --- a/e2e/login_test.go +++ b/e2e/login_test.go @@ -2,8 +2,10 @@ package e2e import ( "testing" + "time" "gotest.tools/v3/assert" + "k8s.io/apimachinery/pkg/util/wait" ) func TestLogin(t *testing.T) { @@ -11,6 +13,13 @@ func TestLogin(t *testing.T) { assert.NilError(t, err) defer removeLocalRegistry() - err = runActionsCommand("login", "testdata/login_test.env") + err = loginLocalRegistry() assert.NilError(t, err) } + +func loginLocalRegistry() error { + return wait.Poll(2*time.Second, 30*time.Second, func() (bool, error) { + err := runActionsCommand("login", "testdata/login_test.env") + return err == nil, err + }) +} diff --git a/go.mod b/go.mod index cccc0fa..24ae310 100644 --- a/go.mod +++ b/go.mod @@ -7,4 +7,5 @@ require ( github.com/magiconair/properties v1.8.1 // indirect github.com/urfave/cli/v2 v2.1.1 gotest.tools/v3 v3.0.2 + k8s.io/apimachinery v0.17.3 ) diff --git a/go.sum b/go.sum index dba47e6..83a8987 100644 --- a/go.sum +++ b/go.sum @@ -1,38 +1,117 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ= +github.com/PuerkitoBio/purell v1.0.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= +github.com/PuerkitoBio/urlesc v0.0.0-20160726150825-5bd2802263f2/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE= github.com/caarlos0/env v3.5.0+incompatible h1:Yy0UN8o9Wtr/jGHZDpCBLpNrzcFLLM2yixi/rBrKyJs= github.com/caarlos0/env/v6 v6.1.0 h1:4FbM+HmZA/Q5wdSrH2kj0KQXm7xnhuO8y3TuOTnOvqc= github.com/caarlos0/env/v6 v6.1.0/go.mod h1:iUA6X3VCAOwDhoqvgKlTGjjwJzQseIJaFYApUqQkt+8= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +github.com/davecgh/go-spew v0.0.0-20151105211317-5215b55f46b2/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96/go.mod h1:Qh8CwZgvJUkLughtfhJv5dyTYa91l1fOUCrgjqmcifM= +github.com/elazarl/goproxy v0.0.0-20170405201442-c4fc26588b6e/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc= +github.com/emicklei/go-restful v0.0.0-20170410110728-ff4f55a20633/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs= +github.com/evanphx/json-patch v4.2.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= +github.com/go-logr/logr v0.1.0/go.mod h1:ixOQHD9gLJUVQQ2ZOR7zLEifBX6tGkNJF4QyIY7sIas= +github.com/go-openapi/jsonpointer v0.0.0-20160704185906-46af16f9f7b1/go.mod h1:+35s3my2LFTysnkMfxsJBAMHj/DoqoB9knIWoYG/Vk0= +github.com/go-openapi/jsonreference v0.0.0-20160704190145-13c6e3589ad9/go.mod h1:W3Z9FmVs9qj+KR4zFKmDPGiLdk1D9Rlm7cyMvf57TTg= +github.com/go-openapi/spec v0.0.0-20160808142527-6aced65f8501/go.mod h1:J8+jY1nAiCcj+friV/PDoE1/3eeccG9LYBs0tYvLOWc= +github.com/go-openapi/swag v0.0.0-20160704191624-1d0bd113de87/go.mod h1:DXUve3Dpr1UfpPtxFw+EFuQ41HhCWZfha5jSVRG7C7I= +github.com/gogo/protobuf v1.2.2-0.20190723190241-65acae22fc9d/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= +github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/golang/protobuf v0.0.0-20161109072736-4bd1920723d7/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/gofuzz v0.0.0-20161122191042-44d81051d367/go.mod h1:HP5RmnzzSNb993RKQDq4+1A4ia9nllfqcQFTQJedwGI= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY= +github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= +github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/json-iterator/go v0.0.0-20180612202835-f2b4162afba3/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= +github.com/json-iterator/go v1.1.8/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4= github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= +github.com/mailru/easyjson v0.0.0-20160728113105-d5b7844b561a/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v0.0.0-20180320133207-05fbef0ca5da/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= +github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw= +github.com/onsi/ginkgo v0.0.0-20170829012221-11459a886d9c/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA= +github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +github.com/spf13/pflag v0.0.0-20170130214245-9ff6c6923cff/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v0.0.0-20151208002404-e3a8ff8ce365/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/urfave/cli/v2 v2.1.1 h1:Qt8FeAtxE/vfdrLmR3rxR6JRE0RoVmbXu8+6kZtYU4k= github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20170114055629-f2499483f923/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20191004110552-13f9640d40b9/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20170830134202-bb24a47a89ea/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190826190057-c7b8b68b1456/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.0.0-20160726164857-2910a502d2bf/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20181011042414-1f849cf54d09/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190624222133-a101b041ded4/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo= gotest.tools/v3 v3.0.2 h1:kG1BFyqVHuQoVQiR1bWGnfz/fmHvvuiSPIV7rvl360E= gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk= +k8s.io/apimachinery v0.17.3 h1:f+uZV6rm4/tHE7xXgLyToprg6xWairaClGVkm2t8omg= +k8s.io/apimachinery v0.17.3/go.mod h1:gxLnyZcGNdZTCLnq3fgzyg2A5BVCHTNDFrw8AmuJ+0g= +k8s.io/gengo v0.0.0-20190128074634-0689ccc1d7d6/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0= +k8s.io/klog v0.0.0-20181102134211-b9b56d5dfc92/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk= +k8s.io/klog v1.0.0 h1:Pt+yjF5aB1xDSVbau4VsWe+dQNzA0qv1LlXdC2dF6Q8= +k8s.io/klog v1.0.0/go.mod h1:4Bi6QPql/J/LkTDqv7R/cd3hPo4k2DG6Ptcz060Ez5I= +k8s.io/kube-openapi v0.0.0-20191107075043-30be4d16710a/go.mod h1:1TqjTSzOxsLGIKfj0lK8EeCP7K1iUG65v09OM0/WG5E= +sigs.k8s.io/structured-merge-diff v0.0.0-20190525122527-15d366b2352e/go.mod h1:wWxsB5ozmmv/SG7nM11ayaAW51xMvak/t1r0CSlcokI= +sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o= From b39a5bfe71457a148d5908989f6ab514e7ae47c0 Mon Sep 17 00:00:00 2001 From: Nick Adcock Date: Mon, 24 Feb 2020 15:42:18 +0000 Subject: [PATCH 09/31] Add e2e tests for build Signed-off-by: Nick Adcock --- e2e/build_test.go | 104 ++++++++++++++++++ e2e/helper_test.go | 24 +++- e2e/testdata/build_tests/Dockerfile | 3 + e2e/testdata/build_tests/auto_labels.env | 10 ++ e2e/testdata/build_tests/auto_tags_branch.env | 8 ++ e2e/testdata/build_tests/auto_tags_master.env | 8 ++ e2e/testdata/build_tests/auto_tags_pr.env | 7 ++ e2e/testdata/build_tests/auto_tags_tag.env | 7 ++ e2e/testdata/build_tests/static_labels.env | 8 ++ e2e/testdata/build_tests/static_tags.env | 7 ++ 10 files changed, 185 insertions(+), 1 deletion(-) create mode 100644 e2e/build_test.go create mode 100644 e2e/testdata/build_tests/Dockerfile create mode 100644 e2e/testdata/build_tests/auto_labels.env create mode 100644 e2e/testdata/build_tests/auto_tags_branch.env create mode 100644 e2e/testdata/build_tests/auto_tags_master.env create mode 100644 e2e/testdata/build_tests/auto_tags_pr.env create mode 100644 e2e/testdata/build_tests/auto_tags_tag.env create mode 100644 e2e/testdata/build_tests/static_labels.env create mode 100644 e2e/testdata/build_tests/static_tags.env diff --git a/e2e/build_test.go b/e2e/build_test.go new file mode 100644 index 0000000..b1bd0f6 --- /dev/null +++ b/e2e/build_test.go @@ -0,0 +1,104 @@ +package e2e + +import ( + "os/exec" + "testing" + + "gotest.tools/v3/assert" +) + +func TestBuildWithStaticTags(t *testing.T) { + testCases := []struct { + name string + envFile string + expectedTags []string + expectedLabels map[string]string + }{ + { + name: "static-tags", + envFile: "testdata/build_tests/static_tags.env", + expectedTags: []string{ + "localhost:5000/my-repository:v1-static-tags", + "localhost:5000/my-repository:v1.1-static-tags", + }, + }, + { + name: "static-labels", + envFile: "testdata/build_tests/static_labels.env", + expectedTags: []string{ + "localhost:5000/my-repository:static-labels", + }, + expectedLabels: map[string]string{ + "a": "a1", + "b": "b1", + }, + }, + { + name: "auto-labels", + envFile: "testdata/build_tests/auto_labels.env", + expectedTags: []string{ + "localhost:5000/my-repository:auto-labels", + }, + expectedLabels: map[string]string{ + "a": "a1", + "com.docker.github-actions-actor": "actor", + "com.docker.github-actions-sha": "sha", + }, + }, + { + name: "auto-tags-master", + envFile: "testdata/build_tests/auto_tags_master.env", + expectedTags: []string{ + "localhost:5000/my-repository:auto-tags-master", + "localhost:5000/my-repository:latest", + }, + }, + { + name: "auto-tags-branch", + envFile: "testdata/build_tests/auto_tags_branch.env", + expectedTags: []string{ + "localhost:5000/my-repository:auto-tags-branch", + "localhost:5000/my-repository:branch", + }, + }, + { + name: "auto-tags-pr", + envFile: "testdata/build_tests/auto_tags_pr.env", + expectedTags: []string{ + "localhost:5000/my-repository:pr-pr1", + }, + }, + { + name: "auto-tags-tag", + envFile: "testdata/build_tests/auto_tags_tag.env", + expectedTags: []string{ + "localhost:5000/my-repository:tag1", + }, + }, + } + + for _, tc := range testCases { + tc := tc + t.Run(tc.name, func(t *testing.T) { + err := runActionsCommand("build", tc.envFile) + assert.NilError(t, err) + defer removeImages(tc.expectedTags) + + for _, tag := range tc.expectedTags { + inspect, err := inspectImage(tag) + assert.NilError(t, err) + assert.DeepEqual(t, tc.expectedTags, inspect.RepoTags) + assert.DeepEqual(t, tc.expectedLabels, inspect.Config.Labels) + } + }) + } +} + +func removeImages(tags []string) error { + for _, tag := range tags { + if err := exec.Command("docker", "rmi", "-f", tag).Run(); err != nil { + return err + } + } + return nil +} diff --git a/e2e/helper_test.go b/e2e/helper_test.go index d34a288..f4afdfd 100644 --- a/e2e/helper_test.go +++ b/e2e/helper_test.go @@ -2,6 +2,7 @@ package e2e import ( "bufio" + "encoding/json" "os" "os/exec" "path" @@ -34,7 +35,7 @@ func parseEnvFile(envFile string) ([]envVar, error) { scanner := bufio.NewScanner(file) for scanner.Scan() { - split := strings.Split(scanner.Text(), "=") + split := strings.SplitN(scanner.Text(), "=", 2) vars = append(vars, envVar{split[0], split[1]}) } @@ -106,3 +107,24 @@ func getActionsBinaryPath() (string, error) { return path.Join(wd, "../bin/github-actions"), nil } + +func inspectImage(image string) (inspectResult, error) { + out, err := exec.Command("docker", "inspect", image).Output() + if err != nil { + return inspectResult{}, err + } + var result []inspectResult + if err = json.Unmarshal(out, &result); err != nil { + return inspectResult{}, err + } + return result[0], nil +} + +type inspectResult struct { + RepoTags []string `json:"RepoTags"` + Config inspectResultConfig `json:"Config"` +} + +type inspectResultConfig struct { + Labels map[string]string `json:"Labels"` +} diff --git a/e2e/testdata/build_tests/Dockerfile b/e2e/testdata/build_tests/Dockerfile new file mode 100644 index 0000000..c251a4b --- /dev/null +++ b/e2e/testdata/build_tests/Dockerfile @@ -0,0 +1,3 @@ +FROM alpine + +ENTRYPOINT ["echo", "hello-world"] \ No newline at end of file diff --git a/e2e/testdata/build_tests/auto_labels.env b/e2e/testdata/build_tests/auto_labels.env new file mode 100644 index 0000000..2185ef5 --- /dev/null +++ b/e2e/testdata/build_tests/auto_labels.env @@ -0,0 +1,10 @@ +INPUT_PATH=./testdata/build_tests +INPUT_DOCKERFILE=./testdata/build_tests/Dockerfile +INPUT_SET_DEFAULT_TAGS=false +INPUT_SET_DEFAULT_LABELS=true +INPUT_TAGS=auto-labels +INPUT_LABELS=a=a1 +INPUT_SERVER=localhost:5000 +INPUT_REPOSITORY=my-repository +GITHUB_ACTOR=actor +GITHUB_SHA=sha diff --git a/e2e/testdata/build_tests/auto_tags_branch.env b/e2e/testdata/build_tests/auto_tags_branch.env new file mode 100644 index 0000000..5c44c09 --- /dev/null +++ b/e2e/testdata/build_tests/auto_tags_branch.env @@ -0,0 +1,8 @@ +INPUT_PATH=./testdata/build_tests +INPUT_DOCKERFILE=./testdata/build_tests/Dockerfile +INPUT_SET_DEFAULT_TAGS=true +INPUT_SET_DEFAULT_LABELS=false +INPUT_TAGS=auto-tags-branch +INPUT_SERVER=localhost:5000 +INPUT_REPOSITORY=my-repository +GITHUB_REF=refs/heads/branch diff --git a/e2e/testdata/build_tests/auto_tags_master.env b/e2e/testdata/build_tests/auto_tags_master.env new file mode 100644 index 0000000..60034da --- /dev/null +++ b/e2e/testdata/build_tests/auto_tags_master.env @@ -0,0 +1,8 @@ +INPUT_PATH=./testdata/build_tests +INPUT_DOCKERFILE=./testdata/build_tests/Dockerfile +INPUT_SET_DEFAULT_TAGS=true +INPUT_SET_DEFAULT_LABELS=false +INPUT_TAGS=auto-tags-master +INPUT_SERVER=localhost:5000 +INPUT_REPOSITORY=my-repository +GITHUB_REF=refs/heads/master diff --git a/e2e/testdata/build_tests/auto_tags_pr.env b/e2e/testdata/build_tests/auto_tags_pr.env new file mode 100644 index 0000000..7d3572f --- /dev/null +++ b/e2e/testdata/build_tests/auto_tags_pr.env @@ -0,0 +1,7 @@ +INPUT_PATH=./testdata/build_tests +INPUT_DOCKERFILE=./testdata/build_tests/Dockerfile +INPUT_SET_DEFAULT_TAGS=true +INPUT_SET_DEFAULT_LABELS=false +INPUT_SERVER=localhost:5000 +INPUT_REPOSITORY=my-repository +GITHUB_REF=refs/pulls/pr1 diff --git a/e2e/testdata/build_tests/auto_tags_tag.env b/e2e/testdata/build_tests/auto_tags_tag.env new file mode 100644 index 0000000..166b963 --- /dev/null +++ b/e2e/testdata/build_tests/auto_tags_tag.env @@ -0,0 +1,7 @@ +INPUT_PATH=./testdata/build_tests +INPUT_DOCKERFILE=./testdata/build_tests/Dockerfile +INPUT_SET_DEFAULT_TAGS=true +INPUT_SET_DEFAULT_LABELS=false +INPUT_SERVER=localhost:5000 +INPUT_REPOSITORY=my-repository +GITHUB_REF=refs/tags/tag1 diff --git a/e2e/testdata/build_tests/static_labels.env b/e2e/testdata/build_tests/static_labels.env new file mode 100644 index 0000000..faf0357 --- /dev/null +++ b/e2e/testdata/build_tests/static_labels.env @@ -0,0 +1,8 @@ +INPUT_PATH=./testdata/build_tests +INPUT_DOCKERFILE=./testdata/build_tests/Dockerfile +INPUT_SET_DEFAULT_TAGS=false +INPUT_SET_DEFAULT_LABELS=false +INPUT_TAGS=static-labels +INPUT_LABELS=a=a1,b=b1 +INPUT_SERVER=localhost:5000 +INPUT_REPOSITORY=my-repository diff --git a/e2e/testdata/build_tests/static_tags.env b/e2e/testdata/build_tests/static_tags.env new file mode 100644 index 0000000..c6c51e9 --- /dev/null +++ b/e2e/testdata/build_tests/static_tags.env @@ -0,0 +1,7 @@ +INPUT_PATH=./testdata/build_tests +INPUT_DOCKERFILE=./testdata/build_tests/Dockerfile +INPUT_SET_DEFAULT_TAGS=false +INPUT_SET_DEFAULT_LABELS=false +INPUT_TAGS=v1-static-tags,v1.1-static-tags +INPUT_SERVER=localhost:5000 +INPUT_REPOSITORY=my-repository From b61c5d7f89ba0353c46802f7a926fbcb1cd403c3 Mon Sep 17 00:00:00 2001 From: Nick Adcock Date: Mon, 24 Feb 2020 17:11:52 +0000 Subject: [PATCH 10/31] Extract tags from build options Signed-off-by: Nick Adcock --- cmd/build.go | 4 +- e2e/testdata/build_tests/auto_labels.env | 2 +- e2e/testdata/build_tests/auto_tags_branch.env | 2 +- e2e/testdata/build_tests/auto_tags_master.env | 2 +- e2e/testdata/build_tests/auto_tags_pr.env | 2 +- e2e/testdata/build_tests/auto_tags_tag.env | 2 +- e2e/testdata/build_tests/static_labels.env | 2 +- e2e/testdata/build_tests/static_tags.env | 2 +- internal/command/args.go | 12 +-- internal/command/args_test.go | 36 +------ internal/options/build.go | 34 ------- internal/options/build_test.go | 71 +------------- internal/options/tag.go | 57 +++++++++++ internal/options/tag_test.go | 96 +++++++++++++++++++ 14 files changed, 172 insertions(+), 152 deletions(-) create mode 100644 internal/options/tag.go create mode 100644 internal/options/tag_test.go diff --git a/cmd/build.go b/cmd/build.go index 5d3826b..7d9778c 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -16,6 +16,8 @@ func build(cmd command.Runner) error { return err } - args := command.BuildArgs(o, github) + tags := options.GetTags(o.Server, github) + + args := command.BuildArgs(o, github, tags) return cmd.Run("docker", args...) } diff --git a/e2e/testdata/build_tests/auto_labels.env b/e2e/testdata/build_tests/auto_labels.env index 2185ef5..b2215c5 100644 --- a/e2e/testdata/build_tests/auto_labels.env +++ b/e2e/testdata/build_tests/auto_labels.env @@ -1,6 +1,6 @@ INPUT_PATH=./testdata/build_tests INPUT_DOCKERFILE=./testdata/build_tests/Dockerfile -INPUT_SET_DEFAULT_TAGS=false +INPUT_AUTO_TAG=false INPUT_SET_DEFAULT_LABELS=true INPUT_TAGS=auto-labels INPUT_LABELS=a=a1 diff --git a/e2e/testdata/build_tests/auto_tags_branch.env b/e2e/testdata/build_tests/auto_tags_branch.env index 5c44c09..e1fc256 100644 --- a/e2e/testdata/build_tests/auto_tags_branch.env +++ b/e2e/testdata/build_tests/auto_tags_branch.env @@ -1,6 +1,6 @@ INPUT_PATH=./testdata/build_tests INPUT_DOCKERFILE=./testdata/build_tests/Dockerfile -INPUT_SET_DEFAULT_TAGS=true +INPUT_AUTO_TAG=true INPUT_SET_DEFAULT_LABELS=false INPUT_TAGS=auto-tags-branch INPUT_SERVER=localhost:5000 diff --git a/e2e/testdata/build_tests/auto_tags_master.env b/e2e/testdata/build_tests/auto_tags_master.env index 60034da..e7cda5d 100644 --- a/e2e/testdata/build_tests/auto_tags_master.env +++ b/e2e/testdata/build_tests/auto_tags_master.env @@ -1,6 +1,6 @@ INPUT_PATH=./testdata/build_tests INPUT_DOCKERFILE=./testdata/build_tests/Dockerfile -INPUT_SET_DEFAULT_TAGS=true +INPUT_AUTO_TAG=true INPUT_SET_DEFAULT_LABELS=false INPUT_TAGS=auto-tags-master INPUT_SERVER=localhost:5000 diff --git a/e2e/testdata/build_tests/auto_tags_pr.env b/e2e/testdata/build_tests/auto_tags_pr.env index 7d3572f..4f2ff27 100644 --- a/e2e/testdata/build_tests/auto_tags_pr.env +++ b/e2e/testdata/build_tests/auto_tags_pr.env @@ -1,6 +1,6 @@ INPUT_PATH=./testdata/build_tests INPUT_DOCKERFILE=./testdata/build_tests/Dockerfile -INPUT_SET_DEFAULT_TAGS=true +INPUT_AUTO_TAG=true INPUT_SET_DEFAULT_LABELS=false INPUT_SERVER=localhost:5000 INPUT_REPOSITORY=my-repository diff --git a/e2e/testdata/build_tests/auto_tags_tag.env b/e2e/testdata/build_tests/auto_tags_tag.env index 166b963..bbaca31 100644 --- a/e2e/testdata/build_tests/auto_tags_tag.env +++ b/e2e/testdata/build_tests/auto_tags_tag.env @@ -1,6 +1,6 @@ INPUT_PATH=./testdata/build_tests INPUT_DOCKERFILE=./testdata/build_tests/Dockerfile -INPUT_SET_DEFAULT_TAGS=true +INPUT_AUTO_TAG=true INPUT_SET_DEFAULT_LABELS=false INPUT_SERVER=localhost:5000 INPUT_REPOSITORY=my-repository diff --git a/e2e/testdata/build_tests/static_labels.env b/e2e/testdata/build_tests/static_labels.env index faf0357..8c5c04f 100644 --- a/e2e/testdata/build_tests/static_labels.env +++ b/e2e/testdata/build_tests/static_labels.env @@ -1,6 +1,6 @@ INPUT_PATH=./testdata/build_tests INPUT_DOCKERFILE=./testdata/build_tests/Dockerfile -INPUT_SET_DEFAULT_TAGS=false +INPUT_AUTO_TAG=false INPUT_SET_DEFAULT_LABELS=false INPUT_TAGS=static-labels INPUT_LABELS=a=a1,b=b1 diff --git a/e2e/testdata/build_tests/static_tags.env b/e2e/testdata/build_tests/static_tags.env index c6c51e9..05096b7 100644 --- a/e2e/testdata/build_tests/static_tags.env +++ b/e2e/testdata/build_tests/static_tags.env @@ -1,6 +1,6 @@ INPUT_PATH=./testdata/build_tests INPUT_DOCKERFILE=./testdata/build_tests/Dockerfile -INPUT_SET_DEFAULT_TAGS=false +INPUT_AUTO_TAG=false INPUT_SET_DEFAULT_LABELS=false INPUT_TAGS=v1-static-tags,v1.1-static-tags INPUT_SERVER=localhost:5000 diff --git a/internal/command/args.go b/internal/command/args.go index d072b5c..dbd1d99 100644 --- a/internal/command/args.go +++ b/internal/command/args.go @@ -1,8 +1,6 @@ package command import ( - "fmt" - "github.com/docker/github-actions/internal/options" ) @@ -16,15 +14,11 @@ func LoginArgs(o options.Login) []string { } // BuildArgs converts build options into the cli arguments used to call `docker build` -func BuildArgs(o options.Build, github options.GitHub) []string { +func BuildArgs(o options.Build, github options.GitHub, tags []string) []string { args := []string{"build"} - for _, tag := range options.GetTags(o, github) { - t := fmt.Sprintf("%s:%s", o.Repository, tag) - if o.Server != "" { - t = fmt.Sprintf("%s/%s", o.Server, t) - } - args = append(args, "-t", t) + for _, tag := range tags { + args = append(args, "-t", tag) } for _, label := range options.GetLabels(o, github) { diff --git a/internal/command/args_test.go b/internal/command/args_test.go index 88005fb..f9cde4d 100644 --- a/internal/command/args_test.go +++ b/internal/command/args_test.go @@ -28,6 +28,7 @@ func TestBuildArgs(t *testing.T) { name string build options.Build github options.GitHub + tags []string expected []string }{ { @@ -45,36 +46,9 @@ func TestBuildArgs(t *testing.T) { expected: []string{"build", "--file", "dockerfile", "."}, }, { - name: "with-static-tags", - build: options.Build{ - Repository: "repository", - Tags: []string{"tag1", "tag2"}, - }, - expected: []string{"build", "-t", "repository:tag1", "-t", "repository:tag2", "."}, - }, - { - name: "with-static-tags-and-server", - build: options.Build{ - Server: "server", - Repository: "repository", - Tags: []string{"tag1", "tag2"}, - }, - expected: []string{"build", "-t", "server/repository:tag1", "-t", "server/repository:tag2", "."}, - }, - { - name: "with-default-tags", - build: options.Build{ - SetDefaultTags: true, - Repository: "repository", - Tags: []string{"tag1"}, - }, - github: options.GitHub{ - Reference: options.GitReference{ - Type: options.GitRefHead, - Name: "branch", - }, - }, - expected: []string{"build", "-t", "repository:tag1", "-t", "repository:branch", "."}, + name: "with-tags", + tags: []string{"tag1", "tag2"}, + expected: []string{"build", "-t", "tag1", "-t", "tag2", "."}, }, { name: "with-static-labels", @@ -120,7 +94,7 @@ func TestBuildArgs(t *testing.T) { for _, tc := range testCases { tc := tc t.Run(tc.name, func(t *testing.T) { - args := BuildArgs(tc.build, tc.github) + args := BuildArgs(tc.build, tc.github, tc.tags) assert.DeepEqual(t, tc.expected, args) }) } diff --git a/internal/options/build.go b/internal/options/build.go index 35c5c1e..947198f 100644 --- a/internal/options/build.go +++ b/internal/options/build.go @@ -15,14 +15,11 @@ type Build struct { Path string `env:"INPUT_PATH"` Dockerfile string `env:"INPUT_DOCKERFILE"` Server string `env:"INPUT_SERVER"` - Repository string `env:"INPUT_REPOSITORY"` - SetDefaultTags bool `env:"INPUT_SET_DEFAULT_TAGS"` SetDefaultLabels bool `env:"INPUT_SET_DEFAULT_LABELS"` Target string `env:"INPUT_TARGET"` AlwaysPull bool `env:"INPUT_ALWAYS_PULL"` BuildArgs []string Labels []string - Tags []string } // GetBuildOptions gets the login action environment variables @@ -40,40 +37,9 @@ func GetBuildOptions() (Build, error) { build.Labels = strings.Split(labels, ",") } - if tags := os.Getenv("INPUT_TAGS"); tags != "" { - build.Tags = strings.Split(tags, ",") - } - return build, nil } -// GetTags gets a list of all tags to build the image with including automatic tags created from github vars when SetDefaultTags is true -func GetTags(build Build, github GitHub) []string { - tags := []string{} - if build.Tags != nil { - tags = build.Tags - } - - if !build.SetDefaultTags { - return tags - } - - switch github.Reference.Type { - case GitRefHead: - if github.Reference.Name == "master" { - tags = append(tags, "latest") - } else { - tags = append(tags, github.Reference.Name) - } - case GitRefPullRequest: - tags = append(tags, fmt.Sprintf("pr-%s", github.Reference.Name)) - case GitRefTag: - tags = append(tags, github.Reference.Name) - } - - return tags -} - // GetLabels gets a list of all labels to build the image with including automatic labels created from github vars when SetDefaultLabels is true func GetLabels(build Build, github GitHub) []string { labels := []string{} diff --git a/internal/options/build_test.go b/internal/options/build_test.go index 15f509c..b6509e8 100644 --- a/internal/options/build_test.go +++ b/internal/options/build_test.go @@ -14,7 +14,7 @@ func TestGetBuildOptions(t *testing.T) { _ = os.Setenv("INPUT_REPOSITORY", "repository") _ = os.Setenv("INPUT_BUILD_ARGS", "buildarg1=b1,buildarg2=b2") _ = os.Setenv("INPUT_LABELS", "label1=l1,label2=l2") - _ = os.Setenv("INPUT_SET_DEFAULT_TAGS", "false") + _ = os.Setenv("INPUT_AUTO_TAG", "false") _ = os.Setenv("INPUT_SET_DEFAULT_LABELS", "false") _ = os.Setenv("INPUT_TARGET", "target") _ = os.Setenv("INPUT_ALWAYS_PULL", "true") @@ -27,83 +27,14 @@ func TestGetBuildOptions(t *testing.T) { Path: "path", Dockerfile: "dockerfile", Server: "server", - Repository: "repository", - SetDefaultTags: false, SetDefaultLabels: false, Target: "target", AlwaysPull: true, BuildArgs: []string{"buildarg1=b1", "buildarg2=b2"}, Labels: []string{"label1=l1", "label2=l2"}, - Tags: []string{"tag1", "tag2"}, }, o) } -func TestGetTags(t *testing.T) { - testCases := []struct { - name string - setDefault bool - tags []string - ref GitReference - expected []string - }{ - { - name: "no-defaults", - tags: []string{"tag1", "tag2"}, - expected: []string{"tag1", "tag2"}, - ref: GitReference{GitRefHead, "master"}, - }, - { - name: "unknown-ref-type", - tags: []string{"tag1", "tag2"}, - expected: []string{"tag1", "tag2"}, - setDefault: true, - ref: GitReference{GitRefUnknown, "master"}, - }, - { - name: "master-branch", - setDefault: true, - tags: []string{"tag1", "tag2"}, - expected: []string{"tag1", "tag2", "latest"}, - ref: GitReference{GitRefHead, "master"}, - }, - { - name: "different-branch", - setDefault: true, - tags: []string{"tag1", "tag2"}, - expected: []string{"tag1", "tag2", "branch-name"}, - ref: GitReference{GitRefHead, "branch-name"}, - }, - { - name: "pull-request", - setDefault: true, - tags: []string{"tag1", "tag2"}, - expected: []string{"tag1", "tag2", "pr-name"}, - ref: GitReference{GitRefPullRequest, "name"}, - }, - { - name: "tag", - setDefault: true, - tags: []string{"tag1", "tag2"}, - expected: []string{"tag1", "tag2", "v1.0"}, - ref: GitReference{GitRefTag, "v1.0"}, - }, - } - - for _, tc := range testCases { - tc := tc - t.Run(tc.name, func(t *testing.T) { - tags := GetTags( - Build{ - SetDefaultTags: tc.setDefault, - Tags: tc.tags, - }, - GitHub{Reference: tc.ref}, - ) - assert.DeepEqual(t, tc.expected, tags) - }) - } -} - func TestGetLabels(t *testing.T) { testCases := []struct { name string diff --git a/internal/options/tag.go b/internal/options/tag.go new file mode 100644 index 0000000..60a5ecc --- /dev/null +++ b/internal/options/tag.go @@ -0,0 +1,57 @@ +package options + +import ( + "fmt" + "os" + "strings" +) + +func autoTag() bool { + return os.Getenv("INPUT_AUTO_TAG") == "true" +} + +func dockerRepo(github GitHub) string { + if repo := os.Getenv("INPUT_REPOSITORY"); repo != "" { + return repo + } + return strings.ToLower(github.Repository) +} + +func staticTags() []string { + if inputTags := os.Getenv("INPUT_TAGS"); inputTags != "" { + return strings.Split(inputTags, ",") + } + return []string{} +} + +func toFullTag(server, repo, tag string) string { + tag = strings.Trim(tag, " ") + if server != "" { + return fmt.Sprintf("%s/%s:%s", server, repo, tag) + } + return fmt.Sprintf("%s:%s", repo, tag) +} + +// GetTags gets a list of all tags for including automatic tags created from github vars when AutTag is true along with the server and repository +func GetTags(server string, github GitHub) []string { + repo := dockerRepo(github) + tags := []string{} + for _, t := range staticTags() { + tags = append(tags, toFullTag(server, repo, t)) + } + if autoTag() { + switch github.Reference.Type { + case GitRefHead: + if github.Reference.Name == "master" { + tags = append(tags, toFullTag(server, repo, "latest")) + } else { + tags = append(tags, toFullTag(server, repo, github.Reference.Name)) + } + case GitRefPullRequest: + tags = append(tags, toFullTag(server, repo, fmt.Sprintf("pr-%s", github.Reference.Name))) + case GitRefTag: + tags = append(tags, toFullTag(server, repo, github.Reference.Name)) + } + } + return tags +} diff --git a/internal/options/tag_test.go b/internal/options/tag_test.go new file mode 100644 index 0000000..3da9532 --- /dev/null +++ b/internal/options/tag_test.go @@ -0,0 +1,96 @@ +package options + +import ( + "fmt" + "os" + "testing" + + "gotest.tools/v3/assert" +) + +func TestGetTags(t *testing.T) { + testCases := []struct { + name string + autoTag bool + tags string + ref GitReference + server string + expected []string + }{ + { + name: "no-auto", + tags: "tag1,tag2", + expected: []string{"my/repo:tag1", "my/repo:tag2"}, + ref: GitReference{GitRefHead, "master"}, + }, + { + name: "with-server", + tags: "tag1,tag2", + expected: []string{"server/my/repo:tag1", "server/my/repo:tag2"}, + server: "server", + ref: GitReference{GitRefHead, "master"}, + }, + { + name: "unknown-ref-type", + tags: "tag1,tag2", + expected: []string{"my/repo:tag1", "my/repo:tag2"}, + autoTag: true, + ref: GitReference{GitRefUnknown, "master"}, + }, + { + name: "master-branch", + autoTag: true, + tags: "tag1,tag2", + expected: []string{"my/repo:tag1", "my/repo:tag2", "my/repo:latest"}, + ref: GitReference{GitRefHead, "master"}, + }, + { + name: "different-branch", + autoTag: true, + tags: "tag1,tag2", + expected: []string{"my/repo:tag1", "my/repo:tag2", "my/repo:branch-name"}, + ref: GitReference{GitRefHead, "branch-name"}, + }, + { + name: "pull-request", + autoTag: true, + tags: "tag1,tag2", + expected: []string{"my/repo:tag1", "my/repo:tag2", "my/repo:pr-name"}, + ref: GitReference{GitRefPullRequest, "name"}, + }, + { + name: "tag", + autoTag: true, + tags: "tag1,tag2", + expected: []string{"my/repo:tag1", "my/repo:tag2", "my/repo:v1.0"}, + ref: GitReference{GitRefTag, "v1.0"}, + }, + } + + for _, tc := range testCases { + tc := tc + t.Run(tc.name, func(t *testing.T) { + defer os.Unsetenv("INPUT_TAGS") + defer os.Unsetenv("INPUT_REPOSITORY") + defer os.Unsetenv("INPUT_AUTO_TAG") + _ = os.Setenv("INPUT_TAGS", tc.tags) + _ = os.Setenv("INPUT_REPOSITORY", "my/repo") + _ = os.Setenv("INPUT_AUTO_TAG", fmt.Sprint(tc.autoTag)) + + tags := GetTags( + tc.server, + GitHub{Reference: tc.ref}, + ) + assert.DeepEqual(t, tc.expected, tags) + }) + } +} + +func TestGetTagsWithGitHubRepo(t *testing.T) { + defer os.Unsetenv("INPUT_TAGS") + _ = os.Setenv("INPUT_TAGS", "tag1") + + github := GitHub{Repository: "My/Repo"} + tags := GetTags("", github) + assert.DeepEqual(t, []string{"my/repo:tag1"}, tags) +} From 7c1edfcc92dbc3d19f2f1aec8760e20c69ff8a0f Mon Sep 17 00:00:00 2001 From: Nick Adcock Date: Mon, 24 Feb 2020 17:18:14 +0000 Subject: [PATCH 11/31] Extract server from build and login options Signed-off-by: Nick Adcock --- cmd/build.go | 2 +- cmd/login.go | 2 +- internal/command/args.go | 6 +++--- internal/command/args_test.go | 5 ++--- internal/options/build.go | 1 - internal/options/build_test.go | 2 -- internal/options/login.go | 1 - internal/options/login_test.go | 2 -- internal/options/server.go | 8 ++++++++ internal/options/server_test.go | 16 ++++++++++++++++ 10 files changed, 31 insertions(+), 14 deletions(-) create mode 100644 internal/options/server.go create mode 100644 internal/options/server_test.go diff --git a/cmd/build.go b/cmd/build.go index 7d9778c..c92b3b8 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -16,7 +16,7 @@ func build(cmd command.Runner) error { return err } - tags := options.GetTags(o.Server, github) + tags := options.GetTags(options.GetServer(), github) args := command.BuildArgs(o, github, tags) return cmd.Run("docker", args...) diff --git a/cmd/login.go b/cmd/login.go index 1d06b2e..fec62f1 100644 --- a/cmd/login.go +++ b/cmd/login.go @@ -11,6 +11,6 @@ func login(cmd command.Runner) error { return err } - args := command.LoginArgs(o) + args := command.LoginArgs(o, options.GetServer()) return cmd.Run("docker", args...) } diff --git a/internal/command/args.go b/internal/command/args.go index dbd1d99..8da43d3 100644 --- a/internal/command/args.go +++ b/internal/command/args.go @@ -5,10 +5,10 @@ import ( ) // LoginArgs converts login options into the cli arguments used to call `docker login` -func LoginArgs(o options.Login) []string { +func LoginArgs(o options.Login, server string) []string { args := []string{"login", "--username", o.Username, "--password", o.Password} - if o.Server != "" { - args = append(args, o.Server) + if server != "" { + args = append(args, server) } return args } diff --git a/internal/command/args_test.go b/internal/command/args_test.go index f9cde4d..0885fcb 100644 --- a/internal/command/args_test.go +++ b/internal/command/args_test.go @@ -13,12 +13,11 @@ func TestLoginArgs(t *testing.T) { Username: "username", Password: "password", } - args := LoginArgs(o) + args := LoginArgs(o, "") assert.DeepEqual(t, expected, args) - o.Server = "server" - args = LoginArgs(o) + args = LoginArgs(o, "server") expected = append(expected, "server") assert.DeepEqual(t, expected, args) } diff --git a/internal/options/build.go b/internal/options/build.go index 947198f..9a917d1 100644 --- a/internal/options/build.go +++ b/internal/options/build.go @@ -14,7 +14,6 @@ const githubLabelPrefix = "com.docker.github-actions" type Build struct { Path string `env:"INPUT_PATH"` Dockerfile string `env:"INPUT_DOCKERFILE"` - Server string `env:"INPUT_SERVER"` SetDefaultLabels bool `env:"INPUT_SET_DEFAULT_LABELS"` Target string `env:"INPUT_TARGET"` AlwaysPull bool `env:"INPUT_ALWAYS_PULL"` diff --git a/internal/options/build_test.go b/internal/options/build_test.go index b6509e8..2d34970 100644 --- a/internal/options/build_test.go +++ b/internal/options/build_test.go @@ -10,7 +10,6 @@ import ( func TestGetBuildOptions(t *testing.T) { _ = os.Setenv("INPUT_PATH", "path") _ = os.Setenv("INPUT_DOCKERFILE", "dockerfile") - _ = os.Setenv("INPUT_SERVER", "server") _ = os.Setenv("INPUT_REPOSITORY", "repository") _ = os.Setenv("INPUT_BUILD_ARGS", "buildarg1=b1,buildarg2=b2") _ = os.Setenv("INPUT_LABELS", "label1=l1,label2=l2") @@ -26,7 +25,6 @@ func TestGetBuildOptions(t *testing.T) { assert.DeepEqual(t, Build{ Path: "path", Dockerfile: "dockerfile", - Server: "server", SetDefaultLabels: false, Target: "target", AlwaysPull: true, diff --git a/internal/options/login.go b/internal/options/login.go index bd13d68..20ba418 100644 --- a/internal/options/login.go +++ b/internal/options/login.go @@ -6,7 +6,6 @@ import "github.com/caarlos0/env/v6" type Login struct { Username string `env:"INPUT_USERNAME"` Password string `env:"INPUT_PASSWORD"` - Server string `env:"INPUT_SERVER"` } // GetLoginOptions gets the login action environment variables diff --git a/internal/options/login_test.go b/internal/options/login_test.go index ae36955..772202e 100644 --- a/internal/options/login_test.go +++ b/internal/options/login_test.go @@ -10,11 +10,9 @@ import ( func TestGetLoginOptions(t *testing.T) { _ = os.Setenv("INPUT_USERNAME", "username") _ = os.Setenv("INPUT_PASSWORD", "password") - _ = os.Setenv("INPUT_SERVER", "server") o, err := GetLoginOptions() assert.NilError(t, err) assert.Equal(t, "username", o.Username) assert.Equal(t, "password", o.Password) - assert.Equal(t, "server", o.Server) } diff --git a/internal/options/server.go b/internal/options/server.go new file mode 100644 index 0000000..90d0186 --- /dev/null +++ b/internal/options/server.go @@ -0,0 +1,8 @@ +package options + +import "os" + +// GetServer gets the server from the github actions environment variables +func GetServer() string { + return os.Getenv("INPUT_SERVER") +} diff --git a/internal/options/server_test.go b/internal/options/server_test.go new file mode 100644 index 0000000..8b7463d --- /dev/null +++ b/internal/options/server_test.go @@ -0,0 +1,16 @@ +package options + +import ( + "os" + "testing" + + "gotest.tools/v3/assert" +) + +func TestGetServer(t *testing.T) { + assert.Equal(t, "", GetServer()) + + defer os.Unsetenv("INPUT_SERVER") + _ = os.Setenv("INPUT_SERVER", "server") + assert.Equal(t, "server", GetServer()) +} From 212300f29a76e816b90986bbf9909d4f015518ed Mon Sep 17 00:00:00 2001 From: Nick Adcock Date: Tue, 25 Feb 2020 09:05:28 +0000 Subject: [PATCH 12/31] Add push command Adds push command and tests Signed-off-by: Nick Adcock --- cmd/main.go | 7 +++++ cmd/push.go | 16 ++++++++++ e2e/build_test.go | 2 +- e2e/push_test.go | 48 ++++++++++++++++++++++++++++++ e2e/testdata/push_tests/Dockerfile | 3 ++ e2e/testdata/push_tests/push.env | 8 +++++ internal/command/args.go | 5 ++++ internal/command/args_test.go | 6 ++++ 8 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 cmd/push.go create mode 100644 e2e/push_test.go create mode 100644 e2e/testdata/push_tests/Dockerfile create mode 100644 e2e/testdata/push_tests/push.env diff --git a/cmd/main.go b/cmd/main.go index 5053d21..46e602a 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -36,6 +36,13 @@ func main() { return build(cmd) }, }, + { + Name: "push", + Description: "Pushes a docker image", + Action: func(c *commandLine.Context) error { + return push(cmd) + }, + }, }, } diff --git a/cmd/push.go b/cmd/push.go new file mode 100644 index 0000000..aa6d845 --- /dev/null +++ b/cmd/push.go @@ -0,0 +1,16 @@ +package main + +import ( + "github.com/docker/github-actions/internal/command" + "github.com/docker/github-actions/internal/options" +) + +func push(cmd command.Runner) error { + github, err := options.GetGitHubOptions() + if err != nil { + return err + } + tags := options.GetTags(options.GetServer(), github) + args := command.PushArgs(tags) + return cmd.Run("docker", args...) +} diff --git a/e2e/build_test.go b/e2e/build_test.go index b1bd0f6..c67d637 100644 --- a/e2e/build_test.go +++ b/e2e/build_test.go @@ -7,7 +7,7 @@ import ( "gotest.tools/v3/assert" ) -func TestBuildWithStaticTags(t *testing.T) { +func TestBuild(t *testing.T) { testCases := []struct { name string envFile string diff --git a/e2e/push_test.go b/e2e/push_test.go new file mode 100644 index 0000000..00b9e09 --- /dev/null +++ b/e2e/push_test.go @@ -0,0 +1,48 @@ +package e2e + +import ( + "os/exec" + "testing" + + "gotest.tools/v3/assert" +) + +func TestPush(t *testing.T) { + err := setupLocalRegistry() + assert.NilError(t, err) + defer removeLocalRegistry() + + err = loginLocalRegistry() + assert.NilError(t, err) + + tags := []string{"localhost:5000/my-repository:push-test"} + + err = removeImages(tags) + assert.NilError(t, err) + + err = runActionsCommand("build", "testdata/push_tests/push.env") + assert.NilError(t, err) + + err = runActionsCommand("push", "testdata/push_tests/push.env") + assert.NilError(t, err) + + err = removeImages(tags) + assert.NilError(t, err) + + err = exec.Command("docker", "pull", tags[0]).Run() + defer removeImages(tags) + assert.NilError(t, err) + + result, err := inspectImage(tags[0]) + assert.NilError(t, err) + assert.DeepEqual(t, + inspectResult{ + RepoTags: tags, + Config: inspectResultConfig{ + Labels: map[string]string{ + "a": "a1", + "b": "b1", + }, + }, + }, result) +} diff --git a/e2e/testdata/push_tests/Dockerfile b/e2e/testdata/push_tests/Dockerfile new file mode 100644 index 0000000..c251a4b --- /dev/null +++ b/e2e/testdata/push_tests/Dockerfile @@ -0,0 +1,3 @@ +FROM alpine + +ENTRYPOINT ["echo", "hello-world"] \ No newline at end of file diff --git a/e2e/testdata/push_tests/push.env b/e2e/testdata/push_tests/push.env new file mode 100644 index 0000000..60fa2c8 --- /dev/null +++ b/e2e/testdata/push_tests/push.env @@ -0,0 +1,8 @@ +INPUT_PATH=./testdata/build_tests +INPUT_DOCKERFILE=./testdata/build_tests/Dockerfile +INPUT_AUTO_TAG=false +INPUT_SET_DEFAULT_LABELS=false +INPUT_TAGS=push-test +INPUT_LABELS=a=a1,b=b1 +INPUT_SERVER=localhost:5000 +INPUT_REPOSITORY=my-repository diff --git a/internal/command/args.go b/internal/command/args.go index 8da43d3..572a704 100644 --- a/internal/command/args.go +++ b/internal/command/args.go @@ -48,3 +48,8 @@ func BuildArgs(o options.Build, github options.GitHub, tags []string) []string { } return args } + +// PushArgs converts tags into the cli arguments used to call `docker push` +func PushArgs(tags []string) []string { + return append([]string{"push"}, tags...) +} diff --git a/internal/command/args_test.go b/internal/command/args_test.go index 0885fcb..a618d44 100644 --- a/internal/command/args_test.go +++ b/internal/command/args_test.go @@ -98,3 +98,9 @@ func TestBuildArgs(t *testing.T) { }) } } + +func TestPushArgs(t *testing.T) { + tags := []string{"tag1", "tag2"} + args := PushArgs(tags) + assert.DeepEqual(t, []string{"push", "tag1", "tag2"}, args) +} From f6482be7fed7cc8e1212c72a4df3f8e5ac374f6b Mon Sep 17 00:00:00 2001 From: Nick Adcock Date: Tue, 25 Feb 2020 09:10:22 +0000 Subject: [PATCH 13/31] Build/lint/unit-test in one docker build Signed-off-by: Nick Adcock --- .github/workflows/main.yml | 2 +- docker.Makefile | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index be19a2a..6b51c79 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -16,7 +16,7 @@ jobs: run: cat $GITHUB_EVENT_PATH - name: Build - run: make -f docker.Makefile image lint test-unit + run: make -f docker.Makefile build - name: E2E run: make -f docker.Makefile test-e2e diff --git a/docker.Makefile b/docker.Makefile index e22b9c7..ea3bad8 100644 --- a/docker.Makefile +++ b/docker.Makefile @@ -6,6 +6,9 @@ ROOT_DIR := $(subst .\,,$(ROOT_DIR)) all: image lint test +build: + $(DOCKER_BUILD) -t docker/github-actions-default --build-arg MAKE_TARGET=default . + image: $(DOCKER_BUILD) -t docker/github-actions:$(TAG) --build-arg MAKE_TARGET=build . From cf7a3c067912ec72438fbf4a99d5e36ec8c0124b Mon Sep 17 00:00:00 2001 From: Nick Adcock Date: Tue, 25 Feb 2020 10:10:03 +0000 Subject: [PATCH 14/31] Add build-push command and tests Signed-off-by: Nick Adcock --- cmd/build.go | 6 +- cmd/build_push.go | 36 ++++++++++++ cmd/login.go | 6 +- cmd/main.go | 7 +++ cmd/push.go | 13 ++++- docker.Makefile | 2 +- e2e/build_push_test.go | 61 ++++++++++++++++++++ e2e/testdata/build_push_tests/Dockerfile | 3 + e2e/testdata/build_push_tests/build_push.env | 13 +++++ e2e/testdata/build_tests/Dockerfile | 2 +- e2e/testdata/push_tests/Dockerfile | 2 +- e2e/testdata/push_tests/push.env | 4 +- internal/command/args.go | 4 +- internal/command/args_test.go | 5 +- 14 files changed, 150 insertions(+), 14 deletions(-) create mode 100644 cmd/build_push.go create mode 100644 e2e/build_push_test.go create mode 100644 e2e/testdata/build_push_tests/Dockerfile create mode 100644 e2e/testdata/build_push_tests/build_push.env diff --git a/cmd/build.go b/cmd/build.go index c92b3b8..36cc802 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -18,6 +18,10 @@ func build(cmd command.Runner) error { tags := options.GetTags(options.GetServer(), github) - args := command.BuildArgs(o, github, tags) + return runBuild(cmd, o, github, tags) +} + +func runBuild(cmd command.Runner, opt options.Build, github options.GitHub, tags []string) error { + args := command.BuildArgs(opt, github, tags) return cmd.Run("docker", args...) } diff --git a/cmd/build_push.go b/cmd/build_push.go new file mode 100644 index 0000000..a95b38f --- /dev/null +++ b/cmd/build_push.go @@ -0,0 +1,36 @@ +package main + +import ( + "github.com/docker/github-actions/internal/command" + "github.com/docker/github-actions/internal/options" +) + +func buildPush(cmd command.Runner) error { + github, err := options.GetGitHubOptions() + if err != nil { + return err + } + + server := options.GetServer() + tags := options.GetTags(server, github) + + login, err := options.GetLoginOptions() + if err != nil { + return err + } + if login.Username != "" && login.Password != "" { + if err = runLogin(cmd, login, server); err != nil { + return err + } + } + + build, err := options.GetBuildOptions() + if err != nil { + return err + } + if err = runBuild(cmd, build, github, tags); err != nil { + return err + } + + return runPush(cmd, tags) +} diff --git a/cmd/login.go b/cmd/login.go index fec62f1..b9583c5 100644 --- a/cmd/login.go +++ b/cmd/login.go @@ -11,6 +11,10 @@ func login(cmd command.Runner) error { return err } - args := command.LoginArgs(o, options.GetServer()) + return runLogin(cmd, o, options.GetServer()) +} + +func runLogin(cmd command.Runner, opt options.Login, server string) error { + args := command.LoginArgs(opt, server) return cmd.Run("docker", args...) } diff --git a/cmd/main.go b/cmd/main.go index 46e602a..42ce7cb 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -43,6 +43,13 @@ func main() { return push(cmd) }, }, + { + Name: "build-push", + Description: "Builds and pushes a docker image to a registry, logging in if necessary", + Action: func(c *commandLine.Context) error { + return buildPush(cmd) + }, + }, }, } diff --git a/cmd/push.go b/cmd/push.go index aa6d845..4b4a672 100644 --- a/cmd/push.go +++ b/cmd/push.go @@ -11,6 +11,15 @@ func push(cmd command.Runner) error { return err } tags := options.GetTags(options.GetServer(), github) - args := command.PushArgs(tags) - return cmd.Run("docker", args...) + return runPush(cmd, tags) +} + +func runPush(cmd command.Runner, tags []string) error { + for _, tag := range tags { + args := command.PushArgs(tag) + if err := cmd.Run("docker", args...); err != nil { + return err + } + } + return nil } diff --git a/docker.Makefile b/docker.Makefile index ea3bad8..6410f0e 100644 --- a/docker.Makefile +++ b/docker.Makefile @@ -4,7 +4,7 @@ DOCKER_BUILD = docker build --progress=plain ROOT_DIR = $(dir $(realpath $(firstword $(MAKEFILE_LIST)))) ROOT_DIR := $(subst .\,,$(ROOT_DIR)) -all: image lint test +all: build test-unit build: $(DOCKER_BUILD) -t docker/github-actions-default --build-arg MAKE_TARGET=default . diff --git a/e2e/build_push_test.go b/e2e/build_push_test.go new file mode 100644 index 0000000..546bf52 --- /dev/null +++ b/e2e/build_push_test.go @@ -0,0 +1,61 @@ +package e2e + +import ( + "os/exec" + "sort" + "testing" + + "gotest.tools/v3/assert" +) + +func TestBuildPush(t *testing.T) { + tags := []string{ + "localhost:5000/my-repository:build-push-tag1", + "localhost:5000/my-repository:build-push-test", + } + labels := map[string]string{ + "a": "a1", + "com.docker.github-actions-actor": "actor", + "com.docker.github-actions-sha": "sha", + } + err := removeImages(tags) + assert.NilError(t, err) + defer removeImages(tags) + + err = setupLocalRegistry() + assert.NilError(t, err) + defer removeLocalRegistry() + + err = loginLocalRegistry() + assert.NilError(t, err) + + err = runActionsCommand("build-push", "testdata/build_push_tests/build_push.env") + assert.NilError(t, err) + + for _, tag := range tags { + assertBuildPushImages(t, tag, tags, labels) + } + + err = removeImages(tags) + assert.NilError(t, err) + + for _, tag := range tags { + err = exec.Command("docker", "pull", tag).Run() + assert.NilError(t, err) + } + + for _, tag := range tags { + assertBuildPushImages(t, tag, tags, labels) + } +} + +func assertBuildPushImages(t *testing.T, image string, expectedTags []string, expectedLabels map[string]string) { + inspect, err := inspectImage(image) + assert.NilError(t, err) + + repoTags := inspect.RepoTags + sort.Strings(repoTags) + + assert.DeepEqual(t, expectedTags, repoTags) + assert.DeepEqual(t, expectedLabels, inspect.Config.Labels) +} diff --git a/e2e/testdata/build_push_tests/Dockerfile b/e2e/testdata/build_push_tests/Dockerfile new file mode 100644 index 0000000..44f2edf --- /dev/null +++ b/e2e/testdata/build_push_tests/Dockerfile @@ -0,0 +1,3 @@ +FROM alpine + +ENTRYPOINT ["echo", "hello-world build-push"] \ No newline at end of file diff --git a/e2e/testdata/build_push_tests/build_push.env b/e2e/testdata/build_push_tests/build_push.env new file mode 100644 index 0000000..e46a608 --- /dev/null +++ b/e2e/testdata/build_push_tests/build_push.env @@ -0,0 +1,13 @@ +INPUT_PATH=./testdata/build_push_tests +INPUT_DOCKERFILE=./testdata/build_push_tests/Dockerfile +INPUT_AUTO_TAG=true +INPUT_SET_DEFAULT_LABELS=true +INPUT_TAGS=build-push-test +INPUT_LABELS=a=a1 +INPUT_SERVER=localhost:5000 +INPUT_USERNAME=my_user +INPUT_PASSWORD=my_password +INPUT_REPOSITORY=my-repository +GITHUB_ACTOR=actor +GITHUB_SHA=sha +GITHUB_REF=refs/tags/build-push-tag1 diff --git a/e2e/testdata/build_tests/Dockerfile b/e2e/testdata/build_tests/Dockerfile index c251a4b..62ecf71 100644 --- a/e2e/testdata/build_tests/Dockerfile +++ b/e2e/testdata/build_tests/Dockerfile @@ -1,3 +1,3 @@ FROM alpine -ENTRYPOINT ["echo", "hello-world"] \ No newline at end of file +ENTRYPOINT ["echo", "hello-world build"] \ No newline at end of file diff --git a/e2e/testdata/push_tests/Dockerfile b/e2e/testdata/push_tests/Dockerfile index c251a4b..dfbed4c 100644 --- a/e2e/testdata/push_tests/Dockerfile +++ b/e2e/testdata/push_tests/Dockerfile @@ -1,3 +1,3 @@ FROM alpine -ENTRYPOINT ["echo", "hello-world"] \ No newline at end of file +ENTRYPOINT ["echo", "hello-world push"] \ No newline at end of file diff --git a/e2e/testdata/push_tests/push.env b/e2e/testdata/push_tests/push.env index 60fa2c8..6d4f803 100644 --- a/e2e/testdata/push_tests/push.env +++ b/e2e/testdata/push_tests/push.env @@ -1,5 +1,5 @@ -INPUT_PATH=./testdata/build_tests -INPUT_DOCKERFILE=./testdata/build_tests/Dockerfile +INPUT_PATH=./testdata/push_tests +INPUT_DOCKERFILE=./testdata/push_tests/Dockerfile INPUT_AUTO_TAG=false INPUT_SET_DEFAULT_LABELS=false INPUT_TAGS=push-test diff --git a/internal/command/args.go b/internal/command/args.go index 572a704..71142cb 100644 --- a/internal/command/args.go +++ b/internal/command/args.go @@ -50,6 +50,6 @@ func BuildArgs(o options.Build, github options.GitHub, tags []string) []string { } // PushArgs converts tags into the cli arguments used to call `docker push` -func PushArgs(tags []string) []string { - return append([]string{"push"}, tags...) +func PushArgs(tag string) []string { + return []string{"push", tag} } diff --git a/internal/command/args_test.go b/internal/command/args_test.go index a618d44..b1b6ec1 100644 --- a/internal/command/args_test.go +++ b/internal/command/args_test.go @@ -100,7 +100,6 @@ func TestBuildArgs(t *testing.T) { } func TestPushArgs(t *testing.T) { - tags := []string{"tag1", "tag2"} - args := PushArgs(tags) - assert.DeepEqual(t, []string{"push", "tag1", "tag2"}, args) + args := PushArgs("tag1") + assert.DeepEqual(t, []string{"push", "tag1"}, args) } From 8754d5afa1e3b6c445db3a17e2ef4f13eecf9700 Mon Sep 17 00:00:00 2001 From: Nick Adcock Date: Tue, 25 Feb 2020 12:53:29 +0000 Subject: [PATCH 15/31] try using buildkit Signed-off-by: Nick Adcock --- .github/workflows/main.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 6b51c79..fc1b248 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -16,7 +16,7 @@ jobs: run: cat $GITHUB_EVENT_PATH - name: Build - run: make -f docker.Makefile build + run: DOCKER_BUILDKIT=1 make -f docker.Makefile build - name: E2E - run: make -f docker.Makefile test-e2e + run: DOCKER_BUILDKIT=1 make -f docker.Makefile test-e2e From c6df8c68eb71799f9c9ab4a4a4650d6aabd7e415 Mon Sep 17 00:00:00 2001 From: Nick Adcock Date: Tue, 25 Feb 2020 16:19:13 +0000 Subject: [PATCH 16/31] Add short-sha to tags Signed-off-by: Nick Adcock --- go.sum | 2 ++ internal/options/tag.go | 11 +++++++++++ internal/options/tag_test.go | 27 ++++++++++++++++++++++++++- 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/go.sum b/go.sum index 83a8987..a7979eb 100644 --- a/go.sum +++ b/go.sum @@ -10,6 +10,7 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:ma github.com/davecgh/go-spew v0.0.0-20151105211317-5215b55f46b2/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= 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/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96/go.mod h1:Qh8CwZgvJUkLughtfhJv5dyTYa91l1fOUCrgjqmcifM= github.com/elazarl/goproxy v0.0.0-20170405201442-c4fc26588b6e/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc= @@ -102,6 +103,7 @@ gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWD gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo= gotest.tools/v3 v3.0.2 h1:kG1BFyqVHuQoVQiR1bWGnfz/fmHvvuiSPIV7rvl360E= diff --git a/internal/options/tag.go b/internal/options/tag.go index 60a5ecc..c1d5d8f 100644 --- a/internal/options/tag.go +++ b/internal/options/tag.go @@ -26,6 +26,7 @@ func staticTags() []string { func toFullTag(server, repo, tag string) string { tag = strings.Trim(tag, " ") + tag = strings.ReplaceAll(tag, "/", "-") if server != "" { return fmt.Sprintf("%s/%s:%s", server, repo, tag) } @@ -47,11 +48,21 @@ func GetTags(server string, github GitHub) []string { } else { tags = append(tags, toFullTag(server, repo, github.Reference.Name)) } + tags = appendShortGitShaTag(tags, github, server, repo) case GitRefPullRequest: tags = append(tags, toFullTag(server, repo, fmt.Sprintf("pr-%s", github.Reference.Name))) + tags = appendShortGitShaTag(tags, github, server, repo) case GitRefTag: tags = append(tags, toFullTag(server, repo, github.Reference.Name)) } } return tags } + +func appendShortGitShaTag(tags []string, github GitHub, server, repo string) []string { + if len(github.Sha) >= 7 { + tag := fmt.Sprintf("sha-%s", github.Sha[0:7]) + return append(tags, toFullTag(server, repo, tag)) + } + return tags +} diff --git a/internal/options/tag_test.go b/internal/options/tag_test.go index 3da9532..e54d3a5 100644 --- a/internal/options/tag_test.go +++ b/internal/options/tag_test.go @@ -16,6 +16,7 @@ func TestGetTags(t *testing.T) { ref GitReference server string expected []string + sha string }{ { name: "no-auto", @@ -65,6 +66,30 @@ func TestGetTags(t *testing.T) { expected: []string{"my/repo:tag1", "my/repo:tag2", "my/repo:v1.0"}, ref: GitReference{GitRefTag, "v1.0"}, }, + { + name: "master-branch-with-sha", + autoTag: true, + tags: "tag1,tag2", + expected: []string{"my/repo:tag1", "my/repo:tag2", "my/repo:latest", "my/repo:sha-1234567"}, + ref: GitReference{GitRefHead, "master"}, + sha: "1234567890", + }, + { + name: "pull-request-with-sha", + autoTag: true, + tags: "tag1,tag2", + expected: []string{"my/repo:tag1", "my/repo:tag2", "my/repo:pr-name", "my/repo:sha-1234567"}, + ref: GitReference{GitRefPullRequest, "name"}, + sha: "1234567890", + }, + { + name: "tag-with-sha", + autoTag: true, + tags: "tag1,tag2", + expected: []string{"my/repo:tag1", "my/repo:tag2", "my/repo:v1.0"}, + ref: GitReference{GitRefTag, "v1.0"}, + sha: "1234567890", + }, } for _, tc := range testCases { @@ -79,7 +104,7 @@ func TestGetTags(t *testing.T) { tags := GetTags( tc.server, - GitHub{Reference: tc.ref}, + GitHub{Reference: tc.ref, Sha: tc.sha}, ) assert.DeepEqual(t, tc.expected, tags) }) From 17373acc3d234644ae7d1baa6cdcff113fb7c0f1 Mon Sep 17 00:00:00 2001 From: Nick Adcock Date: Wed, 26 Feb 2020 11:05:48 +0000 Subject: [PATCH 17/31] Updating readme with env vars Signed-off-by: Nick Adcock --- README.md | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 108 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index bfcc33a..d486473 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,120 @@ # github-actions -The core code base for Docker's GitHub Actions (https://github.com/features/actions). This code is used to build the docker/github-actions image that provides the functionality used by the published Docker GitHub Actions +The core code base for Docker's GitHub Actions (https://github.com/features/actions). This code is used to build the docker/github-actions image that provides the functionality used by the published Docker GitHub Actions. +`github-actions` runs a command line tool that shells out to docker to perform the various functions. Parameters are supplied to `github-actions` using environment variables in the form described by the GitHub Actions documentation. `github-actions` uses some of the default GitHub Actions environment variables as described in the individual commands section. + +## Commands + +Commands can be called using `docker run docker/github-actions {command}` + +### login + +Does a `docker login` using the supplied username and password. Will default to Docker Hub but can be supplied a server address to login to a third-party registry as required. + +#### inputs + +|Environment Variable|Required|Description| +|---|---|---| +|INPUT_USERNAME|yes|Username to login with| +|INPUT_PASSWORD|yes|Password to login with| +|INPUT_SERVER|no|Server to login to. Defaults to Docker Hub| + +### build + +Builds and tags a docker image. + +#### inputs + +|Environment Variable|Required|Description| +|---|---|---| +|INPUT_PATH|no|Path to build from. Defaults to `.`| +|INPUT_DOCKERFILE|no|Path to Dockerfile. Defaults to `./Dockerfile`| +|INPUT_SET_DEFAULT_LABELS|no|Adds default labels (see below)| +|INPUT_TARGET|no|Target of chain Dockerfile to build| +|INPUT_BUILD_ARGS|no|Comma-delimited list of build-args| +|INPUT_LABELS|no|Comma-delimited list of labels| + +See the tagging section for information on tag inputs + + +### push + +Pushs a docker image. + +#### inputs + +See the tagging section for information on tag inputs + + +## Tagging + +Tagging of images can be set manually, left to `github-actions` to automate, or a combination of the both. + +There are 4 input variables used for tagging + +|Environment Variable|Required|Description| +|---|---|---| +|INPUT_SERVER|no|Server to tag with| +|INPUT_REPOSITORY|yes|Repository to tag with| +|INPUT_TAGS|no|Hard coded comma-delimited list of tags| +|INPUT_AUTO_TAG|no|If true then `github-actions` will add tags automatically as described below| + +If `INPUT_SERVER` is set then all tags are prefixed with `{INPUT_SERVER}/{INPUT_REPOSITORY}:`. +If not then all tags are prefixed with `{INPUT_REPOSITORY}:` + +Auto tags depend on the git reference that the run is associated with. The reference is passed to `github-actions` using the default `GITHUB_REF` enviroment variable. + +If the reference is `refs/heads/{branch-name}` then 2 tags are added: `{branch-name}` and `{git-short-sha}`. For the master branch the `{branch-name}` is replaced with `latest`. + +If the reference is `refs/pull-requests/{pr}` then 2 tags are added: `pr-{pr}` and `{git-short-sha}`. + +If the reference is `refs/tags/{tag-name}` then 1 tag is added: `{tag-name}`. + +Any `/` in the auto tags are replaced with `-`. + +For example if the environment variables are as follows: + +|Variable|Value| +|---|---| +|INPUT_SERVER|| +|INPUT_REPOSITORY|myorg/myimage| +|INPUT_TAGS|foo,bar| +|INPUT_AUTO_TAG|true| +|GITHUB_REF|refs/tags/v0.1| + +Then the image will be tagged with: +``` +myorg/myimage:foo +myorg/myimage:bar +myorg/myimage:v0.1 +``` + +If the variables are as follows: + +|Variable|Value| +|---|---| +|INPUT_SERVER|myserver| +|INPUT_REPOSITORY|myorg/myimage| +|INPUT_TAGS|foo,bar| +|INPUT_AUTO_TAG|true| +|GITHUB_REF|refs/heads/master| +|GITHUB_SHA|c6df8c68eb71799f9c9ab4a4a4650d6aabd7e415| + +Then the image will be tagged with: +``` +myserver/myorg/myimage:foo +myserver/myorg/myimage:bar +myserver/myorg/myimage:lastest +myserver/myorg/myimage:c6df8c6 +``` ## Building github-actions The code is written in Go v1.13 with `go mod`. It can be built locally using the `Makefile` or in docker using the `docker.Makefile`. `make -f docker.Makefile` will build the code, check the linting using golangci-lint, run the go tests, and build the image with a tag of docker/github-actions:latest -`make -f docker.Makefile TAG=foo` will build the code, check the linting using golangci-lint, run the go tests, and build the image with a tag of docker/github-actions:foo - `make -f docker.Makefile image` will build the github-actions image without a tag and without running test or lint checking `make -f docker.Makefile cli` will build the cli and copy it to `./bin/github-actions` -`make -f docker.Makefile test` will run the go tests +`make -f docker.Makefile test` will run the unit and e2e tests From 09a935f5895d80a30d1ca3300d7d8dd28dc05774 Mon Sep 17 00:00:00 2001 From: Nick Adcock Date: Wed, 26 Feb 2020 11:09:26 +0000 Subject: [PATCH 18/31] Refactor command running Signed-off-by: Nick Adcock --- cmd/build.go | 7 +------ cmd/build_push.go | 6 +++--- cmd/login.go | 7 +------ cmd/push.go | 12 +----------- internal/command/runner.go | 25 +++++++++++++++++++++++++ 5 files changed, 31 insertions(+), 26 deletions(-) diff --git a/cmd/build.go b/cmd/build.go index 36cc802..c9143d9 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -18,10 +18,5 @@ func build(cmd command.Runner) error { tags := options.GetTags(options.GetServer(), github) - return runBuild(cmd, o, github, tags) -} - -func runBuild(cmd command.Runner, opt options.Build, github options.GitHub, tags []string) error { - args := command.BuildArgs(opt, github, tags) - return cmd.Run("docker", args...) + return command.RunBuild(cmd, o, github, tags) } diff --git a/cmd/build_push.go b/cmd/build_push.go index a95b38f..885d20c 100644 --- a/cmd/build_push.go +++ b/cmd/build_push.go @@ -19,7 +19,7 @@ func buildPush(cmd command.Runner) error { return err } if login.Username != "" && login.Password != "" { - if err = runLogin(cmd, login, server); err != nil { + if err = command.RunLogin(cmd, login, server); err != nil { return err } } @@ -28,9 +28,9 @@ func buildPush(cmd command.Runner) error { if err != nil { return err } - if err = runBuild(cmd, build, github, tags); err != nil { + if err = command.RunBuild(cmd, build, github, tags); err != nil { return err } - return runPush(cmd, tags) + return command.RunPush(cmd, tags) } diff --git a/cmd/login.go b/cmd/login.go index b9583c5..ab9951d 100644 --- a/cmd/login.go +++ b/cmd/login.go @@ -11,10 +11,5 @@ func login(cmd command.Runner) error { return err } - return runLogin(cmd, o, options.GetServer()) -} - -func runLogin(cmd command.Runner, opt options.Login, server string) error { - args := command.LoginArgs(opt, server) - return cmd.Run("docker", args...) + return command.RunLogin(cmd, o, options.GetServer()) } diff --git a/cmd/push.go b/cmd/push.go index 4b4a672..65d36e3 100644 --- a/cmd/push.go +++ b/cmd/push.go @@ -11,15 +11,5 @@ func push(cmd command.Runner) error { return err } tags := options.GetTags(options.GetServer(), github) - return runPush(cmd, tags) -} - -func runPush(cmd command.Runner, tags []string) error { - for _, tag := range tags { - args := command.PushArgs(tag) - if err := cmd.Run("docker", args...); err != nil { - return err - } - } - return nil + return command.RunPush(cmd, tags) } diff --git a/internal/command/runner.go b/internal/command/runner.go index fe64947..bb32d6a 100644 --- a/internal/command/runner.go +++ b/internal/command/runner.go @@ -3,6 +3,8 @@ package command import ( "os" "os/exec" + + "github.com/docker/github-actions/internal/options" ) // Runner executes standard commands @@ -25,3 +27,26 @@ func (runner execRunner) Run(name string, args ...string) error { cmd.Stderr = os.Stderr return cmd.Run() } + +// RunLogin runs a docker login +func RunLogin(cmd Runner, opt options.Login, server string) error { + args := LoginArgs(opt, server) + return cmd.Run("docker", args...) +} + +// RunBuild runs a docker build and tags the resulting image +func RunBuild(cmd Runner, opt options.Build, github options.GitHub, tags []string) error { + args := BuildArgs(opt, github, tags) + return cmd.Run("docker", args...) +} + +// RunPush runs a docker push for each tag +func RunPush(cmd Runner, tags []string) error { + for _, tag := range tags { + args := PushArgs(tag) + if err := cmd.Run("docker", args...); err != nil { + return err + } + } + return nil +} From 8c0a723ac188c9260a5715b88e35cd93001d09a5 Mon Sep 17 00:00:00 2001 From: Nick Adcock Date: Thu, 27 Feb 2020 13:48:43 +0000 Subject: [PATCH 19/31] Add push filter Signed-off-by: Nick Adcock --- README.md | 15 +++++++- cmd/build_push.go | 22 ++++++----- e2e/testdata/build_push_tests/build_push.env | 1 + internal/options/push.go | 12 ++++++ internal/options/push_test.go | 40 ++++++++++++++++++++ 5 files changed, 79 insertions(+), 11 deletions(-) create mode 100644 internal/options/push.go create mode 100644 internal/options/push_test.go diff --git a/README.md b/README.md index d486473..ee22330 100644 --- a/README.md +++ b/README.md @@ -39,13 +39,26 @@ See the tagging section for information on tag inputs ### push -Pushs a docker image. +Pushes a docker image. #### inputs See the tagging section for information on tag inputs +### build-push + +Builds, logs in, and pushes a docker image. + +#### inputs + +Same as the login and build commands with the addition of + +|Environment Variable|Required|Description| +|---|---|---| +|INPUT_PUSH|no|Will push the image if true| + + ## Tagging Tagging of images can be set manually, left to `github-actions` to automate, or a combination of the both. diff --git a/cmd/build_push.go b/cmd/build_push.go index 885d20c..63c9fb4 100644 --- a/cmd/build_push.go +++ b/cmd/build_push.go @@ -14,16 +14,6 @@ func buildPush(cmd command.Runner) error { server := options.GetServer() tags := options.GetTags(server, github) - login, err := options.GetLoginOptions() - if err != nil { - return err - } - if login.Username != "" && login.Password != "" { - if err = command.RunLogin(cmd, login, server); err != nil { - return err - } - } - build, err := options.GetBuildOptions() if err != nil { return err @@ -32,5 +22,17 @@ func buildPush(cmd command.Runner) error { return err } + if options.ShouldPush() { + login, err := options.GetLoginOptions() + if err != nil { + return err + } + if login.Username != "" && login.Password != "" { + if err = command.RunLogin(cmd, login, server); err != nil { + return err + } + } + } + return command.RunPush(cmd, tags) } diff --git a/e2e/testdata/build_push_tests/build_push.env b/e2e/testdata/build_push_tests/build_push.env index e46a608..669f621 100644 --- a/e2e/testdata/build_push_tests/build_push.env +++ b/e2e/testdata/build_push_tests/build_push.env @@ -8,6 +8,7 @@ INPUT_SERVER=localhost:5000 INPUT_USERNAME=my_user INPUT_PASSWORD=my_password INPUT_REPOSITORY=my-repository +INPUT_PUSH=true GITHUB_ACTOR=actor GITHUB_SHA=sha GITHUB_REF=refs/tags/build-push-tag1 diff --git a/internal/options/push.go b/internal/options/push.go new file mode 100644 index 0000000..785434d --- /dev/null +++ b/internal/options/push.go @@ -0,0 +1,12 @@ +package options + +import ( + "os" + "strconv" +) + +// ShouldPush returns true if the user has signalled a docker push should be performed. Defaults to true +func ShouldPush() bool { + b, err := strconv.ParseBool(os.Getenv("INPUT_PUSH")) + return err == nil && b +} diff --git a/internal/options/push_test.go b/internal/options/push_test.go new file mode 100644 index 0000000..0705ec9 --- /dev/null +++ b/internal/options/push_test.go @@ -0,0 +1,40 @@ +package options + +import ( + "os" + "testing" + + "gotest.tools/v3/assert" +) + +func TestShouldPush(t *testing.T) { + testCases := []struct { + name string + input string + expected bool + }{ + { + name: "empty", + input: "", + expected: false, + }, + { + name: "true", + input: "true", + expected: true, + }, + { + name: "false", + input: "false", + expected: false, + }, + } + for _, tc := range testCases { + tc := tc + t.Run(tc.name, func(t *testing.T) { + _ = os.Setenv("INPUT_PUSH", tc.input) + defer os.Unsetenv("INPUT_PUSH") + assert.Equal(t, tc.expected, ShouldPush()) + }) + } +} From 4cc816e3562da9f76d16dc46c3b8119ece0a1e49 Mon Sep 17 00:00:00 2001 From: Nick Adcock Date: Tue, 3 Mar 2020 11:44:42 +0000 Subject: [PATCH 20/31] Split tags for sha and ref Split auto-tagging to enable tagging with sha and tagging with ref separately. The action definition will handle the default settings. Also rename SetDefaultLabels to AddGitLabels Signed-off-by: Nick Adcock --- README.md | 22 ++-- e2e/build_test.go | 8 +- e2e/testdata/build_push_tests/build_push.env | 4 +- e2e/testdata/build_tests/auto_labels.env | 4 +- e2e/testdata/build_tests/static_labels.env | 4 +- e2e/testdata/build_tests/static_tags.env | 4 +- .../{auto_tags_branch.env => tag_branch.env} | 4 +- .../{auto_tags_master.env => tag_master.env} | 4 +- .../{auto_tags_pr.env => tag_pr.env} | 4 +- .../{auto_tags_tag.env => tag_tag.env} | 4 +- e2e/testdata/push_tests/push.env | 4 +- internal/command/args.go | 7 +- internal/command/args_test.go | 18 +-- internal/options/build.go | 18 +-- internal/options/build_test.go | 40 +++---- internal/options/push.go | 2 +- internal/options/tag.go | 32 ++++-- internal/options/tag_test.go | 108 +++++++++--------- 18 files changed, 152 insertions(+), 139 deletions(-) rename e2e/testdata/build_tests/{auto_tags_branch.env => tag_branch.env} (79%) rename e2e/testdata/build_tests/{auto_tags_master.env => tag_master.env} (79%) rename e2e/testdata/build_tests/{auto_tags_pr.env => tag_pr.env} (76%) rename e2e/testdata/build_tests/{auto_tags_tag.env => tag_tag.env} (76%) diff --git a/README.md b/README.md index ee22330..57c81f8 100644 --- a/README.md +++ b/README.md @@ -27,9 +27,9 @@ Builds and tags a docker image. |Environment Variable|Required|Description| |---|---|---| -|INPUT_PATH|no|Path to build from. Defaults to `.`| -|INPUT_DOCKERFILE|no|Path to Dockerfile. Defaults to `./Dockerfile`| -|INPUT_SET_DEFAULT_LABELS|no|Adds default labels (see below)| +|INPUT_PATH|yes|Path to build from| +|INPUT_DOCKERFILE|no|Path to Dockerfile| +|INPUT_ADD_GIT_LABELS|no|Adds git labels (see below)| |INPUT_TARGET|no|Target of chain Dockerfile to build| |INPUT_BUILD_ARGS|no|Comma-delimited list of build-args| |INPUT_LABELS|no|Comma-delimited list of labels| @@ -70,18 +70,19 @@ There are 4 input variables used for tagging |INPUT_SERVER|no|Server to tag with| |INPUT_REPOSITORY|yes|Repository to tag with| |INPUT_TAGS|no|Hard coded comma-delimited list of tags| -|INPUT_AUTO_TAG|no|If true then `github-actions` will add tags automatically as described below| +|INPUT_TAG_WITH_REF|no|If true then `github-actions` will add tags depending on the git ref automatically as described below| +|INPUT_TAG_WITH_SHA|no|If true then `github-actions` will add a tag in the form `sha-{git-short-sha}`| If `INPUT_SERVER` is set then all tags are prefixed with `{INPUT_SERVER}/{INPUT_REPOSITORY}:`. If not then all tags are prefixed with `{INPUT_REPOSITORY}:` -Auto tags depend on the git reference that the run is associated with. The reference is passed to `github-actions` using the default `GITHUB_REF` enviroment variable. +Auto tags depend on the git reference that the run is associated with. The reference is passed to `github-actions` using the GitHub actions `GITHUB_REF` enviroment variable. -If the reference is `refs/heads/{branch-name}` then 2 tags are added: `{branch-name}` and `{git-short-sha}`. For the master branch the `{branch-name}` is replaced with `latest`. +If the reference is `refs/heads/{branch-name}` then the tag `{branch-name}` is added. For the master branch the `{branch-name}` is replaced with `latest`. -If the reference is `refs/pull-requests/{pr}` then 2 tags are added: `pr-{pr}` and `{git-short-sha}`. +If the reference is `refs/pull-requests/{pr}` then the tag `pr-{pr}` is added. -If the reference is `refs/tags/{tag-name}` then 1 tag is added: `{tag-name}`. +If the reference is `refs/tags/{tag-name}` then the tag `{tag-name}` is added. Any `/` in the auto tags are replaced with `-`. @@ -92,7 +93,7 @@ For example if the environment variables are as follows: |INPUT_SERVER|| |INPUT_REPOSITORY|myorg/myimage| |INPUT_TAGS|foo,bar| -|INPUT_AUTO_TAG|true| +|INPUT_TAG_WITH_REF|true| |GITHUB_REF|refs/tags/v0.1| Then the image will be tagged with: @@ -109,7 +110,8 @@ If the variables are as follows: |INPUT_SERVER|myserver| |INPUT_REPOSITORY|myorg/myimage| |INPUT_TAGS|foo,bar| -|INPUT_AUTO_TAG|true| +|INPUT_TAG_WITH_REF|true| +|INPUT_TAG_WITH_SHA|true| |GITHUB_REF|refs/heads/master| |GITHUB_SHA|c6df8c68eb71799f9c9ab4a4a4650d6aabd7e415| diff --git a/e2e/build_test.go b/e2e/build_test.go index c67d637..3305088 100644 --- a/e2e/build_test.go +++ b/e2e/build_test.go @@ -47,7 +47,7 @@ func TestBuild(t *testing.T) { }, { name: "auto-tags-master", - envFile: "testdata/build_tests/auto_tags_master.env", + envFile: "testdata/build_tests/tag_master.env", expectedTags: []string{ "localhost:5000/my-repository:auto-tags-master", "localhost:5000/my-repository:latest", @@ -55,7 +55,7 @@ func TestBuild(t *testing.T) { }, { name: "auto-tags-branch", - envFile: "testdata/build_tests/auto_tags_branch.env", + envFile: "testdata/build_tests/tag_branch.env", expectedTags: []string{ "localhost:5000/my-repository:auto-tags-branch", "localhost:5000/my-repository:branch", @@ -63,14 +63,14 @@ func TestBuild(t *testing.T) { }, { name: "auto-tags-pr", - envFile: "testdata/build_tests/auto_tags_pr.env", + envFile: "testdata/build_tests/tag_pr.env", expectedTags: []string{ "localhost:5000/my-repository:pr-pr1", }, }, { name: "auto-tags-tag", - envFile: "testdata/build_tests/auto_tags_tag.env", + envFile: "testdata/build_tests/tag_tag.env", expectedTags: []string{ "localhost:5000/my-repository:tag1", }, diff --git a/e2e/testdata/build_push_tests/build_push.env b/e2e/testdata/build_push_tests/build_push.env index 669f621..44d7425 100644 --- a/e2e/testdata/build_push_tests/build_push.env +++ b/e2e/testdata/build_push_tests/build_push.env @@ -1,7 +1,7 @@ INPUT_PATH=./testdata/build_push_tests INPUT_DOCKERFILE=./testdata/build_push_tests/Dockerfile -INPUT_AUTO_TAG=true -INPUT_SET_DEFAULT_LABELS=true +INPUT_TAG_WITH_REF=true +INPUT_ADD_GIT_LABELS=true INPUT_TAGS=build-push-test INPUT_LABELS=a=a1 INPUT_SERVER=localhost:5000 diff --git a/e2e/testdata/build_tests/auto_labels.env b/e2e/testdata/build_tests/auto_labels.env index b2215c5..cb44346 100644 --- a/e2e/testdata/build_tests/auto_labels.env +++ b/e2e/testdata/build_tests/auto_labels.env @@ -1,7 +1,7 @@ INPUT_PATH=./testdata/build_tests INPUT_DOCKERFILE=./testdata/build_tests/Dockerfile -INPUT_AUTO_TAG=false -INPUT_SET_DEFAULT_LABELS=true +INPUT_TAG_WITH_REF=false +INPUT_ADD_GIT_LABELS=true INPUT_TAGS=auto-labels INPUT_LABELS=a=a1 INPUT_SERVER=localhost:5000 diff --git a/e2e/testdata/build_tests/static_labels.env b/e2e/testdata/build_tests/static_labels.env index 8c5c04f..0051da9 100644 --- a/e2e/testdata/build_tests/static_labels.env +++ b/e2e/testdata/build_tests/static_labels.env @@ -1,7 +1,7 @@ INPUT_PATH=./testdata/build_tests INPUT_DOCKERFILE=./testdata/build_tests/Dockerfile -INPUT_AUTO_TAG=false -INPUT_SET_DEFAULT_LABELS=false +INPUT_TAG_WITH_REF=false +INPUT_ADD_GIT_LABELS=false INPUT_TAGS=static-labels INPUT_LABELS=a=a1,b=b1 INPUT_SERVER=localhost:5000 diff --git a/e2e/testdata/build_tests/static_tags.env b/e2e/testdata/build_tests/static_tags.env index 05096b7..1cc8b0d 100644 --- a/e2e/testdata/build_tests/static_tags.env +++ b/e2e/testdata/build_tests/static_tags.env @@ -1,7 +1,7 @@ INPUT_PATH=./testdata/build_tests INPUT_DOCKERFILE=./testdata/build_tests/Dockerfile -INPUT_AUTO_TAG=false -INPUT_SET_DEFAULT_LABELS=false +INPUT_TAG_WITH_REF=false +INPUT_ADD_GIT_LABELS=false INPUT_TAGS=v1-static-tags,v1.1-static-tags INPUT_SERVER=localhost:5000 INPUT_REPOSITORY=my-repository diff --git a/e2e/testdata/build_tests/auto_tags_branch.env b/e2e/testdata/build_tests/tag_branch.env similarity index 79% rename from e2e/testdata/build_tests/auto_tags_branch.env rename to e2e/testdata/build_tests/tag_branch.env index e1fc256..e0ddf4e 100644 --- a/e2e/testdata/build_tests/auto_tags_branch.env +++ b/e2e/testdata/build_tests/tag_branch.env @@ -1,7 +1,7 @@ INPUT_PATH=./testdata/build_tests INPUT_DOCKERFILE=./testdata/build_tests/Dockerfile -INPUT_AUTO_TAG=true -INPUT_SET_DEFAULT_LABELS=false +INPUT_TAG_WITH_REF=true +INPUT_ADD_GIT_LABELS=false INPUT_TAGS=auto-tags-branch INPUT_SERVER=localhost:5000 INPUT_REPOSITORY=my-repository diff --git a/e2e/testdata/build_tests/auto_tags_master.env b/e2e/testdata/build_tests/tag_master.env similarity index 79% rename from e2e/testdata/build_tests/auto_tags_master.env rename to e2e/testdata/build_tests/tag_master.env index e7cda5d..2a1e635 100644 --- a/e2e/testdata/build_tests/auto_tags_master.env +++ b/e2e/testdata/build_tests/tag_master.env @@ -1,7 +1,7 @@ INPUT_PATH=./testdata/build_tests INPUT_DOCKERFILE=./testdata/build_tests/Dockerfile -INPUT_AUTO_TAG=true -INPUT_SET_DEFAULT_LABELS=false +INPUT_TAG_WITH_REF=true +INPUT_ADD_GIT_LABELS=false INPUT_TAGS=auto-tags-master INPUT_SERVER=localhost:5000 INPUT_REPOSITORY=my-repository diff --git a/e2e/testdata/build_tests/auto_tags_pr.env b/e2e/testdata/build_tests/tag_pr.env similarity index 76% rename from e2e/testdata/build_tests/auto_tags_pr.env rename to e2e/testdata/build_tests/tag_pr.env index 4f2ff27..373b45a 100644 --- a/e2e/testdata/build_tests/auto_tags_pr.env +++ b/e2e/testdata/build_tests/tag_pr.env @@ -1,7 +1,7 @@ INPUT_PATH=./testdata/build_tests INPUT_DOCKERFILE=./testdata/build_tests/Dockerfile -INPUT_AUTO_TAG=true -INPUT_SET_DEFAULT_LABELS=false +INPUT_TAG_WITH_REF=true +INPUT_ADD_GIT_LABELS=false INPUT_SERVER=localhost:5000 INPUT_REPOSITORY=my-repository GITHUB_REF=refs/pulls/pr1 diff --git a/e2e/testdata/build_tests/auto_tags_tag.env b/e2e/testdata/build_tests/tag_tag.env similarity index 76% rename from e2e/testdata/build_tests/auto_tags_tag.env rename to e2e/testdata/build_tests/tag_tag.env index bbaca31..427888c 100644 --- a/e2e/testdata/build_tests/auto_tags_tag.env +++ b/e2e/testdata/build_tests/tag_tag.env @@ -1,7 +1,7 @@ INPUT_PATH=./testdata/build_tests INPUT_DOCKERFILE=./testdata/build_tests/Dockerfile -INPUT_AUTO_TAG=true -INPUT_SET_DEFAULT_LABELS=false +INPUT_TAG_WITH_REF=true +INPUT_ADD_GIT_LABELS=false INPUT_SERVER=localhost:5000 INPUT_REPOSITORY=my-repository GITHUB_REF=refs/tags/tag1 diff --git a/e2e/testdata/push_tests/push.env b/e2e/testdata/push_tests/push.env index 6d4f803..87f0a18 100644 --- a/e2e/testdata/push_tests/push.env +++ b/e2e/testdata/push_tests/push.env @@ -1,7 +1,7 @@ INPUT_PATH=./testdata/push_tests INPUT_DOCKERFILE=./testdata/push_tests/Dockerfile -INPUT_AUTO_TAG=false -INPUT_SET_DEFAULT_LABELS=false +INPUT_TAG_WITH_REF=false +INPUT_ADD_GIT_LABELS=false INPUT_TAGS=push-test INPUT_LABELS=a=a1,b=b1 INPUT_SERVER=localhost:5000 diff --git a/internal/command/args.go b/internal/command/args.go index 71142cb..e6ec040 100644 --- a/internal/command/args.go +++ b/internal/command/args.go @@ -41,12 +41,7 @@ func BuildArgs(o options.Build, github options.GitHub, tags []string) []string { args = append(args, "--build-arg", buildArg) } - if o.Path == "" { - args = append(args, ".") - } else { - args = append(args, o.Path) - } - return args + return append(args, o.Path) } // PushArgs converts tags into the cli arguments used to call `docker push` diff --git a/internal/command/args_test.go b/internal/command/args_test.go index b1b6ec1..c09267d 100644 --- a/internal/command/args_test.go +++ b/internal/command/args_test.go @@ -32,35 +32,34 @@ func TestBuildArgs(t *testing.T) { }{ { name: "basic", - expected: []string{"build", "."}, - }, - { - name: "with-path", build: options.Build{Path: "path"}, expected: []string{"build", "path"}, }, { name: "with-dockerfile", - build: options.Build{Dockerfile: "dockerfile"}, + build: options.Build{Path: ".", Dockerfile: "dockerfile"}, expected: []string{"build", "--file", "dockerfile", "."}, }, { name: "with-tags", + build: options.Build{Path: "."}, tags: []string{"tag1", "tag2"}, expected: []string{"build", "-t", "tag1", "-t", "tag2", "."}, }, { name: "with-static-labels", build: options.Build{ + Path: ".", Labels: []string{"label1", "label2"}, }, expected: []string{"build", "--label", "label1", "--label", "label2", "."}, }, { - name: "with-default-labels", + name: "with-git-labels", build: options.Build{ - SetDefaultLabels: true, - Labels: []string{"label1"}, + Path: ".", + AddGitLabels: true, + Labels: []string{"label1"}, }, github: options.GitHub{ Actor: "actor", @@ -71,6 +70,7 @@ func TestBuildArgs(t *testing.T) { { name: "with-target", build: options.Build{ + Path: ".", Target: "target", }, expected: []string{"build", "--target", "target", "."}, @@ -78,6 +78,7 @@ func TestBuildArgs(t *testing.T) { { name: "with-always-pull", build: options.Build{ + Path: ".", AlwaysPull: true, }, expected: []string{"build", "--pull", "."}, @@ -85,6 +86,7 @@ func TestBuildArgs(t *testing.T) { { name: "with-build-args", build: options.Build{ + Path: ".", BuildArgs: []string{"build-arg-1", "build-arg-2"}, }, expected: []string{"build", "--build-arg", "build-arg-1", "--build-arg", "build-arg-2", "."}, diff --git a/internal/options/build.go b/internal/options/build.go index 9a917d1..7d634e0 100644 --- a/internal/options/build.go +++ b/internal/options/build.go @@ -12,13 +12,13 @@ const githubLabelPrefix = "com.docker.github-actions" // Build contains the parsed build action environment variables type Build struct { - Path string `env:"INPUT_PATH"` - Dockerfile string `env:"INPUT_DOCKERFILE"` - SetDefaultLabels bool `env:"INPUT_SET_DEFAULT_LABELS"` - Target string `env:"INPUT_TARGET"` - AlwaysPull bool `env:"INPUT_ALWAYS_PULL"` - BuildArgs []string - Labels []string + Path string `env:"INPUT_PATH"` + Dockerfile string `env:"INPUT_DOCKERFILE"` + AddGitLabels bool `env:"INPUT_ADD_GIT_LABELS"` + Target string `env:"INPUT_TARGET"` + AlwaysPull bool `env:"INPUT_ALWAYS_PULL"` + BuildArgs []string + Labels []string } // GetBuildOptions gets the login action environment variables @@ -39,14 +39,14 @@ func GetBuildOptions() (Build, error) { return build, nil } -// GetLabels gets a list of all labels to build the image with including automatic labels created from github vars when SetDefaultLabels is true +// GetLabels gets a list of all labels to build the image with including automatic labels created from github vars when AddGitLabels is true func GetLabels(build Build, github GitHub) []string { labels := []string{} if build.Labels != nil { labels = build.Labels } - if !build.SetDefaultLabels { + if !build.AddGitLabels { return labels } diff --git a/internal/options/build_test.go b/internal/options/build_test.go index 2d34970..b5a133a 100644 --- a/internal/options/build_test.go +++ b/internal/options/build_test.go @@ -13,43 +13,41 @@ func TestGetBuildOptions(t *testing.T) { _ = os.Setenv("INPUT_REPOSITORY", "repository") _ = os.Setenv("INPUT_BUILD_ARGS", "buildarg1=b1,buildarg2=b2") _ = os.Setenv("INPUT_LABELS", "label1=l1,label2=l2") - _ = os.Setenv("INPUT_AUTO_TAG", "false") - _ = os.Setenv("INPUT_SET_DEFAULT_LABELS", "false") + _ = os.Setenv("INPUT_ADD_GIT_LABELS", "false") _ = os.Setenv("INPUT_TARGET", "target") _ = os.Setenv("INPUT_ALWAYS_PULL", "true") - _ = os.Setenv("INPUT_TAGS", "tag1,tag2") o, err := GetBuildOptions() assert.NilError(t, err) assert.DeepEqual(t, Build{ - Path: "path", - Dockerfile: "dockerfile", - SetDefaultLabels: false, - Target: "target", - AlwaysPull: true, - BuildArgs: []string{"buildarg1=b1", "buildarg2=b2"}, - Labels: []string{"label1=l1", "label2=l2"}, + Path: "path", + Dockerfile: "dockerfile", + AddGitLabels: false, + Target: "target", + AlwaysPull: true, + BuildArgs: []string{"buildarg1=b1", "buildarg2=b2"}, + Labels: []string{"label1=l1", "label2=l2"}, }, o) } func TestGetLabels(t *testing.T) { testCases := []struct { - name string - setDefault bool - labels []string - github GitHub - expected []string + name string + addGitLabels bool + labels []string + github GitHub + expected []string }{ { - name: "no-defaults", + name: "no-git-labels", labels: []string{"label1", "label2"}, expected: []string{"label1", "label2"}, }, { - name: "with-defaults", - labels: []string{"label1", "label2"}, - setDefault: true, + name: "with-git-labels", + labels: []string{"label1", "label2"}, + addGitLabels: true, github: GitHub{ Actor: "actor", Sha: "sha", @@ -68,8 +66,8 @@ func TestGetLabels(t *testing.T) { t.Run(tc.name, func(t *testing.T) { labels := GetLabels( Build{ - SetDefaultLabels: tc.setDefault, - Labels: tc.labels, + AddGitLabels: tc.addGitLabels, + Labels: tc.labels, }, tc.github, ) diff --git a/internal/options/push.go b/internal/options/push.go index 785434d..a573374 100644 --- a/internal/options/push.go +++ b/internal/options/push.go @@ -5,7 +5,7 @@ import ( "strconv" ) -// ShouldPush returns true if the user has signalled a docker push should be performed. Defaults to true +// ShouldPush returns true if the user has signalled a docker push should be performed func ShouldPush() bool { b, err := strconv.ParseBool(os.Getenv("INPUT_PUSH")) return err == nil && b diff --git a/internal/options/tag.go b/internal/options/tag.go index c1d5d8f..f0a5b69 100644 --- a/internal/options/tag.go +++ b/internal/options/tag.go @@ -3,11 +3,18 @@ package options import ( "fmt" "os" + "strconv" "strings" ) -func autoTag() bool { - return os.Getenv("INPUT_AUTO_TAG") == "true" +func tagWithRef() bool { + b, err := strconv.ParseBool(os.Getenv("INPUT_TAG_WITH_REF")) + return err == nil && b +} + +func tagWithSha() bool { + b, err := strconv.ParseBool(os.Getenv("INPUT_TAG_WITH_SHA")) + return err == nil && b } func dockerRepo(github GitHub) string { @@ -25,7 +32,7 @@ func staticTags() []string { } func toFullTag(server, repo, tag string) string { - tag = strings.Trim(tag, " ") + tag = strings.TrimSpace(tag) tag = strings.ReplaceAll(tag, "/", "-") if server != "" { return fmt.Sprintf("%s/%s:%s", server, repo, tag) @@ -40,22 +47,24 @@ func GetTags(server string, github GitHub) []string { for _, t := range staticTags() { tags = append(tags, toFullTag(server, repo, t)) } - if autoTag() { + if tagWithRef() { switch github.Reference.Type { case GitRefHead: if github.Reference.Name == "master" { tags = append(tags, toFullTag(server, repo, "latest")) } else { - tags = append(tags, toFullTag(server, repo, github.Reference.Name)) + tags = appendGitRefTag(tags, server, repo, github.Reference.Name) } - tags = appendShortGitShaTag(tags, github, server, repo) case GitRefPullRequest: - tags = append(tags, toFullTag(server, repo, fmt.Sprintf("pr-%s", github.Reference.Name))) - tags = appendShortGitShaTag(tags, github, server, repo) + tags = appendGitRefTag(tags, server, repo, fmt.Sprintf("pr-%s", github.Reference.Name)) + case GitRefTag: - tags = append(tags, toFullTag(server, repo, github.Reference.Name)) + tags = appendGitRefTag(tags, server, repo, github.Reference.Name) } } + if tagWithSha() { + tags = appendShortGitShaTag(tags, github, server, repo) + } return tags } @@ -66,3 +75,8 @@ func appendShortGitShaTag(tags []string, github GitHub, server, repo string) []s } return tags } + +func appendGitRefTag(tags []string, server, repo, refName string) []string { + t := strings.ReplaceAll(refName, "/", "-") + return append(tags, toFullTag(server, repo, t)) +} diff --git a/internal/options/tag_test.go b/internal/options/tag_test.go index e54d3a5..1bb8351 100644 --- a/internal/options/tag_test.go +++ b/internal/options/tag_test.go @@ -10,16 +10,17 @@ import ( func TestGetTags(t *testing.T) { testCases := []struct { - name string - autoTag bool - tags string - ref GitReference - server string - expected []string - sha string + name string + tagWithRef bool + tagWithSha bool + tags string + ref GitReference + server string + expected []string + sha string }{ { - name: "no-auto", + name: "no-standard-tags", tags: "tag1,tag2", expected: []string{"my/repo:tag1", "my/repo:tag2"}, ref: GitReference{GitRefHead, "master"}, @@ -32,63 +33,62 @@ func TestGetTags(t *testing.T) { ref: GitReference{GitRefHead, "master"}, }, { - name: "unknown-ref-type", - tags: "tag1,tag2", - expected: []string{"my/repo:tag1", "my/repo:tag2"}, - autoTag: true, - ref: GitReference{GitRefUnknown, "master"}, + name: "unknown-ref-type", + tags: "tag1,tag2", + expected: []string{"my/repo:tag1", "my/repo:tag2"}, + tagWithRef: true, + ref: GitReference{GitRefUnknown, "master"}, }, { - name: "master-branch", - autoTag: true, - tags: "tag1,tag2", - expected: []string{"my/repo:tag1", "my/repo:tag2", "my/repo:latest"}, - ref: GitReference{GitRefHead, "master"}, + name: "master-branch", + tagWithRef: true, + tags: "tag1,tag2", + expected: []string{"my/repo:tag1", "my/repo:tag2", "my/repo:latest"}, + ref: GitReference{GitRefHead, "master"}, }, { - name: "different-branch", - autoTag: true, - tags: "tag1,tag2", - expected: []string{"my/repo:tag1", "my/repo:tag2", "my/repo:branch-name"}, - ref: GitReference{GitRefHead, "branch-name"}, + name: "different-branch", + tagWithRef: true, + tags: "tag1,tag2", + expected: []string{"my/repo:tag1", "my/repo:tag2", "my/repo:branch-name"}, + ref: GitReference{GitRefHead, "branch-name"}, }, { - name: "pull-request", - autoTag: true, - tags: "tag1,tag2", - expected: []string{"my/repo:tag1", "my/repo:tag2", "my/repo:pr-name"}, - ref: GitReference{GitRefPullRequest, "name"}, + name: "pull-request", + tagWithRef: true, + tags: "tag1,tag2", + expected: []string{"my/repo:tag1", "my/repo:tag2", "my/repo:pr-name"}, + ref: GitReference{GitRefPullRequest, "name"}, }, { - name: "tag", - autoTag: true, - tags: "tag1,tag2", - expected: []string{"my/repo:tag1", "my/repo:tag2", "my/repo:v1.0"}, - ref: GitReference{GitRefTag, "v1.0"}, + name: "tag", + tagWithRef: true, + tags: "tag1,tag2", + expected: []string{"my/repo:tag1", "my/repo:tag2", "my/repo:v1.0"}, + ref: GitReference{GitRefTag, "v1.0"}, }, { - name: "master-branch-with-sha", - autoTag: true, - tags: "tag1,tag2", - expected: []string{"my/repo:tag1", "my/repo:tag2", "my/repo:latest", "my/repo:sha-1234567"}, - ref: GitReference{GitRefHead, "master"}, - sha: "1234567890", + name: "master-branch-with-sha", + tagWithRef: true, + tags: "tag1,tag2", + expected: []string{"my/repo:tag1", "my/repo:tag2", "my/repo:latest"}, + ref: GitReference{GitRefHead, "master"}, + sha: "1234567890", }, { - name: "pull-request-with-sha", - autoTag: true, - tags: "tag1,tag2", - expected: []string{"my/repo:tag1", "my/repo:tag2", "my/repo:pr-name", "my/repo:sha-1234567"}, - ref: GitReference{GitRefPullRequest, "name"}, - sha: "1234567890", + name: "pull-request-with-sha", + tagWithRef: true, + tags: "tag1,tag2", + expected: []string{"my/repo:tag1", "my/repo:tag2", "my/repo:pr-name"}, + ref: GitReference{GitRefPullRequest, "name"}, + sha: "1234567890", }, { - name: "tag-with-sha", - autoTag: true, - tags: "tag1,tag2", - expected: []string{"my/repo:tag1", "my/repo:tag2", "my/repo:v1.0"}, - ref: GitReference{GitRefTag, "v1.0"}, - sha: "1234567890", + name: "tag-with-sha", + tagWithSha: true, + tags: "tag1,tag2", + expected: []string{"my/repo:tag1", "my/repo:tag2", "my/repo:sha-1234567"}, + sha: "1234567890", }, } @@ -97,10 +97,12 @@ func TestGetTags(t *testing.T) { t.Run(tc.name, func(t *testing.T) { defer os.Unsetenv("INPUT_TAGS") defer os.Unsetenv("INPUT_REPOSITORY") - defer os.Unsetenv("INPUT_AUTO_TAG") + defer os.Unsetenv("INPUT_TAG_WITH_REF") + defer os.Unsetenv("INPUT_TAG_WITH_SHA") _ = os.Setenv("INPUT_TAGS", tc.tags) _ = os.Setenv("INPUT_REPOSITORY", "my/repo") - _ = os.Setenv("INPUT_AUTO_TAG", fmt.Sprint(tc.autoTag)) + _ = os.Setenv("INPUT_TAG_WITH_REF", fmt.Sprint(tc.tagWithRef)) + _ = os.Setenv("INPUT_TAG_WITH_SHA", fmt.Sprint(tc.tagWithSha)) tags := GetTags( tc.server, From 5c92bfdc6b3fa4a3333834f6a096edf0d29a4346 Mon Sep 17 00:00:00 2001 From: Nick Adcock Date: Tue, 3 Mar 2020 11:58:17 +0000 Subject: [PATCH 21/31] Switching from urfave/cli to cobra Signed-off-by: Nick Adcock --- cmd/main.go | 64 ++++++++++++++-------------- go.mod | 2 +- go.sum | 118 ++++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 142 insertions(+), 42 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 42ce7cb..cedb007 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -6,7 +6,7 @@ import ( "github.com/docker/github-actions/internal/command" "github.com/docker/github-actions/internal/options" - commandLine "github.com/urfave/cli/v2" + "github.com/spf13/cobra" ) func main() { @@ -16,44 +16,44 @@ func main() { os.Exit(1) } - cmd := command.NewRunner() + runner := command.NewRunner() - app := &commandLine.App{ - Name: "docker github actions", - Usage: "Used in GitHub Actions to run Docker workflows", - Commands: []*commandLine.Command{ - { - Name: "login", - Description: "Logs into a docker server", - Action: func(c *commandLine.Context) error { - return login(cmd) - }, + rootCmd := &cobra.Command{ + Use: "github-actions", + Short: "Used in GitHub Actions to run Docker workflows", + } + rootCmd.AddCommand( + &cobra.Command{ + Use: "login", + Short: "Logs into a docker server", + RunE: func(cmd *cobra.Command, args []string) error { + return login(runner) }, - { - Name: "build", - Description: "Builds a docker image", - Action: func(c *commandLine.Context) error { - return build(cmd) - }, + }, + &cobra.Command{ + Use: "build", + Short: "Builds a docker image", + RunE: func(cmd *cobra.Command, args []string) error { + return build(runner) }, - { - Name: "push", - Description: "Pushes a docker image", - Action: func(c *commandLine.Context) error { - return push(cmd) - }, + }, + &cobra.Command{ + Use: "push", + Short: "Pushes a docker image", + RunE: func(cmd *cobra.Command, args []string) error { + return push(runner) }, - { - Name: "build-push", - Description: "Builds and pushes a docker image to a registry, logging in if necessary", - Action: func(c *commandLine.Context) error { - return buildPush(cmd) - }, + }, + &cobra.Command{ + Use: "build-push", + Short: "Builds and pushes a docker image to a registry, logging in if necessary", + RunE: func(cmd *cobra.Command, args []string) error { + return buildPush(runner) }, }, - } + ) - if err = app.Run(os.Args); err != nil { + if err = rootCmd.Execute(); err != nil { fmt.Println(err) os.Exit(1) } diff --git a/go.mod b/go.mod index 24ae310..bc40662 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.13 require ( github.com/caarlos0/env/v6 v6.1.0 github.com/magiconair/properties v1.8.1 // indirect - github.com/urfave/cli/v2 v2.1.1 + github.com/spf13/cobra v0.0.6 gotest.tools/v3 v3.0.2 k8s.io/apimachinery v0.17.3 ) diff --git a/go.sum b/go.sum index a7979eb..66d108e 100644 --- a/go.sum +++ b/go.sum @@ -1,113 +1,213 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ= +github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= github.com/PuerkitoBio/purell v1.0.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= github.com/PuerkitoBio/urlesc v0.0.0-20160726150825-5bd2802263f2/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE= -github.com/caarlos0/env v3.5.0+incompatible h1:Yy0UN8o9Wtr/jGHZDpCBLpNrzcFLLM2yixi/rBrKyJs= +github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= +github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= +github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= +github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= +github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= github.com/caarlos0/env/v6 v6.1.0 h1:4FbM+HmZA/Q5wdSrH2kj0KQXm7xnhuO8y3TuOTnOvqc= github.com/caarlos0/env/v6 v6.1.0/go.mod h1:iUA6X3VCAOwDhoqvgKlTGjjwJzQseIJaFYApUqQkt+8= -github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY= +github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk= +github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= +github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= +github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= +github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/davecgh/go-spew v0.0.0-20151105211317-5215b55f46b2/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= 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/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= +github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96/go.mod h1:Qh8CwZgvJUkLughtfhJv5dyTYa91l1fOUCrgjqmcifM= github.com/elazarl/goproxy v0.0.0-20170405201442-c4fc26588b6e/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc= github.com/emicklei/go-restful v0.0.0-20170410110728-ff4f55a20633/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs= github.com/evanphx/json-patch v4.2.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= +github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= +github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= +github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= +github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= github.com/go-logr/logr v0.1.0/go.mod h1:ixOQHD9gLJUVQQ2ZOR7zLEifBX6tGkNJF4QyIY7sIas= github.com/go-openapi/jsonpointer v0.0.0-20160704185906-46af16f9f7b1/go.mod h1:+35s3my2LFTysnkMfxsJBAMHj/DoqoB9knIWoYG/Vk0= github.com/go-openapi/jsonreference v0.0.0-20160704190145-13c6e3589ad9/go.mod h1:W3Z9FmVs9qj+KR4zFKmDPGiLdk1D9Rlm7cyMvf57TTg= github.com/go-openapi/spec v0.0.0-20160808142527-6aced65f8501/go.mod h1:J8+jY1nAiCcj+friV/PDoE1/3eeccG9LYBs0tYvLOWc= github.com/go-openapi/swag v0.0.0-20160704191624-1d0bd113de87/go.mod h1:DXUve3Dpr1UfpPtxFw+EFuQ41HhCWZfha5jSVRG7C7I= +github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= +github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4= github.com/gogo/protobuf v1.2.2-0.20190723190241-65acae22fc9d/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/protobuf v0.0.0-20161109072736-4bd1920723d7/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/gofuzz v0.0.0-20161122191042-44d81051d367/go.mod h1:HP5RmnzzSNb993RKQDq4+1A4ia9nllfqcQFTQJedwGI= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY= +github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= +github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= +github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= +github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= +github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= +github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= github.com/json-iterator/go v0.0.0-20180612202835-f2b4162afba3/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.8/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= +github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4= +github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/mailru/easyjson v0.0.0-20160728113105-d5b7844b561a/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= +github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180320133207-05fbef0ca5da/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= +github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw= +github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= github.com/onsi/ginkgo v0.0.0-20170829012221-11459a886d9c/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA= github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= +github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= +github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q= +github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= +github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso= +github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= +github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= +github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= +github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= +github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= +github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= +github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= +github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= +github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= +github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= +github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= +github.com/spf13/cobra v0.0.6 h1:breEStsVwemnKh2/s6gMvSdMEkwW0sK8vGStnlVBMCs= +github.com/spf13/cobra v0.0.6/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE= +github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= github.com/spf13/pflag v0.0.0-20170130214245-9ff6c6923cff/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v0.0.0-20151208002404-e3a8ff8ce365/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/urfave/cli/v2 v2.1.1 h1:Qt8FeAtxE/vfdrLmR3rxR6JRE0RoVmbXu8+6kZtYU4k= +github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= +github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= +github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= +github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= +go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= +go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= +go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= +go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= +golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/net v0.0.0-20170114055629-f2499483f923/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= golang.org/x/net v0.0.0-20191004110552-13f9640d40b9/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20170830134202-bb24a47a89ea/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190826190057-c7b8b68b1456/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.0.0-20160726164857-2910a502d2bf/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20181011042414-1f849cf54d09/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190624222133-a101b041ded4/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM= +gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= +gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo= gotest.tools/v3 v3.0.2 h1:kG1BFyqVHuQoVQiR1bWGnfz/fmHvvuiSPIV7rvl360E= gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= k8s.io/apimachinery v0.17.3 h1:f+uZV6rm4/tHE7xXgLyToprg6xWairaClGVkm2t8omg= k8s.io/apimachinery v0.17.3/go.mod h1:gxLnyZcGNdZTCLnq3fgzyg2A5BVCHTNDFrw8AmuJ+0g= k8s.io/gengo v0.0.0-20190128074634-0689ccc1d7d6/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0= From cb5ad12e428aab60ceaf3068f2dcc83f4b92e3d3 Mon Sep 17 00:00:00 2001 From: Nick Adcock Date: Wed, 4 Mar 2020 09:21:36 +0000 Subject: [PATCH 22/31] Error is username or password set but not both Signed-off-by: Nick Adcock --- internal/options/login.go | 20 +++++++++++++++++--- internal/options/login_test.go | 18 ++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/internal/options/login.go b/internal/options/login.go index 20ba418..f76a0c8 100644 --- a/internal/options/login.go +++ b/internal/options/login.go @@ -1,6 +1,10 @@ package options -import "github.com/caarlos0/env/v6" +import ( + "errors" + + "github.com/caarlos0/env/v6" +) // Login contains the parsed login action environment variables type Login struct { @@ -8,9 +12,19 @@ type Login struct { Password string `env:"INPUT_PASSWORD"` } +var errLoginVarValidation = errors.New("both username and password must be set to login") + // GetLoginOptions gets the login action environment variables func GetLoginOptions() (Login, error) { var login Login - err := env.Parse(&login) - return login, err + if err := env.Parse(&login); err != nil { + return login, err + } + + if login.Username != "" && login.Password == "" || + login.Username == "" && login.Password != "" { + return login, errLoginVarValidation + } + + return login, nil } diff --git a/internal/options/login_test.go b/internal/options/login_test.go index 772202e..e18661b 100644 --- a/internal/options/login_test.go +++ b/internal/options/login_test.go @@ -16,3 +16,21 @@ func TestGetLoginOptions(t *testing.T) { assert.Equal(t, "username", o.Username) assert.Equal(t, "password", o.Password) } + +func TestLoginErrorNoPassword(t *testing.T) { + _ = os.Setenv("INPUT_USERNAME", "username") + _ = os.Unsetenv("INPUT_PASSWORD") + + _, err := GetLoginOptions() + + assert.Equal(t, err, errLoginVarValidation) +} + +func TestLoginErrorNoUsername(t *testing.T) { + _ = os.Setenv("INPUT_PASSWORD", "password") + _ = os.Unsetenv("INPUT_USERNAME") + + _, err := GetLoginOptions() + + assert.Equal(t, err, errLoginVarValidation) +} From 6867dc1008493f2ecbea982167c4d7cbd7d1742c Mon Sep 17 00:00:00 2001 From: Nick Adcock Date: Wed, 4 Mar 2020 09:22:21 +0000 Subject: [PATCH 23/31] Refactoring nits for PR Signed-off-by: Nick Adcock --- cmd/build_push.go | 2 +- e2e/helper_test.go | 2 +- internal/options/build_test.go | 13 ++++++------- internal/options/tag.go | 6 +++--- 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/cmd/build_push.go b/cmd/build_push.go index 63c9fb4..067d4c5 100644 --- a/cmd/build_push.go +++ b/cmd/build_push.go @@ -28,7 +28,7 @@ func buildPush(cmd command.Runner) error { return err } if login.Username != "" && login.Password != "" { - if err = command.RunLogin(cmd, login, server); err != nil { + if err := command.RunLogin(cmd, login, server); err != nil { return err } } diff --git a/e2e/helper_test.go b/e2e/helper_test.go index f4afdfd..4f3aed3 100644 --- a/e2e/helper_test.go +++ b/e2e/helper_test.go @@ -20,7 +20,7 @@ type envVar struct { } func parseEnvFile(envFile string) ([]envVar, error) { - vars := []envVar{} + var vars []envVar wd, err := os.Getwd() if err != nil { diff --git a/internal/options/build_test.go b/internal/options/build_test.go index b5a133a..8e471a0 100644 --- a/internal/options/build_test.go +++ b/internal/options/build_test.go @@ -21,13 +21,12 @@ func TestGetBuildOptions(t *testing.T) { assert.NilError(t, err) assert.DeepEqual(t, Build{ - Path: "path", - Dockerfile: "dockerfile", - AddGitLabels: false, - Target: "target", - AlwaysPull: true, - BuildArgs: []string{"buildarg1=b1", "buildarg2=b2"}, - Labels: []string{"label1=l1", "label2=l2"}, + Path: "path", + Dockerfile: "dockerfile", + Target: "target", + AlwaysPull: true, + BuildArgs: []string{"buildarg1=b1", "buildarg2=b2"}, + Labels: []string{"label1=l1", "label2=l2"}, }, o) } diff --git a/internal/options/tag.go b/internal/options/tag.go index f0a5b69..ad2c02c 100644 --- a/internal/options/tag.go +++ b/internal/options/tag.go @@ -28,7 +28,7 @@ func staticTags() []string { if inputTags := os.Getenv("INPUT_TAGS"); inputTags != "" { return strings.Split(inputTags, ",") } - return []string{} + return nil } func toFullTag(server, repo, tag string) string { @@ -40,10 +40,10 @@ func toFullTag(server, repo, tag string) string { return fmt.Sprintf("%s:%s", repo, tag) } -// GetTags gets a list of all tags for including automatic tags created from github vars when AutTag is true along with the server and repository +// GetTags gets a list of all tags for including automatic tags from github vars when enabled along with the server and repository func GetTags(server string, github GitHub) []string { repo := dockerRepo(github) - tags := []string{} + var tags []string for _, t := range staticTags() { tags = append(tags, toFullTag(server, repo, t)) } From f929a47d6391d9d399e600749c26a359241ceed5 Mon Sep 17 00:00:00 2001 From: Nick Adcock Date: Wed, 4 Mar 2020 09:25:29 +0000 Subject: [PATCH 24/31] Use hashicorp/go-multierror Signed-off-by: Nick Adcock --- e2e/build_test.go | 6 ++++-- go.mod | 1 + go.sum | 4 ++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/e2e/build_test.go b/e2e/build_test.go index 3305088..a446399 100644 --- a/e2e/build_test.go +++ b/e2e/build_test.go @@ -4,6 +4,7 @@ import ( "os/exec" "testing" + "github.com/hashicorp/go-multierror" "gotest.tools/v3/assert" ) @@ -95,10 +96,11 @@ func TestBuild(t *testing.T) { } func removeImages(tags []string) error { + var result error for _, tag := range tags { if err := exec.Command("docker", "rmi", "-f", tag).Run(); err != nil { - return err + result = multierror.Append(result, err) } } - return nil + return result } diff --git a/go.mod b/go.mod index bc40662..a55c953 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.13 require ( github.com/caarlos0/env/v6 v6.1.0 + github.com/hashicorp/go-multierror v1.0.0 github.com/magiconair/properties v1.8.1 // indirect github.com/spf13/cobra v0.0.6 gotest.tools/v3 v3.0.2 diff --git a/go.sum b/go.sum index 66d108e..f11eb99 100644 --- a/go.sum +++ b/go.sum @@ -65,6 +65,10 @@ github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoA github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY= +github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA= +github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= +github.com/hashicorp/go-multierror v1.0.0 h1:iVjPR7a6H0tWELX5NxNe7bYopibicUzc7uPribsnS6o= +github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= From 78d3747a27e25a6f95ba1be62abbf236055d7139 Mon Sep 17 00:00:00 2001 From: Nick Adcock Date: Wed, 4 Mar 2020 09:30:15 +0000 Subject: [PATCH 25/31] Remove unrequired Getwd() from e2e tests Signed-off-by: Nick Adcock --- e2e/helper_test.go | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/e2e/helper_test.go b/e2e/helper_test.go index 4f3aed3..ef9af65 100644 --- a/e2e/helper_test.go +++ b/e2e/helper_test.go @@ -5,7 +5,6 @@ import ( "encoding/json" "os" "os/exec" - "path" "strings" ) @@ -22,12 +21,7 @@ type envVar struct { func parseEnvFile(envFile string) ([]envVar, error) { var vars []envVar - wd, err := os.Getwd() - if err != nil { - return vars, err - } - - file, err := os.Open(path.Join(wd, envFile)) + file, err := os.Open(envFile) if err != nil { return vars, err } @@ -100,12 +94,7 @@ func getActionsBinaryPath() (string, error) { return path, nil } - wd, err := os.Getwd() - if err != nil { - return "", err - } - - return path.Join(wd, "../bin/github-actions"), nil + return "../bin/github-actions", nil } func inspectImage(image string) (inspectResult, error) { From ba8eb4801fab7506feef9496e8205fcd9176b348 Mon Sep 17 00:00:00 2001 From: Nick Adcock Date: Wed, 4 Mar 2020 11:07:18 +0000 Subject: [PATCH 26/31] Use cmd.Env to set env vars in e2e tests Signed-off-by: Nick Adcock --- e2e/helper_test.go | 42 ++++++++---------------------------------- 1 file changed, 8 insertions(+), 34 deletions(-) diff --git a/e2e/helper_test.go b/e2e/helper_test.go index ef9af65..fe5fb8c 100644 --- a/e2e/helper_test.go +++ b/e2e/helper_test.go @@ -3,9 +3,9 @@ package e2e import ( "bufio" "encoding/json" + "fmt" "os" "os/exec" - "strings" ) const ( @@ -13,13 +13,8 @@ const ( githubActionsImage = "github-actions-e2e" ) -type envVar struct { - key string - value string -} - -func parseEnvFile(envFile string) ([]envVar, error) { - var vars []envVar +func readEnvFile(envFile string) ([]string, error) { + var vars []string file, err := os.Open(envFile) if err != nil { @@ -29,31 +24,14 @@ func parseEnvFile(envFile string) ([]envVar, error) { scanner := bufio.NewScanner(file) for scanner.Scan() { - split := strings.SplitN(scanner.Text(), "=", 2) - vars = append(vars, envVar{split[0], split[1]}) + vars = append(vars, scanner.Text()) } + path := os.Getenv("PATH") + vars = append(vars, fmt.Sprintf("PATH=%s", path)) return vars, scanner.Err() } -func setupEnvVars(vars []envVar) error { - for _, v := range vars { - if err := os.Setenv(v.key, v.value); err != nil { - return err - } - } - return nil -} - -func removeEnvVars(vars []envVar) error { - for _, v := range vars { - if err := os.Unsetenv(v.key); err != nil { - return err - } - } - return nil -} - func setupLocalRegistry() error { _ = removeLocalRegistry() @@ -68,22 +46,18 @@ func removeLocalRegistry() error { } func runActionsCommand(command, envFile string) error { - vars, err := parseEnvFile(envFile) + vars, err := readEnvFile(envFile) if err != nil { return err } - if err = setupEnvVars(vars); err != nil { - return err - } - defer removeEnvVars(vars) - bin, err := getActionsBinaryPath() if err != nil { return err } cmd := exec.Command(bin, command) + cmd.Env = vars cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr return cmd.Run() From 791388d13bec5a20f0f88519787486f6f337af70 Mon Sep 17 00:00:00 2001 From: Nick Adcock Date: Wed, 4 Mar 2020 11:13:34 +0000 Subject: [PATCH 27/31] Update doc for INPUT_TARGET to match cli help Signed-off-by: Nick Adcock --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 57c81f8..99572ca 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ Builds and tags a docker image. |INPUT_PATH|yes|Path to build from| |INPUT_DOCKERFILE|no|Path to Dockerfile| |INPUT_ADD_GIT_LABELS|no|Adds git labels (see below)| -|INPUT_TARGET|no|Target of chain Dockerfile to build| +|INPUT_TARGET|no|Target build stage to build| |INPUT_BUILD_ARGS|no|Comma-delimited list of build-args| |INPUT_LABELS|no|Comma-delimited list of labels| From e856f734480b29a47bec58bd0894c8699bebf198 Mon Sep 17 00:00:00 2001 From: Nick Adcock Date: Wed, 4 Mar 2020 11:40:07 +0000 Subject: [PATCH 28/31] Add git labels description in readme Signed-off-by: Nick Adcock --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 99572ca..044d692 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,14 @@ Builds and tags a docker image. See the tagging section for information on tag inputs +##### Git labels + +When `INPUT_ADD_GIT_LABELS` is `true` labels are automatically added to the image that contain data about the current state of the git repo: + +|Label|Description| +|---|---| +|com.docker.github-actions-actor|The username of the user that kicked off this run of the actions (e.g. the user that did the git push)| +|com.docker.github-actions-sha|The full git sha of this commit| ### push From 804908464cfcbbeb8f5ceeb4bdef8ca4b18e76ae Mon Sep 17 00:00:00 2001 From: Nick Adcock Date: Wed, 4 Mar 2020 12:08:56 +0000 Subject: [PATCH 29/31] Rename server input to registry Signed-off-by: Nick Adcock --- README.md | 18 ++++++------ cmd/build.go | 2 +- cmd/build_push.go | 6 ++-- cmd/login.go | 2 +- cmd/main.go | 2 +- cmd/push.go | 2 +- e2e/testdata/build_push_tests/build_push.env | 2 +- e2e/testdata/build_tests/auto_labels.env | 2 +- e2e/testdata/build_tests/static_labels.env | 2 +- e2e/testdata/build_tests/static_tags.env | 2 +- e2e/testdata/build_tests/tag_branch.env | 2 +- e2e/testdata/build_tests/tag_master.env | 2 +- e2e/testdata/build_tests/tag_pr.env | 2 +- e2e/testdata/build_tests/tag_tag.env | 2 +- e2e/testdata/login_test.env | 2 +- e2e/testdata/push_tests/push.env | 2 +- internal/command/args.go | 6 ++-- internal/command/args_test.go | 4 +-- internal/command/runner.go | 4 +-- internal/options/registry.go | 8 ++++++ internal/options/registry_test.go | 17 +++++++++++ internal/options/server.go | 8 ------ internal/options/server_test.go | 16 ----------- internal/options/tag.go | 30 ++++++++++---------- internal/options/tag_test.go | 10 +++---- 25 files changed, 78 insertions(+), 77 deletions(-) create mode 100644 internal/options/registry.go create mode 100644 internal/options/registry_test.go delete mode 100644 internal/options/server.go delete mode 100644 internal/options/server_test.go diff --git a/README.md b/README.md index 044d692..f71fd2b 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Does a `docker login` using the supplied username and password. Will default to |---|---|---| |INPUT_USERNAME|yes|Username to login with| |INPUT_PASSWORD|yes|Password to login with| -|INPUT_SERVER|no|Server to login to. Defaults to Docker Hub| +|INPUT_REGISTRY|no|Registry server to login to. Defaults to Docker Hub| ### build @@ -75,13 +75,13 @@ There are 4 input variables used for tagging |Environment Variable|Required|Description| |---|---|---| -|INPUT_SERVER|no|Server to tag with| +|INPUT_REGISTRY|no|Registry server to tag with| |INPUT_REPOSITORY|yes|Repository to tag with| |INPUT_TAGS|no|Hard coded comma-delimited list of tags| |INPUT_TAG_WITH_REF|no|If true then `github-actions` will add tags depending on the git ref automatically as described below| |INPUT_TAG_WITH_SHA|no|If true then `github-actions` will add a tag in the form `sha-{git-short-sha}`| -If `INPUT_SERVER` is set then all tags are prefixed with `{INPUT_SERVER}/{INPUT_REPOSITORY}:`. +If `INPUT_REGISTRY` is set then all tags are prefixed with `{INPUT_REGISTRY}/{INPUT_REPOSITORY}:`. If not then all tags are prefixed with `{INPUT_REPOSITORY}:` Auto tags depend on the git reference that the run is associated with. The reference is passed to `github-actions` using the GitHub actions `GITHUB_REF` enviroment variable. @@ -98,7 +98,7 @@ For example if the environment variables are as follows: |Variable|Value| |---|---| -|INPUT_SERVER|| +|INPUT_REGISTRY|| |INPUT_REPOSITORY|myorg/myimage| |INPUT_TAGS|foo,bar| |INPUT_TAG_WITH_REF|true| @@ -115,7 +115,7 @@ If the variables are as follows: |Variable|Value| |---|---| -|INPUT_SERVER|myserver| +|INPUT_REGISTRY|myregistry| |INPUT_REPOSITORY|myorg/myimage| |INPUT_TAGS|foo,bar| |INPUT_TAG_WITH_REF|true| @@ -125,10 +125,10 @@ If the variables are as follows: Then the image will be tagged with: ``` -myserver/myorg/myimage:foo -myserver/myorg/myimage:bar -myserver/myorg/myimage:lastest -myserver/myorg/myimage:c6df8c6 +myregistry/myorg/myimage:foo +myregistry/myorg/myimage:bar +myregistry/myorg/myimage:lastest +myregistry/myorg/myimage:c6df8c6 ``` ## Building github-actions diff --git a/cmd/build.go b/cmd/build.go index c9143d9..612de5f 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -16,7 +16,7 @@ func build(cmd command.Runner) error { return err } - tags := options.GetTags(options.GetServer(), github) + tags := options.GetTags(options.GetRegistry(), github) return command.RunBuild(cmd, o, github, tags) } diff --git a/cmd/build_push.go b/cmd/build_push.go index 067d4c5..c5b760d 100644 --- a/cmd/build_push.go +++ b/cmd/build_push.go @@ -11,8 +11,8 @@ func buildPush(cmd command.Runner) error { return err } - server := options.GetServer() - tags := options.GetTags(server, github) + registry := options.GetRegistry() + tags := options.GetTags(registry, github) build, err := options.GetBuildOptions() if err != nil { @@ -28,7 +28,7 @@ func buildPush(cmd command.Runner) error { return err } if login.Username != "" && login.Password != "" { - if err := command.RunLogin(cmd, login, server); err != nil { + if err := command.RunLogin(cmd, login, registry); err != nil { return err } } diff --git a/cmd/login.go b/cmd/login.go index ab9951d..3f9d34c 100644 --- a/cmd/login.go +++ b/cmd/login.go @@ -11,5 +11,5 @@ func login(cmd command.Runner) error { return err } - return command.RunLogin(cmd, o, options.GetServer()) + return command.RunLogin(cmd, o, options.GetRegistry()) } diff --git a/cmd/main.go b/cmd/main.go index cedb007..24c40b6 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -25,7 +25,7 @@ func main() { rootCmd.AddCommand( &cobra.Command{ Use: "login", - Short: "Logs into a docker server", + Short: "Logs into a docker registry", RunE: func(cmd *cobra.Command, args []string) error { return login(runner) }, diff --git a/cmd/push.go b/cmd/push.go index 65d36e3..7b2d24c 100644 --- a/cmd/push.go +++ b/cmd/push.go @@ -10,6 +10,6 @@ func push(cmd command.Runner) error { if err != nil { return err } - tags := options.GetTags(options.GetServer(), github) + tags := options.GetTags(options.GetRegistry(), github) return command.RunPush(cmd, tags) } diff --git a/e2e/testdata/build_push_tests/build_push.env b/e2e/testdata/build_push_tests/build_push.env index 44d7425..fc78c6e 100644 --- a/e2e/testdata/build_push_tests/build_push.env +++ b/e2e/testdata/build_push_tests/build_push.env @@ -4,7 +4,7 @@ INPUT_TAG_WITH_REF=true INPUT_ADD_GIT_LABELS=true INPUT_TAGS=build-push-test INPUT_LABELS=a=a1 -INPUT_SERVER=localhost:5000 +INPUT_REGISTRY=localhost:5000 INPUT_USERNAME=my_user INPUT_PASSWORD=my_password INPUT_REPOSITORY=my-repository diff --git a/e2e/testdata/build_tests/auto_labels.env b/e2e/testdata/build_tests/auto_labels.env index cb44346..be5e42d 100644 --- a/e2e/testdata/build_tests/auto_labels.env +++ b/e2e/testdata/build_tests/auto_labels.env @@ -4,7 +4,7 @@ INPUT_TAG_WITH_REF=false INPUT_ADD_GIT_LABELS=true INPUT_TAGS=auto-labels INPUT_LABELS=a=a1 -INPUT_SERVER=localhost:5000 +INPUT_REGISTRY=localhost:5000 INPUT_REPOSITORY=my-repository GITHUB_ACTOR=actor GITHUB_SHA=sha diff --git a/e2e/testdata/build_tests/static_labels.env b/e2e/testdata/build_tests/static_labels.env index 0051da9..2be65ee 100644 --- a/e2e/testdata/build_tests/static_labels.env +++ b/e2e/testdata/build_tests/static_labels.env @@ -4,5 +4,5 @@ INPUT_TAG_WITH_REF=false INPUT_ADD_GIT_LABELS=false INPUT_TAGS=static-labels INPUT_LABELS=a=a1,b=b1 -INPUT_SERVER=localhost:5000 +INPUT_REGISTRY=localhost:5000 INPUT_REPOSITORY=my-repository diff --git a/e2e/testdata/build_tests/static_tags.env b/e2e/testdata/build_tests/static_tags.env index 1cc8b0d..6e8046d 100644 --- a/e2e/testdata/build_tests/static_tags.env +++ b/e2e/testdata/build_tests/static_tags.env @@ -3,5 +3,5 @@ INPUT_DOCKERFILE=./testdata/build_tests/Dockerfile INPUT_TAG_WITH_REF=false INPUT_ADD_GIT_LABELS=false INPUT_TAGS=v1-static-tags,v1.1-static-tags -INPUT_SERVER=localhost:5000 +INPUT_REGISTRY=localhost:5000 INPUT_REPOSITORY=my-repository diff --git a/e2e/testdata/build_tests/tag_branch.env b/e2e/testdata/build_tests/tag_branch.env index e0ddf4e..7459e9b 100644 --- a/e2e/testdata/build_tests/tag_branch.env +++ b/e2e/testdata/build_tests/tag_branch.env @@ -3,6 +3,6 @@ INPUT_DOCKERFILE=./testdata/build_tests/Dockerfile INPUT_TAG_WITH_REF=true INPUT_ADD_GIT_LABELS=false INPUT_TAGS=auto-tags-branch -INPUT_SERVER=localhost:5000 +INPUT_REGISTRY=localhost:5000 INPUT_REPOSITORY=my-repository GITHUB_REF=refs/heads/branch diff --git a/e2e/testdata/build_tests/tag_master.env b/e2e/testdata/build_tests/tag_master.env index 2a1e635..6891aa7 100644 --- a/e2e/testdata/build_tests/tag_master.env +++ b/e2e/testdata/build_tests/tag_master.env @@ -3,6 +3,6 @@ INPUT_DOCKERFILE=./testdata/build_tests/Dockerfile INPUT_TAG_WITH_REF=true INPUT_ADD_GIT_LABELS=false INPUT_TAGS=auto-tags-master -INPUT_SERVER=localhost:5000 +INPUT_REGISTRY=localhost:5000 INPUT_REPOSITORY=my-repository GITHUB_REF=refs/heads/master diff --git a/e2e/testdata/build_tests/tag_pr.env b/e2e/testdata/build_tests/tag_pr.env index 373b45a..b90bd7a 100644 --- a/e2e/testdata/build_tests/tag_pr.env +++ b/e2e/testdata/build_tests/tag_pr.env @@ -2,6 +2,6 @@ INPUT_PATH=./testdata/build_tests INPUT_DOCKERFILE=./testdata/build_tests/Dockerfile INPUT_TAG_WITH_REF=true INPUT_ADD_GIT_LABELS=false -INPUT_SERVER=localhost:5000 +INPUT_REGISTRY=localhost:5000 INPUT_REPOSITORY=my-repository GITHUB_REF=refs/pulls/pr1 diff --git a/e2e/testdata/build_tests/tag_tag.env b/e2e/testdata/build_tests/tag_tag.env index 427888c..f13ed94 100644 --- a/e2e/testdata/build_tests/tag_tag.env +++ b/e2e/testdata/build_tests/tag_tag.env @@ -2,6 +2,6 @@ INPUT_PATH=./testdata/build_tests INPUT_DOCKERFILE=./testdata/build_tests/Dockerfile INPUT_TAG_WITH_REF=true INPUT_ADD_GIT_LABELS=false -INPUT_SERVER=localhost:5000 +INPUT_REGISTRY=localhost:5000 INPUT_REPOSITORY=my-repository GITHUB_REF=refs/tags/tag1 diff --git a/e2e/testdata/login_test.env b/e2e/testdata/login_test.env index 8139314..b58b3e4 100644 --- a/e2e/testdata/login_test.env +++ b/e2e/testdata/login_test.env @@ -1,3 +1,3 @@ -INPUT_SERVER=localhost:5000 +INPUT_REGISTRY=localhost:5000 INPUT_USERNAME=my_user INPUT_PASSWORD=my_password diff --git a/e2e/testdata/push_tests/push.env b/e2e/testdata/push_tests/push.env index 87f0a18..1a2205d 100644 --- a/e2e/testdata/push_tests/push.env +++ b/e2e/testdata/push_tests/push.env @@ -4,5 +4,5 @@ INPUT_TAG_WITH_REF=false INPUT_ADD_GIT_LABELS=false INPUT_TAGS=push-test INPUT_LABELS=a=a1,b=b1 -INPUT_SERVER=localhost:5000 +INPUT_REGISTRY=localhost:5000 INPUT_REPOSITORY=my-repository diff --git a/internal/command/args.go b/internal/command/args.go index e6ec040..c4a776f 100644 --- a/internal/command/args.go +++ b/internal/command/args.go @@ -5,10 +5,10 @@ import ( ) // LoginArgs converts login options into the cli arguments used to call `docker login` -func LoginArgs(o options.Login, server string) []string { +func LoginArgs(o options.Login, registry string) []string { args := []string{"login", "--username", o.Username, "--password", o.Password} - if server != "" { - args = append(args, server) + if registry != "" { + args = append(args, registry) } return args } diff --git a/internal/command/args_test.go b/internal/command/args_test.go index c09267d..43f561c 100644 --- a/internal/command/args_test.go +++ b/internal/command/args_test.go @@ -17,8 +17,8 @@ func TestLoginArgs(t *testing.T) { assert.DeepEqual(t, expected, args) - args = LoginArgs(o, "server") - expected = append(expected, "server") + args = LoginArgs(o, "registry") + expected = append(expected, "registry") assert.DeepEqual(t, expected, args) } diff --git a/internal/command/runner.go b/internal/command/runner.go index bb32d6a..60dded2 100644 --- a/internal/command/runner.go +++ b/internal/command/runner.go @@ -29,8 +29,8 @@ func (runner execRunner) Run(name string, args ...string) error { } // RunLogin runs a docker login -func RunLogin(cmd Runner, opt options.Login, server string) error { - args := LoginArgs(opt, server) +func RunLogin(cmd Runner, opt options.Login, registry string) error { + args := LoginArgs(opt, registry) return cmd.Run("docker", args...) } diff --git a/internal/options/registry.go b/internal/options/registry.go new file mode 100644 index 0000000..dff7a0f --- /dev/null +++ b/internal/options/registry.go @@ -0,0 +1,8 @@ +package options + +import "os" + +// GetRegistry gets the registry server from the github actions environment variables +func GetRegistry() string { + return os.Getenv("INPUT_REGISTRY") +} diff --git a/internal/options/registry_test.go b/internal/options/registry_test.go new file mode 100644 index 0000000..a35b029 --- /dev/null +++ b/internal/options/registry_test.go @@ -0,0 +1,17 @@ +package options + +import ( + "os" + "testing" + + "gotest.tools/v3/assert" +) + +func TestGetRegistry(t *testing.T) { + _ = os.Unsetenv("INPUT_REGISTRY") + assert.Equal(t, "", GetRegistry()) + + defer os.Unsetenv("INPUT_REGISTRY") + _ = os.Setenv("INPUT_REGISTRY", "registry") + assert.Equal(t, "registry", GetRegistry()) +} diff --git a/internal/options/server.go b/internal/options/server.go deleted file mode 100644 index 90d0186..0000000 --- a/internal/options/server.go +++ /dev/null @@ -1,8 +0,0 @@ -package options - -import "os" - -// GetServer gets the server from the github actions environment variables -func GetServer() string { - return os.Getenv("INPUT_SERVER") -} diff --git a/internal/options/server_test.go b/internal/options/server_test.go deleted file mode 100644 index 8b7463d..0000000 --- a/internal/options/server_test.go +++ /dev/null @@ -1,16 +0,0 @@ -package options - -import ( - "os" - "testing" - - "gotest.tools/v3/assert" -) - -func TestGetServer(t *testing.T) { - assert.Equal(t, "", GetServer()) - - defer os.Unsetenv("INPUT_SERVER") - _ = os.Setenv("INPUT_SERVER", "server") - assert.Equal(t, "server", GetServer()) -} diff --git a/internal/options/tag.go b/internal/options/tag.go index ad2c02c..3c076c2 100644 --- a/internal/options/tag.go +++ b/internal/options/tag.go @@ -31,52 +31,52 @@ func staticTags() []string { return nil } -func toFullTag(server, repo, tag string) string { +func toFullTag(registry, repo, tag string) string { tag = strings.TrimSpace(tag) tag = strings.ReplaceAll(tag, "/", "-") - if server != "" { - return fmt.Sprintf("%s/%s:%s", server, repo, tag) + if registry != "" { + return fmt.Sprintf("%s/%s:%s", registry, repo, tag) } return fmt.Sprintf("%s:%s", repo, tag) } -// GetTags gets a list of all tags for including automatic tags from github vars when enabled along with the server and repository -func GetTags(server string, github GitHub) []string { +// GetTags gets a list of all tags for including automatic tags from github vars when enabled along with the registry and repository +func GetTags(registry string, github GitHub) []string { repo := dockerRepo(github) var tags []string for _, t := range staticTags() { - tags = append(tags, toFullTag(server, repo, t)) + tags = append(tags, toFullTag(registry, repo, t)) } if tagWithRef() { switch github.Reference.Type { case GitRefHead: if github.Reference.Name == "master" { - tags = append(tags, toFullTag(server, repo, "latest")) + tags = append(tags, toFullTag(registry, repo, "latest")) } else { - tags = appendGitRefTag(tags, server, repo, github.Reference.Name) + tags = appendGitRefTag(tags, registry, repo, github.Reference.Name) } case GitRefPullRequest: - tags = appendGitRefTag(tags, server, repo, fmt.Sprintf("pr-%s", github.Reference.Name)) + tags = appendGitRefTag(tags, registry, repo, fmt.Sprintf("pr-%s", github.Reference.Name)) case GitRefTag: - tags = appendGitRefTag(tags, server, repo, github.Reference.Name) + tags = appendGitRefTag(tags, registry, repo, github.Reference.Name) } } if tagWithSha() { - tags = appendShortGitShaTag(tags, github, server, repo) + tags = appendShortGitShaTag(tags, github, registry, repo) } return tags } -func appendShortGitShaTag(tags []string, github GitHub, server, repo string) []string { +func appendShortGitShaTag(tags []string, github GitHub, registry, repo string) []string { if len(github.Sha) >= 7 { tag := fmt.Sprintf("sha-%s", github.Sha[0:7]) - return append(tags, toFullTag(server, repo, tag)) + return append(tags, toFullTag(registry, repo, tag)) } return tags } -func appendGitRefTag(tags []string, server, repo, refName string) []string { +func appendGitRefTag(tags []string, registry, repo, refName string) []string { t := strings.ReplaceAll(refName, "/", "-") - return append(tags, toFullTag(server, repo, t)) + return append(tags, toFullTag(registry, repo, t)) } diff --git a/internal/options/tag_test.go b/internal/options/tag_test.go index 1bb8351..044f84d 100644 --- a/internal/options/tag_test.go +++ b/internal/options/tag_test.go @@ -15,7 +15,7 @@ func TestGetTags(t *testing.T) { tagWithSha bool tags string ref GitReference - server string + registry string expected []string sha string }{ @@ -26,10 +26,10 @@ func TestGetTags(t *testing.T) { ref: GitReference{GitRefHead, "master"}, }, { - name: "with-server", + name: "with-registry", tags: "tag1,tag2", - expected: []string{"server/my/repo:tag1", "server/my/repo:tag2"}, - server: "server", + expected: []string{"registry/my/repo:tag1", "registry/my/repo:tag2"}, + registry: "registry", ref: GitReference{GitRefHead, "master"}, }, { @@ -105,7 +105,7 @@ func TestGetTags(t *testing.T) { _ = os.Setenv("INPUT_TAG_WITH_SHA", fmt.Sprint(tc.tagWithSha)) tags := GetTags( - tc.server, + tc.registry, GitHub{Reference: tc.ref, Sha: tc.sha}, ) assert.DeepEqual(t, tc.expected, tags) From 66d44b6115607ec268c61f3aec00318c73877c42 Mon Sep 17 00:00:00 2001 From: Nick Adcock Date: Wed, 4 Mar 2020 13:31:44 +0000 Subject: [PATCH 30/31] Return err when boolean inputs invalid Returns an error when boolean inputs are invalid: - tag_with_ref - tag_with_sha - push If the inputs are not set then defaults to false Signed-off-by: Nick Adcock --- cmd/build.go | 5 ++++- cmd/build_push.go | 9 +++++++-- cmd/push.go | 5 ++++- internal/options/helper.go | 14 +++++++++++++ internal/options/push.go | 14 ++++++++----- internal/options/push_test.go | 22 +++++++++++---------- internal/options/tag.go | 37 ++++++++++++++++++++++++----------- internal/options/tag_test.go | 6 ++++-- 8 files changed, 80 insertions(+), 32 deletions(-) create mode 100644 internal/options/helper.go diff --git a/cmd/build.go b/cmd/build.go index 612de5f..085f6be 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -16,7 +16,10 @@ func build(cmd command.Runner) error { return err } - tags := options.GetTags(options.GetRegistry(), github) + tags, err := options.GetTags(options.GetRegistry(), github) + if err != nil { + return err + } return command.RunBuild(cmd, o, github, tags) } diff --git a/cmd/build_push.go b/cmd/build_push.go index c5b760d..4e785b6 100644 --- a/cmd/build_push.go +++ b/cmd/build_push.go @@ -12,7 +12,10 @@ func buildPush(cmd command.Runner) error { } registry := options.GetRegistry() - tags := options.GetTags(registry, github) + tags, err := options.GetTags(registry, github) + if err != nil { + return err + } build, err := options.GetBuildOptions() if err != nil { @@ -22,7 +25,9 @@ func buildPush(cmd command.Runner) error { return err } - if options.ShouldPush() { + if shouldPush, err := options.ShouldPush(); err != nil { + return err + } else if shouldPush { login, err := options.GetLoginOptions() if err != nil { return err diff --git a/cmd/push.go b/cmd/push.go index 7b2d24c..1521ea6 100644 --- a/cmd/push.go +++ b/cmd/push.go @@ -10,6 +10,9 @@ func push(cmd command.Runner) error { if err != nil { return err } - tags := options.GetTags(options.GetRegistry(), github) + tags, err := options.GetTags(options.GetRegistry(), github) + if err != nil { + return err + } return command.RunPush(cmd, tags) } diff --git a/internal/options/helper.go b/internal/options/helper.go new file mode 100644 index 0000000..5f84774 --- /dev/null +++ b/internal/options/helper.go @@ -0,0 +1,14 @@ +package options + +import ( + "os" + "strconv" +) + +func readBoolOption(key string) (bool, error) { + o := os.Getenv(key) + if o == "" { + return false, nil + } + return strconv.ParseBool(o) +} diff --git a/internal/options/push.go b/internal/options/push.go index a573374..8c5d2fd 100644 --- a/internal/options/push.go +++ b/internal/options/push.go @@ -1,12 +1,16 @@ package options import ( - "os" - "strconv" + "errors" ) +var errPushParse = errors.New("push input must be a valid boolean value") + // ShouldPush returns true if the user has signalled a docker push should be performed -func ShouldPush() bool { - b, err := strconv.ParseBool(os.Getenv("INPUT_PUSH")) - return err == nil && b +func ShouldPush() (bool, error) { + b, err := readBoolOption("INPUT_PUSH") + if err != nil { + return false, errPushParse + } + return b, nil } diff --git a/internal/options/push_test.go b/internal/options/push_test.go index 0705ec9..849679f 100644 --- a/internal/options/push_test.go +++ b/internal/options/push_test.go @@ -9,14 +9,15 @@ import ( func TestShouldPush(t *testing.T) { testCases := []struct { - name string - input string - expected bool + name string + input string + expected bool + expectedErr error }{ { - name: "empty", - input: "", - expected: false, + name: "invalid", + input: "invalid", + expectedErr: errPushParse, }, { name: "true", @@ -24,9 +25,8 @@ func TestShouldPush(t *testing.T) { expected: true, }, { - name: "false", - input: "false", - expected: false, + name: "false", + input: "false", }, } for _, tc := range testCases { @@ -34,7 +34,9 @@ func TestShouldPush(t *testing.T) { t.Run(tc.name, func(t *testing.T) { _ = os.Setenv("INPUT_PUSH", tc.input) defer os.Unsetenv("INPUT_PUSH") - assert.Equal(t, tc.expected, ShouldPush()) + should, err := ShouldPush() + assert.Equal(t, tc.expectedErr, err) + assert.Equal(t, tc.expected, should) }) } } diff --git a/internal/options/tag.go b/internal/options/tag.go index 3c076c2..cf862ec 100644 --- a/internal/options/tag.go +++ b/internal/options/tag.go @@ -1,20 +1,31 @@ package options import ( + "errors" "fmt" "os" - "strconv" "strings" ) -func tagWithRef() bool { - b, err := strconv.ParseBool(os.Getenv("INPUT_TAG_WITH_REF")) - return err == nil && b +var ( + errTagWithRefParse = errors.New("tag_with_ref input must be a valid boolean value") + errTagWithShaParse = errors.New("tag_with_sha input must be a valid boolean value") +) + +func tagWithRef() (bool, error) { + b, err := readBoolOption("INPUT_TAG_WITH_REF") + if err != nil { + return false, errTagWithRefParse + } + return b, nil } -func tagWithSha() bool { - b, err := strconv.ParseBool(os.Getenv("INPUT_TAG_WITH_SHA")) - return err == nil && b +func tagWithSha() (bool, error) { + b, err := readBoolOption("INPUT_TAG_WITH_SHA") + if err != nil { + return false, errTagWithShaParse + } + return b, nil } func dockerRepo(github GitHub) string { @@ -41,13 +52,15 @@ func toFullTag(registry, repo, tag string) string { } // GetTags gets a list of all tags for including automatic tags from github vars when enabled along with the registry and repository -func GetTags(registry string, github GitHub) []string { +func GetTags(registry string, github GitHub) ([]string, error) { repo := dockerRepo(github) var tags []string for _, t := range staticTags() { tags = append(tags, toFullTag(registry, repo, t)) } - if tagWithRef() { + if withRef, err := tagWithRef(); err != nil { + return nil, err + } else if withRef { switch github.Reference.Type { case GitRefHead: if github.Reference.Name == "master" { @@ -62,10 +75,12 @@ func GetTags(registry string, github GitHub) []string { tags = appendGitRefTag(tags, registry, repo, github.Reference.Name) } } - if tagWithSha() { + if withSha, err := tagWithSha(); err != nil { + return nil, err + } else if withSha { tags = appendShortGitShaTag(tags, github, registry, repo) } - return tags + return tags, nil } func appendShortGitShaTag(tags []string, github GitHub, registry, repo string) []string { diff --git a/internal/options/tag_test.go b/internal/options/tag_test.go index 044f84d..c463202 100644 --- a/internal/options/tag_test.go +++ b/internal/options/tag_test.go @@ -104,10 +104,11 @@ func TestGetTags(t *testing.T) { _ = os.Setenv("INPUT_TAG_WITH_REF", fmt.Sprint(tc.tagWithRef)) _ = os.Setenv("INPUT_TAG_WITH_SHA", fmt.Sprint(tc.tagWithSha)) - tags := GetTags( + tags, err := GetTags( tc.registry, GitHub{Reference: tc.ref, Sha: tc.sha}, ) + assert.NilError(t, err) assert.DeepEqual(t, tc.expected, tags) }) } @@ -118,6 +119,7 @@ func TestGetTagsWithGitHubRepo(t *testing.T) { _ = os.Setenv("INPUT_TAGS", "tag1") github := GitHub{Repository: "My/Repo"} - tags := GetTags("", github) + tags, err := GetTags("", github) + assert.NilError(t, err) assert.DeepEqual(t, []string{"my/repo:tag1"}, tags) } From e93e32e10ec2f87a0e7650e247a17b7cec61b6c2 Mon Sep 17 00:00:00 2001 From: Nick Adcock Date: Wed, 4 Mar 2020 13:41:33 +0000 Subject: [PATCH 31/31] Only replace / when tagging with ref Signed-off-by: Nick Adcock --- internal/options/tag.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/options/tag.go b/internal/options/tag.go index cf862ec..57a2dc9 100644 --- a/internal/options/tag.go +++ b/internal/options/tag.go @@ -44,7 +44,6 @@ func staticTags() []string { func toFullTag(registry, repo, tag string) string { tag = strings.TrimSpace(tag) - tag = strings.ReplaceAll(tag, "/", "-") if registry != "" { return fmt.Sprintf("%s/%s:%s", registry, repo, tag) }