GeezerVison is showing github.com — [release-1.23] fix: source func CLI version from build ldflags (#4030) · knative/func@aff9ef3
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: cmd/config_ci.go +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3,6 +3,7 @@ package cmd | 3 | 3 | import ( | 4 | 4 | "fmt" | 5 | 5 | "io" | | 6 | + "regexp" | 6 | 7 | "strings" | 7 | 8 |
| 8 | 9 | "github.com/ory/viper" | @@ -11,8 +12,13 @@ import ( | 11 | 12 | "knative.dev/func/cmd/common" | 12 | 13 | "knative.dev/func/pkg/ci/github" | 13 | 14 | fn "knative.dev/func/pkg/functions" | | 15 | + "knative.dev/func/pkg/version" | 14 | 16 | ) | 15 | 17 |
| | 18 | +// gitDescribeSuffix matches the "-N-gHASH" suffix produced by git describe | | 19 | +// on commits that are not exactly on a release tag. | | 20 | +var gitDescribeSuffix = regexp.MustCompile(`-\d+-g[0-9a-f]+$`) | | 21 | + | 16 | 22 | // ciGeneratorFactory creates a CIGenerator from resolved CLI flag values. | 17 | 23 | // Using a factory allows tests to capture the resolved config and inject | 18 | 24 | // a mock generator without running the real implementation. | @@ -250,6 +256,7 @@ func newConfigAndLoadedFunc( | 250 | 256 | RegistryUserVar: viper.GetString(registryUserVariableNameFlag), | 251 | 257 | RegistryPassSecret: viper.GetString(registryPassSecretNameFlag), | 252 | 258 | RegistryUrlVar: viper.GetString(registryUrlVariableNameFlag), | | 259 | + FuncCliVersion: gitDescribeSuffix.ReplaceAllString(version.Kver, ""), | 253 | 260 | RegistryLogin: viper.GetBool(registryLoginFlag), | 254 | 261 | SelfHostedRunner: viper.GetBool(selfHostedRunnerFlag), | 255 | 262 | RemoteBuild: viper.GetBool(remoteBuildFlag), | |
Collapse file Copy file name to clipboardExpand all lines: cmd/config_ci_int_test.go +9-1Lines changed: 9 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -18,6 +18,7 @@ import ( | 18 | 18 | "knative.dev/func/pkg/ci/github" | 19 | 19 | fn "knative.dev/func/pkg/functions" | 20 | 20 | fnTest "knative.dev/func/pkg/testing" | | 21 | + "knative.dev/func/pkg/version" | 21 | 22 | ) | 22 | 23 |
| 23 | 24 | // START: Integration Tests | @@ -77,6 +78,13 @@ func TestNewConfigCICmd_CreatesGitHubWorkflowDirectory(t *testing.T) { | 77 | 78 | } | 78 | 79 |
| 79 | 80 | func TestNewConfigCICmd_WritesWorkflowFileToFSWithCorrectYAMLStructure(t *testing.T) { | | 81 | + // Pin the CLI version so the generated workflow is deterministic, | | 82 | + // independent of the version injected via -ldflags at build time | | 83 | + // (which varies with the branch's most recent git tag). | | 84 | + prev := version.Kver | | 85 | + version.Kver = github.DefaultFuncCliVersion | | 86 | + t.Cleanup(func() { version.Kver = prev }) | | 87 | + | 80 | 88 | opts := defaultIntegrationOpts(t, nil) | 81 | 89 |
| 82 | 90 | err := runConfigCiCmdIntegration(t, opts) | @@ -231,7 +239,7 @@ func assertDefaultWorkflow(t *testing.T, actualGw string) { | 231 | 239 |
| 232 | 240 | assert.Assert(t, yamlContains(actualGw, "Install func cli")) | 233 | 241 | assert.Assert(t, yamlContains(actualGw, "functions-dev/action@main")) | 234 | | - assert.Assert(t, yamlContains(actualGw, "version: knative-v1.22.0")) | | 242 | + assert.Assert(t, yamlContains(actualGw, "version: "+github.DefaultFuncCliVersion)) | 235 | 243 | assert.Assert(t, yamlContains(actualGw, "name: func")) | 236 | 244 |
| 237 | 245 | assert.Assert(t, yamlContains(actualGw, "Deploy function")) | |
Collapse file Copy file name to clipboardExpand all lines: pkg/ci/github/generator_test.go +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -566,6 +566,7 @@ func defaultOpts() opts { | 566 | 566 | RegistryUserVar: github.DefaultRegistryUserVariableName, | 567 | 567 | RegistryPassSecret: github.DefaultRegistryPassSecretName, | 568 | 568 | RegistryUrlVar: github.DefaultRegistryUrlVariableName, | | 569 | + FuncCliVersion: github.DefaultFuncCliVersion, | 569 | 570 | RegistryLogin: github.DefaultRegistryLogin, | 570 | 571 | SelfHostedRunner: github.DefaultSelfHostedRunner, | 571 | 572 | RemoteBuild: github.DefaultRemoteBuild, | @@ -643,7 +644,7 @@ func assertDefaultWorkflow(t *testing.T, actualGw string) { | 643 | 644 |
| 644 | 645 | assert.Assert(t, yamlContains(actualGw, "Install func cli")) | 645 | 646 | assert.Assert(t, yamlContains(actualGw, "functions-dev/action@main")) | 646 | | - assert.Assert(t, yamlContains(actualGw, "version: knative-v1.22.0")) | | 647 | + assert.Assert(t, yamlContains(actualGw, "version: "+github.DefaultFuncCliVersion)) | 647 | 648 | assert.Assert(t, yamlContains(actualGw, "name: func")) | 648 | 649 |
| 649 | 650 | assert.Assert(t, yamlContains(actualGw, "Deploy function")) | @@ -676,7 +677,7 @@ func assertSemiDefaultWorkflow(t *testing.T, actualGw string) { | 676 | 677 |
| 677 | 678 | assert.Assert(t, yamlContains(actualGw, "Install func cli")) | 678 | 679 | assert.Assert(t, yamlContains(actualGw, "functions-dev/action@main")) | 679 | | - assert.Assert(t, yamlContains(actualGw, "version: knative-v1.22.0")) | | 680 | + assert.Assert(t, yamlContains(actualGw, "version: "+github.DefaultFuncCliVersion)) | 680 | 681 | assert.Assert(t, yamlContains(actualGw, "name: func")) | 681 | 682 |
| 682 | 683 | assert.Assert(t, yamlContains(actualGw, "Deploy function")) | |
Collapse file Copy file name to clipboardExpand all lines: pkg/ci/github/workflow.go +10-5Lines changed: 10 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -14,7 +14,7 @@ import ( | 14 | 14 | var ErrWorkflowExists = errors.New("existing GitHub workflow detected, overwrite using the --force option") | 15 | 15 |
| 16 | 16 | const ( | 17 | | - defaultFuncCliVersion = "knative-v1.22.0" | | 17 | + DefaultFuncCliVersion = "knative-v1.23.0" | 18 | 18 | DefaultPlatform = "github" | 19 | 19 | DefaultGitHubWorkflowDir = ".github/workflows" | 20 | 20 | DefaultGitHubWorkflowFilename = "func-deploy.yaml" | @@ -45,7 +45,8 @@ type WorkflowConfig struct { | 45 | 45 | RegistryLoginUrlVar, | 46 | 46 | RegistryUserVar, | 47 | 47 | RegistryPassSecret, | 48 | | - RegistryUrlVar string | | 48 | + RegistryUrlVar, | | 49 | + FuncCliVersion string | 49 | 50 | RegistryLogin, | 50 | 51 | SelfHostedRunner, | 51 | 52 | RemoteBuild, | @@ -74,6 +75,7 @@ func defaultWorkflowConfig() WorkflowConfig { | 74 | 75 | RegistryUserVar: DefaultRegistryUserVariableName, | 75 | 76 | RegistryPassSecret: DefaultRegistryPassSecretName, | 76 | 77 | RegistryUrlVar: DefaultRegistryUrlVariableName, | | 78 | + FuncCliVersion: DefaultFuncCliVersion, | 77 | 79 | RegistryLogin: DefaultRegistryLogin, | 78 | 80 | SelfHostedRunner: DefaultSelfHostedRunner, | 79 | 81 | RemoteBuild: DefaultRemoteBuild, | @@ -111,6 +113,9 @@ func setEmptyFieldsToDefaults(defaults WorkflowConfig) WorkflowConfig { | 111 | 113 | if defaults.RegistryUrlVar == "" { | 112 | 114 | defaults.RegistryUrlVar = DefaultRegistryUrlVariableName | 113 | 115 | } | | 116 | + if defaults.FuncCliVersion == "" { | | 117 | + defaults.FuncCliVersion = DefaultFuncCliVersion | | 118 | + } | 114 | 119 |
| 115 | 120 | return defaults | 116 | 121 | } | @@ -149,7 +154,7 @@ func newGitHubWorkflow(cfg WorkflowConfig, runtime string, messageWriter io.Writ | 149 | 154 | steps = createRuntimeTestStep(cfg, runtime, messageWriter, steps) | 150 | 155 | steps = createK8ContextStep(cfg, steps) | 151 | 156 | steps = createRegistryLoginStep(cfg, steps) | 152 | | - steps = createFuncCLIInstallStep(steps) | | 157 | + steps = createFuncCLIInstallStep(cfg, steps) | 153 | 158 |
| 154 | 159 | steps, err := createFuncDeployStep(cfg, runtime, steps) | 155 | 160 | if err != nil { | @@ -223,10 +228,10 @@ func createRegistryLoginStep(opts WorkflowConfig, steps []step) []step { | 223 | 228 | return append(steps, *loginToContainerRegistry) | 224 | 229 | } | 225 | 230 |
| 226 | | -func createFuncCLIInstallStep(steps []step) []step { | | 231 | +func createFuncCLIInstallStep(cfg WorkflowConfig, steps []step) []step { | 227 | 232 | installFuncCli := newStep("Install func cli"). | 228 | 233 | withUses("functions-dev/action@main"). | 229 | | - withActionConfig("version", defaultFuncCliVersion). | | 234 | + withActionConfig("version", cfg.FuncCliVersion). | 230 | 235 | withActionConfig("name", "func") | 231 | 236 |
| 232 | 237 | return append(steps, *installFuncCli) | |
|