forked from woodpecker-ci/plugin-git
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
194 lines (183 loc) · 4.58 KB
/
main.go
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main
import (
"fmt"
"log"
"os"
"time"
"github.com/adrg/xdg"
"github.com/joho/godotenv"
"github.com/urfave/cli/v2"
)
var version = "0.0.0+0"
func main() {
app := cli.NewApp()
app.Name = "git plugin"
app.Usage = "git plugin"
app.Action = run
app.Version = version
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "remote",
Usage: "git remote url",
EnvVars: []string{"PLUGIN_REMOTE", "CI_REPO_REMOTE", "CI_REMOTE_URL"},
},
&cli.StringFlag{
Name: "path",
Usage: "git clone path",
EnvVars: []string{"PLUGIN_PATH", "CI_WORKSPACE"},
},
&cli.StringFlag{
Name: "sha",
Usage: "git commit sha",
EnvVars: []string{"PLUGIN_SHA", "CI_COMMIT_SHA"},
},
&cli.StringFlag{
Name: "ref",
Value: "refs/heads/master",
Usage: "git commit ref",
EnvVars: []string{"PLUGIN_REF", "CI_COMMIT_REF"},
},
&cli.StringFlag{
Name: "event",
Value: "push",
Usage: "build event",
EnvVars: []string{"CI_BUILD_EVENT"},
},
&cli.StringFlag{
Name: "netrc.machine",
Usage: "netrc machine",
EnvVars: []string{"CI_NETRC_MACHINE"},
},
&cli.StringFlag{
Name: "netrc.username",
Usage: "netrc username",
EnvVars: []string{"CI_NETRC_USERNAME"},
},
&cli.StringFlag{
Name: "netrc.password",
Usage: "netrc password",
EnvVars: []string{"CI_NETRC_PASSWORD"},
},
&cli.IntFlag{
Name: "depth",
Usage: "clone depth",
EnvVars: []string{"PLUGIN_DEPTH"},
},
&cli.BoolFlag{
Name: "recursive",
Usage: "clone submodules",
EnvVars: []string{"PLUGIN_RECURSIVE"},
Value: true,
},
&cli.BoolFlag{
Name: "tags",
Usage: "clone tags, if not explicitly set and event is tag its default is true else false",
EnvVars: []string{"PLUGIN_TAGS"},
},
&cli.BoolFlag{
Name: "skip-verify",
Usage: "skip tls verification",
EnvVars: []string{"PLUGIN_SKIP_VERIFY"},
},
&cli.StringFlag{
Name: "custom-cert",
Usage: "path or url to custom cert",
EnvVars: []string{"PLUGIN_CUSTOM_SSL_PATH", "PLUGIN_CUSTOM_SSL_URL"},
},
&cli.BoolFlag{
Name: "submodule-update-remote",
Usage: "update remote submodules",
EnvVars: []string{"PLUGIN_SUBMODULES_UPDATE_REMOTE", "PLUGIN_SUBMODULE_UPDATE_REMOTE"},
},
&cli.GenericFlag{
Name: "submodule-override",
Usage: "json map of submodule overrides",
EnvVars: []string{"PLUGIN_SUBMODULE_OVERRIDE"},
Value: &MapFlag{},
},
&cli.DurationFlag{
Name: "backoff",
Usage: "backoff duration",
EnvVars: []string{"PLUGIN_BACKOFF"},
Value: 5 * time.Second,
},
&cli.IntFlag{
Name: "backoff-attempts",
Usage: "backoff attempts",
EnvVars: []string{"PLUGIN_ATTEMPTS"},
Value: 5,
},
&cli.BoolFlag{
Name: "lfs",
Usage: "whether to retrieve LFS content if available",
EnvVars: []string{"PLUGIN_LFS"},
Value: true,
},
&cli.StringFlag{
Name: "env-file",
Usage: "source env file",
},
&cli.StringFlag{
Name: "branch",
Usage: "Change branch name",
EnvVars: []string{"PLUGIN_BRANCH", "CI_COMMIT_BRANCH", "CI_REPO_DEFAULT_BRANCH"},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func run(c *cli.Context) error {
if c.String("env-file") != "" {
_ = godotenv.Load(c.String("env-file"))
}
// make sure home dir exist and is set
home := xdg.Home
homeExist, err := pathExists(home)
if err != nil {
return err
}
if !homeExist {
return fmt.Errorf("home dir '%s' do not exist", home)
}
defaultEnvVars = append(defaultEnvVars, "HOME="+home)
event := c.String("event")
tags := c.Bool("tags")
if event == "tag" && !c.IsSet("tags") {
// tags clone not explicit set but pipeline is triggered by a tag
// auto set tags cloning to true
tags = true
}
plugin := Plugin{
Repo: Repo{
Clone: c.String("remote"),
},
Build: Build{
Commit: c.String("sha"),
Event: event,
Path: c.String("path"),
Ref: c.String("ref"),
},
Netrc: Netrc{
Login: c.String("netrc.username"),
Machine: c.String("netrc.machine"),
Password: c.String("netrc.password"),
},
Config: Config{
Depth: c.Int("depth"),
Tags: tags,
Recursive: c.Bool("recursive"),
SkipVerify: c.Bool("skip-verify"),
CustomCert: c.String("custom-cert"),
SubmoduleRemote: c.Bool("submodule-update-remote"),
Submodules: c.Generic("submodule-override").(*MapFlag).Get(),
Lfs: c.Bool("lfs"),
Branch: c.String("branch"),
},
Backoff: Backoff{
Attempts: c.Int("backoff-attempts"),
Duration: c.Duration("backoff"),
},
}
return plugin.Exec()
}