Dooobi
Dooobi

Reputation: 75

Jenkins Pipeline - Access environment variable in nodejs builds

i need some help with my Jenkins pipeline. What i want to do is trigger some builds in a specific order to login to a platform, deploy apps and logout again.
The builds are implemented as nodejs scripts and store data and access data from previous builds by using environment variables.
My problem is that when i'm trying to access environment variables from within my nodejs scripts i always receive undefined. For example: In the 'LoginToPlatform' build i'm using process.env.username to receive the username but even though i set the environment variable in the environment block of my pipeline i receive undefined.

So my question is how can i access the environment variables from within my builds (nodejs scripts)?

Here is the build order:

1. 'LoginToPlatform'
Uses process.env.username and process.env.password
Sets process.env.session

2. 'DeployOnPlatform'
Uses process.env.session

3. 'LogoutFromPlatform'
Uses process.env.session

My Jenkins Pipeline:

pipeline {
agent any

environment {
    username = 'abc'
    password = 'asdf'
}
stages {
    stage ('Login') {
        steps {
            echo 'Login.'
            build job: 'LoginToPlatform'
        }
    }
    stage ('Deployment') {
        steps {
            echo 'Deployment.'
            build job: 'DeployOnPlatform'
        }
    }
    stage ('Logout') {
        steps {
            echo 'Logout.'
            build job: 'LogoutFromPlatform'
        }
    }
}
}

Upvotes: 2

Views: 11568

Answers (1)

Alex
Alex

Reputation: 2174

Here is one of the ways to access env variables.

Jenkinsfile

pipeline {
    agent {
        docker {
            image 'node:6-alpine' 
        }
    }

    environment {
        VARIABLE_1="10"
        VARIABLE_2="7"
    }

    stages {
        stage('Build') { 
            steps {
                sh 'node main.js' 
            }
        }
    }
}

main.js

const envOne = process.env.VARIABLE_1;
const envTwo = process.env.VARIABLE_2;

console.log("envOne: " + envOne);
console.log("envTwo: " + envTwo);

Output:

Jenkins seems to be running inside container 646633d29eac6e0e5b56e4aef28055075b5a2274e26b159387a7a34f35919fe3
$ docker run -t -d -u 0:0 -p 3000:3000 -w /var/jenkins_home/workspace/fff_master-RUQD36MGKNUXMF26H5CQBCDE6AKFWFLUOG7MTQ6WMTXNXKQHCNMA --volumes-from 646633d29eac6e0e5b56e4aef28055075b5a2274e26b159387a7a34f35919fe3 -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** node:6-alpine cat
$ docker top 3fe3059a78e890dc2cadd722c25b97d5a023da059cc807cef3acb29237f0261f -eo pid,comm
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] sh
[fff_master-RUQD36MGKNUXMF26H5CQBCDE6AKFWFLUOG7MTQ6WMTXNXKQHCNMA] Running shell script
+ node main.js
envOne: 10
envTwo: 7
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
$ docker stop --time=1 3fe3059a78e890dc2cadd722c25b97d5a023da059cc807cef3acb29237f0261f
$ docker rm -f 3fe3059a78e890dc2cadd722c25b97d5a023da059cc807cef3acb29237f0261f
[Pipeline] // withDockerContainer
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline

Upvotes: 5

Related Questions