Compare commits
No commits in common. "main" and "v1" have entirely different histories.
3 changed files with 12 additions and 74 deletions
25
README.md
25
README.md
|
|
@ -1,26 +1,3 @@
|
||||||
# push-to-dokku
|
# push-to-dokku
|
||||||
|
|
||||||
Action to push a remote repository to Dokku.
|
Action to push to Dokku
|
||||||
|
|
||||||
## Usage
|
|
||||||
|
|
||||||
```
|
|
||||||
uses: https://lmika.dev/actions/push-to-dokku@v1.1
|
|
||||||
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
|
|
||||||
- `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.
|
|
||||||
|
|
||||||
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`.
|
|
||||||
16
config.go
16
config.go
|
|
@ -3,19 +3,15 @@ package main
|
||||||
import "os"
|
import "os"
|
||||||
|
|
||||||
type config struct {
|
type config struct {
|
||||||
HostName string
|
HostName string
|
||||||
AppName string
|
AppName string
|
||||||
PrivateKey string
|
PrivateKey string
|
||||||
LocalBranch string
|
|
||||||
RemoteBranch string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func readConfig() config {
|
func readConfig() config {
|
||||||
return config{
|
return config{
|
||||||
HostName: os.Getenv("INPUT_HOST"),
|
HostName: os.Getenv("INPUT_HOST"),
|
||||||
AppName: os.Getenv("INPUT_APP"),
|
AppName: os.Getenv("INPUT_APP"),
|
||||||
PrivateKey: os.Getenv("INPUT_PRIVATE-KEY"),
|
PrivateKey: os.Getenv("INPUT_PRIVATE-KEY"),
|
||||||
LocalBranch: os.Getenv("INPUT_SOURCE-BRANCH"),
|
|
||||||
RemoteBranch: os.Getenv("INPUT_TARGET-BRANCH"),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
45
services.go
45
services.go
|
|
@ -8,16 +8,14 @@ import (
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Services struct {
|
type Services struct {
|
||||||
cfg config
|
cfg config
|
||||||
keyFileName string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServices(cfg config) *Services {
|
func NewServices(cfg config) *Services {
|
||||||
return &Services{cfg: cfg, keyFileName: "id_ecdsa"}
|
return &Services{cfg: cfg}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Services) AddPrivateKey() error {
|
func (s *Services) AddPrivateKey() error {
|
||||||
|
|
@ -27,8 +25,7 @@ func (s *Services) AddPrivateKey() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
privateKey := strings.TrimSpace(s.cfg.PrivateKey) + "\n"
|
if err := writeFile(`${HOME}/.ssh/id_rsa`, s.cfg.PrivateKey, 0400); err != nil {
|
||||||
if err := writeFile(`${HOME}/.ssh/` + s.keyFileName, privateKey, 0400); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -42,11 +39,9 @@ func (s *Services) ConfigureSSH() error {
|
||||||
if err := sshConfigTemplate.Execute(&bfr, struct {
|
if err := sshConfigTemplate.Execute(&bfr, struct {
|
||||||
Cfg config
|
Cfg config
|
||||||
Home string
|
Home string
|
||||||
KeyFileName string
|
|
||||||
}{
|
}{
|
||||||
Cfg: s.cfg,
|
Cfg: s.cfg,
|
||||||
Home: os.Getenv("HOME"),
|
Home: os.Getenv("HOME"),
|
||||||
KeyFileName: s.keyFileName,
|
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -72,26 +67,7 @@ func (s *Services) PushRepository() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
sourceBranch := s.cfg.LocalBranch
|
if err := runCmd("git", "push", "dokku", "main"); err != nil {
|
||||||
if sourceBranch == "" {
|
|
||||||
makeTempBranch := fmt.Sprintf("release_%v", time.Now().Format(`20060102150405`))
|
|
||||||
if err := runCmd("git", "checkout", "-b", makeTempBranch); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
sourceBranch = strings.TrimSpace(makeTempBranch)
|
|
||||||
}
|
|
||||||
remoteBranch := s.cfg.RemoteBranch
|
|
||||||
if remoteBranch == "" {
|
|
||||||
remoteBranch = "main"
|
|
||||||
}
|
|
||||||
|
|
||||||
branchArg := sourceBranch
|
|
||||||
if sourceBranch != remoteBranch {
|
|
||||||
branchArg = fmt.Sprintf("%v:%v", sourceBranch, remoteBranch)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := runCmd("git", "push", "dokku", branchArg); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -106,20 +82,9 @@ func runCmd(cmd ...string) error {
|
||||||
return e.Run()
|
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 {
|
func writeFile(path string, content string, mode os.FileMode) error {
|
||||||
fullPath := os.ExpandEnv(path)
|
fullPath := os.ExpandEnv(path)
|
||||||
log.Printf(" .. [file] %v (%v bytes)", fullPath, len(content))
|
log.Printf(" .. [file] %v (%v bytes)", fullPath, len(content))
|
||||||
|
|
||||||
return os.WriteFile(fullPath, []byte(content), mode)
|
return os.WriteFile(fullPath, []byte(content), mode)
|
||||||
}
|
}
|
||||||
|
|
@ -132,6 +97,6 @@ Host *
|
||||||
Host {{.Cfg.HostName}}
|
Host {{.Cfg.HostName}}
|
||||||
HostName {{.Cfg.HostName}}
|
HostName {{.Cfg.HostName}}
|
||||||
User dokku
|
User dokku
|
||||||
IdentityFile {{.Home}}/.ssh/{{.KeyFileName}}
|
IdentityFile {{.Home}}/.ssh/id_rsa
|
||||||
`))
|
`))
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue