Accessing Jenkins variable dynamically, which contains another variable substring - jenkins

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/"

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)

How to set multiple environmental variable with Jenkins Groovy Script

I am trying to use Jenkins environmental variable with groovy scripts and assign them to environment variable so I can use those variable through out each Jenkins steps. But I cannot take out Groovy map objects. Is there anything I am doing wrong?
this is simple. In groovy script I have added two keys as "repo" and "version". Environment variables are created from that name and in a shell, I can get those simply by calling their keys.
echo $repo
echo $version

How to pass along custom variable created in .gitlab-ci.yml to Docker?

Within .gitlab-ci.yml I've created a new variable under script: by using $CI_COMMIT_SHA and modifying it. When I echo the variable it returns the proper value. However, I'm not having any success passing it along to Docker. What am I not doing right?
Ultimately, I would like access this custom variable inside my container.
build:
script:
# converts commit SHA to UNIX time
- export COMMIT_TIME_UNIX=$(git show -s --format=%ct $CI_COMMIT_SHA)
- echo $COMMIT_TIME_UNIX
You would need to check, when the same script is executed in a Docker/container environment, if it is still in the right Git repository path.
You can add, before the first export:
pwd
git status
env|grep GIT
That way, you will check if you are doing Git commands where you should, and if there is any GIT_xxx environnement variable which might influence said command.

Jenkinsfile - write environment variable to a file

In my Jenkinsfile, I'm trying to add a environment variable to a file, however not able to do so - seems like some small syntax thing, however I've tried so many different variations.
So the parameter I want is "${params.Spec}", currently used like:
environment {
SPECTORUN = "${params.Spec}"
}
Then in my script block i'm trying:
sh "echo Run:\"${params.Spec}\" >> allure-results/environment.properties"
Any help would be appreciated.
FOUND THE ANSWER HERE:
How to pass variables from Jenkinsfile to shell command

Resources