-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
87 lines (80 loc) · 1.87 KB
/
Copy pathmain.go
File metadata and controls
87 lines (80 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"errors"
"fmt"
"log"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"
"github.com/spf13/viper"
)
var Version string
const remoteName = "drone"
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()
}
func pluginExec() error {
if viper.GetBool("skip-verify") {
cmd := exec.Command("git", "config", "--global", "http.sslVerify", "false")
if err := execute(cmd); err != nil {
return err
}
}
sshKey := viper.GetString("ssh-key")
if sshKey == "" {
return errors.New("missing ssh key")
}
home := "/root"
if currentUser, err := user.Current(); err == nil {
home = currentUser.HomeDir
}
sshpath := filepath.Join(home, ".ssh")
if err := os.MkdirAll(sshpath, 0o700); err != nil {
return err
}
_ = os.WriteFile(filepath.Join(sshpath, "config"), []byte("StrictHostKeyChecking no\n"), 0o700)
err := os.WriteFile(filepath.Join(sshpath, "id_rsa"), []byte(sshKey), 0o600)
if err != nil {
return err
}
remote := viper.GetString("remote")
if remote != "" {
cmd := exec.Command("git", "remote", "add", remoteName, remote)
if err := execute(cmd); err != nil {
return err
}
}
ref := ""
switch os.Getenv("DRONE_BUILD_EVENT") {
case "tag":
ref = os.Getenv("DRONE_TAG")
cmd := exec.Command("git", "tag", "-a", ref, "-m", "Ver: "+ref)
if err := execute(cmd); err != nil {
return err
}
case "push":
ref = os.Getenv("DRONE_BRANCH")
}
if ref == "" {
return errors.New("missing ref")
}
cmd := exec.Command("git", "push", remoteName, ref)
if viper.GetBool("force") {
cmd.Args = append(cmd.Args, "--force")
}
return execute(cmd)
}
func main() {
viper.SetEnvPrefix("PLUGIN")
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
viper.AutomaticEnv()
if err := pluginExec(); err != nil {
log.Fatal(err)
}
}