Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ __pycache__
# E2E Tests
/e2e/testdata/default_home/go
/e2e/testdata/default_home/.cache

# Editors
.vscode
.idea
Expand Down
20 changes: 19 additions & 1 deletion cmd/ci/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ const (
UseRegistryLoginFlag = "use-registry-login"
DefaultUseRegistryLogin = true

WorkflowDispatchFlag = "workflow-dispatch"
DefaultWorkflowDispatch = false

UseRemoteBuildFlag = "remote"
DefaultUseRemoteBuild = false

UseSelfHostedRunnerFlag = "self-hosted-runner"
DefaultUseSelfHostedRunner = false
)
Expand All @@ -55,7 +61,9 @@ type CIConfig struct {
registryPassSecret,
registryUrlVar string
useRegistryLogin,
useSelfHostedRunner bool
useSelfHostedRunner,
useRemoteBuild,
useWorkflowDispatch bool
}

func NewCIGitHubConfig() CIConfig {
Expand All @@ -72,6 +80,8 @@ func NewCIGitHubConfig() CIConfig {
registryUrlVar: viper.GetString(RegistryUrlVariableNameFlag),
useRegistryLogin: viper.GetBool(UseRegistryLoginFlag),
useSelfHostedRunner: viper.GetBool(UseSelfHostedRunnerFlag),
useRemoteBuild: viper.GetBool(UseRemoteBuildFlag),
useWorkflowDispatch: viper.GetBool(WorkflowDispatchFlag),
}
}

Expand Down Expand Up @@ -103,6 +113,14 @@ func (cc *CIConfig) UseSelfHostedRunner() bool {
return cc.useSelfHostedRunner
}

func (cc *CIConfig) UseRemoteBuild() bool {
return cc.useRemoteBuild
}

func (cc *CIConfig) UseWorkflowDispatch() bool {
return cc.useWorkflowDispatch
}

func (cc *CIConfig) KubeconfigSecret() string {
return cc.kubeconfigSecret
}
Expand Down
56 changes: 38 additions & 18 deletions cmd/ci/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ type githubWorkflow struct {
}

type workflowTriggers struct {
Push *pushTrigger `yaml:"push,omitempty"`
Push *pushTrigger `yaml:"push,omitempty"`
WorkflowDispatch *struct{} `yaml:"workflow_dispatch,omitempty"`
}

