Python - How to read files in multiple ways
Read file all in one shot Above code read whole file at once, but if the files…
October 07, 2022
In this post, we will see how to fetch multiple credentials and expose them in environment variable.
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
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.
Read file all in one shot Above code read whole file at once, but if the files…
Introduction I have a host running mysql (not on a container). I have to run an…
So, you want to run your code in parallel so that your can process faster, or…
Introduction In this post, we will see a sample Jenkin Pipeline Groovy script…
Introduction Drupal is an awesome CMS. Drupal content type form, allows you to…
Introduction In this post we will see following: How to schedule a job on cron…
Introduction There are some cases, where I need another git repository while…
Introduction I have an automation script, that I want to run on different…
Introduction I had to write a CICD system for one of our project. I had to…
Introduction Java log4j has many ways to initialize and append the desired…
Introduction You have a running kubernetes setup, and have a webservice (exposed…