Jenkins: How to access a secret file? - jenkins

I'm trying to get my head around Jenkins, I've uploaded a secret file to credentials and made it global but I can't seem to use it in my stage.
I'm trying to just echo out the file for now
steps { echo("My File: ${credentials('local_instance')}") }
but in the log I don't see the contents of the file just My File: #credentials(<anonymous>=local_instance). I need to be able to get the contents of the file and write it to a file named local.env, or as I uploaded it with local.env can I just pull the file and have it stored in my root?

The proper way to get access to credentials in Jenkins is withCredentials step.
withCredentials([file(credentialsId: 'youCredId', variable: 'secretFile')]) {
// do something with the file, for instance
sh 'cat $secretFile'
}
Where youCredId your file credential ID in Jenkins.
More information is here: https://www.jenkins.io/doc/pipeline/steps/credentials-binding/
There is another approach. In environment section of your declarative pipeline, you can assign an env variable with a credential value.
environment {
SECRET_FILE = credentials('youCredId')
}
But the first approach is more preferable because it allows you to reduce the scope of secret variable.
More information is here: https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#handling-credentials

Related

Get multiple secrets from one Credential Store in Jenkins

For my pipeline, I want to keep the subscription ID, tenant ID, client ID, and password all out of source control, and keep all of those in a single credential store in Jenkins. It seems that there is not a $class that matches this for withCredentials, but I just want to use credentials() in the environment anyways. Am I mistaken that the credentials('file') method reads as a single value, or is there a way to format that file such that Jenkins will parse it and make each secret available?
//Jenkinsfile (Declarative Pipeline)
pipeline {
agent { label 'AZcli' }
environment {
SECRETS_FILE = credentials('AZJenkinsSecretsFile')
}
Then what?
Let's say this is the AZJenkinsSecretsFile.txt that I've uploaded.
subscription=xxxx-xxx-xx-xx-x
tenant=xxx-xxx-xxx-xx
client=xxx-xxxx-xxx
password=password
When you create a Secretfile you will be given a Path to a temporary file with the secret content. Secret files are intended to be passed as a whole file. For example, you can have your kubeconfigs file as a Secretfile and then pass it directly to kubectl like kubectl --kubeconfig $SECRET_CONFIG.
If you want to export each line in the secret file as a variable, it's doable. But when you start using them in your shell steps their values will be exposed in the logs. In order to use them, you can use something like below.
sh """
source $SECRETS_FILE
echo "\$subscription"
echo "\$tenant"
"""

Export variable containing secret text from Jenkins pipeline

I am trying to pass to a shell step on my Jenkins job a variable defined like (script step):
env.PGPASSWORD = credentials('test')
When I try too access (sh''' step) to that variable(PGPASSWORD) making the exportto let my postgres grab the secret text:
export "${PGPASSWORD}"
It seems like that command from above is not grabbing the secret contained on the Jenkins credentials.
If I try to echo the PGPASSWORD variable it outputs:
#credentials(<anonymous>=test)

Accessing Jenkins variable dynamically, which contains another variable substring

I have two Jenkins job secret file variables called stage_xfile and prod_xfile.
ENV is another variable which contains the deploy environment variable stage/prod.
I want to dynamically access secret file for stage and prod in sh command like this:
cp ${${ENV}_xfile} db/secret/
I'm not able to make it work after trying different combinations. Any suggestions.
You can create file name in separate variable and then use it:
def fileName = "${ENV}_xfile"
sh "cp ${fileName} db/secret/"

How to work with configuration information in Jenkins instead of hard-coding in Jenkinsfile?

if(env.BRANCH_NAME =~ /PR-\d+-head/ && env.CHANGE_TARGET == 'QA_branch' )){
sh """
sfdx force:source:deploy --url https://abc.salesforce.com
"""
}else if(env.BRANCH_NAME =~ /PR-\d+-head/ && env.CHANGE_TARGET == 'UAT_branch' )){
sh """
sfdx force:source:deploy --url https://def.salesforce.com
"""
}
I have Jenkinsfile similar to this. These urls keep changing and I have to update Jenkinsfile every time new url is generated by salesforce.
Is there a way to store this kind of information in Jenkins and read from Jenkins? Like we do for storing credentials.
I can store these urls in Jenkins as credentials and get those values in Jenkinsfile but not sure if that is a good approach as these are not credentials.
I am looking for a way to avoid updating Jenkinsfile every time environment related information changes and move it to Jenkins or somewhere.
You could store these URLs as global properties and access them in your jenkinsfile as described here.
Another approach (more complicated) is to stored a file in a source control, and let Jenkins download the file, read the data within the file and read the URL's.
Good things about this approach:
Your config code is versioned
Your config code resides outside jenkins, so it is easy to backup
You can config files on different branches, so you use what you need
The only drawback is that you need more coding to implement this solution.

How to invoke Inject environment variables to the build process plugin in jenkinsFIle jenkins 2.x with pipeline

I'm trying to migrate my project from jenkins 1 to jenkins 2.x using pipeline as code or Jenkinsfile.
But I don't see any option in snippet generator to generate environment injector plugin into a script in Jenkinsfile.
Anyone can help?
I'm assuming that you want to read properties from a specific file and inject them as environment variables?
If so, this is a solution:
Create the file that will contain the environment properties
You create some properties file called project.properties with following content:
PROJECT_VERSION='1.4.34'
Then, on your pipeline code, you've to add the following code in order to be able to read the file and inject read variables as environment variables:
node {
load "${WORKSPACE}\\project.properties" // assuming that props file is in
Jenkins Job's workspace
echo "PROJECT VERSION: ${PROJECT_VERSION}"
}
First line read and inject variable PROJECT_VERSION as environment variable
Second line is just to print read variable to make sure that everything worked seamlessly
Result:
Wanted to just comment on your question, but my lack of reputation is hindering me.
The list of supported steps is here: https://jenkins.io/doc/pipeline/steps/
In general, you can use other plugins by using their Java style invocation.
i.e.
step([$class: 'classname', parametername: 'value'])
I used this example for read a properties file and use it in a pipeline stage:
node(){
file = readFile('params.txt')
prop=[:]
file.eachLine{ line ->
l=line.split("=")
prop[l[0]]=l[1]
}
withEnv(['MyVar1='+prop["MyVar1"],'MyVar2='+prop["MyVar2"],'MyVar3='+prop["MyVar3]]){
stage('RUN_TEST'){
echo env.MyVar1
echo env.MyVar2
echo env.MyVar3
sh"echo $MyVar1"
}
}
}

Resources