1

I'm encountering an issue with my pipeline script when running it from SCM. The @Field annotation isn't being recognized, even though I'm using the correct import statement (import groovy.transform.Field). This only happens when running the pipeline from SCM; it works fine when the script is executed directly in Jenkins (version 2.203.3). Does anyone have any suggestions on how to resolve this?

This is an example of a segment of my code :

import groovy.transform.Field @Field def FINAL_SELECTED_RULES = [] @Field def FULL_VERSION = "1.0.0.0" @Field def SERVICE_PARAMS = [ 'TestService1', 'TestService2', ..... ] pipeline { agent { label '' } environment { GIT_URL = "" SERVICE_NAME = "" ARTIFACTORY_SOURCE = '' DEPLOY_DIR = "${env.WORKSPACE}\\Deploy" ZIP_FILE = "${DEPLOY_DIR}\\${SERVICE_NAME}.zip" DEV_SERVER_DIR = "" } options { timeout(time: 1, unit: 'HOURS') buildDiscarder(logRotator(numToKeepStr: '10')) disableConcurrentBuilds() skipStagesAfterUnstable() } parameters { string(name: 'JENKINS_SLAVE', defaultValue: '', description: 'Slave build server') booleanParam(name: 'TestService', defaultValue: false, description: '') } stages { stage('Prepare') { steps { cleanWs() git url: "${GIT_URL}", credentialsId: 'test' } } stage('Retrieve Version Info from Artifactory') { steps { script { echo "Retrieving version info from Artifactory" FINAL_SELECTED_RULES = getSelectedRules() FINAL_SELECTED_RULES.each { rule -> echo "Processing rule: ${rule.name}" def currentVersion = getVersionFromArtifactory(rule.name+"Rules") rule.version = incrementVersion(currentVersion) echo "Rule: ${rule.name}, Current Version: ${currentVersion}, New Version: ${rule.version}" } echo "Final selected rules with versions: ${FINAL_SELECTED_RULES}" } } } } } def getSelectedRules() { SERVICE_PARAMS.findAll { paramName -> params[paramName] } .collect { paramName -> [name: paramName, version: null] } } def getSelectedRulesEnv() { SERVICE_PARAMS.collect { paramName -> [ param: paramName, dir : paramName, envId: "${paramName}" ] } } def incrementVersion(version) { def parts = version.split('\\.') parts[-1] = (parts[-1].toInteger() + 1).toString() return parts.join('.') } 
1
  • 1
    What is the error you are getting? Can you provide a snapshot of the error in the question. Commented Oct 19 at 15:22

1 Answer 1

0

So I identified the problem when I write the code directly in a Jenkins script, the compilation environment is more tolerant and similar to a standard execution of groovy script. That’s why the annotations work.

However, when I use a Jenkins script from the SCM, it adopts a more restricted and optimized compilation process called CPS. This CPS compiler does not support all of Groovy’s features in the same way as standard script compilation. Thus, the CPS compiler considers @Field as an unrecognized annotation in the context of a dynamically loaded Jenkinsfile, causing compilation to fail.

The solution to use a persistent variable between different steps or methods from SCM is to use a shared library. You can view the documentation here: Jenkins Shared Libraries. https://www.jenkins.io/doc/book/pipeline/shared-libraries/ Since I don’t have the necessary rights in my Jenkins instance,

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.