0

I am trying to automate my build using Jenkins. My build process needs to execute three different shell scripts. The first script sets some environment variables which is used by the second and the third scripts. I am trying with a pipeline job in Jenkins where each script is executed stage by stage. However I am unable to get the environment variables from the first script to the next one.

NB: There is a set of variables that are being set.So I don't feel like using a simple variable will do.

Please help

Jackzz
  • 1,417
  • 4
  • 24
  • 53
  • Possible duplicate of [How to set environment variables in Jenkins?](https://stackoverflow.com/questions/10625259/how-to-set-environment-variables-in-jenkins) – 7_R3X Aug 20 '19 at 07:54
  • 1
    each shell script executed in like a sub-process, once execution complete, the sub-process is end, any environment variables exported in shell script is also end. Thus you can pass down them to next shell script by `export AAA=...`. You need write out those export statements into a file, then execute the file in next shell. – yong Aug 20 '19 at 09:21

1 Answers1

0

You are probably confusing declarative pipeline with scripted pipeline

Jenkinsfile (Declarative Pipeline)

pipeline {
agent any

environment {
    DISABLE_AUTH = 'true'
    DB_ENGINE    = 'sqlite'
}

stages {
    stage('Build') {
        steps {
            sh 'printenv'
        }
    }
  }
}

Jenkinsfile (Scripted Pipeline)

node {
withEnv(['DISABLE_AUTH=true',
         'DB_ENGINE=sqlite']) {
    stage('Build') {
        sh 'printenv'
    }
  }
}
Joao Vitorino
  • 2,976
  • 3
  • 26
  • 55
  • I am using declarative pipeline. The problem is that the variables that are being set from the script is not known before-hand. So I cannot set it in `environment` block. – Jackzz Aug 21 '19 at 05:18
  • @Jackzz You can add the `environment` section inside the `stage`section – Joao Vitorino Aug 21 '19 at 12:00