codeberg-pages-deploy/plugin/plugin.go
Dorian Zedler f02931d16a
All checks were successful
continuous-integration/drone/push Build is passing
Fix: copy instead of move
2022-08-06 21:49:35 +02:00

168 lines
3.3 KiB
Go

// Copyright 2020 the Drone Authors. All rights reserved.
// Use of this source code is governed by the Blue Oak Model License
// that can be found in the LICENSE file.
package plugin
import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/appleboy/drone-git-push/repo"
)
// Args provides plugin execution arguments.
type Args struct {
Pipeline
// Level defines the plugin log level.
Level string `envconfig:"PLUGIN_LOG_LEVEL"`
Folder string `envconfig:"PLUGIN_FOLDER"`
SshKey string `envconfig:"PLUGIN_SSH_KEY"`
GitRemote string `envconfig:"PLUGIN_GIT_REMOTE"`
GitBranch string `envconfig:"PLUGIN_GIT_BRANCH"`
GitRemoteFromDrone string `envconfig:"DRONE_GIT_SSH_URL"`
}
const BRANCH_NAME = "pages"
// Exec executes the plugin.
func Exec(ctx context.Context, args Args) error {
if err := checkArgs(&args); err != nil {
return err
}
if err := repo.WriteKey(strings.ReplaceAll(args.SshKey, "\\n", "\n") + "\n"); err != nil {
return err
}
if err := writeConfig(args); err != nil {
return err
}
if err := copyFiles(args); err != nil {
return err
}
if err := initRepo(args); err != nil {
return err
}
if err := doCommit(args); err != nil {
return err
}
if err := push(args); err != nil {
return err
}
// write code here
return nil
}
func checkArgs(args *Args) error {
if args.Folder == "" {
return errors.New("PLUGIN_FOLDER is required!")
}
if folderInfo, err := os.Stat(args.Folder); os.IsNotExist(err) || !folderInfo.IsDir() {
return errors.New("PLUGIN_FOLDER is not a folder!")
}
if args.SshKey == "" {
return errors.New("PLUGIN_SSH_KEY is required!")
}
if args.GitRemote == "" && args.GitRemoteFromDrone == "" {
return errors.New("PLUGIN_GIT_REMOTE is required!")
}
if args.GitRemote == "" && args.GitRemoteFromDrone != "" {
args.GitRemote = args.GitRemoteFromDrone
}
if args.GitBranch == "" {
args.GitBranch = BRANCH_NAME
}
return nil
}
func writeConfig(args Args) error {
if err := repo.GlobalName("[BOT] pages deployer").Run(); err != nil {
return err
}
if err := repo.GlobalUser("noreply@pages.bot").Run(); err != nil {
return err
}
return nil
}
func copyFiles(args Args) error {
if err := os.MkdirAll("/tmp", 777); err != nil {
return err
}
folder, err := filepath.Abs(args.Folder)
if err != nil {
return err
}
cmd := exec.Command("cp", "-r", folder, "/tmp/pages")
if err := execute(cmd); err != nil {
return err
}
if err := os.Chown("/tmp/pages", os.Getuid(), os.Getgid()); err != nil {
return err
}
return os.Chdir("/tmp/pages")
}
func initRepo(args Args) error {
cmd := exec.Command(
"git",
"init", "-b", args.GitBranch, "/tmp/pages")
if err := execute(cmd); err != nil {
return err
}
return nil
}
func doCommit(args Args) error {
if err := execute(repo.Add()); err != nil {
return err
}
if err := execute(repo.ForceCommit("Update pages 🚀", true)); err != nil {
return err
}
return nil
}
func push(args Args) error {
return execute(repo.RemotePushNamedBranch(args.GitRemote, args.GitBranch, args.GitBranch, true, false))
}
func execute(cmd *exec.Cmd) error {
fmt.Println("+", strings.Join(cmd.Args, " "))
cmd.Env = os.Environ()
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
return cmd.Run()
}