Siddarth
Siddarth

Reputation: 381

How to get Jenkins BUILD_NUMBER in JavaScript using GULP/NODE?

I have a gulp task getBuildNumber which uses Child Process to execute the script.

gulp.task('getBuildNumber', function() {
        var buildNumber = child_process.execSync("echo $BUILD_NUMBER").toString().trim();
        console.log(buildNumber);
    });

When I run the following command for gulp

npm run gulp -- getBuildNumber

I always get the output as $BUILD_NUMBER and not the actual Jenkins build number.

Can someone please suggest on how to proceed with this?

Upvotes: 0

Views: 2000

Answers (2)

Alex O
Alex O

Reputation: 8164

According to https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback, you need to escape special characters:

exec('echo "The \\$HOME variable is $HOME"');

In your case, this means you'd need to use

[...]child_process.execSync("echo \\$BUILD_NUMBER").toString().trim();[...]
                                  ^^

Upvotes: 0

James Monger
James Monger

Reputation: 10675

You can access environment variables with process.env.

For example:

console.log(process.env.BUILD_NUMBER);

Upvotes: 5

Related Questions