codeberg-pages-deploy/plugin/plugin.go

169 lines
3.3 KiB
Go
Raw Permalink Normal View History

2022-07-17 18:28:14 +02:00
// 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
2022-07-23 20:28:41 +02:00
import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/appleboy/drone-git-push/repo"
)
2022-07-17 18:28:14 +02:00
// Args provides plugin execution arguments.
type Args struct {
Pipeline
// Level defines the plugin log level.
Level string `envconfig:"PLUGIN_LOG_LEVEL"`
2022-07-23 20:54:33 +02:00
Folder string `envconfig:"PLUGIN_FOLDER"`
SshKey string `envconfig:"PLUGIN_SSH_KEY"`
GitRemote string `envconfig:"PLUGIN_GIT_REMOTE"`
2022-07-23 21:17:55 +02:00
GitBranch string `envconfig:"PLUGIN_GIT_BRANCH"`
2022-07-23 20:54:33 +02:00
GitRemoteFromDrone string `envconfig:"DRONE_GIT_SSH_URL"`
2022-07-17 18:28:14 +02:00
}
2022-07-23 20:28:41 +02:00
const BRANCH_NAME = "pages"
2022-07-17 18:28:14 +02:00
// Exec executes the plugin.
func Exec(ctx context.Context, args Args) error {
2022-07-23 20:28:41 +02:00
2022-07-23 20:54:33 +02:00
if err := checkArgs(&args); err != nil {
2022-07-23 20:28:41 +02:00
return err
}
2022-07-23 20:44:55 +02:00
if err := repo.WriteKey(strings.ReplaceAll(args.SshKey, "\\n", "\n") + "\n"); err != nil {
2022-07-23 20:28:41 +02:00
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
}
2022-07-17 18:28:14 +02:00
// write code here
return nil
}
2022-07-23 20:28:41 +02:00
2022-07-23 20:54:33 +02:00
func checkArgs(args *Args) error {
2022-07-23 20:28:41 +02:00
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!")
}
2022-07-23 20:54:33 +02:00
if args.GitRemote == "" && args.GitRemoteFromDrone == "" {
2022-07-23 20:28:41 +02:00
return errors.New("PLUGIN_GIT_REMOTE is required!")
}
2022-07-23 20:54:33 +02:00
if args.GitRemote == "" && args.GitRemoteFromDrone != "" {
args.GitRemote = args.GitRemoteFromDrone
}
2022-07-23 21:17:55 +02:00
if args.GitBranch == "" {
args.GitBranch = BRANCH_NAME
}
2022-07-23 20:28:41 +02:00
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
}
2022-08-06 21:49:35 +02:00
cmd := exec.Command("cp", "-r", folder, "/tmp/pages")
2022-07-23 20:28:41 +02:00
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",
2022-07-23 21:17:55 +02:00
"init", "-b", args.GitBranch, "/tmp/pages")
2022-07-23 20:28:41 +02:00
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 {
2022-07-23 21:17:55 +02:00
return execute(repo.RemotePushNamedBranch(args.GitRemote, args.GitBranch, args.GitBranch, true, false))
2022-07-23 20:28:41 +02:00
}
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()
}