'how to extract jenkins credentials to python script

I am having a groovy script that uses some credentials from jenkins credentials like this:

stage('Download tokens') {
        withCredentials([string(credentialsId: 'jenkins-token', variable: 'token')])

I want something similar to this in python to retrive the token in order for passing it to a python module which needs the token to e authenticated. Is there a way I can do this?

Thanks,



Solution 1:[1]

I got it work by doing this:


stage('Download tokens') {
        withCredentials([string(credentialsId: 'jenkins-token', variable: 'token')]) {

            sh "export token=${token}"

             sh """
                   export token="${token}"

                   python3 script.py
             """

        }
    }

and in the script itself I called the token I need like this:

vault_token = os.environ['token']

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1