From 2d2cf27318f75603dac74a5b03973acc6d9b5158 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E6=8C=AF=E5=AE=87?= <> Date: Wed, 22 Jan 2025 15:42:56 +0800 Subject: [PATCH] refactor(DependenciesResolver, EnvironmentVars, SourceFetcher, pipelineCall): standardize configuration property names to camelCase MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 孙振宇 <> --- .../devops/DependenciesResolver.groovy | 20 +++++------ .../freeleaps/devops/EnvironmentVars.groovy | 32 ++++++++--------- .../com/freeleaps/devops/SourceFetcher.groovy | 12 +++---- first-class-pipeline/tests/Jenkinsfile | 14 ++++---- first-class-pipeline/vars/pipelineCall.groovy | 34 +++++++++---------- 5 files changed, 56 insertions(+), 56 deletions(-) diff --git a/first-class-pipeline/src/com/freeleaps/devops/DependenciesResolver.groovy b/first-class-pipeline/src/com/freeleaps/devops/DependenciesResolver.groovy index 51059d6d..feb05f37 100644 --- a/first-class-pipeline/src/com/freeleaps/devops/DependenciesResolver.groovy +++ b/first-class-pipeline/src/com/freeleaps/devops/DependenciesResolver.groovy @@ -28,7 +28,7 @@ class DependenciesResolver { this.mgr = mgr } - def resolve(Map configurations) { + def resolve(configurations) { if (mgr == null) { steps.error("Dependencies manager is not set") } @@ -43,11 +43,11 @@ class DependenciesResolver { switch (mgr) { case DependenciesManager.PIP: - if (configurations.PIP_REQUIREMENTS_FILE == null || configurations.PIP_REQUIREMENTS_FILE.isEmpty()) { - steps.error("PIP_REQUIREMENTS_FILE is required when using PIP as dependencies manager") + if (configurations.pipRequirementsFile == null || configurations.pipRequirementsFile.isEmpty()) { + steps.error("pipRequirementsFile is required when using PIP as dependencies manager") } - def requirementsFile = configurations.PIP_REQUIREMENTS_FILE + def requirementsFile = configurations.pipRequirementsFile if (cachingEnabled) { steps.cache(maxCacheSize: 512, caches: [[$class: 'ArbitraryFileCache', excludes: '', includes: '**/*', path: '.pip-cache']]) { @@ -57,11 +57,11 @@ class DependenciesResolver { steps.sh "pip install -r ${requirementsFile}" } case DependenciesManager.NPM: - if (configurations.NPM_PACKAGE_JSON_FILE == null || configurations.NPM_PACKAGE_JSON_FILE.isEmpty()) { - steps.error("NPM_PACKAGE_JSON_FILE is required when using NPM as dependencies manager") + if (configurations.npmPackageJsonFile == null || configurations.npmPackageJsonFile.isEmpty()) { + steps.error("npmPackageJsonFile is required when using NPM as dependencies manager") } - def packageJsonFile = configurations.NPM_PACKAGE_JSON_FILE + def packageJsonFile = configurations.npmPackageJsonFile if (cachingEnabled) { steps.cache(maxCacheSize: 512, caches: [[$class: 'ArbitraryFileCache', excludes: '', includes: '**/*', path: '.npm-cache']]) { @@ -71,11 +71,11 @@ class DependenciesResolver { steps.sh "npm install" } case DependenciesManager.YARN: - if (configurations.YARN_PACKAGE_JSON_FILE == null || configurations.YARN_PACKAGE_JSON_FILE.isEmpty()) { - steps.error("YARN_PACKAGE_JSON_FILE is required when using YARN as dependencies manager") + if (configurations.yarnPackageJsonFile == null || configurations.yarnPackageJsonFile.isEmpty()) { + steps.error("yarnPackageJsonFile is required when using YARN as dependencies manager") } - def packageJsonFile = configurations.YARN_PACKAGE_JSON_FILE + def packageJsonFile = configurations.yarnPackageJsonFile if (cachingEnabled) { steps.cache(maxCacheSize: 512, caches: [[$class: 'ArbitraryFileCache', excludes: '', includes: '**/*', path: '.yarn-cache']]) { diff --git a/first-class-pipeline/src/com/freeleaps/devops/EnvironmentVars.groovy b/first-class-pipeline/src/com/freeleaps/devops/EnvironmentVars.groovy index 29536033..8ccaffb0 100644 --- a/first-class-pipeline/src/com/freeleaps/devops/EnvironmentVars.groovy +++ b/first-class-pipeline/src/com/freeleaps/devops/EnvironmentVars.groovy @@ -9,30 +9,30 @@ class EnvironmentVars { this.steps = steps } - def injectVars(Map configurations) { - if (configurations.SERVICE_NAME == null || configurations.SERVICE_NAME.isEmpty()) { - steps.error("SERVICE_NAME is required") + def injectVars(configurations) { + if (configurations.serviceName == null || configurations.serviceName.isEmpty()) { + steps.error("serviceName is required") } - steps.env.SERVICE_NAME = configurations.SERVICE_NAME + steps.env.serviceName = configurations.serviceName - steps.env.SERVICE_LANG = ServiceLanguage.parse(configurations.SERVICE_LANG) - if (steps.env.SERVICE_LANG == ServiceLanguage.UNKNOWN) { - steps.error("Unknown service language: ${configurations.SERVICE_LANG}") + steps.env.serviceLang = ServiceLanguage.parse(configurations.serviceLang) + if (steps.env.serviceLang == ServiceLanguage.UNKNOWN) { + steps.error("Unknown service language: ${configurations.serviceLang}") } - if (configurations.SERVICE_GIT_REPO == null || configurations.SERVICE_GIT_REPO.isEmpty()) { - steps.error("SERVICE_GIT_REPO is required") + if (configurations.serviceGitRepo == null || configurations.serviceGitRepo.isEmpty()) { + steps.error("serviceGitRepo is required") } - steps.env.SERVICE_GIT_REPO = configurations.SERVICE_GIT_REPO + steps.env.serviceGitRepo = configurations.serviceGitRepo - if (configurations.SERVICE_GIT_BRANCH == null || configurations.SERVICE_GIT_BRANCH.isEmpty()) { - steps.error("SERVICE_GIT_BRANCH is required") + if (configurations.serviceGitBranch == null || configurations.serviceGitBranch.isEmpty()) { + steps.error("serviceGitBranch is required") } - steps.env.SERVICE_GIT_BRANCH = configurations.SERVICE_GIT_BRANCH + steps.env.serviceGitBranch = configurations.serviceGitBranch - if (configurations.ENVIRONMENT_SLUG == null || configurations.ENVIRONMENT_SLUG.isEmpty()) { - steps.error("ENVIRONMENT_SLUG is required") + if (configurations.environmentSlug == null || configurations.environmentSlug.isEmpty()) { + steps.error("environmentSlug is required") } - steps.env.ENVIRONMENT_SLUG = configurations.ENVIRONMENT_SLUG + steps.env.environmentSlug = configurations.environmentSlug } } \ No newline at end of file diff --git a/first-class-pipeline/src/com/freeleaps/devops/SourceFetcher.groovy b/first-class-pipeline/src/com/freeleaps/devops/SourceFetcher.groovy index e7f9704b..a0f2f37a 100644 --- a/first-class-pipeline/src/com/freeleaps/devops/SourceFetcher.groovy +++ b/first-class-pipeline/src/com/freeleaps/devops/SourceFetcher.groovy @@ -7,16 +7,16 @@ class SourceFetcher { this.steps = steps } - def fetch(Map configurations) { + def fetch(configurations) { echo configurations - if (configurations.SERVICE_GIT_REPO == null || configurations.SERVICE_GIT_REPO.isEmpty()) { - steps.error("SERVICE_GIT_REPO is required") + if (configurations.serviceGitRepo == null || configurations.serviceGitRepo.isEmpty()) { + steps.error("serviceGitRepo is required") } - if (configurations.SERVICE_GIT_BRANCH == null || configurations.SERVICE_GIT_BRANCH.isEmpty()) { - steps.error("SERVICE_GIT_BRANCH is required") + if (configurations.serviceGitBranch == null || configurations.serviceGitBranch.isEmpty()) { + steps.error("serviceGitBranch is required") } - steps.git branch: configurations.SERVICE_GIT_BRANCH, credentialsId: 'git-bot-credentials', url: configurations.SERVICE_GIT_REPO + steps.git branch: configurations.serviceGitBranch, credentialsId: 'git-bot-credentials', url: configurations.serviceGitRepo } } \ No newline at end of file diff --git a/first-class-pipeline/tests/Jenkinsfile b/first-class-pipeline/tests/Jenkinsfile index 56d93fa4..5e7a5769 100644 --- a/first-class-pipeline/tests/Jenkinsfile +++ b/first-class-pipeline/tests/Jenkinsfile @@ -3,11 +3,11 @@ library 'first-class-pipeline' pipelineCall { - SERVICE_NAME = 'magicleaps' - SERVICE_LANG = 'Python' - SERVICE_GIT_BRANCH = 'master' - SERVICE_GIT_REPO = "https://freeleaps@dev.azure.com/freeleaps/magicleaps/_git/magicleaps", - ENVIRONMENT_SLUG = 'alpha' - PY_DEPENDENCIES_MANAGER = 'PIP' - REQUIREMENTS_FILE_PATH = 'requirements.txt' + serviceName = 'magicleaps' + serviceLang = 'Python' + serviceGitBranch = 'master' + serviceGitRepo = "https://freeleaps@dev.azure.com/freeleaps/magicleaps/_git/magicleaps", + environmentSlug = 'alpha' + dependenciesManager = 'PIP' + pipRequirementsFile = 'requirements.txt' } \ No newline at end of file diff --git a/first-class-pipeline/vars/pipelineCall.groovy b/first-class-pipeline/vars/pipelineCall.groovy index 52abe28e..2668595f 100644 --- a/first-class-pipeline/vars/pipelineCall.groovy +++ b/first-class-pipeline/vars/pipelineCall.groovy @@ -5,11 +5,11 @@ import com.freeleaps.devops.SourceFetcher import com.freeleaps.devops.DependenciesResolver import com.freeleaps.devops.enums.DependenciesManager -def call(configurations) { - def configurationMap = [:] - configurations.resolveStrategy = Closure.DELEGATE_FIRST - configurations.delegate = configurationMap - configurations() +def call(body) { + def configurations = [:] + body.resolveStrategy = Closure.DELEGATE_FIRST + body.delegate = configurations + body() def environmentVars = new EnvironmentVars(this) @@ -26,7 +26,7 @@ def call(configurations) { steps { script { def sourceFetcher = new SourceFetcher(this) - sourceFetcher.fetch(configurationMap) + sourceFetcher.fetch(configurations) } } } @@ -34,7 +34,7 @@ def call(configurations) { stage("Prepared Environment Variables") { steps { script { - environmentVars.injectVars(configurationMap) + environmentVars.injectVars(configurations) } } } @@ -42,7 +42,7 @@ def call(configurations) { stage("Commit Linting If Enabled") { steps { script { - def enabled = configurations.COMMIT_MESSAGE_LINT_ENABLED + def enabled = configurations.commitMessageLintEnabled if (enabled != null && enabled == "true") { print("Commit Linting is enabled") @@ -54,11 +54,11 @@ def call(configurations) { stage("Build Agent Setup") { steps { script { - def buildAgentImage = configurationMap.BUILD_AGENT_IMAGE + def buildAgentImage = configurations.buildAgentImage if (buildAgentImage != null && !buildAgentImage.isEmpty()) { - echo "Not set BUILD_AGENT_IMAGE, using default build agent image" + echo "Not set buildAgentImage, using default build agent image" - def language = env.SERVICE_LANG + def language = env.serviceLang switch(language) { case PYTHON: buildAgentImage = "python:3.10-slim-buster" @@ -68,7 +68,7 @@ def call(configurations) { error("Unknown service language") } - env.BUILD_AGENT_IMAGE = buildAgentImage + env.buildAgentImage = buildAgentImage } } } @@ -87,7 +87,7 @@ metadata: spec: containers: - name: dep-resolver - image: ${env.BUILD_AGENT_IMAGE} + image: ${env.buildAgentImage} command: - cat tty: true @@ -102,9 +102,9 @@ spec: } steps { script { - def language = env.SERVICE_LANG + def language = env.serviceLang - def depManager = DependenciesManager.parse(configurationMap.DEPENDENCIES_MANAGER) + def depManager = DependenciesManager.parse(configurations.dependenciesManager) if (depManager == DependenciesManager.UNKNOWN) { error("Unknown dependencies manager") } @@ -112,13 +112,13 @@ spec: def dependenciesResolver = new DependenciesResolver(this, language) dependenciesResolver.useManager(depManager) - if (configurationMap.BUILD_CACHE_ENABLED == "true") { + if (configurations.BUILD_CACHE_ENABLED == "true") { dependenciesResolver.enableCachingSupport() } else { dependenciesResolver.disableCachingSupport() } - dependenciesResolver.resolve(configurationMap) + dependenciesResolver.resolve(configurations) } } }