DEV Community

Cover image for How to Handle Sensitive Data in Jenkins Pipeline
Jay Panchal
Jay Panchal

Posted on

How to Handle Sensitive Data in Jenkins Pipeline

Introduction

In this article, you will learn how to handle sensitive data in Jenkins Pipeline Code.

Prerequisites:

  • Jenkins is Installed and Basic Understanding of Creating Pipeline

How to Handle Sensitive Data in Jenkins Pipeline:

Create Credentials from Jenkins

  • Login to Jenkins and Under admin profile click Credentials

    jenkins-admin-credentials

  • Click on (global)

    jenkins-global-scoped-credentials

  • Click Add Credentials

  • Create any kind of Credentials as per your requirement and give a unique ID. We will use this ID inside Pipeline Code

    jenkins-secret-text-credentials

  • Here i have given ID to Secret text as CRED_ID

Use Credentials in Pipeline Code

  • Create a pipeline script and use the credentials as shown below
pipeline{
  agent any
  environment {
    secure_key = credentials('CRED_ID')
  }
  stages {
    stage('Using Creds in Pipeline'){
      steps {
        sh 'echo $secure_key'
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode
  • Now you can use the credentials as shown above, Create a enviornment variable and use credentials function to fetch the created CRED_ID to your pipeline code.
  • The output will be secure as shown below

    jenkins-secret-text-console-output

Top comments (0)