1

I have a freeStyleJob that currently sets a few default env variables using the following syntax:

freeStyleJob('myjob') {
    environmentVariables {
        env('MY_VARIABLE', 'default value')
    }
}

I want to add a parameter to this job so that I can override this environment variable with whatever parameter the user enters when they 'build with parameters'

I tried adding a parameter to take input as follows

job('example') {
    parameters {
        stringParam('myParameterName', 'my default stringParam value', 'my description')
    }
}

However, I am not sure how to then feed that value into the environment Variables.

Andrej Istomin
  • 2,527
  • 2
  • 15
  • 22
  • `$myParameterName` will be accessible across the Jenkins pipeline with whatever value the user entered. So you can use that instead of `$MY_VARIABLE`. Any specific reason you want to assign `$myParameterName` to `$MY_VARIABLE`? – M B Feb 14 '23 at 06:39
  • I need the value as a parameter so I can add it via 'build with parameters' That value is then required by a bash script which is called from my Jenkins pipelineJob, which is why I wanted to assign the value of $myParameterName to and environment variable $MY_VARIABLE. Unless you are saying that stringParam's in a freeStyleJob() are automatically set as environment variables? – user21113865 Feb 14 '23 at 21:30
  • No, that won't happen automatically. If you want to access a variable set in the Jenkins pipeline from the host machine, just setting it as an environment variable from the pipeline won't work either. You can use the EnvInject plugin. Look at this answer for more details: https://stackoverflow.com/a/10626193/3730626 – M B Feb 15 '23 at 06:48

0 Answers0