how to read AssemblyFileversion from assemblyinfo.cs file using groovy script

68 views Asked by At

we are trying to fetch the AssemblyFileversion from assemblyinfo.cs file, written code as below and included it in build stage script:

def content = readFile(assemblyfile)
def versionPattern = /AssemblyFileVersion\("([^"]+)"\)/
def versionMatch = content =~ versionPattern
def newbuildversion = versionMatch[0][1]
echo "AssemblyFileversion Version: ${newbuildversion}"

when we triggered the Jenkins pipeline Assembly file version got printed but the later steps in the build script contains bat file those are not executing.

receiving the error as "PIPELINE FAILED: java.io.NotSerializableException: java.util.regex.Matcher.... Finished: FAILURE"

  • tried to run the script in Jenkins Pipeline but receiving the error.
  • we are expecting to get Assemblyfile version and run the build script.
1

There are 1 answers

4
Simon MᶜKenzie On BEST ANSWER

Jenkins requires all variables to be serialisable so it can save a pipeline's state at any time and continue it later (or on a different machine).

Generally, if you want to use non-serialisable variables, you need to put that code into a function annotated with @NonCPS (more info is available in the docs here).

In this case, you can probably get away with just not capturing the variable at all, e.g.:

def content = readFile(assemblyfile)
def newbuildversion = (content =~ /AssemblyFileVersion\("([^"]+)"\)/)[0][1]
echo "AssemblyFileversion Version: ${newbuildversion}"