title: How to return environment values in jenkins declarative pipeline to a file?
body: <p>I have a Jenkinsfile as below and </p>
  
  <ul>
  <li>i am passing environment variables per stage, </li>
  <li>want to create a <code>docker.env</code> file using these environment variables, </li>
  <li>so i can use this docker.env file as part of my docker run <code>'docker run -d --env-file=docker.env java'</code> per stage</li>
  <li><strong>looking for a <code>&lt;command_to_copy_all_values_under_'environment'_to_file_'docker.env'&gt;</code> ???</strong></li>
  </ul>
  
  <h3>Jenkinsfile:</h3>
  
  <pre class="lang-none prettyprint-override"><code>pipeline {
    agent any
    stages {
      stage('Staging') {
        environment {
          KEY1_1=VALUE1_1
          KEY1_2=VALUE1_2
          KEY2_1=VALUE2_1
          KEY2_2=VALUE2_2
        }
        steps {
          timestamps() {
            deleteDir()
            sh '''
           &lt;command_to_copy_all_values_under_above_'environment'_to_file_'docker.env'&gt;
               docker run -d --env-file=docker.env java
  '''
          }
  
        }
      }
      stage('Production') {
        when {
          branch 'release'
        }
        environment {
          KEY1_1=VALUE1_1
          KEY1_2=VALUE1_2
          KEY2_1=VALUE2_1
          KEY2_2=VALUE2_2
        }
        steps {
          timestamps() {
            timeout(time: 30, unit: 'MINUTES') {
              input 'Ready to deploy to PRODUCTION, click PROCEED or ABORT ?'
            }
            deleteDir()
            sh '''
               &lt;command_to_copy_all_values_under_above_'environment'_to_file_'docker.env'&gt;
               docker run -d --env-file=docker.env java
  '''
          }
        }
      }
    }
    tools {
      maven 'apache_maven_352'
    }
    environment {
          KEY1=VALUE1
          KEY2=VALUE2
          KEY3=VALUE3
          KEY4=VALUE4
    }
  }
  </code></pre>