diff --git a/src/eu/indigo/compose/DockerCompose.groovy b/src/eu/indigo/compose/DockerCompose.groovy index 7f940a36..04dacc0f 100644 --- a/src/eu/indigo/compose/DockerCompose.groovy +++ b/src/eu/indigo/compose/DockerCompose.groovy @@ -3,6 +3,7 @@ package eu.indigo.compose import eu.indigo.JenkinsDefinitions import eu.indigo.compose.ProjectConfiguration import eu.indigo.Tox +import eu.indigo.scm.* /** * Definitions for Docker Compose integration in Jenkins @@ -316,13 +317,13 @@ class DockerCompose extends JenkinsDefinitions implements Serializable { steps.stage("Environment Setup") { // Checkout repositories to workspace with defined repository name projectConfig.config.project_repos?.each { repo_name, repo_confs -> - steps.checkout scm: [$class: 'GitSCM', userRemoteConfigs: [[url: repo_confs.repo, credentialsId: repo_confs.credentials_id]], - branches: [[name: repo_confs.branch]], - extensions: [[$class: 'CleanCheckout', deleteUntrackedNestedRepositories: true], - [$class: 'GitLFSPull'], - [$class: 'RelativeTargetDirectory', relativeTargetDir: repo_name], - [$class: 'ScmName', name: repo_name]] ], - changelog: false, poll: false + props = new GitProperties(steps) + gitObj = new Git(steps, props) + gitObj = new GitScmName(steps, props, gitObj, repo_name) + gitObj = new GitCleanCheckout(steps, props, gitObj, repo_confs.deleteUntrackedNestedRepositories) + repo_confs.gitLfsPull ? gitObj = new GitLFSPull(steps, props, gitObj) + gitObj = new GeneralOptions(steps, props, gitObj, repo_confs.changelog, repo_confs.poll) + gitObj.checkoutRepository(baseRepository: repo_confs.repo, credentialsId: repo_confs.credentials_id, baseBranch: repo_confs.branch, relativeTargetDir: repo_name) } } @@ -332,7 +333,7 @@ class DockerCompose extends JenkinsDefinitions implements Serializable { withCredentialsClosure(credentials) { // Deploy the environment services using docker-compose composeUp(composeFile: projectConfig.config.deploy_template, workdir: workspace, forceBuild: steps.env.JPL_DOCKERFORCEBUILD) - + if (_DEBUG_) { steps.sh 'echo "after loading credentials:\n$(env)"' } projectConfig.stagesList.each { stageMap -> diff --git a/src/eu/indigo/scm/Git.groovy b/src/eu/indigo/scm/Git.groovy index 6a7827a1..95ce24a9 100644 --- a/src/eu/indigo/scm/Git.groovy +++ b/src/eu/indigo/scm/Git.groovy @@ -11,13 +11,40 @@ class Git extends JenkinsDefinitions implements Serializable { private static final long serialVersionUID = 0L + protected GitProperties properties + protected Git gitObject /** * Define constructor to import definitions from Jenkins context * @see https://www.jenkins.io/doc/book/pipeline/shared-libraries/#accessing-steps */ - Git(steps) { + Git(steps, properties) { super(steps) + this.properties = properties + } + + Git(steps, properties, gitObject) { + this.Git(steps, properties) + this.gitObject = gitObject + } + + protected def checkoutScm() { + gitObject ? gitObject.checkoutRepository() : checkoutRepository() + } + + protected def checkoutScm( + String repository, + String credentialsId, + String name, + String refspec, + String branch, + String targetDirectory) { + if (gitObject) { + gitObject.checkoutRepository(repository, credentialsId, name, refspec, branch, targetDirectory) + } + else { + checkoutRepository(repository, credentialsId, name, refspec, branch, targetDirectory) + } } @NonCPS @@ -25,18 +52,63 @@ class Git extends JenkinsDefinitions implements Serializable { [ $class: 'GitSCM' ] + config } - def checkoutRepository() { - if (_DEBUG_) { steps.echo "** Git.checkoutRepository() **" } - steps.checkout steps.scm + @NonCPS + protected def userRemoteConfigs(url, name, refspec, credentialsId) { + properties.remoteConfigs += [[url: url, name: name, refspec: refspec, credentialsId: credentialsId]] } - def checkoutRepository(String repository, String branch='master', String credentialsId) { - if (_DEBUG_) { steps.echo "** Git.checkoutRepository($repository, $branch, $credentialsId) **" } + @NonCPS + protected def branches(names) { + properties.branches = names.collect { name -> + [name: name] + } + } + + @NonCPS + protected def relativeTargetDirectory(relativeTargetDir) { + [$class: 'RelativeTargetDirectory', relativeTargetDir: relativeTargetDir] + } + + @NonCPS + protected def localBranch(localBranch) { + [$class: 'LocalBranch', localBranch: localBranch] + } + + @NonCPS + protected def extensionsLoader(extension) { + properties.extensions += extension + } + + def setDefaults(settings) { + Map defaultSettings = [ + baseRepository: null, + credentialsId: null, + baseBranch: 'master', + relativeTargetDir: '.', + remoteName: '', + refspec: '', + ] + settings ? defaultSettings + settings : + defaultSettings + } + + def checkoutRepository() { + if (_DEBUG_) { steps.echo "** Git.checkoutRepository() **" } + if (properties.remoteConfigs == []) { properties.remoteConfigs = steps.scm.userRemoteConfigs } steps.checkout transformGitSCM([ - branches: [[name: "*/${branch}"]], - extensions: steps.scm.extensions + [$class: 'RelativeTargetDirectory', relativeTargetDir: '.'], - userRemoteConfigs: [[url: repository, credentialsId: credentialsId]] + branches: properties.branches, + extensions: properties.extensions, + userRemoteConfigs: properties.remoteConfigs, ]) } + def checkoutRepository(Map settings) { + settings = setDefaults(settings) + if (_DEBUG_) { steps.echo "** Git.checkoutRepository(${settings.baseRepository}, ${settings.credentialsId}, ${settings.baseBranch}, ${settings.remoteName}, ${settings.refspec}, ${settings.relativeTargetDir}) **" } + userRemoteConfigs(settings.baseRepository, settings.remoteName, settings.refspec, settings.credentialsId) + branches(["*/${settings.baseBranch}"]) + extensionsLoader(relativeTargetDirectory(settings.relativeTargetDir)) + checkoutRepository() + } + } diff --git a/src/eu/indigo/scm/GitLocalBranch.groovy b/src/eu/indigo/scm/GitLocalBranch.groovy index a3ba51c8..8621eee6 100644 --- a/src/eu/indigo/scm/GitLocalBranch.groovy +++ b/src/eu/indigo/scm/GitLocalBranch.groovy @@ -12,26 +12,25 @@ class GitLocalBranch extends Git implements Serializable { private static final long serialVersionUID = 0L + GitLocalBranch(steps, properties, gitObject, localBranch) { + super(steps, properties, gitObject) + super.properties.localBranch = localBranch + } + @Override def checkoutRepository() { if (_DEBUG_) { steps.echo "** GitLocalBranch.checkoutRepository() **" } - steps.checkout transformGitSCM([ - branches: steps.scm.branches, - extensions: steps.scm.extensions + [$class: 'LocalBranch', localBranch: '**'], - userRemoteConfigs: steps.scm.userRemoteConfigs - ]) + userRemoteConfigs(steps.scm.userRemoteConfigs[0].url, 'origin', '+refs/heads/*:refs/remotes/origin/*', steps.scm.userRemoteConfigs[0].credentialsId) + extensionsLoader(localBranch(properties.localBranch)) + checkoutScm() } @Override - def checkoutRepository(String repository, String branch='master', String credentialsId) { - if (_DEBUG_) { steps.echo "** Git.checkoutRepository($repository, $branch, $credentialsId) **" } - steps.checkout transformGitSCM([ - branches: [[name: "*/${branch}"]], - extensions: steps.scm.extensions + - [$class: 'RelativeTargetDirectory', relativeTargetDir: '.'] + - [$class: 'LocalBranch', localBranch: '**'], - userRemoteConfigs: [[url: repository, credentialsId: credentialsId]] - ]) + def checkoutRepository(Map settings) { + settings = setDefaults(settings) + if (_DEBUG_) { steps.echo "** GitLocalBranch.checkoutRepository(${settings.baseRepository}, ${settings.credentialsId}, ${settings.baseBranch}, ${settings.remoteName}, ${settings.refspec}, ${settings.relativeTargetDir}) **" } + extensionsLoader(localBranch(properties.localBranch)) + checkoutScm(baseRepository: settings.baseRepository, credentialsId: settings.credentialsId, baseBranch: settings.baseBranch, relativeTargetDir: settings.relativeTargetDir, remoteName: settings.remoteName, refspec: settings.refspec) } } diff --git a/src/eu/indigo/scm/GitProperties.groovy b/src/eu/indigo/scm/GitProperties.groovy new file mode 100644 index 00000000..9b1ed062 --- /dev/null +++ b/src/eu/indigo/scm/GitProperties.groovy @@ -0,0 +1,31 @@ +package eu.indigo.scm + +import eu.indigo.JenkinsDefinitions + +/** + * Jenkins workflow SCM step + * @see: https://www.jenkins.io/doc/pipeline/steps/workflow-scm-step/ + */ +@groovy.transform.InheritConstructors +class GitProperties extends JenkinsDefinitions implements Serializable { + + private static final long serialVersionUID = 0L + + def remoteConfigs + def extensions + def branches + def localBranch + + /** + * Define constructor to import definitions from Jenkins context + * @see https://www.jenkins.io/doc/book/pipeline/shared-libraries/#accessing-steps + */ + GitProperties(steps) { + super(steps) + this.remoteConfigs = [] + this.extensions = steps.scm.extensions + this.branches = steps.scm.branches + this.localBranch = '' + } + +} \ No newline at end of file diff --git a/vars/pipelineConfig.groovy b/vars/pipelineConfig.groovy index 45c561f0..3eb451bc 100644 --- a/vars/pipelineConfig.groovy +++ b/vars/pipelineConfig.groovy @@ -7,23 +7,13 @@ import eu.indigo.compose.ComposeFactoryBuilder import eu.indigo.compose.DockerCompose import eu.indigo.Tox import eu.indigo.scm.Git +import eu.indigo.scm.GitProperties import eu.indigo.scm.GitLocalBranch -def call( - Map configs=[ - configFile: './.sqa/config.yml', - baseRepository: null, - baseBranch: null, - credentialsId: null, - validatorDockerImage: 'eoscsynergy/jpl-validator:1.1.0', - scmConfigs: [ - localBranch: false, - ], - ] - ) { +def call(Map configs) { Map scmConfigsDefault = [ - localBranch: false + localBranch: null, ] Map scmConfigs = configs?.scmConfigs ? scmConfigsDefault + configs?.scmConfigs : scmConfigsDefault @@ -41,7 +31,7 @@ def call( def scmCheckout = { -> if (configs?.baseRepository) { - checkoutRepository(configs?.baseRepository, configs?.baseBranch, configs?.credentialsId) + checkoutRepository(baseRepository: configs?.baseRepository, credentialsId: configs?.credentialsId, baseBranch: configs?.baseBranch) } else { checkoutRepository() @@ -49,12 +39,12 @@ def call( } scmCheckout.resolveStrategy = Closure.DELEGATE_FIRST + props = new GitProperties(this) + gitObj = new Git(this, props) if (configs?.scmConfigs?.localBranch) { - scmCheckout.delegate = new GitLocalBranch(this) - } - else { - scmCheckout.delegate = new Git(this) + scmCheckout.delegate = new GitLocalBranch(this, props, gitObj, configs.scmConfigs.localBranch) } + scmCheckout.delegate = gitObj scmCheckout() def yaml = readYaml file: configs.configFile