Jenkins Post-Build Environment Variables

I understand that Jenkins sets certain environment variables at build time. But my question is, can I access these variables in my post-build script?

I checked a quick test and I cannot access PROJECT_NAME and BUILD_URL etc. from the post build python script stage.

Is there a way to access these variables from the post build python script stage? Am I doing something wrong?

+4
source share
4 answers

Another solution is to use the Jenkins EnvInject Plugin to override Jenkins settings as environment variables:

enter image description here

- Python script.

Linux script ( Jenkins ):

enter image description here

enter image description here

+9

EnvInject , .

, , Groovy PostBuild , :

/*
Inject environment variables using Groovy because EnvInject plugin is not user-friendly
*/

import hudson.model.*

def console = manager.listener.logger.&println

// read the props file
def props = new Properties()
new File("${manager.envVars['WORKSPACE']}/postbuild.props").withInputStream { 
    stream -> props.load(stream) 
}

props.each{
    key, value -> console("${key}:${value}")
    def pa = new ParametersAction([
        new StringParameterValue(key, value)
    ])
    manager.build.addAction(pa)
} 

, , - :

echo "hipchat_message=Server build succeded: <a href='https://$SERVER_NAME/'>$SERVER_NAME</a> (<a href='$BUILD_URL'>Job</a>)" > "$WORKSPACE/postbuild.props"
+3

python, Jenkins

/code/workspace/myscript.sh ${BUILD_NUMBER}

Jenkins post step screen

+2

Yes, the environment variables that are generated by Jenkins himself are available in the steps after the build (for your own environment variables you will need the EnvInject plugin )

You can post-run "execute shell" or "execute batch command" and simply type set. Everything you see there is available.

+2
source

All Articles