CloudBees CI Managed Controller 2.361.1.2-rolling running on OpenShift.
There is a k8s secret in the agents namespace:
I need to access this in my Jenkins pipeline e.g.
script.withCredentials([script.string(
credentialsId: "${credentialsEnvironment}",
variable: 'usernamePassword')
]) {
String auth = "${script.usernamePassword}".bytes.encodeBase64().toString()
Map params = defaultArgs + httpParams
def response = []
try {
response = script.httpRequest(
params +
[
url: "${apiEndpoint}/${url}",
customHeaders: [[name: 'Authorization', value: "Basic ${auth}"]]
]
)
}
The pipeline fails because it cannot find the credentialId:
09:14:12 credentialsEnvironment: grafana-admin-user
09:14:12 [Pipeline] withCredentials
09:14:12 [Pipeline] // withCredentials
09:14:12 [Pipeline] echo
09:14:12 org.jenkinsci.plugins.credentialsbinding.impl.CredentialNotFoundException: Could not find credentials entry with ID 'grafana-admin-user'
There is a suggestion to use the Kubernetes Credentials Provider plugin but we wish to do it in a Jenkins-native way than to add one more plug-in.
Related
I have created credentials (method: username and password) in Jenkins with name wwe_hello.
For test, i created pipeline with name test:
pipeline {
agent {label 'slave1'}
environment {
CREDS = credentials("wwe_hello")
}
stages {
stage('WWE') {
steps {
sh 'echo "$CREDS"'
}
}
}
}
In result i have:
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on slave1 in /var/lib/jenkins/workspace/test
[Pipeline] {
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: wwe_hello
Finished: FAILURE
Where, i have error. I am doing everything according to working examples and documentation. But i don't understand, why it does not work.
Look at the pipeline step documentation for withCredentials. You don't need to create the environment variable with env - withCredentials does it for you:
pipeline {
agent any
stages {
stage('only') {
steps {
withCredentials([
usernamePassword(
credentialsId: 'CRED',
usernameVariable: 'USER',
passwordVariable: 'PASS'
)]) {
sh '''
echo "The username is: ${USER}"
echo "The password is : ${PASS}"
'''
}
}
}
}
}
you need to create credentials with name "wwe_hello"
Go to credentials Manager
Under stores scoped to jenkins, select jenkins
Select global credentials
Select add credentials
give credentails (see in the dropdown, secret text has been selected)
I have a Jenkins multi branch pipeline job that uses a secret value in Jenkinsfile:
pipeline {
agent any
stages {
stage('Test') {
steps {
echo "DOCKER_REGISTRY_USER is ${env.DOCKER_REGISTRY_USER_NSV}"
}
}
}
}
The secret value is stored in Credentials Manager as secret text with the ID DOCKER_REGISTRY_USER_NSV:
I'm trying to read this value in Jenkinsfile as shown above but I get the following output that prints out the value null for my secret:
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] echo
DOCKER_REGISTRY_USER is null
[Pipeline] sh
I also tried referencing the secret text in my pipeline like this:
echo "DOCKER_REGISTRY_USER is ${DOCKER_REGISTRY_USER_NSV}"
But then I get this error when running the Jenkins job:
groovy.lang.MissingPropertyException: No such property: DOCKER_REGISTRY_USER_NSV for class: groovy.lang.Binding
at groovy.lang.Binding.getVariable(Binding.java:63)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:264)
I think I need to bind that credential to the job but I don't see an option to do that for a Multi-Branch Pipeline job, the way you can for a Freestyle or Pipeline job.
How can I use a secret credential in a Multi Branch Pipeline job?
You can use credentials() helper method to archive your purpose.
pipeline {
agent any
environment {
DOCKER_REGISTRY_USER = credentials('DOCKER_REGISTRY_USER_NSV')
// put the ID of credential as credentials()'s parameter.
}
stages {
stage('Test') {
steps {
echo "DOCKER_REGISTRY_USER is ${DOCKER_REGISTRY_USER}"
}
}
}
}
I have a Jenkins Pipeline job that uses a secret value in Jenkinsfile:
pipeline {
agent any
stages {
stage('Test') {
steps {
echo "DOCKER_REGISTRY_USER is $DOCKER_REGISTRY_USER"
}
}
}
}
The secret value is stored in Credentials Manager as secret text with the ID DOCKER_REGISTRY_USER. I parameterized my Jenkins pipeline job by referring to the secret text from Credential Manager:
I'm trying to read this value in Jenkinsfile as shown above but I get the following output:
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] echo
DOCKER_REGISTRY_USER is DOCKER_REGISTRY_USER
[Pipeline] sh
It just prints the ID of the credential but not the value. I thought it might be a Jenkins security mechanism but when I try and do something similar with a Freestyle job, I get a masked output (******).
But what DOES work is if I use a parameter in my pipeline job but instead of a Credential Parameter I use a String Parameter or Password Parameter - then the secret value gets printed out:
So I'm either not using the Credential Parameter correctly or there's a bug.
That is how it's supposed to work. To actually make use of that reference to a Jenkins credentials, you need to use credentials binding plugin. For example, if your credentials are of type 'Username and password' you'll do as follows:
withCredentials([userNamePassword: $DOCKER_REGISTRY_USER, usernameVariable: 'USER', passwordVariable: 'PASSWORD']) {
echo "DOCKER_REGISTRY_USER is $USER and password is $PASSWORD"
}
Nota that Jenkis will not show private credentials in the output, but the value it is properly populated.
I am trying to parameterize a Jenkins pipeline. The only input parameter will be GITHUB_URL. I have a Jenkinsfile as a part of the repo. I want to use this variable (defined as parameter) in my pipeline configuration as "Repository URL". How can I access the parameter ?
I have tried $GITHUB_URL, ${GITHUB_URL} and ${params.GITHUB_URL}. No luck
Any other suggestions?
Because you are telling you have a jenkinsfile inside your git repo I suppose you do not mean that you want to call a Jenkinsfile using parameters from a shared library.
It's also not sure if you are using a declarative or scripted pipeline.
I will explain the "recommended" declarative pipeline:
pipeline {
agent any
parameters {
string(defaultValue: "https://github.com", description: 'Whats the github URL?', name: 'URL')
}
stages {
stage('Checkout Git repository') {
steps {
git branch: 'master', url: "${params.URL}"
}
}
stage('echo') {
steps {
echo "${params.URL}"
}
}
}
}
In this pipeline you will add a string parameter to which you can add a URL. When you run the build it will ask for the parameter:
To use this parameter use "${params.URL}":
This pipeline will clone the github repo in the first stage and print the URL in the next (echo) stage:
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (echo)
[Pipeline] echo
https://github.com/lvthillo/docker-ghost-mysql.git
[Pipeline] }
We am using Jenkins Pipeline to configure jobs in jenkins. For a bunch of jobs we need user input for which we use parameterised build where user can input parameter values and later we use the values in our .jenkinsfile in sh like
sh "./build-apply.sh ${accountnumber} ${volumename} ${vpcname} services ${snapshotid}"
This used to work with
Jenkins 2.16
Pipeline 2.3
Groovy 2.15
However, when I rebuild Jenkins to:
2.16 or latest 2.26
Pipeline 2.5
Pipeline: Groovy 2.19
The above sh stopped working. Error being
groovy.lang.MissingPropertyException: No such property: accountnumber for class: groovy.lang.Binding
Any idea what I am missing? Is the syntax not correct?
For reference full Jenkinsfile for reference
node {
// Mark the code checkout 'stage'....
stage 'Checkout'
git branch: '****', credentialsId: '***', url: '****'
stage 'Provision Volume'
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', accessKeyVariable: '*****',
credentialsId: '****',
secretKeyVariable: '*****']]) {
// Run the terraform build
env.PATH = "${env.PATH}:/jenkins/terraform"
sh "./build-apply.sh ${accountnumber} ${volumename} ${vpcname} services ${snapshotid}"
}
}
Copy and paste the below code in the pipeline script
node: {
stage ('BCCdlVsLib') {
build job: 'BCCdlVsLib', parameters:
[
[$class: 'StringParameterValue', name: 'BINPATH', value: 'BINPATH'],
[$class: 'StringParameterValue', name: 'SOURCEFILE', value: 'SOURCEFILE']
]
}
In the jobs (BCCdlVsLib) enable the option "this project is parametrized" and enter 2 string parameters job_binpath,job_sourcefile.
Print the variables in the pipeline jobs
echo job_binpath
echo job_sourcefile
After run the pipeline job,will get the below output.
BINPATH
SOURCEFILE