jenkins2 Min Read

How to Fetch Multiple Credentials and Expose them in Environment using Jenkinsfile pipeline

Gorav Singal

October 07, 2022

TL;DR

Use the environment block with credentials() helper to bind single creds, or nest multiple withCredentials blocks for accessing several credential sets in the same pipeline stage.

How to Fetch Multiple Credentials and Expose them in Environment using Jenkinsfile pipeline

Introduction

In this post, we will see how to fetch multiple credentials and expose them in environment variable.

Jenkinsfile Stage to Fetch Single Credential

There are many types of credentials that can be stored in Jenkins. The most commonly used is the username/password one.

Lets take an example of a stage, where I need to authenticate against an artifactory server.

stages {
  stage('Install requirements') {
      steps {
          withCredentials([[
              $class: 'UsernamePasswordMultiBinding',
              credentialsId: 'MY_API_KEY',
              usernameVariable: 'ARTIFACTORY_USERNAME',
              passwordVariable: 'ARTIFACTORY_PASSWORD']
          ]) {
              sh """
              python3.8 -m pip install virtualenv
              python3.8 -m venv python_env
              source python_env/bin/activate
              python3.8 -m pip install --upgrade pip
              python3.8 -m pip install -r requirements.txt -i https://${ARTIFACTORY_USERNAME}:${ARTIFACTORY_PASSWORD}@artifactory.myorg.com/artifactory/api/pypi/pypi-myorg-release/simple

              deactivate
              """
          }
      } 
  }
}

In above Jenkinsfile, I’m reading credentials from a stored credential in Jenkin with id MY_API_KEY and exposing the username in variable: ARTIFACTORY_USERNAME, and password in variable: ARTIFACTORY_PASSWORD

Jenkinsfile Stage to Fetch Multiple Credentials

There are many cases, where I need to read more than one credentials,

stage('Run deployer') {
  steps {
      echo 'Deploying Artifacts to k8s Env...'

      withCredentials([
          [$class: 'UsernamePasswordMultiBinding', 
          credentialsId:"AZURE_SERVICE_SECRET",
              usernameVariable: 'AZURE_SERVICE_USERNAME', 
              passwordVariable: 'AZURE_SERVICE_SECRET'],
          [$class: 'UsernamePasswordMultiBinding', credentialsId:"MY_API_KEY",
              usernameVariable: 'ARTIFACTORY_USERNAME', 
              passwordVariable: 'ARTIFACTORY_PASSWORD']
      ]) {
          sh """
              sudo curl -LO "https://dl.k8s.io/release/v1.25.2/bin/linux/amd64/kubectl"
              sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
              kubectl version --client

              echo "deploy to env=${params.DeployEnv}, artifact=${params.ArtifactId}, version=${params.Version}"
              
              echo "Preparing runtime env..."
              python3.8 -m pip install virtualenv
              python3.8 -m venv python_env
              source python_env/bin/activate
              python3.8 -m pip install --upgrade pip
              python3.8 -m pip install -r requirements.txt -i https://${ARTIFACTORY_USERNAME}:${ARTIFACTORY_PASSWORD}@artifactory.myorg.com/artifactory/api/pypi/pypi-myorg-release/simple

              echo "Ready to run deployment..."
              python3.8 deploy.py "${params.DeployEnv}" "${params.ArtifactId}" "${params.Version}" "./ethos_deployment"

              deactivate
          """
      }
  }
}

The syntax is the same, it is just that I’ve used multiple credentials id and usage will be the same.

Share

Related Posts

How to Git Clone Another Repository from Jenkin Pipeline in Jenkinsfile

How to Git Clone Another Repository from Jenkin Pipeline in Jenkinsfile

Introduction There are some cases, where I need another git repository while…

Jenkins Pipeline - How to run Automation on Different Environment (Dev/Stage/Prod), with Credentials

Jenkins Pipeline - How to run Automation on Different Environment (Dev/Stage/Prod), with Credentials

Introduction I have an automation script, that I want to run on different…

Jenkins Pipeline with Jenkinsfile - How To Schedule Job on Cron and Not on Code Commit

Jenkins Pipeline with Jenkinsfile - How To Schedule Job on Cron and Not on Code Commit

Introduction In this post we will see following: How to schedule a job on cron…

Jenkinsfile - How to Create UI Form Text fields, Drop-down and Run for Different Conditions

Jenkinsfile - How to Create UI Form Text fields, Drop-down and Run for Different Conditions

Introduction I had to write a CICD system for one of our project. I had to…

Example Jenkin Groovy Pipeline Script for Building Python Projects with Git Events and Push to Artifactory

Example Jenkin Groovy Pipeline Script for Building Python Projects with Git Events and Push to Artifactory

Introduction In this post, we will see a sample Jenkin Pipeline Groovy script…

Python - How to Maintain Quality Build Process Using Pylint and Unittest Coverage With Minimum Threshold Values

Python - How to Maintain Quality Build Process Using Pylint and Unittest Coverage With Minimum Threshold Values

Introduction It is very important to introduce few process so that your code and…

Latest Posts

AI Video Generation in 2025 — Models, Costs, and How to Build a Cost-Effective Pipeline

AI Video Generation in 2025 — Models, Costs, and How to Build a Cost-Effective Pipeline

AI video generation went from “cool demo” to “usable in production” in 2024-202…

AI Models in 2025 — Cost, Capabilities, and Which One to Use

AI Models in 2025 — Cost, Capabilities, and Which One to Use

Choosing the right AI model is one of the most impactful decisions you’ll make…

AI Image Generation in 2025 — Models, Costs, and How to Optimize Spend

AI Image Generation in 2025 — Models, Costs, and How to Optimize Spend

Generating one image with AI costs between $0.002 and $0.12. That might sound…

AI Coding Assistants in 2025 — Every Tool Compared, and Which One to Actually Use

AI Coding Assistants in 2025 — Every Tool Compared, and Which One to Actually Use

Two years ago, AI coding meant one thing: GitHub Copilot autocompleting your…

AI Agents Demystified — It's Just Automation With a Better Brain

AI Agents Demystified — It's Just Automation With a Better Brain

Let’s cut through the noise. If you read Twitter or LinkedIn, you’d think “AI…

Supply Chain Security — Protecting Your Software Pipeline

Supply Chain Security — Protecting Your Software Pipeline

In 2024, a single malicious contributor nearly compromised every Linux system on…