From 3e7cd4be0f3b5fe08d5781d3a93f79cbf50b6366 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Le=20Meur?= Date: Fri, 10 Jun 2022 10:43:07 +0200 Subject: [PATCH 1/5] fix: add a try catch around github release step in case there is no release --- vars/buildDockerAndPublishImage.groovy | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/vars/buildDockerAndPublishImage.groovy b/vars/buildDockerAndPublishImage.groovy index 127ebd9d2..4585501a7 100644 --- a/vars/buildDockerAndPublishImage.groovy +++ b/vars/buildDockerAndPublishImage.groovy @@ -233,18 +233,23 @@ def call(String imageName, Map userConfig=[:]) { 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() - } 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() - } - withEnv(["GH_NEXT_RELEASE_URI=${ghReleasesApiUri}/${release}"]) { + try { if (isUnix()) { - sh 'gh api -X PATCH -F draft=false -F name="${TAG_NAME}" -F tag_name="${TAG_NAME}" "${GH_NEXT_RELEASE_URI}"' + release = sh(returnStdout: true, script: 'gh api ${GH_RELEASES_API_URI} | jq -e -r \'[ .[] | select(.draft == true and .name == "next").id] | max\'').trim() } 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"' + release = powershell(returnStdout: true, script: 'gh api $env:GH_RELEASES_API_URI | jq -e -r \'[ .[] | select(.draft == true and .name == "next").id] | max\'').trim() } - } // withEnv + 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 + } catch (e) { + echo 'Warning: an error occurred during Github Release: ' + e.toString() + echo 'It could be due to the absence of release-drafter on the repository.' + } } // withEnv } // withCredentials } // stage From a0e11d608722a50b2910aae87a8abd0b9068c815 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Le=20Meur?= Date: Fri, 10 Jun 2022 10:57:00 +0200 Subject: [PATCH 2/5] fix: isUnix for git remote get-url origin --- vars/buildDockerAndPublishImage.groovy | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/vars/buildDockerAndPublishImage.groovy b/vars/buildDockerAndPublishImage.groovy index 4585501a7..db9bca9e9 100644 --- a/vars/buildDockerAndPublishImage.groovy +++ b/vars/buildDockerAndPublishImage.groovy @@ -227,7 +227,12 @@ 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" From 20661392972a865809e5487f2db5fcfaabf1c4fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Le=20Meur?= Date: Fri, 10 Jun 2022 11:02:21 +0200 Subject: [PATCH 3/5] chore: missing comment --- ...BuildDockerAndPublishImageStepTests.groovy | 26 +++++++++++++++++++ vars/buildDockerAndPublishImage.groovy | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/test/groovy/BuildDockerAndPublishImageStepTests.groovy b/test/groovy/BuildDockerAndPublishImageStepTests.groovy index bbac8999c..691cbfe35 100644 --- a/test/groovy/BuildDockerAndPublishImageStepTests.groovy +++ b/test/groovy/BuildDockerAndPublishImageStepTests.groovy @@ -355,6 +355,32 @@ class BuildDockerAndPublishImageStepTests extends BaseTest { verifyMocks() } + @Test + void itBuildsAndDeploysButNoReleaseWhenTriggeredByTagAndSemVerEnabledButNoReleaseDrafter() throws Exception { + 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 5a2ecc610..52cc23a38 100644 --- a/vars/buildDockerAndPublishImage.groovy +++ b/vars/buildDockerAndPublishImage.groovy @@ -254,7 +254,7 @@ def call(String imageName, Map userConfig=[:]) { } catch (e) { echo 'Warning: an error occurred during Github Release: ' + e.toString() echo 'It could be due to the absence of release-drafter on the repository.' - } + } // try } // withEnv } // withCredentials } // stage From 3824f4a8cb2e96597fc827931223e9896818d77d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Le=20Meur?= Date: Fri, 10 Jun 2022 13:52:12 +0200 Subject: [PATCH 4/5] fix: test --- .../BuildDockerAndPublishImageStepTests.groovy | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/test/groovy/BuildDockerAndPublishImageStepTests.groovy b/test/groovy/BuildDockerAndPublishImageStepTests.groovy index 691cbfe35..0c2526bc7 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,9 @@ 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) { + throw new Exception('[Warn] No release found in GitHub') + } return defaultReleaseId break default: @@ -155,7 +158,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 @@ -357,6 +360,13 @@ class BuildDockerAndPublishImageStepTests extends BaseTest { @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{ From ca35f1839161d918ff7e3e58e36af79f4c0bca31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Le=20Meur?= Date: Fri, 10 Jun 2022 16:31:35 +0200 Subject: [PATCH 5/5] fix: no try catch --- ...BuildDockerAndPublishImageStepTests.groovy | 3 ++- vars/buildDockerAndPublishImage.groovy | 19 +++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/test/groovy/BuildDockerAndPublishImageStepTests.groovy b/test/groovy/BuildDockerAndPublishImageStepTests.groovy index 0c2526bc7..a63983696 100644 --- a/test/groovy/BuildDockerAndPublishImageStepTests.groovy +++ b/test/groovy/BuildDockerAndPublishImageStepTests.groovy @@ -52,7 +52,8 @@ class BuildDockerAndPublishImageStepTests extends BaseTest { case {command.contains('gh api ${GH_RELEASES_API_URI}')}: case {command.contains('gh api $env:GH_RELEASES_API_URI')}: if (failing) { - throw new Exception('[Warn] No release found in GitHub') + // No draft release found + return '' } return defaultReleaseId break diff --git a/vars/buildDockerAndPublishImage.groovy b/vars/buildDockerAndPublishImage.groovy index 52cc23a38..824f6f1e4 100644 --- a/vars/buildDockerAndPublishImage.groovy +++ b/vars/buildDockerAndPublishImage.groovy @@ -238,12 +238,12 @@ def call(String imageName, Map userConfig=[:]) { final String ghReleasesApiUri = "/repos/${org}/${repository}/releases" withEnv(["GH_RELEASES_API_URI=${ghReleasesApiUri}"]) { String release = '' - try { - 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() - } 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() - } + if (isUnix()) { + 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\' || echo \'\'').trim() + } + 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}"' @@ -251,10 +251,9 @@ def call(String imageName, Map userConfig=[:]) { 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 - } catch (e) { - echo 'Warning: an error occurred during Github Release: ' + e.toString() - echo 'It could be due to the absence of release-drafter on the repository.' - } // try + } else { + echo "No draft release found for ${org}/${repository}" + } // if } // withEnv } // withCredentials } // stage