feat(pipeline): implement environment variable injection and dependencies resolution

Signed-off-by: 孙振宇 <>
This commit is contained in:
孙振宇 2025-01-21 17:01:31 +08:00
parent f88662b1e3
commit 52d7f78fa6
6 changed files with 193 additions and 8 deletions

View File

@ -0,0 +1,88 @@
package com.freeleaps.devops
class DependenciesResolver {
def steps
def language
def cachingEnabled
def mgr
DependenciesResolver(steps, language) {
this.steps = steps
this.language = language
}
enableCachingSupport() {
this.cachingEnabled = true
}
disableCachingSupport() {
this.cachingEnabled = false
}
useManager(DependenciesManager mgr) {
if (mgr == DependenciesManager.UNKNOWN) {
steps.error("Unknown dependencies manager")
}
this.mgr = mgr
}
def resolve(Map configurations) {
if (mgr == null) {
steps.error("Dependencies manager is not set")
}
echo "Ready to resolve dependencies for ${language}..."
echo "Using ${mgr.manager} to resolve dependencies..."
if (cachingEnabled) {
echo "Dependencies caching is enabled"
}
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")
}
def requirementsFile = configurations.PIP_REQUIREMENTS_FILE
if (cachingEnabled) {
steps.cache(maxCacheSize: 512, caches: [[$class: 'ArbitraryFileCache', excludes: '', includes: '**/*', path: '.pip-cache']]) {
steps.sh "pip install -r ${requirementsFile} --cache-dir .pip-cache"
}
} else {
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")
}
def packageJsonFile = configurations.NPM_PACKAGE_JSON_FILE
if (cachingEnabled) {
steps.cache(maxCacheSize: 512, caches: [[$class: 'ArbitraryFileCache', excludes: '', includes: '**/*', path: '.npm-cache']]) {
steps.sh "npm install --cache .npm-cache"
}
} else {
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")
}
def packageJsonFile = configurations.YARN_PACKAGE_JSON_FILE
if (cachingEnabled) {
steps.cache(maxCacheSize: 512, caches: [[$class: 'ArbitraryFileCache', excludes: '', includes: '**/*', path: '.yarn-cache']]) {
steps.sh "yarn install --cache-folder .yarn-cache"
}
} else {
steps.sh "yarn install"
}
default:
steps.error("Unsupported dependencies manager")
}
}

View File

