From 72607f2ee985559086ed742d3a0be5e457ac2afd Mon Sep 17 00:00:00 2001 From: Leon Mika Date: Sat, 13 Jul 2024 11:41:14 +1000 Subject: [PATCH 1/9] Added a bit of a readme --- README.md | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index fcca627..5163121 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,22 @@ # push-to-dokku -Action to push to Dokku \ No newline at end of file +Action to push a remote repository to Dokku. + +## Usage + +``` +uses: https://lmika.dev/actions/push-to-dokku@v1 +with: + app: dokku-app-name + host: dokku-host + private-key: SSH key +``` + +Parameters are: + +- `host`: hostname of the Dokku instance to push the application to. +- `app`: the Dokku application name. +- `private-key`: the private SSH key + +This action will write the private key to the .ssh instance and will push to the remote repository as the `dokku` +user. Both the source and target branch must be `main`. From 7999a86f582b1c48417a04888bbf8248473d3a53 Mon Sep 17 00:00:00 2001 From: Leon Mika Date: Sat, 13 Jul 2024 11:42:38 +1000 Subject: [PATCH 2/9] Added a reference to needing Go --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 5163121..483aa2e 100644 --- a/README.md +++ b/README.md @@ -18,5 +18,7 @@ Parameters are: - `app`: the Dokku application name. - `private-key`: the private SSH key +This action requires Go 1.22.4 or later. + This action will write the private key to the .ssh instance and will push to the remote repository as the `dokku` user. Both the source and target branch must be `main`. From ec773bc57d6452d028da2d072017bf4681b83092 Mon Sep 17 00:00:00 2001 From: Leon Mika Date: Thu, 2 Jan 2025 11:48:27 +1100 Subject: [PATCH 3/9] Added option to configure the source and target branch --- README.md | 4 +++- config.go | 16 ++++++++++------ services.go | 16 +++++++++++++++- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 483aa2e..4404009 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Action to push a remote repository to Dokku. ## Usage ``` -uses: https://lmika.dev/actions/push-to-dokku@v1 +uses: https://lmika.dev/actions/push-to-dokku@v1.1 with: app: dokku-app-name host: dokku-host @@ -17,6 +17,8 @@ Parameters are: - `host`: hostname of the Dokku instance to push the application to. - `app`: the Dokku application name. - `private-key`: the private SSH key +- `source-branch`: the code branch to push (default = `main`) +- `target-branch`: the Dokku branch to push to (default = same as source-branch) This action requires Go 1.22.4 or later. diff --git a/config.go b/config.go index 87c6d1f..e2e1e24 100644 --- a/config.go +++ b/config.go @@ -3,15 +3,19 @@ package main import "os" type config struct { - HostName string - AppName string - PrivateKey string + HostName string + AppName string + PrivateKey string + LocalBranch string + RemoteBranch string } func readConfig() config { return config{ - HostName: os.Getenv("INPUT_HOST"), - AppName: os.Getenv("INPUT_APP"), - PrivateKey: os.Getenv("INPUT_PRIVATE-KEY"), + HostName: os.Getenv("INPUT_HOST"), + AppName: os.Getenv("INPUT_APP"), + PrivateKey: os.Getenv("INPUT_PRIVATE-KEY"), + LocalBranch: os.Getenv("INPUT_SOURCE-BRANCH"), + RemoteBranch: os.Getenv("INPUT_REMOTE-BRANCH"), } } diff --git a/services.go b/services.go index 67aba79..1286b98 100644 --- a/services.go +++ b/services.go @@ -67,7 +67,21 @@ func (s *Services) PushRepository() error { return err } - if err := runCmd("git", "push", "dokku", "main"); err != nil { + sourceBranch := s.cfg.LocalBranch + if sourceBranch == "" { + sourceBranch = "main" + } + remoteBranch := s.cfg.RemoteBranch + if remoteBranch == "" { + remoteBranch = sourceBranch + } + + branchArg := sourceBranch + if sourceBranch != remoteBranch { + branchArg = fmt.Sprintf("%v:%v", sourceBranch, remoteBranch) + } + + if err := runCmd("git", "push", "dokku", branchArg); err != nil { return err } From 644390f59ab24d2ebafc59ca4319aa1c234fb94d Mon Sep 17 00:00:00 2001 From: Leon Mika Date: Thu, 2 Jan 2025 11:53:37 +1100 Subject: [PATCH 4/9] Switch from remote-branch to target-branch --- config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.go b/config.go index e2e1e24..def963b 100644 --- a/config.go +++ b/config.go @@ -16,6 +16,6 @@ func readConfig() config { AppName: os.Getenv("INPUT_APP"), PrivateKey: os.Getenv("INPUT_PRIVATE-KEY"), LocalBranch: os.Getenv("INPUT_SOURCE-BRANCH"), - RemoteBranch: os.Getenv("INPUT_REMOTE-BRANCH"), + RemoteBranch: os.Getenv("INPUT_TARGET-BRANCH"), } } From 3f9c10c04b28901d8188893ade2efd10fc1c6157 Mon Sep 17 00:00:00 2001 From: Leon Mika Date: Thu, 2 Jan 2025 12:35:14 +1100 Subject: [PATCH 5/9] Switched from the default branch being main to whatever is currently head --- services.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/services.go b/services.go index 1286b98..218a0b3 100644 --- a/services.go +++ b/services.go @@ -69,7 +69,13 @@ func (s *Services) PushRepository() error { sourceBranch := s.cfg.LocalBranch if sourceBranch == "" { - sourceBranch = "main" + // Get the current ref + ref, err := runCmdExpectingOutput("git", "rev-parse", "--abbrev-ref", "HEAD") + if err != nil { + return err + } + + sourceBranch = strings.TrimSpace(ref) } remoteBranch := s.cfg.RemoteBranch if remoteBranch == "" { @@ -96,6 +102,17 @@ func runCmd(cmd ...string) error { return e.Run() } +func runCmdExpectingOutput(cmd ...string) (string, error) { + log.Printf(" .. [exec] %v", strings.Join(cmd, " ")) + e := exec.Command(cmd[0], cmd[1:]...) + e.Stderr = os.Stderr + res, err := e.Output() + if err != nil { + return "", err + } + return string(res), nil +} + func writeFile(path string, content string, mode os.FileMode) error { fullPath := os.ExpandEnv(path) log.Printf(" .. [file] %v (%v bytes)", fullPath, len(content)) From 16fd63b06f74c0a7299d0721f5e7f4521ee81e11 Mon Sep 17 00:00:00 2001 From: Leon Mika Date: Thu, 2 Jan 2025 12:39:50 +1100 Subject: [PATCH 6/9] Fixed hardcoded target and remove abbreviated ref --- services.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services.go b/services.go index 218a0b3..3a55d65 100644 --- a/services.go +++ b/services.go @@ -70,7 +70,7 @@ func (s *Services) PushRepository() error { sourceBranch := s.cfg.LocalBranch if sourceBranch == "" { // Get the current ref - ref, err := runCmdExpectingOutput("git", "rev-parse", "--abbrev-ref", "HEAD") + ref, err := runCmdExpectingOutput("git", "rev-parse", "HEAD") if err != nil { return err } @@ -79,7 +79,7 @@ func (s *Services) PushRepository() error { } remoteBranch := s.cfg.RemoteBranch if remoteBranch == "" { - remoteBranch = sourceBranch + remoteBranch = "main" } branchArg := sourceBranch From 2ad19740fc71e00b911c4eae21f63baf33b10a2f Mon Sep 17 00:00:00 2001 From: lmika Date: Tue, 16 Jun 2026 12:40:10 +0000 Subject: [PATCH 7/9] Dumped file content --- services.go | 1 + 1 file changed, 1 insertion(+) diff --git a/services.go b/services.go index 3a55d65..56abaa8 100644 --- a/services.go +++ b/services.go @@ -116,6 +116,7 @@ func runCmdExpectingOutput(cmd ...string) (string, error) { func writeFile(path string, content string, mode os.FileMode) error { fullPath := os.ExpandEnv(path) log.Printf(" .. [file] %v (%v bytes)", fullPath, len(content)) + log.Printf(" .. [file] %v", content) return os.WriteFile(fullPath, []byte(content), mode) } From 657104bf912222cd747fca76d1fdfc6c16b7587e Mon Sep 17 00:00:00 2001 From: lmika Date: Tue, 16 Jun 2026 21:35:31 +0000 Subject: [PATCH 8/9] Made the key filename ECDSA --- services.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/services.go b/services.go index 56abaa8..cb2ea0a 100644 --- a/services.go +++ b/services.go @@ -12,10 +12,11 @@ import ( type Services struct { cfg config + keyFileName string } func NewServices(cfg config) *Services { - return &Services{cfg: cfg} + return &Services{cfg: cfg, keyFileName: "id_ecdsa"} } func (s *Services) AddPrivateKey() error { @@ -25,7 +26,7 @@ func (s *Services) AddPrivateKey() error { return err } - if err := writeFile(`${HOME}/.ssh/id_rsa`, s.cfg.PrivateKey, 0400); err != nil { + if err := writeFile(`${HOME}/.ssh/` + s.keyFileName, s.cfg.PrivateKey, 0400); err != nil { return err } @@ -39,9 +40,11 @@ func (s *Services) ConfigureSSH() error { if err := sshConfigTemplate.Execute(&bfr, struct { Cfg config Home string + KeyFileName string }{ Cfg: s.cfg, Home: os.Getenv("HOME"), + KeyFileName: s.keyFileName, }); err != nil { return err } @@ -115,8 +118,7 @@ func runCmdExpectingOutput(cmd ...string) (string, error) { func writeFile(path string, content string, mode os.FileMode) error { fullPath := os.ExpandEnv(path) - log.Printf(" .. [file] %v (%v bytes)", fullPath, len(content)) - log.Printf(" .. [file] %v", content) + log.Printf(" .. [file] %v (%v bytes)", fullPath, len(content)) return os.WriteFile(fullPath, []byte(content), mode) } @@ -129,6 +131,6 @@ Host * Host {{.Cfg.HostName}} HostName {{.Cfg.HostName}} User dokku - IdentityFile {{.Home}}/.ssh/id_rsa + IdentityFile {{.Home}}/.ssh/{{.KeyFileName}} `)) ) From 366102b55bd788ab375d736cac296c7cbc40ddc0 Mon Sep 17 00:00:00 2001 From: lmika Date: Tue, 16 Jun 2026 22:46:25 +0000 Subject: [PATCH 9/9] Added trailing space to the private key --- services.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/services.go b/services.go index cb2ea0a..c4548e3 100644 --- a/services.go +++ b/services.go @@ -26,7 +26,8 @@ func (s *Services) AddPrivateKey() error { return err } - if err := writeFile(`${HOME}/.ssh/` + s.keyFileName, s.cfg.PrivateKey, 0400); err != nil { + privateKey := strings.TrimSpace(s.cfg.PrivateKey) + "\n" + if err := writeFile(`${HOME}/.ssh/` + s.keyFileName, privateKey, 0400); err != nil { return err }