From 58a7f5bd1e41751a01d2201b3991d4c593be6653 Mon Sep 17 00:00:00 2001 From: Dorian Zedler Date: Fri, 3 Nov 2023 09:26:41 +0100 Subject: [PATCH] Fix: don't use rsync --- README.md | 25 +++++++++++++------------ go.mod | 7 ++++++- go.sum | 6 ++++++ main.go | 40 +++++++++++++++++++++++++++------------- 4 files changed, 52 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 5b370ee..6c9fe2a 100644 --- a/README.md +++ b/README.md @@ -41,15 +41,16 @@ 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 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 | -| 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` | \ No newline at end of file +| parameter | description | default | +| ------------- | ------------------------------------------------------------ | --------------------------------------------- | +| 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 | +| git_name | The name to use for the commit | `[BOT] pages deployer` | +| git_email | The email to use for the commit | `noreply@pages.bot` | +| commit_message | The commit message | `Update pages 🚀` | +| 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` | \ No newline at end of file diff --git a/go.mod b/go.mod index 6af4c49..90ccf3e 100644 --- a/go.mod +++ b/go.mod @@ -4,4 +4,9 @@ go 1.21.0 require github.com/appleboy/drone-git-push v1.0.5 -require github.com/kelseyhightower/envconfig v1.4.0 // indirect +require ( + github.com/kelseyhightower/envconfig v1.4.0 // indirect + github.com/otiai10/copy v1.14.0 // indirect + golang.org/x/sync v0.3.0 // indirect + golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect +) diff --git a/go.sum b/go.sum index 75c778f..b37b9bc 100644 --- a/go.sum +++ b/go.sum @@ -2,3 +2,9 @@ github.com/appleboy/drone-git-push v1.0.5 h1:2ndC+lcFpsbnaIt9dZ0acniewJnSAh6SOqD github.com/appleboy/drone-git-push v1.0.5/go.mod h1:A71Q1/0OVhVe872aipwdVrpcCyz2VxhspfZN1nAUZWg= github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8= github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg= +github.com/otiai10/copy v1.14.0 h1:dCI/t1iTdYGtkvCuBG2BgR6KZa83PTclw4U5n2wAllU= +github.com/otiai10/copy v1.14.0/go.mod h1:ECfuL02W+/FkTWZWgQqXPWZgW9oeKCSQ5qVfSc4qc4w= +golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= +golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/main.go b/main.go index 4ff6af7..8f722db 100644 --- a/main.go +++ b/main.go @@ -14,22 +14,24 @@ import ( "strings" "github.com/kelseyhightower/envconfig" + "github.com/otiai10/copy" "github.com/appleboy/drone-git-push/repo" ) // 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"` + 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"` + CommitMessage string `envconfig:"INPUT_COMMIT_MESSAGE" default:"Update pages 🚀"` + 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"` @@ -180,8 +182,20 @@ func copyFiles(args Args) error { 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 { + // if remote folder is a subdir, delete it + if args.RemoteFolder != "/" { + if err := os.RemoveAll("/tmp/pages" + args.RemoteFolder); err != nil { + return err + } + } + + opt := copy.Options{ + Skip: func(info os.FileInfo, src, dest string) (bool, error) { + return strings.HasSuffix(src, ".git"), nil + }, + } + + if err := copy.Copy(args.Folder, "/tmp/pages"+args.RemoteFolder, opt); err != nil { return err } @@ -251,7 +265,7 @@ func doCommit(args Args) error { return err } - if err := execute(repo.ForceCommit("Update pages 🚀", true, args.GitName, args.GitEmail)); err != nil { + if err := execute(repo.ForceCommit(args.CommitMessage, true, args.GitName, args.GitEmail)); err != nil { return err }