Skip to content

Commit cf46238

Browse files
authored
feat: add remote build and workflow dispatch (knative#3128)
Add support for building functions on Tekton-enabled clusters through the new --remote flag, which modifies the deployment workflow to use remote builds and updates the workflow name accordingly. Add workflow_dispatch trigger support via --workflow-dispatch flag to enable manual workflow execution from terminal with GitHub cli or via GitHub UI. Update func CLI GitHub Action from gauron99 to the official functions-dev/action source with version bump to knative-v1.20.1. Issue knative#3256 Signed-off-by: Stanislav Jakuschevskij <[email protected]>
1 parent e7a440b commit cf46238

File tree

6 files changed

+115
-26
lines changed

6 files changed

+115
-26
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ __pycache__
3434
# E2E Tests
3535
/e2e/testdata/default_home/go
3636
/e2e/testdata/default_home/.cache
37-
37+
3838
# Editors
3939
.vscode
4040
.idea

cmd/ci/config.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ const (
3838
UseRegistryLoginFlag = "use-registry-login"
3939
DefaultUseRegistryLogin = true
4040

41+
WorkflowDispatchFlag = "workflow-dispatch"
42+
DefaultWorkflowDispatch = false
43+
44+
UseRemoteBuildFlag = "remote"
45+
DefaultUseRemoteBuild = false
46+
4147
UseSelfHostedRunnerFlag = "self-hosted-runner"
4248
DefaultUseSelfHostedRunner = false
4349
)
@@ -55,7 +61,9 @@ type CIConfig struct {
5561
registryPassSecret,
5662
registryUrlVar string
5763
useRegistryLogin,
58-
useSelfHostedRunner bool
64+
useSelfHostedRunner,
65+
useRemoteBuild,
66+
useWorkflowDispatch bool
5967
}
6068

6169
func NewCIGitHubConfig() CIConfig {
@@ -72,6 +80,8 @@ func NewCIGitHubConfig() CIConfig {
7280
registryUrlVar: viper.GetString(RegistryUrlVariableNameFlag),
7381
useRegistryLogin: viper.GetBool(UseRegistryLoginFlag),
7482
useSelfHostedRunner: viper.GetBool(UseSelfHostedRunnerFlag),
83+
useRemoteBuild: viper.GetBool(UseRemoteBuildFlag),
84+
useWorkflowDispatch: viper.GetBool(WorkflowDispatchFlag),
7585
}
7686
}
7787

@@ -103,6 +113,14 @@ func (cc *CIConfig) UseSelfHostedRunner() bool {
103113
return cc.useSelfHostedRunner
104114
}
105115

116+
func (cc *CIConfig) UseRemoteBuild() bool {
117+
return cc.useRemoteBuild
118+
}
119+
120+
func (cc *CIConfig) UseWorkflowDispatch() bool {
121+
return cc.useWorkflowDispatch
122+
}
123+
106124
func (cc *CIConfig) KubeconfigSecret() string {
107125
return cc.kubeconfigSecret
108126
}

cmd/ci/workflow.go

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ type githubWorkflow struct {
1414
}
1515

1616
type workflowTriggers struct {
17-
Push *pushTrigger `yaml:"push,omitempty"`
17+
Push *pushTrigger `yaml:"push,omitempty"`
18+
WorkflowDispatch *struct{} `yaml:"workflow_dispatch,omitempty"`
1819
}
1920

2021
type pushTrigger struct {
@@ -34,8 +35,9 @@ type step struct {
3435
}
3536

3637
func NewGitHubWorkflow(conf CIConfig) *githubWorkflow {
37-
runsOn := createRunsOn(conf.UseSelfHostedRunner())
38-
pushTrigger := createPushTrigger(conf.Branch())
38+
name := createWorkflowName(conf)
39+
runsOn := createRunsOn(conf)
40+
pushTrigger := createPushTrigger(conf)
3941

4042
var steps []step
4143
steps = createCheckoutStep(steps)
@@ -45,7 +47,7 @@ func NewGitHubWorkflow(conf CIConfig) *githubWorkflow {
4547
steps = createFuncDeployStep(conf, steps)
4648

4749
return &githubWorkflow{
48-
Name: conf.WorkflowName(),
50+
Name: name,
4951
On: pushTrigger,
5052
Jobs: map[string]job{
5153
"deploy": {
@@ -56,17 +58,30 @@ func NewGitHubWorkflow(conf CIConfig) *githubWorkflow {
5658
}
5759
}
5860

59-
func createRunsOn(useSelfHostedRunner bool) string {
61+
func createWorkflowName(conf CIConfig) string {
62+
result := conf.WorkflowName()
63+
if conf.UseRemoteBuild() {
64+
result = "Remote Func Deploy"
65+
}
66+
67+
return result
68+
}
69+
70+
func createRunsOn(conf CIConfig) string {
6071
runsOn := "ubuntu-latest"
61-
if useSelfHostedRunner {
72+
if conf.UseSelfHostedRunner() {
6273
runsOn = "self-hosted"
6374
}
6475
return runsOn
6576
}
6677

67-
func createPushTrigger(branch string) workflowTriggers {
78+
func createPushTrigger(conf CIConfig) workflowTriggers {
6879
result := workflowTriggers{
69-
Push: &pushTrigger{Branches: []string{branch}},
80+
Push: &pushTrigger{Branches: []string{conf.Branch()}},
81+
}
82+
83+
if conf.UseWorkflowDispatch() {
84+
result.WorkflowDispatch = &struct{}{}
7085
}
7186

7287
return result
@@ -102,26 +117,31 @@ func createRegistryLoginStep(conf CIConfig, steps []step) []step {
102117
return append(steps, *loginToContainerRegistry)
103118
}
104119

120+
func createFuncCLIInstallStep(steps []step) []step {
121+
installFuncCli := newStep("Install func cli").
122+
withUses("functions-dev/action@main").
123+
withActionConfig("version", "knative-v1.20.1").
124+
withActionConfig("name", "func")
125+
126+
return append(steps, *installFuncCli)
127+
}
128+
105129
func createFuncDeployStep(conf CIConfig, steps []step) []step {
130+
runFuncDeploy := "func deploy"
131+
if conf.UseRemoteBuild() {
132+
runFuncDeploy += " --remote"
133+
}
134+
106135
registryUrl := newVariable(conf.RegistryUrlVar())
107136
if conf.UseRegistryLogin() {
108137
registryUrl = newVariable(conf.RegistryLoginUrlVar()) + "/" + newVariable(conf.RegistryUserVar())
109138
}
110139
deployFunc := newStep("Deploy function").
111-
withRun("func deploy --registry=" + registryUrl + " -v")
140+
withRun(runFuncDeploy + " --registry=" + registryUrl + " -v")
112141

113142
return append(steps, *deployFunc)
114143
}
115144

116-
func createFuncCLIInstallStep(steps []step) []step {
117-
installFuncCli := newStep("Install func cli").
118-
withUses("gauron99/knative-func-action@main").
119-
withActionConfig("version", "knative-v1.19.1").
120-
withActionConfig("name", "func")
121-
122-
return append(steps, *installFuncCli)
123-
}
124-
125145
func newStep(name string) *step {
126146
return &step{Name: name}
127147
}

cmd/config_ci.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ func NewConfigCICmd(loaderSaver common.FunctionLoaderSaver, writer ci.WorkflowWr
1616
PreRunE: bindEnv(
1717
ci.PathFlag,
1818
ci.UseRegistryLoginFlag,
19+
ci.WorkflowDispatchFlag,
20+
ci.UseRemoteBuildFlag,
1921
ci.UseSelfHostedRunnerFlag,
2022
ci.WorkflowNameFlag,
2123
ci.BranchFlag,
@@ -38,6 +40,19 @@ func NewConfigCICmd(loaderSaver common.FunctionLoaderSaver, writer ci.WorkflowWr
3840
"Add a registry login step in the github workflow",
3941
)
4042

43+
cmd.Flags().Bool(
44+
ci.WorkflowDispatchFlag,
45+
ci.DefaultWorkflowDispatch,
46+
"Add a workflow dispatch trigger for manual workflow execution",
47+
)
48+
_ = cmd.Flags().MarkHidden(ci.WorkflowDispatchFlag)
49+
50+
cmd.Flags().Bool(
51+
ci.UseRemoteBuildFlag,
52+
ci.DefaultUseRemoteBuild,
53+
"Build the function on a Tekton-enabled cluster",
54+
)
55+
4156
cmd.Flags().Bool(
4257
ci.UseSelfHostedRunnerFlag,
4358
ci.DefaultUseSelfHostedRunner,

cmd/config_ci_test.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,33 @@ func TestNewConfigCICmd_WorkflowHasNoRegistryLogin(t *testing.T) {
9393
assert.Assert(t, yamlContains(result.gwYamlString, "--registry=${{ vars.REGISTRY_URL }}"))
9494
}
9595

96+
func TestNewConfigCICmd_RemoteBuildAndDeployWorkflow(t *testing.T) {
97+
// GIVEN
98+
opts := unitTestOpts()
99+
opts.args = append(opts.args, "--remote")
100+
101+
// WHEN
102+
result := runConfigCiCmd(t, opts)
103+
104+
// THEN
105+
assert.NilError(t, result.executeErr)
106+
assert.Assert(t, yamlContains(result.gwYamlString, "Remote Func Deploy"))
107+
assert.Assert(t, yamlContains(result.gwYamlString, "func deploy --remote"))
108+
}
109+
110+
func TestNewConfigCICmd_HasWorkflowDispatch(t *testing.T) {
111+
// GIVEN
112+
opts := unitTestOpts()
113+
opts.args = append(opts.args, "--workflow-dispatch")
114+
115+
// WHEN
116+
result := runConfigCiCmd(t, opts)
117+
118+
// THEN
119+
assert.NilError(t, result.executeErr)
120+
assert.Assert(t, yamlContains(result.gwYamlString, "workflow_dispatch"))
121+
}
122+
96123
// ---------------------
97124
// END: Broad Unit Tests
98125

@@ -303,8 +330,8 @@ func assertDefaultWorkflow(t *testing.T, actualGw string) {
303330
assert.Assert(t, yamlContains(actualGw, "password: ${{ secrets.REGISTRY_PASSWORD }}"))
304331

305332
assert.Assert(t, yamlContains(actualGw, "Install func cli"))
306-
assert.Assert(t, yamlContains(actualGw, "gauron99/knative-func-action@main"))
307-
assert.Assert(t, yamlContains(actualGw, "version: knative-v1.19.1"))
333+
assert.Assert(t, yamlContains(actualGw, "functions-dev/action@main"))
334+
assert.Assert(t, yamlContains(actualGw, "version: knative-v1.20.1"))
308335
assert.Assert(t, yamlContains(actualGw, "name: func"))
309336

310337
assert.Assert(t, yamlContains(actualGw, "Deploy function"))

docs/reference/func_config_ci.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## func config ci
22

3-
Generate a Github Workflow for function deployment
3+
Generate a GitHub Workflow for function deployment
44

55
```
66
func config ci
@@ -9,9 +9,18 @@ func config ci
99
### Options
1010

1111
```
12-
-h, --help help for ci
13-
-p, --path string Path to the function. Default is current directory ($FUNC_PATH)
14-
--workflow-name string Use a custom workflow name (default "Func Deploy")
12+
--branch string Use a custom branch name in the workflow (default "main")
13+
-h, --help help for ci
14+
--kubeconfig-secret-name string Use a custom secret name in the workflow, e.g. secret.YOUR_CUSTOM_KUBECONFIG (default "KUBECONFIG")
15+
-p, --path string Path to the function. Default is current directory ($FUNC_PATH)
16+
--registry-login-url-variable-name string Use a custom registry login url variable name in the workflow, e.g. vars.YOUR_REGISTRY_LOGIN_URL (default "REGISTRY_LOGIN_URL")
17+
--registry-pass-secret-name string Use a custom registry pass secret name in the workflow, e.g. secret.YOUR_REGISTRY_PASSWORD (default "REGISTRY_PASSWORD")
18+
--registry-url-variable-name string Use a custom registry url variable name in the workflow, e.g. vars.YOUR_REGISTRY_URL (default "REGISTRY_URL")
19+
--registry-user-variable-name string Use a custom registry user variable name in the workflow, e.g. vars.YOUR_REGISTRY_USER (default "REGISTRY_USERNAME")
20+
--remote Build the function on a Tekton-enabled cluster
21+
--self-hosted-runner Use a 'self-hosted' runner instead of the default 'ubuntu-latest' for local runner execution
22+
--use-registry-login Add a registry login step in the github workflow (default true)
23+
--workflow-name string Use a custom workflow name (default "Func Deploy")
1524
```
1625

1726
### SEE ALSO

0 commit comments

Comments
 (0)