5

We're using Jenkins to test our software on both Windows and Linux. Our Jenkins server (which doesn't have any data drives mounted) starts a Jenkins slave Job on one of our Windows machine which then needs to get data from a network drive that needs to be mounted with different user credentials.

So, I need something like

net use \\server\share /u:username PASSWORD
copy data

The problem is I don't want to write PASSWORD in my Jenkins execution script.

On Linux, we use an SSH keyfile and scp for that, but what's the equivalent for that on Windows?

  • You can use OpenSSH and WINSCP, just keep i mind that if you use WINSCP you will have to use keys with no PASSPHRASE, or else you would have to enter the passphrase everytime you run the script. – arana Mar 18 '16 at 16:35
  • Thanks for your efforts. I would need to figure out if our IT department is willing to install that on the machines though... From your answer I assume there are no native Windows commands / tools to do that? – fpnick Mar 21 '16 at 15:57
  • well not native no, but there are other alternatives to installing that, : you could: ' Compile' the batch file, so you dont have plain text pwd autoitscript.com/autoit3/docs/intro/compiler.htm but if someone is into it they could decompile or sniff from memory(no problem if this is just for regular users) or use RunAs /savecred (internal cmd) and save your credentials and ask for them if other uses runs the batch howtogeek.com/124087/… – arana Mar 22 '16 at 16:57
4

You can use Jenkins' built-in "Credentials" store just for that. Just create a new "Credentials" object (type: Username and password) and then -

If it's a pipeline job:

withCredentials([usernamePassword(credentialsId: 'MyCredentialsObjectId', passwordVariable: 'CREDS_P', usernameVariable: 'CREDS_U')]) {
    bat """
        net use \\server\share /u:%CREDS_U% %CREDS_P%
        REM copy data...
    """
}

If it's a freestyle job, configure credential object to be used in "build environment" section - then use a batch step in the same manner.

Credentials will be maintained securely inside Jenkins, and will never appear in code. Additionally, Jenkins will make sure credentials are not leaked to console output, by replacing usernames and passwords with asterisks (***).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.