Interview Prep/CI/CD/Jenkins Shared Libraries, Credentials and Security

A pipeline needs an AWS secret key and a DB password. How do you provide them without exposing them in logs?

BeginnerScenarioJenkins Shared Libraries, Credentials and Security
Answer

Situation read → Secrets must be injected at runtime and masked.

What I'd do →

withCredentials([usernamePassword(credentialsId: 'db-creds',
    usernameVariable: 'DB_USER', passwordVariable: 'DB_PASS')]) {
  sh 'psql "postgresql://$DB_USER:$DB_PASS@db/app" -f migrate.sql'
}

Use single quotes in sh so the shell expands the variable, not Groovy.

Why → With single quotes Jenkins never interpolates the secret into the Groovy string, and the console masks it.

Interview tip → Double-quoted Groovy interpolation of secrets triggers a Jenkins warning for good reason.

All CI/CD questions

More on Jenkins Shared Libraries, Credentials and Security