@ -10,7 +10,7 @@ class EnvironmentVars {
}
def injectVars(Map configurations) {
if (steps.env.SERVICE_NAME == null || steps.env.SERVICE_NAME.isEmpty()) {
if (configurations.SERVICE_NAME == null || configurations.SERVICE_NAME.isEmpty()) {
steps.error("SERVICE_NAME is required")
}
steps.env.SERVICE_NAME = configurations.SERVICE_NAME
@ -20,17 +20,17 @@ class EnvironmentVars {
steps.error("Unknown service language: ${configurations.SERVICE_LANG}")
}
if (steps.env.SERVICE_GIT_REPO == null || steps.env.SERVICE_GIT_REPO.isEmpty()) {
if (configurations.SERVICE_GIT_REPO == null || configurations.SERVICE_GIT_REPO.isEmpty()) {
steps.error("SERVICE_GIT_REPO is required")
}
steps.env.SERVICE_GIT_REPO = configurations.SERVICE_GIT_REPO
if (steps.env.SERVICE_GIT_BRANCH == null || steps.env.SERVICE_GIT_BRANCH.isEmpty()) {
if (configurations.SERVICE_GIT_BRANCH == null || configurations.SERVICE_GIT_BRANCH.isEmpty()) {
steps.error("SERVICE_GIT_BRANCH is required")
}
steps.env.SERVICE_GIT_BRANCH = configurations.SERVICE_GIT_BRANCH
if (steps.env.ENVIRONMENT_SLUG == null || steps.env.ENVIRONMENT_SLUG.isEmpty()) {
if (configurations.ENVIRONMENT_SLUG == null || configurations.ENVIRONMENT_SLUG.isEmpty()) {
steps.error("ENVIRONMENT_SLUG is required")
}
steps.env.ENVIRONMENT_SLUG = configurations.ENVIRONMENT_SLUG

View File

@ -7,7 +7,15 @@ class SourceFetcher {
this.steps = steps
}
def fetch(config) {
// TODO: Implement me!
def fetch(Map configurations) {
if (configurations.SERVICE_GIT_REPO == null || configurations.SERVICE_GIT_REPO.isEmpty()) {
steps.error("SERVICE_GIT_REPO is required")
}
if (configurations.SERVICE_GIT_BRANCH == null || configurations.SERVICE_GIT_BRANCH.isEmpty()) {
steps.error("SERVICE_GIT_BRANCH is required")
}
steps.git branch: configurations.SERVICE_GIT_BRANCH, credentialsId: 'git-bot-credentials', url: configurations.SERVICE_GIT_REPO
}
}

View File

@ -0,0 +1,27 @@
package com.freeleaps.devops.enums
enum DependenciesManager {
PIP('pip'),
NPM('npm'),
YARN('yarn'),
UNKNOWN('Unknown')
final String manager
DependenciesManager(String manager) {
this.manager = manager
}
static DependenciesManager parse(String manager) {
switch (manager) {
case 'pip':
return DependenciesManager.PIP
case 'npm':
return DependenciesManager.NPM
case 'yarn':
return DependenciesManager.YARN
default:
return DependenciesManager.UNKNOWN
}
}
}

View File

@ -2,5 +2,13 @@
library 'first-class-pipeline'
def configurations = [:]
configurations.put('SERVICE_NAME', 'magicleaps')
configurations.put('SERVICE_LANG', 'Python')
configurations.put('SERVICE_GIT_REPO', '')
configurations.put('SERVICE_GIT_BRANCH', 'master')
configurations.put('ENVIRONMENT_SLUG', 'alpha')
configurations.put('PY_DEPENDENCIES_MANAGER', 'PIP')
configurations.put('REQUIREMENTS_FILE_PATH', 'requirements.txt')
pipelineCall(configurations)

View File

@ -2,6 +2,8 @@
import com.freeleaps.devops.EnvironmentVars
import com.freeleaps.devops.SourceFetcher
import com.freeleaps.devops.DependenciesResolver
import com.freeleaps.devops.enums.DependenciesManager
def call(Map configurations) {
def environmentVars = new EnvironmentVars(this)
@ -35,15 +37,67 @@ def call(Map configurations) {
stage("Commit Linting If Enabled") {
steps {
script {
def enabled = "${configurations.COMMIT_MESSAGE_LINT_ENABLED}"
def enabled = configurations.COMMIT_MESSAGE_LINT_ENABLED
if (enabled == "true") {
if (enabled != null && enabled == "true") {
print("Commit Linting is enabled")
}
}
}
}
stage("Build Agent Setup") {
steps {
script {
def buildAgentImage = configurations.BUILD_AGENT_IMAGE
if (buildAgentImage != null && !buildAgentImage.isEmpty()) {
echo "Not set BUILD_AGENT_IMAGE, using default build agent image"
def language = env.SERVICE_LANG
switch(language) {
case PYTHON:
buildAgentImage = "python:3.10-slim-buster"
case NODE:
buildAgentImage = "node:lts-alpine"
default:
error("Unknown service language")
}
env.BUILD_AGENT_IMAGE = buildAgentImage
}
}
}
}
stage("Dependencies Resolving") {
agent {
docker {
image env.BUILD_AGENT_IMAGE
}
}
steps {
script {
def language = env.SERVICE_LANG
def depManager = DependenciesManager.parse(configurations.DEPENDENCIES_MANAGER)
if (depManager == DependenciesManager.UNKNOWN) {
error("Unknown dependencies manager")
}
def dependenciesResolver = new DependenciesResolver(this, language)
dependenciesResolver.useManager(depManager)
if (configurations.BUILD_CACHE_ENABLED == "true") {
dependenciesResolver.enableCachingSupport()
} else {
dependenciesResolver.disableCachingSupport()
}
dependenciesResolver.resolve(configurations)
}
}
}
}
}
}