overwrite an environment variable

38 views
Skip to first unread message

webde...@gmail.com

unread,
Apr 14, 2021, 3:01:31 PM4/14/21
to Jenkins Users
hey, all!

Here's what I would like to do:

1. in the pipeline itself, create an environment variable.
2. pass that environment variable to a job
3. that job will overwrite the environment variable's value at the top level
4. a later job will use this new value within its execution.

something like:

pipeline {
    agent {
        label 'Computer'
    }

    environment {
1.        pipelineVariable = "15"    3. (after the changeVariableValue job, variable value is now 7)
    }

    stages {
        stage('change variable value') {
            steps {
                script {
2                    build job: changeVariableValue
                    parameter: [[$class: 'StringParameterValue', name: 'PipelineVariable', """${pipelineVariable}"""]]
               }
            }
        }
        stage('new variable value') {
            steps {
                script {
4                    build job: newVariableValue                                                                                                              parameter: [[$class: 'StringParameterValue', name: 'PipelineVariable', """${pipelineVariable}"""]]
               }
            }
        }
    }
}

The jobs are written in Powershell.

Is there a way to do this?
 

Jochen Wiedmann

unread,
Apr 15, 2021, 3:35:53 AM4/15/21
to jenkins...@googlegroups.com
How about writing the value to a temporary file instead? A shell
script could use the file later on (in another stage) to set the
environment variable.
> --
> You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/13fe1896-18d5-4566-99b6-5b1044b7934cn%40googlegroups.com.



--

Look, that's why there's rules, understand? So that you think before
you break 'em.

-- (Terry Pratchett, Thief of Time)

Harry G.

unread,
Apr 16, 2021, 8:58:03 AM4/16/21
to Jenkins Users
Yes, a file would be a valid way - still you have to get that file in this pipeline, e.g. by copy artifacts plugin.

By far easier would be if you switch to scripted pipeline and run the variable modification directly in this pipeline instead of a separate job.
Then you could get a file or simply a return value from the script directly.

webde...@gmail.com

unread,
Apr 30, 2021, 10:06:52 AM4/30/21
to Jenkins Users
hey, thanks guys! you're absolutely right. I made a file, wrote the information to that file, then instructed a later job to read that file
Reply all
Reply to author
Forward
0 new messages