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 cmd/ci/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ci
import "fmt"

func runner(conf CIConfig) string {
if conf.UseSelfHostedRunner() {
if conf.SelfHostedRunner() {
return "self-hosted"
}
return "ubuntu-latest"
Expand Down
83 changes: 46 additions & 37 deletions cmd/ci/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,20 @@ const (
RegistryUrlVariableNameFlag = "registry-url-variable-name"
DefaultRegistryUrlVariableName = "REGISTRY_URL"

UseRegistryLoginFlag = "use-registry-login"
DefaultUseRegistryLogin = true
RegistryLoginFlag = "registry-login"
DefaultRegistryLogin = true

WorkflowDispatchFlag = "workflow-dispatch"
DefaultWorkflowDispatch = false

UseRemoteBuildFlag = "remote"
DefaultUseRemoteBuild = false
RemoteBuildFlag = "remote"
DefaultRemoteBuild = false

UseSelfHostedRunnerFlag = "self-hosted-runner"
DefaultUseSelfHostedRunner = false
SelfHostedRunnerFlag = "self-hosted-runner"
DefaultSelfHostedRunner = false

TestStepFlag = "test-step"
DefaultTestStep = true

ForceFlag = "force"
DefaultForce = false
Expand All @@ -73,10 +76,11 @@ type CIConfig struct {
registryUserVar,
registryPassSecret,
registryUrlVar string
useRegistryLogin,
useSelfHostedRunner,
useRemoteBuild,
useWorkflowDispatch,
registryLogin,
selfHostedRunner,
remoteBuild,
workflowDispatch,
testStep,
force,
verbose bool
}
Expand Down Expand Up @@ -113,10 +117,11 @@ func NewCIConfig(
registryUserVar: viper.GetString(RegistryUserVariableNameFlag),
registryPassSecret: viper.GetString(RegistryPassSecretNameFlag),
registryUrlVar: viper.GetString(RegistryUrlVariableNameFlag),
useRegistryLogin: viper.GetBool(UseRegistryLoginFlag),
useSelfHostedRunner: viper.GetBool(UseSelfHostedRunnerFlag),
useRemoteBuild: viper.GetBool(UseRemoteBuildFlag),
useWorkflowDispatch: viper.GetBool(WorkflowDispatchFlag),
registryLogin: viper.GetBool(RegistryLoginFlag),
selfHostedRunner: viper.GetBool(SelfHostedRunnerFlag),
remoteBuild: viper.GetBool(RemoteBuildFlag),
workflowDispatch: viper.GetBool(WorkflowDispatchFlag),
testStep: viper.GetBool(TestStepFlag),
force: viper.GetBool(ForceFlag),
verbose: viper.GetBool(VerboseFlag),
}, nil
Expand Down Expand Up @@ -165,7 +170,7 @@ func resolveWorkflowName(explicit bool) string {
return workflowName
}

if viper.GetBool(UseRemoteBuildFlag) {
if viper.GetBool(RemoteBuildFlag) {
return DefaultRemoteBuildWorkflowName
}

Expand All @@ -180,32 +185,20 @@ func (cc CIConfig) FnGitHubWorkflowFilepath(fnRoot string) string {
return filepath.Join(cc.fnGitHubWorkflowDir(fnRoot), cc.githubWorkflowFilename)
}

func (cc CIConfig) Path() string {
return cc.path
func (cc CIConfig) OutputPath() string {
return filepath.Join(cc.githubWorkflowDir, cc.githubWorkflowFilename)
}

func (cc CIConfig) WorkflowName() string {
return cc.workflowName
func (cc CIConfig) Path() string {
return cc.path
}

func (cc CIConfig) Branch() string {
return cc.branch
}

func (cc CIConfig) UseRegistryLogin() bool {
return cc.useRegistryLogin
}

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) WorkflowName() string {
return cc.workflowName
}

func (cc CIConfig) KubeconfigSecret() string {
Expand All @@ -228,14 +221,30 @@ func (cc CIConfig) RegistryUrlVar() string {
return cc.registryUrlVar
}

func (cc CIConfig) RegistryLogin() bool {
return cc.registryLogin
}

func (cc CIConfig) SelfHostedRunner() bool {
return cc.selfHostedRunner
}

func (cc CIConfig) RemoteBuild() bool {
return cc.remoteBuild
}

func (cc CIConfig) WorkflowDispatch() bool {
return cc.workflowDispatch
}

func (cc CIConfig) TestStep() bool {
return cc.testStep
}

func (cc CIConfig) Force() bool {
return cc.force
}

func (cc CIConfig) Verbose() bool {
return cc.verbose
}

func (cc CIConfig) OutputPath() string {
return filepath.Join(cc.githubWorkflowDir, cc.githubWorkflowFilename)
}
12 changes: 7 additions & 5 deletions cmd/ci/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ GitHub Workflow Configuration
Branch: %s
Build: %s
Runner: %s
Test step: %s
Registry login: %s
Manual dispatch: %s
Workflow overwrite: %s
Expand Down Expand Up @@ -54,14 +55,15 @@ func PrintConfiguration(w io.Writer, conf CIConfig) error {
conf.Branch(),
builder(conf),
runner(conf),
enabledOrDisabled(conf.UseRegistryLogin()),
enabledOrDisabled(conf.UseWorkflowDispatch()),
enabledOrDisabled(conf.TestStep()),
enabledOrDisabled(conf.RegistryLogin()),
enabledOrDisabled(conf.WorkflowDispatch()),
enabledOrDisabled(conf.Force()),
); err != nil {
return err
}

if conf.UseRegistryLogin() {
if conf.RegistryLogin() {
if _, err := fmt.Fprintf(w, RequireManyPlainText,
secretsPrefix(conf.KubeconfigSecret()),
secretsPrefix(conf.RegistryPassSecret()),
Expand All @@ -86,7 +88,7 @@ func PrintConfiguration(w io.Writer, conf CIConfig) error {
}

func PrintPostExportMessage(w io.Writer, conf CIConfig) error {
if conf.UseRegistryLogin() {
if conf.RegistryLogin() {
_, err := fmt.Fprintf(w, PostExportManyPlainText,
conf.OutputPath(),
secretsPrefix(conf.KubeconfigSecret()),
Expand All @@ -106,7 +108,7 @@ func PrintPostExportMessage(w io.Writer, conf CIConfig) error {
}

func builder(conf CIConfig) string {
if conf.UseRemoteBuild() {
if conf.RemoteBuild() {
return "remote"
}
return "host"
Expand Down
8 changes: 4 additions & 4 deletions cmd/ci/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestPrintConfigurationFail(t *testing.T) {

t.Run("registry secrets write fails", func(t *testing.T) {
w := &failWriter{failOnCall: 2, err: errWrite}
conf := CIConfig{useRegistryLogin: true}
conf := CIConfig{registryLogin: true}

err := PrintConfiguration(w, conf)

Expand All @@ -44,7 +44,7 @@ func TestPrintConfigurationFail(t *testing.T) {

t.Run("single secret write fails", func(t *testing.T) {
w := &failWriter{failOnCall: 2, err: errWrite}
conf := CIConfig{useRegistryLogin: false}
conf := CIConfig{registryLogin: false}

err := PrintConfiguration(w, conf)

Expand All @@ -55,7 +55,7 @@ func TestPrintConfigurationFail(t *testing.T) {
func TestPrintPostExportMessageFail(t *testing.T) {
t.Run("with registry login write fails", func(t *testing.T) {
w := &failWriter{failOnCall: 1, err: errWrite}
conf := CIConfig{useRegistryLogin: true}
conf := CIConfig{registryLogin: true}

err := PrintPostExportMessage(w, conf)

Expand All @@ -64,7 +64,7 @@ func TestPrintPostExportMessageFail(t *testing.T) {

t.Run("without registry login write fails", func(t *testing.T) {
w := &failWriter{failOnCall: 1, err: errWrite}
conf := CIConfig{useRegistryLogin: false}
conf := CIConfig{registryLogin: false}

err := PrintPostExportMessage(w, conf)

Expand Down
58 changes: 42 additions & 16 deletions cmd/ci/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ type step struct {
With map[string]string `yaml:"with,omitempty"`
}

func NewGitHubWorkflow(conf CIConfig) *githubWorkflow {
func NewGitHubWorkflow(conf CIConfig, runtime string, messageWriter io.Writer) *githubWorkflow {
var steps []step
steps = createCheckoutStep(steps)
steps = createRuntimeTestStep(conf, steps, runtime, messageWriter)
steps = createK8ContextStep(conf, steps)
steps = createRegistryLoginStep(conf, steps)
steps = createFuncCLIInstallStep(steps)
Expand All @@ -61,25 +62,38 @@ func NewGitHubWorkflow(conf CIConfig) *githubWorkflow {
}
}

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

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

return result
}

func createCheckoutStep(steps []step) []step {
checkoutCode := newStep("Checkout code").
withUses("actions/checkout@v4")

return append(steps, *checkoutCode)
}

func createRuntimeTestStep(conf CIConfig, steps []step, runtime string, messageWriter io.Writer) []step {
if !conf.TestStep() {
return steps
}

testStep := newStep("Run tests")

switch runtime {
case "go":
testStep.withRun("go test ./...")
case "node", "typescript":
testStep.withRun("npm ci && npm test")
case "python":
testStep.withRun("pip install . && python -m pytest")
case "quarkus":
testStep.withRun("./mvnw test")
default:
// best-effort user message; errors are non-critical
_, _ = fmt.Fprintf(messageWriter, "WARNING: test step not supported for runtime %s\n", runtime)
return steps
}

return append(steps, *testStep)
}

func createK8ContextStep(conf CIConfig, steps []step) []step {
setupK8Context := newStep("Setup Kubernetes context").
withUses("azure/k8s-set-context@v4").
Expand All @@ -90,7 +104,7 @@ func createK8ContextStep(conf CIConfig, steps []step) []step {
}

func createRegistryLoginStep(conf CIConfig, steps []step) []step {
if !conf.UseRegistryLogin() {
if !conf.RegistryLogin() {
return steps
}

Expand All @@ -114,12 +128,12 @@ func createFuncCLIInstallStep(steps []step) []step {

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

registryUrl := newVariable(conf.RegistryUrlVar())
if conf.UseRegistryLogin() {
if conf.RegistryLogin() {
registryUrl = newVariable(conf.RegistryLoginUrlVar()) + "/" + newVariable(conf.RegistryUserVar())
}
deployFunc := newStep("Deploy function").
Expand All @@ -128,6 +142,18 @@ func createFuncDeployStep(conf CIConfig, steps []step) []step {
return append(steps, *deployFunc)
}

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

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

return result
}

func newStep(name string) *step {
return &step{Name: name}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/ci/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestGitHubWorkflow_Export(t *testing.T) {
common.WorkDirStub("", nil),
false,
)
gw := ci.NewGitHubWorkflow(cfg)
gw := ci.NewGitHubWorkflow(cfg, "", &bytes.Buffer{})
bufferWriter := ci.NewBufferWriter()

// WHEN
Expand Down
Loading
Loading