mirror of
https://itsblue.dev/actions/codeberg-pages-deploy.git
synced 2024-11-18 06:49:42 +00:00
Feat: push without force to a subfolder of remote repo
This commit is contained in:
parent
cd3e03ad7d
commit
29d2eca187
2 changed files with 105 additions and 30 deletions
20
README.md
20
README.md
|
@ -41,11 +41,15 @@ Note: you have to either use an image which includes go or set it up before runn
|
||||||
|
|
||||||
## Options
|
## Options
|
||||||
|
|
||||||
| parameter | description | default |
|
| parameter | description | default |
|
||||||
| ---------- | ----------------------------------------------------------- | --------------------------------------------- |
|
| ------------- | ------------------------------------------------------------ | --------------------------------------------- |
|
||||||
| folder | The folder to deploy | |
|
| folder | The folder to deploy from | |
|
||||||
| ssh_key | The private ssh key to use if pushing to an ssh remote | empty |
|
| remote_folder | The folder to deploy to in the remote repo | `/` |
|
||||||
| git_remote | A custom git remote to push to | the current repo |
|
| ssh_key | The private ssh key to use if pushing to an ssh remote | empty |
|
||||||
| git_branch | The branch to push to | pages |
|
| git_remote | A custom git remote to push to | the current repo |
|
||||||
| token | The token/password to use if pusing to a custom http remote | the gitea-supplied token (aka `GITHUB_TOKEN`) |
|
| git_branch | The branch to push to | pages |
|
||||||
| 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` |
|
115
main.go
115
main.go
|
@ -20,12 +20,16 @@ import (
|
||||||
|
|
||||||
// Args provides plugin execution arguments.
|
// Args provides plugin execution arguments.
|
||||||
type Args struct {
|
type Args struct {
|
||||||
Folder string `required:"true" envconfig:"INPUT_FOLDER"`
|
Folder string `required:"true" envconfig:"INPUT_FOLDER"`
|
||||||
SshKey string `envconfig:"INPUT_SSH_KEY"`
|
RemoteFolder string `envconfig:"INPUT_REMOTE_FOLDER" default:"/"`
|
||||||
GitRemote string `envconfig:"INPUT_GIT_REMOTE"`
|
SshKey string `envconfig:"INPUT_SSH_KEY"`
|
||||||
GitBranch string `envconfig:"INPUT_GIT_BRANCH"`
|
GitRemote string `envconfig:"INPUT_GIT_REMOTE"`
|
||||||
Token string `envconfig:"INPUT_TOKEN"`
|
GitBranch string `envconfig:"INPUT_GIT_BRANCH"`
|
||||||
Username string `envconfig:"INPUT_USERNAME"`
|
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"`
|
GithubToken string `envconfig:"GITHUB_TOKEN"`
|
||||||
GithubTokenActor string `envconfig:"GITHUB_ACTOR"`
|
GithubTokenActor string `envconfig:"GITHUB_ACTOR"`
|
||||||
|
@ -65,11 +69,21 @@ func Exec(args Args) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := copyFiles(args); err != nil {
|
if err := cleanTempDir(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := initRepo(args); err != nil {
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -118,22 +132,34 @@ func checkArgs(args *Args) error {
|
||||||
args.GitBranch = BRANCH_NAME
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeConfig(args Args) error {
|
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
|
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 err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func copyFiles(args Args) error {
|
func cleanTempDir() error {
|
||||||
if err := os.RemoveAll("/tmp/pages"); err != nil {
|
if err := os.RemoveAll("/tmp/pages"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -142,12 +168,19 @@ func copyFiles(args Args) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
folder, err := filepath.Abs(args.Folder)
|
return nil
|
||||||
if err != nil {
|
}
|
||||||
return err
|
|
||||||
|
func copyFiles(args Args) error {
|
||||||
|
if args.Folder[len(args.Folder)-1:] != "/" {
|
||||||
|
args.Folder += "/"
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := exec.Command("cp", "-r", folder, "/tmp/pages")
|
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 {
|
if err := execute(cmd); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -156,11 +189,7 @@ func copyFiles(args Args) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := os.RemoveAll("/tmp/pages/.git"); err != nil {
|
return nil
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return os.Chdir("/tmp/pages")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func initRepo(args Args) error {
|
func initRepo(args Args) error {
|
||||||
|
@ -172,6 +201,48 @@ func initRepo(args Args) error {
|
||||||
return err
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -180,7 +251,7 @@ func doCommit(args Args) error {
|
||||||
return err
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -188,7 +259,7 @@ func doCommit(args Args) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func push(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 {
|
func execute(cmd *exec.Cmd) error {
|
||||||
|
|
Loading…
Reference in a new issue