freeleaps-ops/first-class-pipeline/vars/executeFreeleapsPipeline.groovy
2025-02-05 16:29:52 +08:00

304 lines
9.8 KiB
Groovy

#!groovy
import com.freeleaps.devops.SourceFetcher
import com.freeleaps.devops.DependenciesResolver
import com.freeleaps.devops.CommitMessageLinter
import com.freeleaps.devops.ChangedComponentsDetector
import com.freeleaps.devops.CodeLintExecutor
import com.freeleaps.devops.enums.DependenciesManager
import com.freeleaps.devops.enums.ServiceLanguage
import com.freeleaps.devops.enums.CodeLinterTypes
def generateComponentStages(component, configurations) {
return [
stage("${component.name} :: Build Agent Setup") {
script {
if (env.executeMode == "fully" || env.changedComponents.contains(component.name)) {
def buildAgentImage = component.buildAgentImage
if (buildAgentImage == null || buildAgentImage.isEmpty()) {
log.warn("Pipeline", "Not set buildAgentImage for ${component.name}, using default build agent image")
def language = ServiceLanguage.parse(component.language)
switch(language) {
case ServiceLanguage.PYTHON:
buildAgentImage = "docker.io/python:3.10-slim-buster"
break
case ServiceLanguage.JS:
buildAgentImage = "docker.io/node:lts-alpine"
break
default:
error("Unknown service language")
}
}
log.info("Pipeline", "Using ${buildAgentImage} as build agent image for ${component.name}")
env.buildAgentImage = buildAgentImage
}
}
},
stage("${component.name} :: Dependencies Resolving") {
podTemplate(
label: "dep-resolver-${component.name}",
containers: [
containerTemplate(
name: 'dep-resolver',
image: env.buildAgentImage,
ttyEnabled: true,
command: 'sleep',
args: 'infinity'
)
]
) {
node("dep-resolver-${component.name}") {
container('dep-resolver') {
script {
if (env.executeMode == "fully" || env.changedComponents.contains(component.name)) {
log.info("Pipeline", "Using ${env.buildAgentImage} as build agent image for dependencies resolving")
def sourceFetcher = new SourceFetcher(this)
sourceFetcher.fetch(configurations)
def language = ServiceLanguage.parse(component.language)
def depManager = DependenciesManager.parse(component.dependenciesManager)
def dependenciesResolver = new DependenciesResolver(this, language, env.workspace + "/" + component.root + "/")
dependenciesResolver.useManager(depManager)
if (component.buildCacheEnabled) {
dependenciesResolver.enableCachingSupport()
} else {
dependenciesResolver.disableCachingSupport()
}
dependenciesResolver.resolve(component)
}
}
}
}
}
},
stage("${component.name} :: Code Linter Preparation") {
script {
if (env.executeMode == "fully" || env.changedComponents.contains(component.name)) {
if (component.lintEnabled != null && component.lintEnabled) {
log.info("Pipeline", "Code linting has enabled, preparing linter...")
if (linter == null || linter.isEmpty()) {
log.error("Pipeline", "Not set linter for ${component.name}, using default linter settings as fallback")
}
def linter = CodeLinterTypes.parse(component.linter)
if (linter == null) {
log.error("Pipeline", "Unknown linter for ${component.name}, skipping code linting")
}
if (linter.language != ServiceLanguage.parse(component.language)) {
log.error("Pipeline", "Linter ${linter.linter} is not supported for ${component.language}, skipping code linting")
}
log.info("Pipeline", "Using ${linter.linter} with image ${linter.containerImage} as linter for ${component.name}")
env.linterContainerImage = linter.containerImage
}
log.info("Pipeline", "Code linting is not enabled for ${component.name}, skipping...")
}
}
},
stage("${component.name} :: Code Linting If Enabled") {
podTemplate(
label: "code-linter-${component.name}",
containers: [
containerTemplate(
name: 'code-linter',
image: env.linterContainerImage,
ttyEnabled: true,
command: 'sleep',
args: 'infinity'
)
]
) {
node("code-linter-${component.name}") {
container('code-linter') {
script {
if (env.executeMode == "fully" || env.changedComponents.contains(component.name)) {
if (component.lintEnabled != null && component.lintEnabled) {
log.info("Pipeline", "Code linting has enabled, linting code...")
def sourceFetcher = new SourceFetcher(this)
sourceFetcher.fetch(configurations)
def linterType = CodeLinterTypes.parse(component.linter)
def codeLintExecutor = new CodeLintExecutor(this, env.workspace + "/" + component.root + "/", component.linterConfig, linterType)
codeLintExecutor.execute()
}
}
}
}
}
}
},
stage("${component.name} :: SAST Scanner Preparation") {
script {
if (env.executeMode == "fully" || env.changedComponents.contains(component.name)) {
if (component.sastEnabled != null && component.sastEnabled) {
log.info("Pipeline", "SAST scanning has enabled, preparing scanner...")
if (sastScanner == null || sastScanner.isEmpty()) {
log.error("Pipeline", "Not set sastScanner for ${component.name}")
}
log.info("Pipeline", "Using ${sastScanner} as SAST scanner for ${component.name}")
env.sastScanner = sastScanner
}
}
}
},
stage("${component.name} :: SAST Scanning If Enabled") {
},
stage("${component.name} :: Semantic Release Preparation") {
},
stage("${component.name} :: Semantic Releasing If Enabled") {
},
stage("${component.name} :: Compilation & Packaging") {
},
stage("${component.name} :: Image Builder Setup") {
},
stage("${component.name} :: Image Building & Publishing") {
},
stage("${component.name} :: Argo Application Version Updating (Deploying)") {
}
]
}
def call(Closure closure) {
def configurations = [:]
closure.resolveStrategy = Closure.DELEGATE_FIRST
closure.delegate = configurations
closure()
pipeline {
agent any
options {
buildDiscarder(logRotator(numToKeepStr: '25'))
timeout(time: 30, unit: 'MINUTES')
parallelsAlwaysFailFast()
}
stages {
stage("Commit Linting If Enabled") {
when {
expression {
return configurations.commitMessageLintEnabled != null && configurations.commitMessageLintEnabled
}
}
agent {
kubernetes {
defaultContainer 'commit-message-linter'
yaml """
apiVersion: v1
kind: Pod
metadata:
labels:
freeleaps-devops-system/milestone: commit-message-linting
spec:
containers:
- name: commit-message-linter
image: docker.io/commitlint/commitlint:master
command:
- cat
tty: true
volumeMounts:
- name: workspace
mountPath: /workspace
volumes:
- name: workspace
emptyDir: {}
"""
}
}
steps {
script {
log.info("Pipeline","Commit message linting is enabled")
def sourceFetcher = new SourceFetcher(this)
sourceFetcher.fetch(configurations)
def linter = new CommitMessageLinter(this)
linter.lint(configurations)
}
}
}
stage("Execute Mode Detection") {
steps {
script {
def executeMode = configurations.executeMode
if (executeMode == null || executeMode.isEmpty()) {
log.warn("Pipeline","Not set executeMode, using fully as default execute mode")
env.executeMode = "fully"
} else if (executeMode == 'on-demand' && configurations.serviceGitRepoType != 'monorepo') {
log.warn("Pipeline","serviceGirRepoType is not monorepo, on-demand mode is not supported, using fully mode")
env.executeMode = "fully"
} else {
log.info("Pipeline","Using ${executeMode} as execute mode")
env.executeMode = executeMode
}
}
}
}
stage("Code Changes Detection") {
when {
expression {
return env.executeMode == "on-demand"
}
}
steps {
script {
sourceFetcher.fetch(configurations)
def changedComponentsDetector = new ChangedComponentsDetector(this)
def changedComponents = changedComponentsDetector.detect(env.workspace, configurations.components)
log.info("Pipeline","Changed components: ${changedComponents}")
env.changedComponents = changedComponents.join(' ')
}
}
}
stage("Components Build (Dynamic Generated Stages)") {
when {
expression {
return env.executeMode == "fully" || env.changedComponents.size() > 0
}
}
steps {
script {
configurations.components.each { component ->
generateComponentStages(component, configurations)
}
}
}
}
}
}
}