diff --git a/test/groovy/BuildDockerAndPublishImageStepTests.groovy b/test/groovy/BuildDockerAndPublishImageStepTests.groovy index bbac8999c..a63983696 100644 --- a/test/groovy/BuildDockerAndPublishImageStepTests.groovy +++ b/test/groovy/BuildDockerAndPublishImageStepTests.groovy @@ -35,7 +35,7 @@ class BuildDockerAndPublishImageStepTests extends BaseTest { @Rule public ExpectedException thrown = ExpectedException.none() - String shellMock(String command) { + String shellMock(String command, Boolean failing = false) { switch (command) { case {command.contains('git tag --list')}: return defaultGitTag @@ -51,6 +51,10 @@ class BuildDockerAndPublishImageStepTests extends BaseTest { break case {command.contains('gh api ${GH_RELEASES_API_URI}')}: case {command.contains('gh api $env:GH_RELEASES_API_URI')}: + if (failing) { + // No draft release found + return '' + } return defaultReleaseId break default: @@ -155,7 +159,7 @@ class BuildDockerAndPublishImageStepTests extends BaseTest { return assertMethodCallContainsPattern('stage','GitHub Release') \ && assertMethodCallContainsPattern('withCredentials', 'GITHUB_TOKEN') \ && assertMethodCallContainsPattern('withCredentials', 'GITHUB_USERNAME') \ - && (assertMethodCallContainsPattern('sh', 'gh api ${GH_RELEASES_API_URI}') || assertMethodCallContainsPattern('powershell', 'gh api $env:GH_RELEASES_API_URI')) + && (assertMethodCallContainsPattern('sh', 'gh api -X PATCH') || assertMethodCallContainsPattern('powershell', 'gh api -X PATCH')) } @Test @@ -355,6 +359,39 @@ class BuildDockerAndPublishImageStepTests extends BaseTest { verifyMocks() } + @Test + void itBuildsAndDeploysButNoReleaseWhenTriggeredByTagAndSemVerEnabledButNoReleaseDrafter() throws Exception { + helper.registerAllowedMethod('sh', [Map.class], { m -> + return shellMock(m.script, true) + }) + helper.registerAllowedMethod('powershell', [Map.class], { m -> + return shellMock(m.script, true) + }) + + def script = loadScript(scriptName) + mockTag() + withMocks{ + script.call(testImageName, [ + automaticSemanticVersioning: true, + gitCredentials: 'git-itbuildsanddeploysandreleaseswhentriggeredbytagandsemverenabled', + ]) + } + printCallStack() + // Then we expect a successful build + assertJobStatusSuccess() + // With the common workflow run as expected + assertTrue(assertBaseWorkflow()) + assertTrue(assertMethodCallContainsPattern('node', 'docker')) + // And the deploy step called for latest + assertTrue(assertMakeDeploy("${fullTestImageName}:${defaultGitTag}")) + // And the release is created (tag triggering the build) + assertFalse(assertReleaseCreated()) + // But no tag pushed + assertFalse(assertTagPushed(defaultGitTag)) + // And all mocked/stubbed methods have to be called + verifyMocks() + } + @Test void itDeploysWithCorrectNameWhenTriggeredByTagAndImagenameHasTag() throws Exception { def script = loadScript(scriptName) diff --git a/vars/buildDockerAndPublishImage.groovy b/vars/buildDockerAndPublishImage.groovy index a66d5ddc7..824f6f1e4 100644 --- a/vars/buildDockerAndPublishImage.groovy +++ b/vars/buildDockerAndPublishImage.groovy @@ -227,24 +227,33 @@ def call(String imageName, Map userConfig=[:]) { withCredentials([ usernamePassword(credentialsId: "${finalConfig.gitCredentials}", passwordVariable: 'GITHUB_TOKEN', usernameVariable: 'GITHUB_USERNAME') ]) { - final String origin = sh(returnStdout: true, script: 'git remote get-url origin').trim() - '.git' + String origin = '' + if (isUnix()) { + origin = sh(returnStdout: true, script: 'git remote get-url origin').trim() - '.git' + } else { + origin = powershell(returnStdout: true, script: 'git remote get-url origin').trim() - '.git' + } final String org = origin.split('/')[3] final String repository = origin.split('/')[4] final String ghReleasesApiUri = "/repos/${org}/${repository}/releases" withEnv(["GH_RELEASES_API_URI=${ghReleasesApiUri}"]) { String release = '' if (isUnix()) { - release = sh(returnStdout: true, script: 'gh api ${GH_RELEASES_API_URI} | jq -e -r \'[ .[] | select(.draft == true and .name == "next").id] | max\'').trim() + release = sh(returnStdout: true, script: 'gh api ${GH_RELEASES_API_URI} | jq -e -r \'[ .[] | select(.draft == true and .name == "next").id] | max\' || echo \'\'').trim() } else { - release = powershell(returnStdout: true, script: 'gh api $env:GH_RELEASES_API_URI | jq -e -r \'[ .[] | select(.draft == true and .name == "next").id] | max\'').trim() + release = powershell(returnStdout: true, script: 'gh api $env:GH_RELEASES_API_URI | jq -e -r \'[ .[] | select(.draft == true and .name == "next").id] | max\' || echo \'\'').trim() } - withEnv(["GH_NEXT_RELEASE_URI=${ghReleasesApiUri}/${release}"]) { - if (isUnix()) { - sh 'gh api -X PATCH -F draft=false -F name="${TAG_NAME}" -F tag_name="${TAG_NAME}" "${GH_NEXT_RELEASE_URI}"' - } else { - powershell 'gh api -X PATCH -F draft=false -F name="$env:TAG_NAME" -F tag_name="$env:TAG_NAME" "$env:GH_NEXT_RELEASE_URI"' - } - } // withEnv + if (release) { + withEnv(["GH_NEXT_RELEASE_URI=${ghReleasesApiUri}/${release}"]) { + if (isUnix()) { + sh 'gh api -X PATCH -F draft=false -F name="${TAG_NAME}" -F tag_name="${TAG_NAME}" "${GH_NEXT_RELEASE_URI}"' + } else { + powershell 'gh api -X PATCH -F draft=false -F name="$env:TAG_NAME" -F tag_name="$env:TAG_NAME" "$env:GH_NEXT_RELEASE_URI"' + } + } // withEnv + } else { + echo "No draft release found for ${org}/${repository}" + } // if } // withEnv } // withCredentials } // stage