codeberg-pages-deploy/plugin/plugin.go
Dorian Zedler 13e379150f
Some checks failed
continuous-integration/drone Build is failing
Feat: first working version
2022-07-23 20:28:41 +02:00

158 lines
2.9 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"`
}
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")); 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 == "" {
return errors.New("PLUGIN_GIT_REMOTE is required!")
}
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("mv", 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", BRANCH_NAME, "/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, BRANCH_NAME, BRANCH_NAME, 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()
}