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

Support userRemoteConfigs #117

Draft
wants to merge 23 commits into
base: release/2.1.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
a4f8b91
Patch to fetch all branches required to jenkins documentation when us…
samuelbernardolip Dec 4, 2020
afa9552
Replace exactly with issue code to debug
samuelbernardolip Dec 4, 2020
13243c4
Add steps.scm to correct previous commit bug
samuelbernardolip Dec 4, 2020
75bc51b
Remove wrong checkout call added from copy paste
samuelbernardolip Dec 4, 2020
3b697a5
Revert "Remove wrong checkout call added from copy paste"
samuelbernardolip Dec 4, 2020
00f518a
Revert "Add steps.scm to correct previous commit bug"
samuelbernardolip Dec 4, 2020
0252012
Revert "Replace exactly with issue code to debug"
samuelbernardolip Dec 4, 2020
e523079
Correct bug in userRemoteConfigs
samuelbernardolip Dec 4, 2020
d0b9658
Correct bug in userRemoteConfigs also for scm.Git
samuelbernardolip Dec 4, 2020
4dfe8fd
Bug: correct userRemoteConfigs because operation was being applied to…
samuelbernardolip Dec 4, 2020
f04d3ba
Debug userRemoteConfigs
samuelbernardolip Dec 4, 2020
6f5b58f
Force type cast to Map for userRemoteConfigs
samuelbernardolip Dec 4, 2020
a2946b0
Need to implement as operator to manage userRemoteConfigs dinamically…
samuelbernardolip Dec 4, 2020
1e0df9a
Need to implement as operator to manage userRemoteConfigs dinamically…
samuelbernardolip Dec 4, 2020
80d6b5a
Code improvement, but not yet the best composition to support GitSCM …
samuelbernardolip Dec 4, 2020
f481e6a
Debugging remoteConfigs
samuelbernardolip Dec 7, 2020
183f32f
Apply adapter pattern and optimize code
samuelbernardolip Dec 8, 2020
94ff435
pipelineConfig: optimize arguments and correct default value for loca…
samuelbernardolip Dec 10, 2020
430709b
Merge branch 'release/2.1.0' into feature/userRemoteConfigs
samuelbernardolip Dec 10, 2020
2d661a9
Merge branch 'fix/localbranch_type' into feature/userRemoteConfigs
samuelbernardolip Dec 10, 2020
7725ad2
Set adapter pattern for scm classes
samuelbernardolip Dec 10, 2020
03bc6bb
Define checkoutRepository arguments as a Map. Replace DockerCompose s…
samuelbernardolip Dec 10, 2020
f7a46fe
Add all classes from package scm in DockerCompose
samuelbernardolip Dec 10, 2020
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
17 changes: 9 additions & 8 deletions src/eu/indigo/compose/DockerCompose.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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 ->
Expand Down
90 changes: 81 additions & 9 deletions src/eu/indigo/scm/Git.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,104 @@ 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
protected def transformGitSCM(config) {
[ $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()
}

}
27 changes: 13 additions & 14 deletions src/eu/indigo/scm/GitLocalBranch.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}
31 changes: 31 additions & 0 deletions src/eu/indigo/scm/GitProperties.groovy
Original file line number Diff line number Diff line change
@@ -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 = ''
}

}
26 changes: 8 additions & 18 deletions vars/pipelineConfig.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -41,20 +31,20 @@ 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()
}
}
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
Expand Down