Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get UID in Jenkins Pipeline job

How to access UID variable in Jenkins pipeline job?

I got null when do:

pipeline {

    agent any
    environment {
        def userId = "${env.UID}";
    }
    stages {
        stage('Print UID') { 
            steps {
                script {
                   echo "${userId}"
                }
            }
        }
    }
}

Jenkins is hosted on Ubuntu 16.04

like image 493
Vadim Kovrizhkin Avatar asked Jan 03 '23 15:01

Vadim Kovrizhkin


2 Answers

This code creates a local variable in the scope of the environment block.

From the documentation the environment dir uses "setter" style properties. You can change your code to:

environment {
    userId = "${env.UID}";
} 

This will make the environment variable userId available in the other parts of the pipeline (could be proven by performing a sh 'env' or something similar).

But, before that, I am not sure where "${env.UID}" comes from. If it is a plugin you have, then it will work great. If not, then you will have to figure out how to get the value you are expecting.

like image 162
mkobit Avatar answered Jan 06 '23 13:01

mkobit


you can use this code.

environment {
    userId = sh(script: "id -u ${USER}", returnStdout: true)
}
like image 25
Fede Ogrizovic Avatar answered Jan 06 '23 13:01

Fede Ogrizovic