I have uploaded .env file into my Jenkins Credentials and then created a pipeline (mentioned below).
Jenkins file content
pipeline {
agent any
environment {
/* Define any ENV variables */
SECRET_FILE_IDS = credentials('FlaskCredId')
}
stages {
stage("Clone repository") {
steps {
checkout scm
sh "echo ${SECRET_FILE_IDS}"
sh "echo ${SECRET_FILE_IDS} > \"/var/lib/jenkins/workspace/Flask API Test/.env\" "
}
}
}
I am trying to copy the uploaded .env file to my workspace(project directory: /var/lib/jenkins/workspace/Flask API Test/.env), file is getting created but the content is rubbish.
Output file content as follows
/var/lib/jenkins/workspace/Flask API Test#tmp/secretFiles/3c670c98-c7a1-4a76-b839-9299bc2d017b/.env
Updating question
I have even tried copying the file to the workplace using this line but it gives error.
sh "cp ${SECRET_FILE_IDS} \"/var/lib/jenkins/workspace/Flask API Test/\" "
Error
Warning: A secret was passed to "sh" using Groovy String interpolation, which is insecure.
Affected argument(s) used the following variable(s): [SECRET_FILE_IDS]
See https://jenkins.io/redirect/groovy-string-interpolation for details.
+ cp **** /var/lib/jenkins/workspace/Flask API Test/
cp: cannot stat 'API': No such file or directory
cp: cannot stat 'Test#tmp/secretFiles/4ad9e8d4-ab6f-4ae4-8040-288b51dc5797/.env': No such file or directory```
I have a config file that has some environment variable including a variable called MONGO_UUID and I pass this variable to a test step via a configFileProvider plugin and save it to .env file as in step below:
stage('Build ') {
steps {
configFileProvider([configFile(fileId: 'jenkins_config_filename', variable: "CONFIG_FILE")]) {
sh '''
cp -f $CONFIG_FILE ./.env
npm run test // this step dynamically updates MONGO_UUID
echo "$MONGO_UUID" // trying to output newly updated value here
'''
jenkins_config_filename has following format:
MONGO_HOST=123.123.123.1
MONGO_DB=dbname
MONGO_US=user
MONGO_UUID=null
What I am trying to do is to output variable MONGO_UUID which was passed to the test step via config file and the value of MONGO_UUID get updated in the process. I can see that the MONGO_UUID got updated because I can see new record in mongodb but I am wondering how to echo that value in jenkins console.
add this line (with proper changes based on your MongoDB configuration) after npm run test:
MONGO_UUID=`mongo db.collection.find("whatever you query is")`
and then you can echo the variable to Jenkins output.
If you want to update the file, you can use sed like below :
sed -i "s/MONGO_UUID=.*/MONGO_UUID=${MONGO_UUID}/" .env
I've implemented loading a groovy file for env variables by following this post.
env.groovy:
env.DB_USER = 'testuser'
env.DB_PASS = credentials('DB_PASS')
Jenkinsfile:
stages {
stage ("print") {
steps {
load "${WORKSPACE}/env.groovy"
echo "${env.DB_USER}"
echo "${env.DB_PASS}"
}
}
}
Output:
[Pipeline] echo
testuser
[Pipeline] echo
#credentials(<anonymous>=DB_PASS)
Is accessing the Jenkins credential store possible for DB_PASS when loading a groovy env variable file?
Note: I know I can access the environment variables in the environment { } block of my Jenkinsfile. But since I have so many env variables, I was wondering if I could reference them all in a separate groovy file instead.
For the time being, try to do it like this inside your jenkinsfile.
environment {
DB_PASS = credentials('DB_PASS')
}
However you still can't echo your DB_PASS environment variable since credentials type variables retain the property of secrecy. And also keep in mind that variables have scope in jenkinsfile.
If you have
environment {
DB_PASS = credentials('DB_PASS')
}
If you really would like to see the contents of that
you could do
sh """
echo "$DB_PASS" | base64
"""
That will show you the secret, hope this helps.
I have already added 2 secret files to Jenkins credentials with names PRIVATE-KEY and PUBLIC-KEY.
How can I copy those 2 files to /src/resources directory inside a job?
I have the following snippet
withCredentials([file(credentialsId: 'PRIVATE_KEY', variable: 'my-private-key'),
file(credentialsId: 'PUBLIC_KEY', variable: 'my-public-key')]) {
//how to copy, where are those files to copy from?
}
Ok, I think I managed to do it. my-private-key variable is a path to the secret, so I had to copy that secret to the destination I needed.
withCredentials([file(credentialsId: 'PRIVATE_KEY', variable: 'my-private-key'),
file(credentialsId: 'PUBLIC_KEY', variable: 'my-public-key')]) {
sh "cp \$my-public-key /src/main/resources/my-public-key.der"
sh "cp \$my-private-key /src/main/resources/my-private-key.der"
}
Both solution is good for specific OS(win, unix). There are some basic function to check is system unix isUnix(). Instead of this you can use the read/write basic methods for any machine.
withCredentials([file(credentialsId: PRIVATE_KEY, variable: 'my_private_key'),
file(credentialsId: PUBLIC_KEY, variable: 'my_public_key')]) {
writeFile file: 'key/private.pem', text: readFile(my_private_key)
writeFile file: 'key/public.pem', text: readFile(my_public_key)
}
Following on from #Humberds answer, the equivalent for powershell is:
withCredentials([file(credentialsId: 'PRIVATE_KEY', variable: 'my-private-key')]) {
bat "powershell Copy-Item $appSettings -Destination src\\main\\resources "
}
I am doing a simple pipeline:
Build -> Staging -> Production
I need different environment variables for staging and production, so i am trying to source variables.
sh 'source $JENKINS_HOME/.envvars/stacktest-staging.sh'
But it returns Not found
[Stack Test] Running shell script
+ source /var/jenkins_home/.envvars/stacktest-staging.sh
/var/jenkins_home/workspace/Stack Test#tmp/durable-bcbe1515/script.sh: 2: /var/jenkins_home/workspace/Stack Test#tmp/durable-bcbe1515/script.sh: source: not found
The path is right, because i run the same command when i log via ssh, and it works fine.
Here is the pipeline idea:
node {
stage name: 'Build'
// git and gradle build OK
echo 'My build stage'
stage name: 'Staging'
sh 'source $JENKINS_HOME/.envvars/stacktest-staging.sh' // PROBLEM HERE
echo '$DB_URL' // Expects http://production_url/my_db
sh 'gradle flywayMigrate' // To staging
input message: "Does Staging server look good?"
stage name: 'Production'
sh 'source $JENKINS_HOME/.envvars/stacktest-production.sh'
echo '$DB_URL' // Expects http://production_url/my_db
sh 'gradle flywayMigrate' // To production
sh './deploy.sh'
}
What should i do?
I was thinking about not using pipeline (but i will not be able to use my Jenkinsfile).
Or make different jobs for staging and production, using EnvInject Plugin (But i lose my stage view)
Or make withEnv (but the code gets big, because today i am working with 12 env vars)
One way you could load environment variables from a file is to load a Groovy file.
For example:
Let's say you have a groovy file in '$JENKINS_HOME/.envvars' called 'stacktest-staging.groovy'.
Inside this file, you define 2 environment variables you want to load
env.DB_URL="hello"
env.DB_URL2="hello2"
You can then load this in using
load "$JENKINS_HOME/.envvars/stacktest-staging.groovy"
Then you can use them in subsequent echo/shell steps.
For example, here is a short pipeline script:
node {
load "$JENKINS_HOME/.envvars/stacktest-staging.groovy"
echo "${env.DB_URL}"
echo "${env.DB_URL2}"
}
From the comments to the accepted answer
Don't use global 'env' but use 'withEnv' construct, eg see:
issue #9: don't set env vars with global env in top 10 best practices jenkins pipeline plugin
In the following example: VAR1 is a plain java string (no groovy variable expansion), VAR2 is a groovy string (so variable 'someGroovyVar' is expanded).
The passed script is a plain java string, so $VAR1 and $VAR2 are passed literally to the shell, and the echo's are accessing environment variables VAR1 and VAR2.
stage('build') {
def someGroovyVar = 'Hello world'
withEnv(['VAR1=VALUE ONE',
"VAR2=${someGroovyVar}"
]) {
def result = sh(script: 'echo $VAR1; echo $VAR2', returnStdout: true)
echo result
}
}
For secrets / passwords you can use credentials binding plugin
Example:
NOTE: CREDENTIALS_ID1 is a registered username/password secret on the Jenkins settings.
stage('Push') {
withCredentials([usernamePassword(
credentialsId: 'CREDENTIALS_ID1',
passwordVariable: 'PASSWORD',
usernameVariable: 'USER')]) {
echo "User name: $USER"
echo "Password: $PASSWORD"
}
}
The jenkisn console log output hides the real values:
[Pipeline] echo
User name: ****
[Pipeline] echo
Password: ****
Jenkins and credentials is a big issue, probably see: credentials plugin
For completeness: Most of the time, we need the secrets in environment variables, as we use them from shell scripts, so we combine the withCredentials and withEnv like follows:
stage('Push') {
withCredentials([usernamePassword(
credentialsId: 'CREDENTIALS_ID1',
passwordVariable: 'PASSWORD',
usernameVariable: 'USER')]) {
withEnv(["ENV_USERNAME=${USER}",
"ENV_PASSWORD=${PASSWORD}"
]) {
def result = sh(script: 'echo $ENV_USERNAME', returnStdout: true)
echo result
}
}
}
Another way to resolve this install 'Pipeline Utility Steps' plugin that provides us readProperties method ( for reference please go to the link https://jenkins.io/doc/pipeline/steps/pipeline-utility-steps/#pipeline-utility-steps)
Here in the example we can see that they are storing the keys into an array and using the keys to retrieve the value.
But in that case the in production the problem will be like if we add any variable later into property file that variable needs to be added into the array of Jenkins file as well.
To get rid of this tight coupling, we can write code in such a way so that the Jenkins build environment can get information automatically about all the existing keys which presents currently in the Property file. Here is an example for the reference
def loadEnvironmentVariables(path){
def props = readProperties file: path
keys= props.keySet()
for(key in keys) {
value = props["${key}"]
env."${key}" = "${value}"
}
}
And the client code looks like
path = '\\ABS_Output\\EnvVars\\pic_env_vars.properties'
loadEnvironmentVariables(path)
With declarative pipeline, you can do it in one line ( change path by your value):
script {
readProperties(file: path).each {key, value -> env[key] = value }
}
Using withEnv() to pass environment variables from file splitted by new line and casted to List:
writeFile file: 'version.txt', text: 'version=6.22.0'
withEnv(readFile('version.txt').split('\n') as List) {
sh "echo ${version}"
}
If you are using Jenkins 2.0 you can load the property file (which consists of all required Environment variables along with their corresponding values) and read all the environment variables listed there automatically and inject it into the Jenkins provided env entity.
Here is a method which performs the above stated action.
def loadProperties(path) {
properties = new Properties()
File propertiesFile = new File(path)
properties.load(propertiesFile.newDataInputStream())
Set<Object> keys = properties.keySet();
for(Object k:keys){
String key = (String)k;
String value =(String) properties.getProperty(key)
env."${key}" = "${value}"
}
}
To call this method we need to pass the path of property file as a string variable For example, in our Jenkins file using groovy script we can call like
path = "${workspace}/pic_env_vars.properties"
loadProperties(path)
Please ask me if you have any doubt
Here is a complete example of externalizing environment variables and loading them in Jenkins pipeline execution. The pipeline is written in a declarative style.
stage('Reading environment variable defined in groovy file') {
steps {
script {
load "./pipeline/basics/extenvvariable/env.groovy"
echo "${env.env_var1}"
echo "${env.env_var2}"
}
}
}
Complete code example:
https://github.com/dhruv-bansal/jenkins-pipeline-exploration/blob/master/pipeline/basics/extenvvariable/Jenkinsfile
Where variables are loaded from a groovy file placed with the pipeline code only.
https://github.com/dhruv-bansal/jenkins-pipeline-exploration/blob/master/pipeline/basics/extenvvariable/env.groovy
This pattern comes very handy when you are creating a generic pipeline that could be used across teams.
You can externalize the dependent variable in such groovy file and each team can define their values according to their ecosystem.
Another solution is to use a custom method without allowing extra permissions such as for new Properties() which leads to this error before allowing:
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use new java.util.Properties
or adding extra plugin methods such as readProperties.
here is a method which reads a simple file named env_vars in this format:
FOO=bar
FOO2=bar
pipeline {
<... skipped lines ...>
script {
loadEnvironmentVariablesFromFile("env_vars")
echo "show time! ${BAR} ${BAR2}"
}
<... skipped lines ...>
}
private void loadEnvironmentVariablesFromFile(String path) {
def file = readFile(path)
file.split('\n').each { envLine ->
def (key, value) = envLine.tokenize('=')
env."${key}" = "${value}"
}
}