Benjamin Hubbard
Benjamin Hubbard

Reputation: 2917

How to pass BUILD_USER from Jenkins to PowerShell

I'm having difficulty passing BUILD_USER from Jenkins to PowerShell. I'm using the Build User Vars plugin in Jenkins. I can output "${BUILD_USER}" just fine, but when I try to pass it to PowerShell, I get nothing.

Here's my current groovy file:

#!groovy
pipeline {
    agent {
        label 'WS'
    }
    stages {
        stage ('Pass build user'){
            steps {
                wrap([$class: 'BuildUser']) {
                    script {
                        def BUILDUSER = "${BUILD_USER}"
                        echo "(Jenkins) BUILDUSER = ${BUILD_USER}"
                        powershell returnStatus: true, script: '.\\Jenkins\\Testing\\Output-BuildUser.ps1'
                    }
                }
            }
        }
    }
    post {
        always {
            cleanWs(deleteDirs: true)
        }
    }
}

Here's my PowerShell file:

"(PS) Build user is $($env:BUILDUSER)"

Not sure what else to try.

Upvotes: 1

Views: 565

Answers (1)

mklement0
mklement0

Reputation: 437568

def BUILDUSER = ... does not create an environment variable, so PowerShell (which invariably runs in a child process) won't see it.

To create an environment variable named BUILDUSER that child processes see, use
env.BUILDUSER = ...

Upvotes: 1

Related Questions