<p>I have Kubernetes Cluster where deployed and scaled Jenkins,
below podTemplate yaml file which I run in Jenkins pipeline:</p>
<pre><code>podTemplate(yaml: &quot;&quot;&quot;
apiVersion: v1
kind: Pod
spec:
  containers:
  - name: docker
    image: docker/compose
    command: ['cat']
    tty: true
    volumeMounts:
    - name: dockersock
      mountPath: /var/run/docker.sock
  volumes:
  - name: dockersock
    hostPath:
      path: /var/run/docker.sock
&quot;&quot;&quot;
  ) {

  def image = &quot;image/name&quot;
  node(POD_LABEL) {
    stage('Build and run Docker image') {
      git branch: 'test', 
      credentialsId: 'github-credentials',
      url: 'https://github.com/project/project.git'
      container('docker') {
        sh &quot;docker-compose up -d --build&quot;
      }
    }
  }
}
</code></pre>
<p>I have got an error:</p>
<pre><code>[Pipeline] sh
+ docker-compose up -d --build
The ENV variable is not set. Defaulting to a blank string.
[Pipeline] }
</code></pre>
<p>Which is best practice to set env vars during the this kind of deployments?</p>
<p><em><strong>Update:</strong></em>
yes, it's working outside of Jenkins,
I have listed env var files in docker compose yaml file:</p>
<pre><code>...
    context: ./validator
    ports:
      - '${VALIDATOR_PROD_ONE_PORT}:${VALIDATOR_PROD_ONE_PORT}'
    volumes:
      - ./validator/logs_1:/usr/src/app/logs
    ***env_file: .env.validator.test***
...
</code></pre>
<p>Of course I cat set env var in Jenkins pipeline before executing docker-compose build like this, for example:</p>
<pre><code>container('docker') {
***sh ' echo &quot;someEnvVar=value&quot; &gt; .env.validator.test'***
   sh &quot;docker-compose up -d --build&quot;
 }
</code></pre>
<p>This way also working, but not beautiful (:</p>

<p>You should be able to set environment variables on your stage as following:</p>
<pre><code>    stage('Build and run Docker image') {
      environment {
        SOME_ENV_VAR = &quot;SOME_VAL&quot;
      }
      git branch: 'test', 
      credentialsId: 'github-credentials',
      url: 'https://github.com/project/project.git'
      container('docker') {
        sh &quot;docker-compose up -d --build&quot;
      }
    }
</code></pre>
<p>This would essentially set shell Environment variables, which should precede over those in .env file.</p>
