Jenkins Pipeline: "No credential specified" error

I’m trying to use Jenkins Pipelines for Continuous Delivery, but I can’t seem to get past the “no credential specified” error.

I followed this tutorial: https://www.youtube.com/watch?v=pts8zdHel5E

I’m using global credentials with a username and password.

Here is the Pipeline script:

pipeline {
    agent any
    stages {
        stage ('Build') {
            steps {
               echo 'Building'
            }
        }
        stage ('Test') {
            steps {
               echo 'Testing'
            }
        }
        stage ('Deploy') {
            steps {
               echo 'Deploying'
            }
        }
    }
}

I tried to build, but I got this error:

Jenkins Error

The authentication failed Jenkins Authentication failure

Other work items in Jenkins are not failing, while using the same credential. Only pipeline projects appear to be broken.

I’ve tried updating the credential binding plugins, deleting and creating the credential again, but nothing seems to work.

Issue

I’m having trouble using Jenkins Pipelines for Continuous Delivery. I’m getting a “no credential specified” error when I try to build, even though I’m using global credentials with a username and password.

Answer

You need to include the withCredentials block in your Pipeline script to specify the credential to use. Here’s an example:

pipeline {
    agent any
    stages {
        stage ('Build') {
            steps {
               echo 'Building'
            }
        }
        stage ('Test') {
            steps {
               echo 'Testing'
            }
        }
        stage ('Deploy') {
            steps {
               withCredentials([usernamePassword(credentialsId: 'your-credential-id', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
                   sh './deploy.sh $USERNAME $PASSWORD'
               }
            }
        }
    }
}

Replace your-credential-id with the ID of your global credential. In the withCredentials block, we’re specifying that we want to use a username and password credential and storing the values in environment variables. Then, we’re running a shell command and passing in the environment variables for the username and password.