To make grunt-jenkins-build-number work in GRUNT to sniff the current build number:
- You need something like this in dependencies in package.json:
"grunt-jenkins-build-number": ">=0.0.2", - You need this blob in the opening .initConfig of Gruntfile.js:
jenkinsBuildNumber: {
dist: {
options: {
hostname: 'http://jenkins.example.com/',
state: 'lastBuild',
username: 'usersuper',
password: 'badpassword',
projectName: 'TheNameOfMyJenkinsProject'
}
}
},
- Farther down Gruntfile.js you need:
grunt.loadNpmTasks('grunt-jenkins-build-number');
- Now I'm going to tell what they don't tell you in the documentation! You may get the build number farther down yet in Gruntfile.js (in a .registerMultiTask) like so:
var lastBuild = process.env.BUILD_NUMBER;
This is not correct.
ReplyDeleteThe process.env.BUILD_NUMBER is available only if the BUILD_NUMBER environment variable is set. That is, if you run this in the context of a Jenkins job, you'll get the current build number. Outside of Jenkins, that will return undefined.
To get the plugin to work, you have to call the jenkinsBuildNumber task in your grunt file in addition to specifying the configuration. If you run that task, you'll get an error with this configuration. The error is "getaddrinfo ENOENT" because you're only supposed to pass the host name, jenkins.example.com, to the hostname parameter. There's a separate parameter for port which also must be specified if it's not the default (80). Once that's configured and you call the task, you can get the number by calling: grunt.config.get("lastBuild").
That obviously will work anywhere that has access to that hostname but it's going to give you the last build number and not the current one. If you were trying to get the build number for a new build, this wouldn't give you the right answer.
Thanks Tim!
ReplyDelete