diff --git a/test/groovy/BuildDockerAndPublishImageStepTests.groovy b/test/groovy/BuildDockerAndPublishImageStepTests.groovy index 0c8d8367f..5c7dbf466 100644 --- a/test/groovy/BuildDockerAndPublishImageStepTests.groovy +++ b/test/groovy/BuildDockerAndPublishImageStepTests.groovy @@ -562,4 +562,38 @@ class BuildDockerAndPublishImageStepTests extends BaseTest { // And all mocked/stubbed methods have to be called verifyMocks() } + + @Test + void itBuildsAndDeploysWithPlatformsSpecificOnPrincipalBranch() throws Exception { + def script = loadScript(scriptName) + mockPrincipalBranch() + withMocks{ + script.call(testImageName, [ + dockerfile: 'build.Dockerfile', + imageDir: 'docker/', + platforms: ['linux/amd64','linux/arm64'], + automaticSemanticVersioning: true, + gitCredentials: 'git-creds', + registryNamespace: 'jenkins', + ]) + } + printCallStack() + // Then we expect a successful build with the code cloned + assertJobStatusSuccess() + // With the common workflow run as expected + assertTrue(assertBaseWorkflow()) + assertTrue(assertMethodCallContainsPattern('node', 'docker')) + // And the expected environment variables set to their default values + assertTrue(assertMethodCallContainsPattern('withEnv', 'IMAGE_DIR=.')) + assertTrue(assertMethodCallContainsPattern('withEnv', 'IMAGE_DOCKERFILE=Dockerfile')) + assertTrue(assertMethodCallContainsPattern('withEnv', 'IMAGE_PLATFORM=linux/amd64')) + // And generated reports recorded + assertTrue(assertRecordIssues()) + // And the deploy step called + assertTrue(assertMakeDeploy()) + // But no release created automatically + assertFalse(assertTagPushed(defaultGitTag)) + // And all mocked/stubbed methods been called + verifyMocks() + } } diff --git a/vars/buildDockerAndPublishImage.groovy b/vars/buildDockerAndPublishImage.groovy index 4d9fdc598..ab25ce578 100644 --- a/vars/buildDockerAndPublishImage.groovy +++ b/vars/buildDockerAndPublishImage.groovy @@ -10,7 +10,7 @@ def call(String imageShortName, Map userConfig=[:]) { includeImageNameInTag: false, // Set to true for multiple semversioned images built in parallel, will include the image name in tag to avoid conflict dockerfile: 'Dockerfile', // Obvious default platform: 'linux/amd64', // Intel/AMD 64 Bits, following Docker platform identifiers - platforms: [:], // Docker platform identifiers + platforms: [], // Docker platform identifiers nextVersionCommand: 'jx-release-version', // Commmand line used to retrieve the next version gitCredentials: 'github-app-infra', // Credential ID for tagging and creating release imageDir: '.', // Relative path to the context directory for the Docker build @@ -38,27 +38,31 @@ def call(String imageShortName, Map userConfig=[:]) { echo "INFO: Using platforms from the pipeline configuration" } else { echo "INFO: Using platform from the pipeline configuration" - finalConfig.platforms[finalConfig.platform]=finalConfig.platform + finalConfig.platforms = [finalConfig.platform] } - platforms.each {platform -> + + final InfraConfig infraConfig = new InfraConfig(env) + final String defaultRegistryNamespace = infraConfig.getDockerRegistryNamespace() + final String registryNamespace = finalConfig.registryNamespace ?: defaultRegistryNamespace + final String imageName = registryNamespace + '/' + imageShortName + finalConfig.platforms.each {oneplatform -> + + echo "DEBUG platform in build '${oneplatform}'." + // Warn about potential Linux/Windows contradictions between platform & agentLabels, and set the Windows config suffix for CST files String cstConfigSuffix = '' - if (finalConfig.agentLabels.contains('windows') || platform.contains('windows')) { - if (finalConfig.agentLabels.contains('windows') && !platform.contains('windows')) { - echo "WARNING: A 'windows' agent is requested, but the 'platform' is set to '${platform}'." + if (finalConfig.agentLabels.contains('windows') || oneplatform.contains('windows')) { + if (finalConfig.agentLabels.contains('windows') && !oneplatform.contains('windows')) { + echo "WARNING: A 'windows' agent is requested, but the 'platform' is set to '${oneplatform}'." } - if (!finalConfig.agentLabels.contains('windows') && platform.contains('windows')) { - echo "WARNING: The 'platform' is set to '${platform}', but there isn't any 'windows' agent requested." + if (!finalConfig.agentLabels.contains('windows') && oneplatform.contains('windows')) { + echo "WARNING: The 'platform' is set to '${oneplatform}', but there isn't any 'windows' agent requested." } cstConfigSuffix = '-windows' } - String operatingSystem = platform.split('/')[0] + String operatingSystem = oneplatform.split('/')[0] - final InfraConfig infraConfig = new InfraConfig(env) - final String defaultRegistryNamespace = infraConfig.getDockerRegistryNamespace() - final String registryNamespace = finalConfig.registryNamespace ?: defaultRegistryNamespace - final String imageName = registryNamespace + '/' + imageShortName echo "INFO: Resolved Container Image Name: ${imageName}" node(finalConfig.agentLabels) { @@ -67,7 +71,7 @@ def call(String imageShortName, Map userConfig=[:]) { "IMAGE_NAME=${imageName}", "IMAGE_DIR=${finalConfig.imageDir}", "IMAGE_DOCKERFILE=${finalConfig.dockerfile}", - "IMAGE_PLATFORM=${platform}", + "IMAGE_PLATFORM=${oneplatform}", ]) { infra.withDockerPullCredentials{ String nextVersion = ''