You've already forked jenkins-lib
forked from jenkins/jenkins-lib
Вынос scm-шага в отдельный класс
This commit is contained in:
97
src/ru/pulsar/jenkins/library/steps/Checkout.groovy
Normal file
97
src/ru/pulsar/jenkins/library/steps/Checkout.groovy
Normal file
@@ -0,0 +1,97 @@
|
||||
package ru.pulsar.jenkins.library.steps
|
||||
|
||||
import hudson.plugins.git.GitSCM
|
||||
import hudson.plugins.git.UserRemoteConfig
|
||||
import ru.pulsar.jenkins.library.IStepExecutor
|
||||
import ru.pulsar.jenkins.library.configuration.JobConfiguration
|
||||
import ru.pulsar.jenkins.library.ioc.ContextRegistry
|
||||
import ru.pulsar.jenkins.library.utils.Logger
|
||||
|
||||
class Checkout implements Serializable {
|
||||
|
||||
private final JobConfiguration config;
|
||||
|
||||
Checkout(JobConfiguration config) {
|
||||
this.config = config
|
||||
}
|
||||
|
||||
def run() {
|
||||
IStepExecutor steps = ContextRegistry.getContext().getStepExecutor()
|
||||
|
||||
Logger.printLocation()
|
||||
|
||||
def gitSCMOptions = config.gitSCMOptions
|
||||
|
||||
if (!gitSCMOptions.lfsPull) {
|
||||
return
|
||||
}
|
||||
|
||||
def scm = steps.scm()
|
||||
|
||||
scm = addLFSRemoteConfig(scm)
|
||||
|
||||
steps.checkout(scm)
|
||||
|
||||
}
|
||||
|
||||
private GitSCM addLFSRemoteConfig(GitSCM scm) {
|
||||
def gitSCMOptions = config.gitSCMOptions
|
||||
|
||||
if (gitSCMOptions.lfsURI.isEmpty()) {
|
||||
return scm
|
||||
}
|
||||
|
||||
IStepExecutor steps = ContextRegistry.getContext().getStepExecutor()
|
||||
|
||||
// TODO: get git.exe path from scm settings
|
||||
steps.cmd("git config -f .lfsconfig lfs.url $gitSCMOptions.lfsURI")
|
||||
|
||||
List<UserRemoteConfig> userRemoteConfigs = new ArrayList<>(scm.getUserRemoteConfigs())
|
||||
|
||||
if (gitSCMOptions.lfsRepoURI.isEmpty()) {
|
||||
return scm
|
||||
}
|
||||
|
||||
def userRemoteConfig = userRemoteConfigs.find { it.url == gitSCMOptions.lfsRepoURI }
|
||||
boolean needToUpdateUserRemoteConfigs
|
||||
if (userRemoteConfig == null) {
|
||||
def credentialsId = config.secrets.lfs.isEmpty() ? null : config.secrets.lfs
|
||||
userRemoteConfig = new UserRemoteConfig(
|
||||
config.gitSCMOptions.lfsRepoURI,
|
||||
null,
|
||||
null,
|
||||
credentialsId
|
||||
)
|
||||
|
||||
needToUpdateUserRemoteConfigs = true
|
||||
} else {
|
||||
def credentialsId = config.secrets.lfs.isEmpty() ? userRemoteConfig.credentialsId : config.secrets.lfs
|
||||
|
||||
if (userRemoteConfig.credentialsId != credentialsId) {
|
||||
userRemoteConfig = new UserRemoteConfig(
|
||||
userRemoteConfig.url,
|
||||
null,
|
||||
userRemoteConfig.refspec,
|
||||
credentialsId
|
||||
)
|
||||
|
||||
needToUpdateUserRemoteConfigs = true
|
||||
}
|
||||
}
|
||||
|
||||
if (needToUpdateUserRemoteConfigs) {
|
||||
userRemoteConfigs.add(0, userRemoteConfig)
|
||||
scm = new GitSCM(
|
||||
userRemoteConfigs,
|
||||
scm.branches,
|
||||
scm.doGenerateSubmoduleConfigurations,
|
||||
scm.submoduleCfg,
|
||||
scm.browser,
|
||||
scm.gitTool,
|
||||
scm.extensions
|
||||
)
|
||||
}
|
||||
|
||||
return scm
|
||||
}
|
||||
}
|
@@ -1,9 +1,6 @@
|
||||
package ru.pulsar.jenkins.library.steps
|
||||
|
||||
|
||||
import hudson.plugins.git.GitSCM
|
||||
import hudson.plugins.git.UserRemoteConfig
|
||||
import hudson.plugins.git.extensions.impl.GitLFSPull
|
||||
import ru.pulsar.jenkins.library.IStepExecutor
|
||||
import ru.pulsar.jenkins.library.configuration.JobConfiguration
|
||||
import ru.pulsar.jenkins.library.ioc.ContextRegistry
|
||||
@@ -54,95 +51,4 @@ class EdtTransform implements Serializable {
|
||||
steps.stash(WORKSPACE_ZIP_STASH, WORKSPACE_ZIP)
|
||||
}
|
||||
|
||||
private void doSCM() {
|
||||
|
||||
def gitSCMOptions = config.gitSCMOptions
|
||||
|
||||
if (!gitSCMOptions.lfsPull) {
|
||||
return
|
||||
}
|
||||
|
||||
IStepExecutor steps = ContextRegistry.getContext().getStepExecutor()
|
||||
|
||||
def scm = steps.scm()
|
||||
|
||||
boolean needToCheckout = false
|
||||
needToCheckout = addLFS(scm, needToCheckout)
|
||||
scm = addLFSRemoteConfig(scm)
|
||||
|
||||
if (needToCheckout) {
|
||||
steps.checkout(scm)
|
||||
}
|
||||
}
|
||||
|
||||
private boolean addLFS(GitSCM scm, boolean needToCheckout) {
|
||||
GitLFSPull gitLFS = new GitLFSPull();
|
||||
def extensions = scm.getExtensions()
|
||||
if (!extensions.contains(gitLFS)) {
|
||||
needToCheckout = true
|
||||
extensions.add(gitLFS)
|
||||
}
|
||||
needToCheckout
|
||||
}
|
||||
|
||||
private GitSCM addLFSRemoteConfig(GitSCM scm) {
|
||||
def gitSCMOptions = config.gitSCMOptions
|
||||
|
||||
if (gitSCMOptions.lfsURI.isEmpty()) {
|
||||
return scm
|
||||
}
|
||||
|
||||
IStepExecutor steps = ContextRegistry.getContext().getStepExecutor()
|
||||
|
||||
// TODO: get git.exe path from scm settings
|
||||
steps.cmd("git config -f .lfsconfig lfs.url $gitSCMOptions.lfsURI")
|
||||
|
||||
List<UserRemoteConfig> userRemoteConfigs = new ArrayList<>(scm.getUserRemoteConfigs())
|
||||
|
||||
if (gitSCMOptions.lfsRepoURI.isEmpty()) {
|
||||
return scm
|
||||
}
|
||||
|
||||
def userRemoteConfig = userRemoteConfigs.find { it.url == gitSCMOptions.lfsRepoURI }
|
||||
boolean needToUpdateUserRemoteConfigs
|
||||
if (userRemoteConfig == null) {
|
||||
def credentialsId = config.secrets.lfs.isEmpty() ? null : config.secrets.lfs
|
||||
userRemoteConfig = new UserRemoteConfig(
|
||||
config.gitSCMOptions.lfsRepoURI,
|
||||
null,
|
||||
null,
|
||||
credentialsId
|
||||
)
|
||||
|
||||
needToUpdateUserRemoteConfigs = true
|
||||
} else {
|
||||
def credentialsId = config.secrets.lfs.isEmpty() ? userRemoteConfig.credentialsId : config.secrets.lfs
|
||||
|
||||
if (userRemoteConfig.credentialsId != credentialsId) {
|
||||
userRemoteConfig = new UserRemoteConfig(
|
||||
userRemoteConfig.url,
|
||||
null,
|
||||
userRemoteConfig.refspec,
|
||||
credentialsId
|
||||
)
|
||||
|
||||
needToUpdateUserRemoteConfigs = true
|
||||
}
|
||||
}
|
||||
|
||||
if (needToUpdateUserRemoteConfigs) {
|
||||
userRemoteConfigs.add(0, userRemoteConfig)
|
||||
scm = new GitSCM(
|
||||
userRemoteConfigs,
|
||||
scm.branches,
|
||||
scm.doGenerateSubmoduleConfigurations,
|
||||
scm.submoduleCfg,
|
||||
scm.browser,
|
||||
scm.gitTool,
|
||||
scm.extensions
|
||||
)
|
||||
}
|
||||
|
||||
return scm
|
||||
}
|
||||
}
|
||||
|
10
vars/customCheckout.groovy
Normal file
10
vars/customCheckout.groovy
Normal file
@@ -0,0 +1,10 @@
|
||||
import ru.pulsar.jenkins.library.configuration.JobConfiguration
|
||||
import ru.pulsar.jenkins.library.ioc.ContextRegistry
|
||||
import ru.pulsar.jenkins.library.steps.Checkout
|
||||
|
||||
void call(JobConfiguration config) {
|
||||
ContextRegistry.registerDefaultContext(this)
|
||||
|
||||
def checkout = new Checkout(config)
|
||||
checkout.run()
|
||||
}
|
@@ -29,13 +29,16 @@ void call() {
|
||||
label 'agent'
|
||||
}
|
||||
|
||||
options {
|
||||
skipDefaultCheckout(false)
|
||||
}
|
||||
|
||||
environment {
|
||||
GIT_LFS_SKIP_SMUDGE=1
|
||||
}
|
||||
|
||||
steps {
|
||||
echo "test"
|
||||
checkout scm
|
||||
script {
|
||||
config = jobConfiguration() as JobConfiguration
|
||||
agent1C = config.v8version
|
||||
@@ -59,7 +62,7 @@ void call() {
|
||||
steps {
|
||||
printLocation()
|
||||
|
||||
checkout scm
|
||||
customCheckout config
|
||||
|
||||
installLocalDependencies()
|
||||
|
||||
@@ -102,7 +105,7 @@ void call() {
|
||||
expression { config.stageFlags.edtValidate }
|
||||
}
|
||||
steps {
|
||||
checkout scm
|
||||
customCheckout config
|
||||
edtTransform config
|
||||
}
|
||||
}
|
||||
@@ -120,7 +123,7 @@ void call() {
|
||||
expression { config.stageFlags.edtValidate }
|
||||
}
|
||||
steps {
|
||||
checkout scm
|
||||
customCheckout config
|
||||
edtValidate config
|
||||
}
|
||||
}
|
||||
@@ -134,7 +137,7 @@ void call() {
|
||||
expression { config.stageFlags.bdd }
|
||||
}
|
||||
steps {
|
||||
checkout scm
|
||||
customCheckout config
|
||||
unzipInfobase()
|
||||
|
||||
bdd config
|
||||
@@ -150,7 +153,7 @@ void call() {
|
||||
expression { config.stageFlags.syntaxCheck }
|
||||
}
|
||||
steps {
|
||||
checkout scm
|
||||
customCheckout config
|
||||
syntaxCheck config
|
||||
}
|
||||
}
|
||||
@@ -164,7 +167,7 @@ void call() {
|
||||
expression { config.stageFlags.smoke }
|
||||
}
|
||||
steps {
|
||||
checkout scm
|
||||
customCheckout config
|
||||
smoke config
|
||||
}
|
||||
}
|
||||
@@ -180,7 +183,7 @@ void call() {
|
||||
expression { config.stageFlags.edtValidate }
|
||||
}
|
||||
steps {
|
||||
checkout scm
|
||||
customCheckout config
|
||||
transform config
|
||||
}
|
||||
}
|
||||
@@ -194,7 +197,7 @@ void call() {
|
||||
expression { config.stageFlags.sonarqube }
|
||||
}
|
||||
steps {
|
||||
checkout scm
|
||||
customCheckout config
|
||||
sonarScanner config
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user