0
0
Jenkinsdevops~10 mins

Credential scoping (global, folder) in Jenkins - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a global credential in Jenkins using the Credentials plugin.

Jenkins
credentials {
  usernamePassword {
    id = '[1]'
    username = 'admin'
    password = 'secret'
  }
}
Drag options to blanks, or click blank then click option'
Ajob-cred
Bfolder-cred
Cglobal-cred
Dlocal-cred
Attempts:
3 left
💡 Hint
Common Mistakes
Using a folder-scoped ID for a global credential.
2fill in blank
medium

Complete the code to reference a folder-scoped credential in a Jenkins pipeline.

Jenkins
pipeline {
  agent any
  stages {
    stage('Use Credential') {
      steps {
        withCredentials([usernamePassword(credentialsId: '[1]', usernameVariable: 'USER', passwordVariable: 'PASS')]) {
          sh 'echo $USER'
        }
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
Aglobal-cred
Bjob-cred
Clocal-cred
Dfolder-cred
Attempts:
3 left
💡 Hint
Common Mistakes
Using a global credential ID when the credential is folder-scoped.
3fill in blank
hard

Fix the error in the Jenkinsfile to correctly use a folder-scoped credential.

Jenkins
pipeline {
  agent any
  stages {
    stage('Test') {
      steps {
        withCredentials([usernamePassword(credentialsId: '[1]', usernameVariable: 'USER', passwordVariable: 'PASS')]) {
          sh 'echo $PASS'
        }
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
Afolder-cred
Bglobal-cred
Cwrong-cred
Djob-cred
Attempts:
3 left
💡 Hint
Common Mistakes
Using a credential ID that does not exist or is not scoped properly.
4fill in blank
hard

Fill both blanks to create a folder-scoped credential and use it in a Jenkins pipeline.

Jenkins
folder('MyFolder') {
  credentials {
    usernamePassword {
      id = '[1]'
      username = 'user1'
      password = 'pass1'
    }
  }
}

pipeline {
  agent any
  stages {
    stage('Use Folder Credential') {
      steps {
        withCredentials([usernamePassword(credentialsId: '[2]', usernameVariable: 'USER', passwordVariable: 'PASS')]) {
          sh 'echo $USER'
        }
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
Afolder-cred
Bglobal-cred
Cjob-cred
Dlocal-cred
Attempts:
3 left
💡 Hint
Common Mistakes
Using different IDs for definition and usage.
5fill in blank
hard

Fill all three blanks to define a global credential, a folder credential, and use the folder credential in a pipeline.

Jenkins
credentials {
  usernamePassword {
    id = '[1]'
    username = 'admin'
    password = 'adminpass'
  }
}

folder('ProjectFolder') {
  credentials {
    usernamePassword {
      id = '[2]'
      username = 'projuser'
      password = 'projpass'
    }
  }
}

pipeline {
  agent any
  stages {
    stage('Run') {
      steps {
        withCredentials([usernamePassword(credentialsId: '[3]', usernameVariable: 'USER', passwordVariable: 'PASS')]) {
          sh 'echo $USER'
        }
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
Aglobal-cred
Bfolder-cred
Cjob-cred
Dlocal-cred
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up global and folder credential IDs.