Skip to content

Commit

Permalink
feat(buildDockerAndPublishImage): add optional enablePublication pa…
Browse files Browse the repository at this point in the history
…rameter
  • Loading branch information
lemeurherve committed Feb 9, 2024
1 parent 8583c1c commit 5e3e4a6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
33 changes: 33 additions & 0 deletions test/groovy/BuildDockerAndPublishImageStepTests.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,39 @@ class BuildDockerAndPublishImageStepTests extends BaseTest {
verifyMocks()
}

@Test
void itBuildsButDontDeploysWithAutomaticSemanticTagAndReleaseAndEnabledPublicationFalseOnPrincipalBranch() throws Exception {
def script = loadScript(scriptName)
mockPrincipalBranch()
withMocks{
script.call(testImageName, [
automaticSemanticVersioning: true,
gitCredentials: 'git-itbuildsanddeployswithautomaticsemantictagandreleaseonprincipalbranch',
enablePublication: false,
])
}
printCallStack()
// Then we expect a successful build with the code cloned
assertJobStatusSuccess()
// With the common workflow run as expected
assertTrue(assertMethodCallContainsPattern('libraryResource','io/jenkins/infra/docker/Makefile'))
assertTrue(assertMethodCallContainsPattern('withEnv', "BUILD_DATE=${mockedSimpleDate}"))
assertTrue(assertMethodCallContainsPattern('sh','make lint'))
assertTrue(assertMethodCallContainsPattern('sh','make bake-build'))
assertTrue(assertMethodCallContainsPattern('node', 'docker'))
// And generated reports are recorded
assertTrue(assertRecordIssues())
// But the deploy step is not called
assertFalse(assertMethodCallContainsPattern('sh','make bake-deploy'))

// And the tag is not pushed
assertFalse(assertTagPushed(defaultGitTag))
// And no release created (no tag triggering the build)
assertFalse(assertReleaseCreated())
// And all mocked/stubbed methods have to be called
verifyMocks()
}

@Test
void itBuildsAndDeploysWithAutomaticSemanticTagAndincludeImageNameInTagAndReleaseOnPrincipalBranch() throws Exception {
def script = loadScript(scriptName)
Expand Down
22 changes: 12 additions & 10 deletions vars/buildDockerAndPublishImage.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@ def call(String imageShortName, Map userConfig=[:]) {
unstash: '', // Allow to unstash files if not empty
dockerBakeFile: '', // Specify the path to a custom Docker Bake file to use instead of the default one
dockerBakeTarget: 'default', // Specify the target of a custom Docker Bake file to work with
enablePublication: true, // Enable tagging and publication of container image and GitHub release (true by default)
]
// Merging the 2 maps - https://blog.mrhaki.com/2010/04/groovy-goodness-adding-maps-to-map_21.html
final Map finalConfig = defaultConfig << userConfig

// Retrieve Library's Static File Resources
final String makefileContent = libraryResource 'io/jenkins/infra/docker/Makefile'

final boolean semVerEnabledOnPrimaryBranch = finalConfig.automaticSemanticVersioning && env.BRANCH_IS_PRIMARY
final boolean semVerEnabledOnPrimaryBranch = finalConfig.automaticSemanticVersioning && finalConfig.enablePublication && env.BRANCH_IS_PRIMARY

// Only run 1 build at a time on primary branch to ensure builds won't use the same tag when semantic versionning is activated
if (env.BRANCH_IS_PRIMARY) {
Expand Down Expand Up @@ -225,7 +226,7 @@ def call(String imageShortName, Map userConfig=[:]) {
} // if else
} // each

// Automatic tagging on principal branch is not enabled by default
// Automatic tagging on principal branch is not enabled by default, and disabled if enablePublication is set to false
if (semVerEnabledOnPrimaryBranch) {
stage("Semantic Release of ${imageName}") {
echo "Configuring credential.helper"
Expand Down Expand Up @@ -264,14 +265,15 @@ def call(String imageShortName, Map userConfig=[:]) {
} // stage
} // if
}// withDockerPullCredentials
infra.withDockerPushCredentials{
if (env.TAG_NAME || env.BRANCH_IS_PRIMARY) {
stage("Deploy ${imageName}") {
makecall('deploy', imageName, operatingSystem, finalConfig.dockerBakeFile, finalConfig.dockerBakeTarget)
}
} // if
} // withDockerPushCredentials

if (finalConfig.enablePublication) {
infra.withDockerPushCredentials{
if (env.TAG_NAME || env.BRANCH_IS_PRIMARY) {
stage("Deploy ${imageName}") {
makecall('deploy', imageName, operatingSystem, finalConfig.dockerBakeFile, finalConfig.dockerBakeTarget)
}
} // if
} // withDockerPushCredentials
} // if

if (env.TAG_NAME && finalConfig.automaticSemanticVersioning) {
stage('GitHub Release') {
Expand Down

0 comments on commit 5e3e4a6

Please sign in to comment.