I want to pass Kubernetes secret key and value in the Jenkins job using a Declarative pipeline.
I am trying to read the k8s secret values using the pod template. Configured the k8s plugin and the image has environment variables set. I need to overwrite the environment values with the k8s secret.
K8S-Secret:
spec:
containers:
- env:
- name: K8S-SECRET
valueFrom:
secretKeyRef:
key: USERNAME
name: PASSWORD
Jenkins pipeline:
pipeline {
agent {
kubernetes {
label 'docker'
envVars: [containersecretEnvVar(key: 'USERNAME', value:'PASSWORD')]
}
}
environment {
BRANCH = 'origin/master'
PROJECT_NAME = 'k8s'
}
stages{
stage('print k8s secret'){
steps {
echo "k8s_secret_name: ${env.k8s_secret_name}"
echo "k8s_secret_valie: ${env.k8s_secret_value}"
}
}
}
}