type pushTrigger struct {
Expand All @@ -34,8 +35,9 @@ type step struct {
}

func NewGitHubWorkflow(conf CIConfig) *githubWorkflow {
runsOn := createRunsOn(conf.UseSelfHostedRunner())
pushTrigger := createPushTrigger(conf.Branch())
name := createWorkflowName(conf)
runsOn := createRunsOn(conf)
pushTrigger := createPushTrigger(conf)

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

return &githubWorkflow{
Name: conf.WorkflowName(),
Name: name,
On: pushTrigger,
Jobs: map[string]job{
"deploy": {
Expand All @@ -56,17 +58,30 @@ func NewGitHubWorkflow(conf CIConfig) *githubWorkflow {
}
}

func createRunsOn(useSelfHostedRunner bool) string {
func createWorkflowName(conf CIConfig) string {
result := conf.WorkflowName()
if conf.UseRemoteBuild() {
result = "Remote Func Deploy"
}

return result
}

func createRunsOn(conf CIConfig) string {
runsOn := "ubuntu-latest"
if useSelfHostedRunner {
if conf.UseSelfHostedRunner() {
runsOn = "self-hosted"
}
return runsOn
}

func createPushTrigger(branch string) workflowTriggers {
func createPushTrigger(conf CIConfig) workflowTriggers {
result := workflowTriggers{
Push: &pushTrigger{Branches: []string{branch}},
Push: &pushTrigger{Branches: []string{conf.Branch()}},
}

if conf.UseWorkflowDispatch() {
result.WorkflowDispatch = &struct{}{}
}

return result
Expand Down Expand Up @@ -102,26 +117,31 @@ func createRegistryLoginStep(conf CIConfig, steps []step) []step {
return append(steps, *loginToContainerRegistry)
}

func createFuncCLIInstallStep(steps []step) []step {
installFuncCli := newStep("Install func cli").
withUses("functions-dev/action@main").
withActionConfig("version", "knative-v1.20.1").
withActionConfig("name", "func")

return append(steps, *installFuncCli)
}

func createFuncDeployStep(conf CIConfig, steps []step) []step {
runFuncDeploy := "func deploy"
if conf.UseRemoteBuild() {
runFuncDeploy += " --remote"
}

registryUrl := newVariable(conf.RegistryUrlVar())
if conf.UseRegistryLogin() {
registryUrl = newVariable(conf.RegistryLoginUrlVar()) + "/" + newVariable(conf.RegistryUserVar())
}
deployFunc := newStep("Deploy function").
withRun("func deploy --registry=" + registryUrl + " -v")
withRun(runFuncDeploy + " --registry=" + registryUrl + " -v")

return append(steps, *deployFunc)
}

func createFuncCLIInstallStep(steps []step) []step {
installFuncCli := newStep("Install func cli").
withUses("gauron99/knative-func-action@main").
withActionConfig("version", "knative-v1.19.1").
withActionConfig("name", "func")

return append(steps, *installFuncCli)
}

func newStep(name string) *step {
return &step{Name: name}
}
Expand Down
15 changes: 15 additions & 0 deletions cmd/config_ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ func NewConfigCICmd(loaderSaver common.FunctionLoaderSaver, writer ci.WorkflowWr
PreRunE: bindEnv(
ci.PathFlag,
ci.UseRegistryLoginFlag,
ci.WorkflowDispatchFlag,
ci.UseRemoteBuildFlag,
ci.UseSelfHostedRunnerFlag,
ci.WorkflowNameFlag,
ci.BranchFlag,
Expand All @@ -38,6 +40,19 @@ func NewConfigCICmd(loaderSaver common.FunctionLoaderSaver, writer ci.WorkflowWr
"Add a registry login step in the github workflow",
)

cmd.Flags().Bool(
ci.WorkflowDispatchFlag,
ci.DefaultWorkflowDispatch,
"Add a workflow dispatch trigger for manual workflow execution",
)
_ = cmd.Flags().MarkHidden(ci.WorkflowDispatchFlag)

cmd.Flags().Bool(
ci.UseRemoteBuildFlag,
ci.DefaultUseRemoteBuild,
"Build the function on a Tekton-enabled cluster",
)

cmd.Flags().Bool(
ci.UseSelfHostedRunnerFlag,
ci.DefaultUseSelfHostedRunner,
Expand Down
31 changes: 29 additions & 2 deletions cmd/config_ci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,33 @@ func TestNewConfigCICmd_WorkflowHasNoRegistryLogin(t *testing.T) {
assert.Assert(t, yamlContains(result.gwYamlString, "--registry=${{ vars.REGISTRY_URL }}"))
}

func TestNewConfigCICmd_RemoteBuildAndDeployWorkflow(t *testing.T) {
// GIVEN
opts := unitTestOpts()
opts.args = append(opts.args, "--remote")

// WHEN
result := runConfigCiCmd(t, opts)

// THEN
assert.NilError(t, result.executeErr)
assert.Assert(t, yamlContains(result.gwYamlString, "Remote Func Deploy"))
assert.Assert(t, yamlContains(result.gwYamlString, "func deploy --remote"))
}

func TestNewConfigCICmd_HasWorkflowDispatch(t *testing.T) {
// GIVEN
opts := unitTestOpts()
opts.args = append(opts.args, "--workflow-dispatch")

// WHEN
result := runConfigCiCmd(t, opts)

// THEN
assert.NilError(t, result.executeErr)
assert.Assert(t, yamlContains(result.gwYamlString, "workflow_dispatch"))
}

// ---------------------
// END: Broad Unit Tests

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

assert.Assert(t, yamlContains(actualGw, "Install func cli"))
assert.Assert(t, yamlContains(actualGw, "gauron99/knative-func-action@main"))
assert.Assert(t, yamlContains(actualGw, "version: knative-v1.19.1"))
assert.Assert(t, yamlContains(actualGw, "functions-dev/action@main"))
assert.Assert(t, yamlContains(actualGw, "version: knative-v1.20.1"))
assert.Assert(t, yamlContains(actualGw, "name: func"))

assert.Assert(t, yamlContains(actualGw, "Deploy function"))
Expand Down
17 changes: 13 additions & 4 deletions docs/reference/func_config_ci.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## func config ci

Generate a Github Workflow for function deployment
Generate a GitHub Workflow for function deployment

```
func config ci
Expand All @@ -9,9 +9,18 @@ func config ci
### Options

```
-h, --help help for ci
-p, --path string Path to the function. Default is current directory ($FUNC_PATH)
--workflow-name string Use a custom workflow name (default "Func Deploy")
--branch string Use a custom branch name in the workflow (default "main")
-h, --help help for ci
--kubeconfig-secret-name string Use a custom secret name in the workflow, e.g. secret.YOUR_CUSTOM_KUBECONFIG (default "KUBECONFIG")
-p, --path string Path to the function. Default is current directory ($FUNC_PATH)
--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")
--registry-pass-secret-name string Use a custom registry pass secret name in the workflow, e.g. secret.YOUR_REGISTRY_PASSWORD (default "REGISTRY_PASSWORD")
--registry-url-variable-name string Use a custom registry url variable name in the workflow, e.g. vars.YOUR_REGISTRY_URL (default "REGISTRY_URL")
--registry-user-variable-name string Use a custom registry user variable name in the workflow, e.g. vars.YOUR_REGISTRY_USER (default "REGISTRY_USERNAME")
--remote Build the function on a Tekton-enabled cluster
--self-hosted-runner Use a 'self-hosted' runner instead of the default 'ubuntu-latest' for local runner execution
--use-registry-login Add a registry login step in the github workflow (default true)
--workflow-name string Use a custom workflow name (default "Func Deploy")
```

### SEE ALSO
Expand Down
Loading