diff --git a/Dockerfile b/Dockerfile index 850483f..e080e95 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,5 +6,7 @@ ENV SSH_KNOWN_HOSTS="/etc/ssh/ssh_known_hosts" RUN apk add --no-cache openssh-client && ssh-keyscan -H github.com >> /etc/ssh/ssh_known_hosts -ENTRYPOINT ["/gitops-commit"] +COPY entrypoint.sh . + +ENTRYPOINT ["/entrypoint.sh"] COPY gitops-commit / diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..3f415c8 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,6 @@ +#/bin/sh +set -e + +ssh-keyscan -H github.com >> /etc/ssh/ssh_known_hosts + +exec "$@" \ No newline at end of file diff --git a/internal/app/gitops-commit/cmd/run.go b/internal/app/gitops-commit/cmd/run.go index 3464d04..d04f965 100644 --- a/internal/app/gitops-commit/cmd/run.go +++ b/internal/app/gitops-commit/cmd/run.go @@ -12,6 +12,7 @@ func newRunCommand() *cobra.Command { c := cobra.Command{ Use: "run", RunE: func(cmd *cobra.Command, args []string) error { + commitScope := cmd.Flag("commit-scope").Value.String() key := cmd.Flag("key").Value.String() email := cmd.Flag("email").Value.String() newVersion := cmd.Flag("version").Value.String() @@ -38,15 +39,17 @@ func newRunCommand() *cobra.Command { defer c() return gitops.DeployVersionHandler(gitops.DeployVersionCommand{ - GitOptions: *options, - Repository: repo, - Notation: notation, - File: file, - Version: newVersion, + GitOptions: *options, + Repository: repo, + Notation: notation, + File: file, + Version: newVersion, + CommitScope: commitScope, }) }, } + c.Flags().String("commit-scope", "", "If you want to add a commit scope inline with conventional commits") c.Flags().String("notation", "", "The yaml path in dot notation i.e. image.tag") c.Flags().String("email", "", "The email address of the commit") c.Flags().String("version", "", "The semver version you want to deploy i.e. v1.1.2") diff --git a/internal/app/gitops-commit/slackhttp/manifest.go b/internal/app/gitops-commit/slackhttp/manifest.go index 9ee5926..7aca18e 100644 --- a/internal/app/gitops-commit/slackhttp/manifest.go +++ b/internal/app/gitops-commit/slackhttp/manifest.go @@ -3,7 +3,7 @@ package slackhttp import ( "fmt" "gopkg.in/yaml.v3" - "io/ioutil" + "os" ) type Manifest struct { @@ -29,7 +29,7 @@ func LoadManifest(f string) (*Manifest, error) { registry: NewNamedRepositoryRegistry(), } - d, err := ioutil.ReadFile(f) + d, err := os.ReadFile(f) if err != nil { return nil, fmt.Errorf("cannot read yaml file: %w", err) diff --git a/internal/app/gitops-commit/slackhttp/middleware.go b/internal/app/gitops-commit/slackhttp/middleware.go index a372794..cc4729f 100644 --- a/internal/app/gitops-commit/slackhttp/middleware.go +++ b/internal/app/gitops-commit/slackhttp/middleware.go @@ -4,7 +4,6 @@ import ( "github.com/google/martian/log" "github.com/slack-go/slack" "io" - "io/ioutil" "net/http" ) @@ -17,7 +16,7 @@ func (s *server) SlackCommandMiddleware(next func(w http.ResponseWriter, s slack return } - r.Body = ioutil.NopCloser(io.TeeReader(r.Body, &verifier)) + r.Body = io.NopCloser(io.TeeReader(r.Body, &verifier)) s, err := slack.SlashCommandParse(r) if err != nil { w.WriteHeader(http.StatusInternalServerError) diff --git a/internal/pkg/gitops/git.go b/internal/pkg/gitops/git.go index 4f3ce3f..99274f8 100644 --- a/internal/pkg/gitops/git.go +++ b/internal/pkg/gitops/git.go @@ -5,7 +5,6 @@ import ( "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing/object" "github.com/go-git/go-git/v5/plumbing/transport/ssh" - "io/ioutil" "os" "time" ) @@ -19,7 +18,7 @@ type GitOptions struct { } func NewGitOptions(keys *ssh.PublicKeys) (*GitOptions, func(), error) { - dir, err := ioutil.TempDir("/tmp", "prefix") + dir, err := os.MkdirTemp("/tmp", "prefix") if err != nil { return nil, nil, err } diff --git a/internal/pkg/gitops/handler.go b/internal/pkg/gitops/handler.go index 5080695..e17bc1e 100644 --- a/internal/pkg/gitops/handler.go +++ b/internal/pkg/gitops/handler.go @@ -2,15 +2,16 @@ package gitops import ( "fmt" - "io/ioutil" + "os" ) type DeployVersionCommand struct { - GitOptions GitOptions - Repository string - Notation string - File string - Version string + GitOptions GitOptions + Repository string + Notation string + File string + Version string + CommitScope string } func DeployVersionHandler(c DeployVersionCommand) error { @@ -22,7 +23,7 @@ func DeployVersionHandler(c DeployVersionCommand) error { filename := fmt.Sprintf("%s/%s", c.GitOptions.WorkingDirectory, c.File) - f, err := ioutil.ReadFile(filename) + f, err := os.ReadFile(filename) if err != nil { return fmt.Errorf("cannot read file: %w", err) @@ -38,5 +39,11 @@ func DeployVersionHandler(c DeployVersionCommand) error { return fmt.Errorf("cannot write new version: %w", err) } - return PushVersion(r, &c.GitOptions, c.File, fmt.Sprintf("ci: update tag to %s", c.Version)) + message := fmt.Sprintf("ci: update tag to %s", c.Version) + + if len(c.CommitScope) > 0 { + message = fmt.Sprintf("ci(%s): update tag to %s", c.CommitScope, c.Version) + } + + return PushVersion(r, &c.GitOptions, c.File, message) } diff --git a/internal/pkg/gitops/version.go b/internal/pkg/gitops/version.go index 9e38b48..4a831e6 100644 --- a/internal/pkg/gitops/version.go +++ b/internal/pkg/gitops/version.go @@ -5,7 +5,7 @@ import ( "errors" "fmt" "gopkg.in/yaml.v3" - "io/ioutil" + "os" "regexp" "strconv" "strings" @@ -32,7 +32,7 @@ func ReadCurrentVersion(f []byte, notation string) (string, error) { func WriteVersion(f []byte, version string, newVersion string, filename string) error { output := bytes.Replace(f, []byte(version), []byte(newVersion), -1) - err := ioutil.WriteFile(filename, output, 0666) + err := os.WriteFile(filename, output, 0666) if err != nil { return fmt.Errorf("cannot replace version: %w", err)