diff --git a/README.md b/README.md index 97a42ad..870c7ed 100644 --- a/README.md +++ b/README.md @@ -343,32 +343,30 @@ First of all there are some mandatory properties e.g. the information about your The GitOps-build-lib uses some docker images internally (To run Helm or Kubectl commands and specific Validators inside a docker container). All of these have set default images, but you can change them if you wish to. +```groovy +def gitopsConfig = [ + buildImages: [ + // These are used to run helm and kubectl commands in the core logic + helm: 'ghcr.io/cloudogu/helm:3.5.4-1', + kubectl: 'lachlanevenson/k8s-kubectl:v1.19.3', + // These are used for each specific validator via an imageRef property inside the validators config. See [Validators] for examples. + kubeval: 'ghcr.io/cloudogu/helm:3.5.4-1', + helmKubeval: 'ghcr.io/cloudogu/helm:3.5.4-1', + yamllint: 'cytopia/yamllint:1.25-0.7' + ] +] +``` + +Optional - if image is in a private repository, you can pass a `credentialsId` for pulling images. + ```groovy def gitopsConfig = [ buildImages: [ - // These are used to run helm and kubectl commands in the core logic - // helm: [ - image: 'ghcr.io/cloudogu/helm:3.11.1-2' - credentialsId: 'myCredentials' (optional - only needed if image is in a private repository. CredentialsId is getting pulled from Jenkins credentials) - ], - kubectl: [ - image: 'lachlanevenson/k8s-kubectl:v1.24.8' - credentialsId: 'myCredentials' (optional - only needed if image is in a private repository. CredentialsId is getting pulled from Jenkins credentials) - ], - // These are used for each specific validator via an imageRef property inside the validators config. See [Validators] for examples. - kubeval: [ - image: 'ghcr.io/cloudogu/helm:3.5.4-1' - credentialsId: 'myCredentials' (optional - only needed if image is in a private repository. CredentialsId is getting pulled from Jenkins credentials) + image: 'ghcr.io/cloudogu/helm:3.11.1-2', + credentialsId: 'myCredentials' ], - helmKubeval: [ - image: 'ghcr.io/cloudogu/helm:3.5.4-1' - credentialsId: 'myCredentials' (optional - only needed if image is in a private repository. CredentialsId is getting pulled from Jenkins credentials) - ], - yamllint: [ - image: 'cytopia/yamllint:1.25-0.9' - credentialsId: 'myCredentials' (optional - only needed if image is in a private repository. CredentialsId is getting pulled from Jenkins credentials) - ] + // ... ] ] ``` diff --git a/src/com/cloudogu/gitopsbuildlib/docker/DockerWrapper.groovy b/src/com/cloudogu/gitopsbuildlib/docker/DockerWrapper.groovy index 089e22a..8b3694f 100644 --- a/src/com/cloudogu/gitopsbuildlib/docker/DockerWrapper.groovy +++ b/src/com/cloudogu/gitopsbuildlib/docker/DockerWrapper.groovy @@ -10,13 +10,15 @@ class DockerWrapper { void withDockerImage(def imageConfig, Closure body) { // imageConfig can either be a Map or a String, depending on the old or the new format if this field // The old format was a String containing an image url. The new one is a map with an image url and optional credentials - if(imageConfig instanceof Map && imageConfig.containsKey('credentialsId') && imageConfig.credentialsId) { - def registryUrl = getRegistryUrlFromImage(imageConfig.image) - script.docker.withRegistry("https://${registryUrl}", imageConfig.credentialsId) { + if (imageConfig instanceof Map) { + if (imageConfig.containsKey('credentialsId') && imageConfig.credentialsId) { + def registryUrl = getRegistryUrlFromImage(imageConfig.image) + script.docker.withRegistry("https://${registryUrl}", imageConfig.credentialsId) { + runDockerImage(imageConfig.image, body) + } + } else { runDockerImage(imageConfig.image, body) } - } else if (imageConfig instanceof Map && imageConfig.containsKey('image')){ - runDockerImage(imageConfig.image, body) } else { // When imageConfig is a String runDockerImage(imageConfig, body) diff --git a/test/com/cloudogu/gitopsbuildlib/docker/DockerWrapperTest.groovy b/test/com/cloudogu/gitopsbuildlib/docker/DockerWrapperTest.groovy new file mode 100644 index 0000000..fd55cfa --- /dev/null +++ b/test/com/cloudogu/gitopsbuildlib/docker/DockerWrapperTest.groovy @@ -0,0 +1,48 @@ +package com.cloudogu.gitopsbuildlib.docker + +import com.cloudogu.gitopsbuildlib.ScriptMock +import org.junit.jupiter.api.Test + +import static org.assertj.core.api.Assertions.assertThat + +class DockerWrapperTest { + + public static final String EXPECTED_IMAGE = 'ghcr.io/cloudogu/helm:3.11.1-2' + + def scriptMock = new ScriptMock() + + def dockerWrapper = new DockerWrapper(scriptMock.mock) + + def imageConfigMap = [ + image: EXPECTED_IMAGE, + ] + def imageConfigMapWithCredentials = [ + image: EXPECTED_IMAGE, + credentialsId: 'myCredentials' + ] + def imageConfigString = EXPECTED_IMAGE + + @Test + void 'works with imageConfig string'() { + + dockerWrapper.withDockerImage(imageConfigString) { + } + assertThat(scriptMock.dockerMock.actualImages[0]).isEqualTo(EXPECTED_IMAGE) + } + + @Test + void 'works with imageConfig Map'() { + dockerWrapper.withDockerImage(imageConfigMap) { + } + assertThat(scriptMock.dockerMock.actualImages[0]).isEqualTo(EXPECTED_IMAGE) + } + + @Test + void 'works with imageConfig Map with Credentials'() { + dockerWrapper.withDockerImage(imageConfigMapWithCredentials) { + } + assertThat(scriptMock.dockerMock.actualImages[0]).isEqualTo(EXPECTED_IMAGE) + assertThat(scriptMock.dockerMock.actualRegistryArgs[0]).isEqualTo('https://ghcr.io/cloudogu') + assertThat(scriptMock.dockerMock.actualRegistryArgs[1]).isEqualTo('myCredentials') + } +}