Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(buildDockerAndPublishImage): add optional disablePublication parameter #784

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ Name of the docker image to build
* unstash: Allow to unstash files if not empty
* dockerBakeFile: Allow to build from a bake file instead
* dockerBakeTarget: Allow to specify a docker bake target other than 'default'
* disablePublication: (Optional, default to false) Allow to disable tagging and publication of container image and GitHub release

==== Example
[source, groovy]
Expand Down
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',
disablePublication: true,
])
}
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
23 changes: 14 additions & 9 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
disablePublication: false, // Allow to disable 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.disablePublication && 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 disablePublication is set to true
if (semVerEnabledOnPrimaryBranch) {
stage("Semantic Release of ${imageName}") {
echo "Configuring credential.helper"
Expand Down Expand Up @@ -264,14 +265,18 @@ 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 (env.TAG_NAME || env.BRANCH_IS_PRIMARY) {
stage("Deploy ${imageName}") {
if (!finalConfig.disablePublication) {
infra.withDockerPushCredentials{
makecall('deploy', imageName, operatingSystem, finalConfig.dockerBakeFile, finalConfig.dockerBakeTarget)
}
} else {
echo 'INFO: publication disabled.'
} // else
} // stage
} // if

if (env.TAG_NAME && finalConfig.automaticSemanticVersioning) {
stage('GitHub Release') {
Expand Down
1 change: 1 addition & 0 deletions vars/buildDockerAndPublishImage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ The following arguments are available for this function:
* String **unstash**: (Optional, default to "") Allow to restore files from a previously saved stash if not empty, should contain the name of the last stashed as per https://www.jenkins.io/doc/pipeline/steps/workflow-basic-steps/#unstash-restore-files-previously-stashed.
* String **dockerBakeFile**: (Optionan, default to "") Specify the path to a custom Docker Bake file to use instead of the default one
* String **dockerBakeTarget**: (Optional, default to "default") Specify the target of a custom Docker Bake file to work with
* Boolean **disablePublication**: (Optional, default to false) Allow to disable tagging and publication of container image and GitHub release

The lint phase generates a report when it fails, recorded by the hadolint tool in your Jenkins instance.

Expand Down