6

I have Jenkins 2.19.4 with Pipeline: Declarative Agent API 1.0.1. How does one use readProperties if you cannot define a variable to assign properties read to?

For example, to capture SVN revision number, I currently capture it with following in Script style:

```

echo "SVN_REVISION=\$(svn info ${svnUrl}/projects | \
grep Revision | \
sed 's/Revision: //g')" > svnrev.txt

```

def svnProp = readProperties file: 'svnrev.txt'

Then I can access using:

${svnProp['SVN_REVISION']}

Since it is not legal to def svnProp in Declarative style, how is readProperties used?

georgeawg
  • 48,608
  • 13
  • 72
  • 95
P. Beatty
  • 63
  • 1
  • 1
  • 4
  • Just to be clear, the above example script is from a Jenkinsfile and would be wrapped in a pipeline step "sh". – P. Beatty Feb 15 '17 at 16:26
  • Possible duplicate of [Load properties from properties file and make them available throughout the job/pipeline - Jenkins declarative syntax](https://stackoverflow.com/questions/43754764/load-properties-from-properties-file-and-make-them-available-throughout-the-job) – GhostCat Aug 09 '18 at 13:15

3 Answers3

15

You can use the script step inside the steps tag to run arbitrary pipeline code.

So something in the lines of:

pipeline {
    agent any
    stages {
        stage('A') {
            steps {
                writeFile file: 'props.txt', text: 'foo=bar'
                script {
                    def props = readProperties file:'props.txt';
                    env['foo'] = props['foo'];
                }
            }
        }
        stage('B') {
            steps {
                echo env.foo
            }
        }
    }
}

Here I'm using env to propagate the values between stages, but it might be possible to do other solutions.

Jon S
  • 15,846
  • 4
  • 44
  • 45
  • 3
    Initially got this exception `org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods putAt java.lang.Object` I was able to "whitelist" by going to "In-process Script Approvals" and `approving staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods putAt java.lang.Object` Now it works! – P. Beatty Feb 15 '17 at 18:02
  • @P.Beatty I am seeing similar issue. Is it possible to mentioned whitelisting process in details. I am kind of lost, not sure where to start – Balkrishna Jun 05 '17 at 20:38
  • 1
    Never mind found it "Navigate to jenkins > Manage jenkins > In-process Script Approval" – Balkrishna Jun 05 '17 at 20:57
4

The Jon S answer requires granting script approval because it is setting environment variables. This is not needed when running in same stage.

pipeline {
  agent any
  stages {
    stage('A') {
      steps {
        writeFile file: 'props.txt', text: 'foo=bar'
        script {
           def props = readProperties file:'props.txt';
        }
        sh "echo $props['foo']"
      }
    }
  }
}
Neuron
  • 5,141
  • 5
  • 38
  • 59
Dennis Hoer
  • 3,039
  • 2
  • 23
  • 34
3

To define general vars available to all stages, define values for example in props.txt as:

version=1.0
fix=alfa

and mix script and declarative Jenkins pipeline as:

def props
def VERSION
def FIX
def RELEASE

node {
   props = readProperties file:'props.txt'
   VERSION = props['version']
   FIX = props['fix']
   RELEASE = VERSION + "_" + FIX
}

pipeline {
   stages {
      stage('Build') {
         echo ${RELEASE}
      }
   }
}
dpinya
  • 542
  • 7
  • 16
  • Didn't know we could mix them. Is there any doc on this? – LppEdd Aug 12 '22 at 07:57
  • In [Pipeline Concepts](https://www.jenkins.io/doc/book/pipeline/#pipeline-concepts) you can find differences between declarative and scripted pipelines – dpinya Aug 23 '22 at 14:57