GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — func/cmd/config_ci.go at main · stackbuilders/func

Expand file tree

/

config_ci.go

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
342 lines (293 loc) · 9.42 KB
/

config_ci.go

Copy path

File metadata and controls

342 lines (293 loc) · 9.42 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package cmd
import (
"fmt"
"io"
"regexp"
"strings"
"github.com/ory/viper"
"github.com/spf13/cobra"
"knative.dev/func/cmd/common"
"knative.dev/func/pkg/ci/github"
fn "knative.dev/func/pkg/functions"
"knative.dev/func/pkg/version"
)
// gitDescribeSuffix matches the "-N-gHASH" suffix produced by git describe
// on commits that are not exactly on a release tag.
var gitDescribeSuffix = regexp.MustCompile(`-\d+-g[0-9a-f]+$`)
// ciGeneratorFactory creates a CIGenerator from resolved CLI flag values.
// Using a factory allows tests to capture the resolved config and inject
// a mock generator without running the real implementation.
type ciGeneratorFactory func(github.WorkflowConfig, github.WorkflowWriter, io.Writer, bool) fn.CIGenerator
const (
ConfigCIFeatureFlag = "FUNC_ENABLE_CI_CONFIG"
pathFlag = "path"
platformFlag = "platform"
branchFlag = "branch"
workflowNameFlag = "workflow-name"
kubeconfigSecretNameFlag = "kubeconfig-secret-name"
registryLoginUrlVariableNameFlag = "registry-login-url-variable-name"
registryUserVariableNameFlag = "registry-user-variable-name"
registryPassSecretNameFlag = "registry-pass-secret-name"
registryUrlVariableNameFlag = "registry-url-variable-name"
registryLoginFlag = "registry-login"
workflowDispatchFlag = "workflow-dispatch"
remoteBuildFlag = "remote"
selfHostedRunnerFlag = "self-hosted-runner"
testStepFlag = "test-step"
forceFlag = "force"
verboseFlag = "verbose"
)
func NewConfigCICmd(
loaderSaver common.FunctionLoaderSaver,
workflowWriter github.WorkflowWriter,
currentBranch common.CurrentBranchFunc,
workingDir common.WorkDirFunc,
newCIGenerator ciGeneratorFactory,
newClient ClientFactory,
) *cobra.Command {
cmd := &cobra.Command{
Use: "ci",
Short: "Generate a GitHub Workflow for function deployment",
PreRunE: bindEnv(
pathFlag,
platformFlag,
registryLoginFlag,
workflowNameFlag,
kubeconfigSecretNameFlag,
registryLoginUrlVariableNameFlag,
registryUserVariableNameFlag,
registryPassSecretNameFlag,
registryUrlVariableNameFlag,
workflowDispatchFlag,
remoteBuildFlag,
selfHostedRunnerFlag,
testStepFlag,
branchFlag,
forceFlag,
verboseFlag,
),
RunE: func(cmd *cobra.Command, args []string) (err error) {
// Detect explicit config via CLI flag or env var
workflowNameExplicit :=
cmd.Flags().Changed(workflowNameFlag) || viper.IsSet(workflowNameFlag)
return runConfigCIGitHub(
cmd,
workflowWriter,
loaderSaver,
currentBranch,
workingDir,
workflowNameExplicit,
newCIGenerator,
newClient,
)
},
}
addPathFlag(cmd)
cmd.Flags().String(
platformFlag,
github.DefaultPlatform,
"Pick a CI/CD platform for which a manifest will be generated. Currently only GitHub is supported.",
)
cmd.Flags().String(
branchFlag,
"",
"Use a custom branch name in the workflow",
)
cmd.Flags().String(
workflowNameFlag,
github.DefaultWorkflowName,
"Use a custom workflow name",
)
cmd.Flags().String(
kubeconfigSecretNameFlag,
github.DefaultKubeconfigSecretName,
"Use a custom secret name in the workflow, e.g. secret.YOUR_CUSTOM_KUBECONFIG",
)
cmd.Flags().String(
registryLoginUrlVariableNameFlag,
github.DefaultRegistryLoginUrlVariableName,
"Use a custom registry login url variable name in the workflow, e.g. vars.YOUR_REGISTRY_LOGIN_URL",
)
cmd.Flags().String(
registryUserVariableNameFlag,
github.DefaultRegistryUserVariableName,
"Use a custom registry user variable name in the workflow, e.g. vars.YOUR_REGISTRY_USER",
)
cmd.Flags().String(
registryPassSecretNameFlag,
github.DefaultRegistryPassSecretName,
"Use a custom registry pass secret name in the workflow, e.g. secret.YOUR_REGISTRY_PASSWORD",
)
cmd.Flags().String(
registryUrlVariableNameFlag,
github.DefaultRegistryUrlVariableName,
"Use a custom registry url variable name in the workflow, e.g. vars.YOUR_REGISTRY_URL",
)
cmd.Flags().Bool(
registryLoginFlag,
github.DefaultRegistryLogin,
"Add a registry login step in the github workflow",
)
cmd.Flags().Bool(
workflowDispatchFlag,
github.DefaultWorkflowDispatch,
"Add a workflow dispatch trigger for manual workflow execution",
)
_ = cmd.Flags().MarkHidden(workflowDispatchFlag)
cmd.Flags().Bool(
remoteBuildFlag,
github.DefaultRemoteBuild,
"Build the function on a Tekton-enabled cluster",
)
cmd.Flags().Bool(
selfHostedRunnerFlag,
github.DefaultSelfHostedRunner,
"Use a 'self-hosted' runner instead of the default 'ubuntu-latest' for local runner execution",
)
cmd.Flags().Bool(
testStepFlag,
github.DefaultTestStep,
"Add a language-specific test step (supported: go, node, typescript, python, quarkus)",
)
cmd.Flags().Bool(
forceFlag,
github.DefaultForce,
"Use to overwrite an existing GitHub workflow",
)
addVerboseFlag(cmd, github.DefaultVerbose)
return cmd
}
func runConfigCIGitHub(
cmd *cobra.Command,
workflowWriter github.WorkflowWriter,
fnLoaderSaver common.FunctionLoaderSaver,
currentBranch common.CurrentBranchFunc,
workingDir common.WorkDirFunc,
workflowNameExplicit bool,
newCIGenerator ciGeneratorFactory,
newClient ClientFactory,
) error {
cfg, f, err := newConfigAndLoadedFunc(
fnLoaderSaver,
currentBranch,
workingDir,
workflowNameExplicit,
)
if err != nil {
return err
}
verbose := viper.GetBool(verboseFlag)
ciGenerator := newCIGenerator(cfg, workflowWriter, cmd.OutOrStdout(), verbose)
client, done := newClient(
ClientConfig{Verbose: verbose},
fn.WithCIGenerator(ciGenerator),
)
defer done()
if err := client.GenerateCIWorkflow(cmd.Context(), f); err != nil {
return err
}
return nil
}
func newConfigAndLoadedFunc(
fnLoader common.FunctionLoader,
currentBranch common.CurrentBranchFunc,
workingDir common.WorkDirFunc,
workflowNameExplicit bool,
) (github.WorkflowConfig, fn.Function, error) {
if err := resolvePlatform(); err != nil {
return github.WorkflowConfig{}, fn.Function{}, err
}
path, err := resolvePath(workingDir)
if err != nil {
return github.WorkflowConfig{}, fn.Function{}, err
}
branch, err := resolveBranch(path, currentBranch)
if err != nil {
return github.WorkflowConfig{}, fn.Function{}, err
}
workflowName := resolveWorkflowName(workflowNameExplicit)
f, err := fnLoader.Load(path)
if err != nil {
return github.WorkflowConfig{}, fn.Function{}, err
}
return github.WorkflowConfig{
GithubWorkflowDir: github.DefaultGitHubWorkflowDir,
GithubWorkflowFilename: github.DefaultGitHubWorkflowFilename,
Branch: branch,
WorkflowName: workflowName,
KubeconfigSecret: viper.GetString(kubeconfigSecretNameFlag),
RegistryLoginUrlVar: viper.GetString(registryLoginUrlVariableNameFlag),
RegistryUserVar: viper.GetString(registryUserVariableNameFlag),
RegistryPassSecret: viper.GetString(registryPassSecretNameFlag),
RegistryUrlVar: viper.GetString(registryUrlVariableNameFlag),
FuncCliVersion: gitDescribeSuffix.ReplaceAllString(version.Kver, ""),
RegistryLogin: viper.GetBool(registryLoginFlag),
SelfHostedRunner: viper.GetBool(selfHostedRunnerFlag),
RemoteBuild: viper.GetBool(remoteBuildFlag),
WorkflowDispatch: viper.GetBool(workflowDispatchFlag),
TestStep: viper.GetBool(testStepFlag),
Force: viper.GetBool(forceFlag),
}, f, nil
}
func resolvePlatform() error {
platform := viper.GetString(platformFlag)
if platform == "" {
return fmt.Errorf("platform must not be empty, supported: %s", github.DefaultPlatform)
}
if strings.ToLower(platform) != github.DefaultPlatform {
return fmt.Errorf("%s support is not implemented, supported: %s", platform, github.DefaultPlatform)
}
return nil
}
func resolvePath(workingDir common.WorkDirFunc) (string, error) {
path := viper.GetString(pathFlag)
if path != "" && path != "." {
return path, nil
}
cwd, err := workingDir()
if err != nil {
return "", err
}
return cwd, nil
}
func resolveBranch(path string, currentBranch common.CurrentBranchFunc) (string, error) {
branch := viper.GetString(branchFlag)
if branch != "" {
return branch, nil
}
branch, err := currentBranch(path)
if err != nil {
return "", err
}
return branch, nil
}
func resolveWorkflowName(explicit bool) string {
workflowName := viper.GetString(workflowNameFlag)
if explicit {
return workflowName
}
if viper.GetBool(remoteBuildFlag) {
return github.DefaultRemoteBuildWorkflowName
}
return github.DefaultWorkflowName
}
// NewCIGeneratorFactory returns the default production factory that
// constructs a github.WorkflowGenerator from resolved flag values.
func NewCIGeneratorFactory() ciGeneratorFactory {
return func(cfg github.WorkflowConfig, ww github.WorkflowWriter, mw io.Writer, v bool) fn.CIGenerator {
return github.NewWorkflowGenerator(
github.WithWorkflowConfig(cfg),
github.WithWorkflowWriter(ww),
github.WithMessageWriter(mw),
github.WithVerbose(v),
)
}
}
// NewTestCIGeneratorFactory returns a test factory that captures the resolved
// WorkflowConfig and returns the given mock as the CIGenerator.
func NewTestCIGeneratorFactory(mock *github.WorkflowGeneratorMock) ciGeneratorFactory {
return func(cfg github.WorkflowConfig, _ github.WorkflowWriter, _ io.Writer, _ bool) fn.CIGenerator {
mock.Config = cfg
return mock
}
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.