How to invoke Jenkins credentials in a jenkins scripted pipeline (not declarative)
i am trying to use jenkins scripted pipeline to invoke config file provider plugin along with fetching credentials from jenkins for the username and password, but the below doesn't seem to work.
node {
def mvnHome
def mvnSettings
stage('Prepare') {
mvnHome = tool 'maven-3.5.4'
}
stage('Checkout') {
checkout scm
}
stage('Deploy'){
def usernameLocal, passwordLocal, usr, psw
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'xyz', passwordVariable: 'PASSWORD', usernameVariable: 'USERNAME']]) {
usernameLocal = env.USERNAME
passwordLocal = env.PASSWORD
}
configFileProvider(
[configFile(fileId: '*********', variable: 'MAVEN_SETTINGS', replaceTokens: true)])
{
usr="${usernameLocal}"
psw="${passwordLocal}"
sh "echo $usr"
sh "'${mvnHome}/bin/mvn' -s $MAVEN_SETTINGS deploy -Dserver.username="${usernameLocal}" -Dserver.password="${passwordLocal}""
}
}
}
where server.username and server.password are defined as properties under settings.xml server section for username and password.
Looks like i found out the issue and its nothing to do with withCredentials used here rather to do with the config file provider plugin. So i am able to print the credentials username correctly but somehow the config file provider is unable to substitute the variable value in the settings.xml.
so i don't get any error anymore, its just that the deployment doesn't go through with 401 unauthorized since the below in my settings.xml never gets the correct values :-
<server>
<id>snapshot</id>
<username>${server.username}</username>
<password>${server.password}</password>
</server>
Could you please advise how to resolve this?
jenkins jenkins-plugins jenkins-pipeline
|
show 2 more comments
i am trying to use jenkins scripted pipeline to invoke config file provider plugin along with fetching credentials from jenkins for the username and password, but the below doesn't seem to work.
node {
def mvnHome
def mvnSettings
stage('Prepare') {
mvnHome = tool 'maven-3.5.4'
}
stage('Checkout') {
checkout scm
}
stage('Deploy'){
def usernameLocal, passwordLocal, usr, psw
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'xyz', passwordVariable: 'PASSWORD', usernameVariable: 'USERNAME']]) {
usernameLocal = env.USERNAME
passwordLocal = env.PASSWORD
}
configFileProvider(
[configFile(fileId: '*********', variable: 'MAVEN_SETTINGS', replaceTokens: true)])
{
usr="${usernameLocal}"
psw="${passwordLocal}"
sh "echo $usr"
sh "'${mvnHome}/bin/mvn' -s $MAVEN_SETTINGS deploy -Dserver.username="${usernameLocal}" -Dserver.password="${passwordLocal}""
}
}
}
where server.username and server.password are defined as properties under settings.xml server section for username and password.
Looks like i found out the issue and its nothing to do with withCredentials used here rather to do with the config file provider plugin. So i am able to print the credentials username correctly but somehow the config file provider is unable to substitute the variable value in the settings.xml.
so i don't get any error anymore, its just that the deployment doesn't go through with 401 unauthorized since the below in my settings.xml never gets the correct values :-
<server>
<id>snapshot</id>
<username>${server.username}</username>
<password>${server.password}</password>
</server>
Could you please advise how to resolve this?
jenkins jenkins-plugins jenkins-pipeline
Perhaps add to this with the error you are receiving
– metalisticpain
Nov 22 '18 at 4:28
I have provided all details that might serve useful, including the error message.The short of it is that i want to invoke credentials from Jenkins to retrieve username and password (for some reason) in the scripted pipeline and pass them on to the config file provider mvn command as parameters.
– Ashley
Nov 23 '18 at 15:03
Ok the previous issue is resolved and i have updated the post now. Is that something you could have a look at now and help me figure out the issue?
– Ashley
Nov 23 '18 at 20:11
Thanks Mark for the prompt response. Yes the password uses special chars. But how do i know if the config file provider sh script is even substituting the settings.xml username and password with the values i am passing in that script.?
– Ashley
Nov 23 '18 at 20:14
Try sh "echo '${mvnHome}/bin/mvn' -s $MAVEN_SETTINGS deploy -Dserver.username="${usernameLocal}" -Dserver.password="${passwordLocal}""
– Mark Bidewell
Nov 23 '18 at 20:16
|
show 2 more comments
i am trying to use jenkins scripted pipeline to invoke config file provider plugin along with fetching credentials from jenkins for the username and password, but the below doesn't seem to work.
node {
def mvnHome
def mvnSettings
stage('Prepare') {
mvnHome = tool 'maven-3.5.4'
}
stage('Checkout') {
checkout scm
}
stage('Deploy'){
def usernameLocal, passwordLocal, usr, psw
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'xyz', passwordVariable: 'PASSWORD', usernameVariable: 'USERNAME']]) {
usernameLocal = env.USERNAME
passwordLocal = env.PASSWORD
}
configFileProvider(
[configFile(fileId: '*********', variable: 'MAVEN_SETTINGS', replaceTokens: true)])
{
usr="${usernameLocal}"
psw="${passwordLocal}"
sh "echo $usr"
sh "'${mvnHome}/bin/mvn' -s $MAVEN_SETTINGS deploy -Dserver.username="${usernameLocal}" -Dserver.password="${passwordLocal}""
}
}
}
where server.username and server.password are defined as properties under settings.xml server section for username and password.
Looks like i found out the issue and its nothing to do with withCredentials used here rather to do with the config file provider plugin. So i am able to print the credentials username correctly but somehow the config file provider is unable to substitute the variable value in the settings.xml.
so i don't get any error anymore, its just that the deployment doesn't go through with 401 unauthorized since the below in my settings.xml never gets the correct values :-
<server>
<id>snapshot</id>
<username>${server.username}</username>
<password>${server.password}</password>
</server>
Could you please advise how to resolve this?
jenkins jenkins-plugins jenkins-pipeline
i am trying to use jenkins scripted pipeline to invoke config file provider plugin along with fetching credentials from jenkins for the username and password, but the below doesn't seem to work.
node {
def mvnHome
def mvnSettings
stage('Prepare') {
mvnHome = tool 'maven-3.5.4'
}
stage('Checkout') {
checkout scm
}
stage('Deploy'){
def usernameLocal, passwordLocal, usr, psw
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'xyz', passwordVariable: 'PASSWORD', usernameVariable: 'USERNAME']]) {
usernameLocal = env.USERNAME
passwordLocal = env.PASSWORD
}
configFileProvider(
[configFile(fileId: '*********', variable: 'MAVEN_SETTINGS', replaceTokens: true)])
{
usr="${usernameLocal}"
psw="${passwordLocal}"
sh "echo $usr"
sh "'${mvnHome}/bin/mvn' -s $MAVEN_SETTINGS deploy -Dserver.username="${usernameLocal}" -Dserver.password="${passwordLocal}""
}
}
}
where server.username and server.password are defined as properties under settings.xml server section for username and password.
Looks like i found out the issue and its nothing to do with withCredentials used here rather to do with the config file provider plugin. So i am able to print the credentials username correctly but somehow the config file provider is unable to substitute the variable value in the settings.xml.
so i don't get any error anymore, its just that the deployment doesn't go through with 401 unauthorized since the below in my settings.xml never gets the correct values :-
<server>
<id>snapshot</id>
<username>${server.username}</username>
<password>${server.password}</password>
</server>
Could you please advise how to resolve this?
node {
def mvnHome
def mvnSettings
stage('Prepare') {
mvnHome = tool 'maven-3.5.4'
}
stage('Checkout') {
checkout scm
}
stage('Deploy'){
def usernameLocal, passwordLocal, usr, psw
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'xyz', passwordVariable: 'PASSWORD', usernameVariable: 'USERNAME']]) {
usernameLocal = env.USERNAME
passwordLocal = env.PASSWORD
}
configFileProvider(
[configFile(fileId: '*********', variable: 'MAVEN_SETTINGS', replaceTokens: true)])
{
usr="${usernameLocal}"
psw="${passwordLocal}"
sh "echo $usr"
sh "'${mvnHome}/bin/mvn' -s $MAVEN_SETTINGS deploy -Dserver.username="${usernameLocal}" -Dserver.password="${passwordLocal}""
}
}
}
node {
def mvnHome
def mvnSettings
stage('Prepare') {
mvnHome = tool 'maven-3.5.4'
}
stage('Checkout') {
checkout scm
}
stage('Deploy'){
def usernameLocal, passwordLocal, usr, psw
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'xyz', passwordVariable: 'PASSWORD', usernameVariable: 'USERNAME']]) {
usernameLocal = env.USERNAME
passwordLocal = env.PASSWORD
}
configFileProvider(
[configFile(fileId: '*********', variable: 'MAVEN_SETTINGS', replaceTokens: true)])
{
usr="${usernameLocal}"
psw="${passwordLocal}"
sh "echo $usr"
sh "'${mvnHome}/bin/mvn' -s $MAVEN_SETTINGS deploy -Dserver.username="${usernameLocal}" -Dserver.password="${passwordLocal}""
}
}
}
<server>
<id>snapshot</id>
<username>${server.username}</username>
<password>${server.password}</password>
</server>
<server>
<id>snapshot</id>
<username>${server.username}</username>
<password>${server.password}</password>
</server>
jenkins jenkins-plugins jenkins-pipeline
jenkins jenkins-plugins jenkins-pipeline
edited Nov 23 '18 at 20:06
Ashley
asked Nov 22 '18 at 1:16
AshleyAshley
799
799
Perhaps add to this with the error you are receiving
– metalisticpain
Nov 22 '18 at 4:28
I have provided all details that might serve useful, including the error message.The short of it is that i want to invoke credentials from Jenkins to retrieve username and password (for some reason) in the scripted pipeline and pass them on to the config file provider mvn command as parameters.
– Ashley
Nov 23 '18 at 15:03
Ok the previous issue is resolved and i have updated the post now. Is that something you could have a look at now and help me figure out the issue?
– Ashley
Nov 23 '18 at 20:11
Thanks Mark for the prompt response. Yes the password uses special chars. But how do i know if the config file provider sh script is even substituting the settings.xml username and password with the values i am passing in that script.?
– Ashley
Nov 23 '18 at 20:14
Try sh "echo '${mvnHome}/bin/mvn' -s $MAVEN_SETTINGS deploy -Dserver.username="${usernameLocal}" -Dserver.password="${passwordLocal}""
– Mark Bidewell
Nov 23 '18 at 20:16
|
show 2 more comments
Perhaps add to this with the error you are receiving
– metalisticpain
Nov 22 '18 at 4:28
I have provided all details that might serve useful, including the error message.The short of it is that i want to invoke credentials from Jenkins to retrieve username and password (for some reason) in the scripted pipeline and pass them on to the config file provider mvn command as parameters.
– Ashley
Nov 23 '18 at 15:03
Ok the previous issue is resolved and i have updated the post now. Is that something you could have a look at now and help me figure out the issue?
– Ashley
Nov 23 '18 at 20:11
Thanks Mark for the prompt response. Yes the password uses special chars. But how do i know if the config file provider sh script is even substituting the settings.xml username and password with the values i am passing in that script.?
– Ashley
Nov 23 '18 at 20:14
Try sh "echo '${mvnHome}/bin/mvn' -s $MAVEN_SETTINGS deploy -Dserver.username="${usernameLocal}" -Dserver.password="${passwordLocal}""
– Mark Bidewell
Nov 23 '18 at 20:16
Perhaps add to this with the error you are receiving
– metalisticpain
Nov 22 '18 at 4:28
Perhaps add to this with the error you are receiving
– metalisticpain
Nov 22 '18 at 4:28
I have provided all details that might serve useful, including the error message.The short of it is that i want to invoke credentials from Jenkins to retrieve username and password (for some reason) in the scripted pipeline and pass them on to the config file provider mvn command as parameters.
– Ashley
Nov 23 '18 at 15:03
I have provided all details that might serve useful, including the error message.The short of it is that i want to invoke credentials from Jenkins to retrieve username and password (for some reason) in the scripted pipeline and pass them on to the config file provider mvn command as parameters.
– Ashley
Nov 23 '18 at 15:03
Ok the previous issue is resolved and i have updated the post now. Is that something you could have a look at now and help me figure out the issue?
– Ashley
Nov 23 '18 at 20:11
Ok the previous issue is resolved and i have updated the post now. Is that something you could have a look at now and help me figure out the issue?
– Ashley
Nov 23 '18 at 20:11
Thanks Mark for the prompt response. Yes the password uses special chars. But how do i know if the config file provider sh script is even substituting the settings.xml username and password with the values i am passing in that script.?
– Ashley
Nov 23 '18 at 20:14
Thanks Mark for the prompt response. Yes the password uses special chars. But how do i know if the config file provider sh script is even substituting the settings.xml username and password with the values i am passing in that script.?
– Ashley
Nov 23 '18 at 20:14
Try sh "echo '${mvnHome}/bin/mvn' -s $MAVEN_SETTINGS deploy -Dserver.username="${usernameLocal}" -Dserver.password="${passwordLocal}""
– Mark Bidewell
Nov 23 '18 at 20:16
Try sh "echo '${mvnHome}/bin/mvn' -s $MAVEN_SETTINGS deploy -Dserver.username="${usernameLocal}" -Dserver.password="${passwordLocal}""
– Mark Bidewell
Nov 23 '18 at 20:16
|
show 2 more comments
2 Answers
2
active
oldest
votes
The variables created by withCredentials are Groovy variables not environment variables. Try the following:
stage('Deploy'){
withCredentials([usernamePassword(credentialsId:'xyz', passwordVariable: 'Password', usernameVariable: 'Username')]) {
configFileProvider([configFile(fileId: 'abcde', variable:'MAVEN_SETTINGS')]) {
sh "'${mvnHome}/bin/mvn' -s $MAVEN_SETTINGS deploy -Dserver.username=${Username} -Dserver.password=${Password}"
}
}
}
That doesn’t work either. And the most weird error is :- unexpected “ in the sh line which implies you can only use ‘ ' ( single quotes) while dealing with withCredentials.
– Ashley
Nov 23 '18 at 3:05
add a comment |
Ok i figured out the solution, declare the configFileProvider entire section under the block of withCredentials and pass -Dserver.username='${usernameLocal}' -Dserver.password='${passwordLocal}' (Pls note single quotes) This way the values also get substituted and are outputted in the logs as masked
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422633%2fhow-to-invoke-jenkins-credentials-in-a-jenkins-scripted-pipeline-not-declarativ%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The variables created by withCredentials are Groovy variables not environment variables. Try the following:
stage('Deploy'){
withCredentials([usernamePassword(credentialsId:'xyz', passwordVariable: 'Password', usernameVariable: 'Username')]) {
configFileProvider([configFile(fileId: 'abcde', variable:'MAVEN_SETTINGS')]) {
sh "'${mvnHome}/bin/mvn' -s $MAVEN_SETTINGS deploy -Dserver.username=${Username} -Dserver.password=${Password}"
}
}
}
That doesn’t work either. And the most weird error is :- unexpected “ in the sh line which implies you can only use ‘ ' ( single quotes) while dealing with withCredentials.
– Ashley
Nov 23 '18 at 3:05
add a comment |
The variables created by withCredentials are Groovy variables not environment variables. Try the following:
stage('Deploy'){
withCredentials([usernamePassword(credentialsId:'xyz', passwordVariable: 'Password', usernameVariable: 'Username')]) {
configFileProvider([configFile(fileId: 'abcde', variable:'MAVEN_SETTINGS')]) {
sh "'${mvnHome}/bin/mvn' -s $MAVEN_SETTINGS deploy -Dserver.username=${Username} -Dserver.password=${Password}"
}
}
}
That doesn’t work either. And the most weird error is :- unexpected “ in the sh line which implies you can only use ‘ ' ( single quotes) while dealing with withCredentials.
– Ashley
Nov 23 '18 at 3:05
add a comment |
The variables created by withCredentials are Groovy variables not environment variables. Try the following:
stage('Deploy'){
withCredentials([usernamePassword(credentialsId:'xyz', passwordVariable: 'Password', usernameVariable: 'Username')]) {
configFileProvider([configFile(fileId: 'abcde', variable:'MAVEN_SETTINGS')]) {
sh "'${mvnHome}/bin/mvn' -s $MAVEN_SETTINGS deploy -Dserver.username=${Username} -Dserver.password=${Password}"
}
}
}
The variables created by withCredentials are Groovy variables not environment variables. Try the following:
stage('Deploy'){
withCredentials([usernamePassword(credentialsId:'xyz', passwordVariable: 'Password', usernameVariable: 'Username')]) {
configFileProvider([configFile(fileId: 'abcde', variable:'MAVEN_SETTINGS')]) {
sh "'${mvnHome}/bin/mvn' -s $MAVEN_SETTINGS deploy -Dserver.username=${Username} -Dserver.password=${Password}"
}
}
}
answered Nov 22 '18 at 12:31
Mark BidewellMark Bidewell
30228
30228
That doesn’t work either. And the most weird error is :- unexpected “ in the sh line which implies you can only use ‘ ' ( single quotes) while dealing with withCredentials.
– Ashley
Nov 23 '18 at 3:05
add a comment |
That doesn’t work either. And the most weird error is :- unexpected “ in the sh line which implies you can only use ‘ ' ( single quotes) while dealing with withCredentials.
– Ashley
Nov 23 '18 at 3:05
That doesn’t work either. And the most weird error is :- unexpected “ in the sh line which implies you can only use ‘ ' ( single quotes) while dealing with withCredentials.
– Ashley
Nov 23 '18 at 3:05
That doesn’t work either. And the most weird error is :- unexpected “ in the sh line which implies you can only use ‘ ' ( single quotes) while dealing with withCredentials.
– Ashley
Nov 23 '18 at 3:05
add a comment |
Ok i figured out the solution, declare the configFileProvider entire section under the block of withCredentials and pass -Dserver.username='${usernameLocal}' -Dserver.password='${passwordLocal}' (Pls note single quotes) This way the values also get substituted and are outputted in the logs as masked
add a comment |
Ok i figured out the solution, declare the configFileProvider entire section under the block of withCredentials and pass -Dserver.username='${usernameLocal}' -Dserver.password='${passwordLocal}' (Pls note single quotes) This way the values also get substituted and are outputted in the logs as masked
add a comment |
Ok i figured out the solution, declare the configFileProvider entire section under the block of withCredentials and pass -Dserver.username='${usernameLocal}' -Dserver.password='${passwordLocal}' (Pls note single quotes) This way the values also get substituted and are outputted in the logs as masked
Ok i figured out the solution, declare the configFileProvider entire section under the block of withCredentials and pass -Dserver.username='${usernameLocal}' -Dserver.password='${passwordLocal}' (Pls note single quotes) This way the values also get substituted and are outputted in the logs as masked
answered Nov 23 '18 at 21:01
AshleyAshley
799
799
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422633%2fhow-to-invoke-jenkins-credentials-in-a-jenkins-scripted-pipeline-not-declarativ%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Perhaps add to this with the error you are receiving
– metalisticpain
Nov 22 '18 at 4:28
I have provided all details that might serve useful, including the error message.The short of it is that i want to invoke credentials from Jenkins to retrieve username and password (for some reason) in the scripted pipeline and pass them on to the config file provider mvn command as parameters.
– Ashley
Nov 23 '18 at 15:03
Ok the previous issue is resolved and i have updated the post now. Is that something you could have a look at now and help me figure out the issue?
– Ashley
Nov 23 '18 at 20:11
Thanks Mark for the prompt response. Yes the password uses special chars. But how do i know if the config file provider sh script is even substituting the settings.xml username and password with the values i am passing in that script.?
– Ashley
Nov 23 '18 at 20:14
Try sh "echo '${mvnHome}/bin/mvn' -s $MAVEN_SETTINGS deploy -Dserver.username="${usernameLocal}" -Dserver.password="${passwordLocal}""
– Mark Bidewell
Nov 23 '18 at 20:16