diff --git a/README.md b/README.md index 0337b30..2bf7f79 100644 --- a/README.md +++ b/README.md @@ -270,7 +270,8 @@ mainBranch: 'main' ## Stages The GitOps-build-lib supports builds on multiple stages. A stage is defined by a name and contains a namespace (used to -generate the resources) and a deployment-flag. +generate the resources) and a deployment-flag. If no stages is passed into the gitops-config by the user, the default +is set to: ```groovy def gitopsConfig = [ @@ -287,8 +288,9 @@ def gitopsConfig = [ ] ``` +The defaults above can be overwritten by providing an entry for 'stages' within your config. If it is set to deploy directly it will commit and push to your desired `gitops-folder` and therefore triggers a deployment. If it is set to false -it will create a PR on your `gitops-folder`. **Remember** there are [ipmortant](#important-note) conventions regarding namespaces and the folder structure. +it will create a PR on your `gitops-folder`. **Remember** there are [important](#important-note) conventions regarding namespaces and the folder structure. ### Namespaces diff --git a/test/com/cloudogu/gitopsbuildlib/DeployViaGitopsTest.groovy b/test/com/cloudogu/gitopsbuildlib/DeployViaGitopsTest.groovy index 9292abf..2406e36 100644 --- a/test/com/cloudogu/gitopsbuildlib/DeployViaGitopsTest.groovy +++ b/test/com/cloudogu/gitopsbuildlib/DeployViaGitopsTest.groovy @@ -42,17 +42,17 @@ class DeployViaGitopsTest extends BasePipelineTest { Map gitopsConfig(Map stages, Map deployments) { return [ - scmmCredentialsId : 'scmManagerCredentials', - scmmConfigRepoUrl : 'configRepositoryUrl', - scmmPullRequestBaseUrl: 'http://scmm-scm-manager/scm', - scmmPullRequestRepo : 'fluxv1/gitops', - cesBuildLibRepo : 'cesBuildLibRepo', - cesBuildLibVersion : 'cesBuildLibVersion', + scmmCredentialsId : 'scmManagerCredentials', + scmmConfigRepoUrl : 'configRepositoryUrl', + scmmPullRequestBaseUrl : 'http://scmm-scm-manager/scm', + scmmPullRequestRepo : 'fluxv1/gitops', + cesBuildLibRepo : 'cesBuildLibRepo', + cesBuildLibVersion : 'cesBuildLibVersion', cesBuildLibCredentialsId: 'cesBuildLibCredentialsId', - application : 'application', - mainBranch : 'main', - deployments : deployments, - validators : [ + application : 'application', + mainBranch : 'main', + deployments : deployments, + validators : [ kubeval : [ validator: new Kubeval(deployViaGitops), enabled : true, @@ -73,14 +73,14 @@ class DeployViaGitopsTest extends BasePipelineTest { ] ] ], - stages : stages + stages : stages ] } def plainDeployment = [ sourcePath: 'k8s', - plain : [ - updateImages : [ + plain : [ + updateImages: [ [deploymentFilename: "deployment.yaml", containerName : 'application', imageName : 'newImageName'] @@ -95,7 +95,7 @@ class DeployViaGitopsTest extends BasePipelineTest { def multipleStages = [ staging : [deployDirectly: true], production: [deployDirectly: false], - qa : [] + qa : [deployDirectly: false] ] @BeforeAll @@ -211,6 +211,29 @@ spec: deployViaGitops([cesBuildLibRepo: 'abc', cesBuildLibCredentialsId: 'testuser']) } + @Test + void 'default stages defined as staging and production'() { + deployViaGitops.metaClass.deploy = { Map actualGitOpsConfig -> + assertThat(actualGitOpsConfig.stages.containsKey('staging')).isEqualTo(true) + assertThat(actualGitOpsConfig.stages.containsKey('production')).isEqualTo(true) + assertThat(actualGitOpsConfig.stages.staging.deployDirectly).isEqualTo(true) + assertThat(actualGitOpsConfig.stages.production.deployDirectly).isEqualTo(false) + } + + deployViaGitops([:]) + } + + @Test + void 'stages definition gets overwritten rather than merged'() { + deployViaGitops.metaClass.deploy = { Map actualGitOpsConfig -> + assertThat(actualGitOpsConfig.stages.containsKey('staging')).isEqualTo(true) + assertThat(actualGitOpsConfig.stages.containsKey('production')).isEqualTo(false) + assertThat(actualGitOpsConfig.stages.staging.deployDirectly).isEqualTo(true) + } + + deployViaGitops(gitopsConfig(singleStages, plainDeployment)) + } + @Test void 'default validator can be disabled'() { @@ -327,9 +350,7 @@ spec: ArgumentCaptor argumentCaptor2 = ArgumentCaptor.forClass(String.class) verify(git).commit(argumentCaptor2.capture(), eq('staging'), anyString()) - println("before argument get value") assertThat(argumentCaptor2.getValue()).isEqualTo('[staging] #0001 backend/k8s-gitops@1234abcd') - println("after argument get value") argumentCaptor2 = ArgumentCaptor.forClass(String.class) verify(git).commit(argumentCaptor2.capture(), eq('production'), anyString()) diff --git a/test/com/cloudogu/gitopsbuildlib/validation/HelmKubevalTest.groovy b/test/com/cloudogu/gitopsbuildlib/validation/HelmKubevalTest.groovy index d91dc92..f10f42f 100644 --- a/test/com/cloudogu/gitopsbuildlib/validation/HelmKubevalTest.groovy +++ b/test/com/cloudogu/gitopsbuildlib/validation/HelmKubevalTest.groovy @@ -31,10 +31,8 @@ class HelmKubevalTest { ] ) assertThat(dockerMock.actualImages[0]).isEqualTo('img') - //assertThat(scriptMock.actualShArgs[0]).isEqualTo('git clone chartRepo/namespace/repoPath target/chart || true') - //assertThat(scriptMock.actualShArgs[1]).isEqualTo('git --git-dir=target/chart/.git --work-tree=target/chart checkout version') - //assertThat(scriptMock.actualShArgs[1]).isEqualTo('helm kubeval target/chart/chartPath -v 1.5') - //assertThat(scriptMock.actualShArgs[2]).isEqualTo('rm -rf target/chart') + assertThat(scriptMock.actualShArgs[0]).isEqualTo('helm kubeval target/chart/chartPath -v 1.5') + assertThat(scriptMock.actualShArgs[1]).isEqualTo('rm -rf target/chart') } @Test diff --git a/vars/deployViaGitops.groovy b/vars/deployViaGitops.groovy index 7dea42b..52bb926 100644 --- a/vars/deployViaGitops.groovy +++ b/vars/deployViaGitops.groovy @@ -18,15 +18,15 @@ List getMandatoryFields() { Map getDefaultConfig() { return [ - cesBuildLibRepo : 'https://github.com/cloudogu/ces-build-lib', - cesBuildLibVersion: '1.45.0', + cesBuildLibRepo : 'https://github.com/cloudogu/ces-build-lib', + cesBuildLibVersion : '1.45.0', cesBuildLibCredentialsId: '', - mainBranch : 'main', - deployments : [ + mainBranch : 'main', + deployments : [ sourcePath: 'k8s', ], - validators : [ - kubeval : [ + validators : [ + kubeval : [ validator: new Kubeval(this), enabled : true, config : [ @@ -44,7 +44,7 @@ Map getDefaultConfig() { k8sSchemaVersion: '1.18.1' ] ], - yamllint: [ + yamllint : [ validator: new Yamllint(this), enabled : true, config : [ @@ -54,6 +54,10 @@ Map getDefaultConfig() { profile: 'relaxed' ] ] + ], + stages : [ + staging : [deployDirectly: true], + production: [deployDirectly: false], ] ] } @@ -69,7 +73,12 @@ void call(Map gitopsConfig) { def mergeMaps(Map a, Map b) { return b.inject(a.clone()) { map, entry -> if (map[entry.key] instanceof Map && entry.value instanceof Map) { - map[entry.key] = mergeMaps(map[entry.key], entry.value) + + // due to the stages being the definition of the environment its not a merge but overwriting + if (entry.key == 'stages') + map[entry.key] = entry.value + else + map[entry.key] = mergeMaps(map[entry.key], entry.value) } else { map[entry.key] = entry.value } @@ -117,7 +126,7 @@ def validateDeploymentConfig(Map gitopsConfig) { protected initCesBuildLib(cesBuildLibRepo, cesBuildLibVersion, credentialsId) { Map retrieverParams = [$class: 'GitSCMSource', remote: cesBuildLibRepo] - if(credentialsId?.trim()) { + if (credentialsId?.trim()) { retrieverParams << [credentialsId: credentialsId] }