Feat: push without force to a subfolder of remote repo

This commit is contained in:
Dorian Zedler 2023-11-03 08:48:06 +01:00
parent cd3e03ad7d
commit 29d2eca187
No known key found for this signature in database
GPG key ID: 989DE36109AFA354
2 changed files with 105 additions and 30 deletions

View file

@ -42,10 +42,14 @@ Note: you have to either use an image which includes go or set it up before runn
## Options
| parameter | description | default |
| ---------- | ----------------------------------------------------------- | --------------------------------------------- |
| folder | The folder to deploy | |
| ------------- | ------------------------------------------------------------ | --------------------------------------------- |
| folder | The folder to deploy from | |
| remote_folder | The folder to deploy to in the remote repo | `/` |
| ssh_key | The private ssh key to use if pushing to an ssh remote | empty |
| git_remote | A custom git remote to push to | the current repo |
| git_branch | The branch to push to | pages |
| token | The token/password to use if pusing to a custom http remote | the gitea-supplied token (aka `GITHUB_TOKEN`) |
| username | The username to use if pusing to a custom http remote | the actor (aka `GITHUB_ACTOR`) |
| git_name | The name to use for the commit | `[BOT] pages deployer` |
| git_email | The email to use for the commit | `noreply@pages.bot` |
| token | The token/password to use if pushing to a custom http remote | the gitea-supplied token (aka `GITHUB_TOKEN`) |
| username | The username to use if pushing to a custom http remote | the actor (aka `GITHUB_ACTOR`) |
| force | Force commit and overwrite all previous commits | `false` |

101
main.go
View file

@ -21,11 +21,15 @@ import (
// Args provides plugin execution arguments.
type Args struct {
Folder string `required:"true" envconfig:"INPUT_FOLDER"`
RemoteFolder string `envconfig:"INPUT_REMOTE_FOLDER" default:"/"`
SshKey string `envconfig:"INPUT_SSH_KEY"`
GitRemote string `envconfig:"INPUT_GIT_REMOTE"`
GitBranch string `envconfig:"INPUT_GIT_BRANCH"`
GitName string `envconfig:"INPUT_GIT_NAME" default:"[BOT] pages deployer"`
GitEmail string `envconfig:"INPUT_GIT_EMAIL" default:"noreply@pages.bot"`
Token string `envconfig:"INPUT_TOKEN"`
Username string `envconfig:"INPUT_USERNAME"`
Force bool `envconfig:"INPUT_FORCE" default:"false"`
GithubToken string `envconfig:"GITHUB_TOKEN"`
GithubTokenActor string `envconfig:"GITHUB_ACTOR"`
@ -65,13 +69,23 @@ func Exec(args Args) error {
}
}
if err := copyFiles(args); err != nil {
if err := cleanTempDir(); err != nil {
return err
}
if args.Force {
if err := initRepo(args); err != nil {
return err
}
} else {
if err := cloneRepo(args); err != nil {
return err
}
}
if err := copyFiles(args); err != nil {
return err
}
if err := writeConfig(args); err != nil {
return err
@ -118,22 +132,34 @@ func checkArgs(args *Args) error {
args.GitBranch = BRANCH_NAME
}
folder, err := filepath.Abs(args.Folder)
if err != nil {
return err
}
args.Folder = folder
_, err = os.Stat(args.Folder)
if err != nil {
return err
}
return nil
}
func writeConfig(args Args) error {
if err := execute(exec.Command("git", "config", "user.name", "[BOT] pages deployer")); err != nil {
if err := execute(exec.Command("git", "config", "user.name", args.GitName)); err != nil {
return err
}
if err := execute(exec.Command("git", "config", "user.email", "noreply@pages.bot")); err != nil {
if err := execute(exec.Command("git", "config", "user.email", args.GitEmail)); err != nil {
return err
}
return nil
}
func copyFiles(args Args) error {
func cleanTempDir() error {
if err := os.RemoveAll("/tmp/pages"); err != nil {
return err
}
@ -142,12 +168,19 @@ func copyFiles(args Args) error {
return err
}
folder, err := filepath.Abs(args.Folder)
if err != nil {
return err
return nil
}
cmd := exec.Command("cp", "-r", folder, "/tmp/pages")
func copyFiles(args Args) error {
if args.Folder[len(args.Folder)-1:] != "/" {
args.Folder += "/"
}
if args.RemoteFolder[:1] != "/" {
args.RemoteFolder = "/" + args.RemoteFolder
}
cmd := exec.Command("rsync", "-av", args.Folder, "/tmp/pages"+args.RemoteFolder, "--exclude", ".git", "--ignore-times", "--delete")
if err := execute(cmd); err != nil {
return err
}
@ -156,11 +189,7 @@ func copyFiles(args Args) error {
return err
}
if err := os.RemoveAll("/tmp/pages/.git"); err != nil {
return err
}
return os.Chdir("/tmp/pages")
return nil
}
func initRepo(args Args) error {
@ -172,6 +201,48 @@ func initRepo(args Args) error {
return err
}
return os.Chdir("/tmp/pages")
}
func cloneRepo(args Args) error {
cmd := exec.Command(
"git",
"clone", args.GitRemote, "/tmp/pages")
if err := execute(cmd); err != nil {
return err
}
// check if branch exists and create if not
cmd = exec.Command(
"git",
"ls-remote", "--heads", args.GitRemote, args.GitBranch)
out, err := cmd.Output()
if err != nil {
return err
}
fmt.Println("remote branch: ", string(out))
if err := os.Chdir("/tmp/pages"); err != nil {
return err
}
if len(out) == 0 {
cmd = exec.Command(
"git",
"checkout", "-b", args.GitBranch)
} else {
cmd = exec.Command(
"git",
"checkout", args.GitBranch)
}
if err := execute(cmd); err != nil {
return err
}
return nil
}
@ -180,7 +251,7 @@ func doCommit(args Args) error {
return err
}
if err := execute(repo.ForceCommit("Update pages 🚀", true, "[BOT] pages deployer", "noreply@pages.bot")); err != nil {
if err := execute(repo.ForceCommit("Update pages 🚀", true, args.GitName, args.GitEmail)); err != nil {
return err
}
@ -188,7 +259,7 @@ func doCommit(args Args) error {
}
func push(args Args) error {
return execute(repo.RemotePushNamedBranch(args.GitRemote, args.GitBranch, args.GitBranch, true, false))
return execute(repo.RemotePushNamedBranch(args.GitRemote, args.GitBranch, args.GitBranch, args.Force, false))
}
func execute(cmd *exec.Cmd) error {