A practice project to teach myself how to create Jenkins builds. It contains a sample Gulp task to run JSHint validation on a piece of JavaScript code.
This assumes you have Jenkins installed. If not, that is beyond the scope of this README; consult the Jenkins documentation for details.
You will need to install the Docker and Docker Pipeline plugins to use this.
Most of these are taken from the Jenkins pipeline tutorial. This is the summary version:
- Log into Jenkins and select New Item.
- Select "Multibranch Pipeline" and click "OK".
- Under "Branch Sources", select "GitHub" and enter the repository URL (https://github.com/aceade/jenkins-npm-practice.git)
- Click "Save" and "Apply".
The comments here are to explain what is required.
pipeline {
agent {
docker {
image 'node:14-alpine' // if the build tools require Git, replace with e.g. 'node:14'
args '--user root' // required due to permissions issues inside Docker
}
}
stages {
stage('build') {
steps {
sh 'npm --version' // debugging step to check NPM version
sh 'npm install -g gulp' // install globally so that Gulp is on the global PATH
sh 'npm install' // install the actual dependencies
sh 'gulp' // run the gulp task
}
}
}
post {
success {
echo 'Build completed successfully'
archiveArtifacts "dist/*" // where to find the build artifacts
stash includes: 'dist/*.js', name: 'debugBuiltArtifacts' // what they are
}
failure {
echo 'The build failed'
}
unstable {
echo 'The build is unstable!'
}
changed {
echo 'Pipeline state has changed'
}
}
}
The Gulpfile contains two task: validating some JavaScript code using JSHint, and then minifying with UglifyJS.
The following JSHint rules are configured inside the .jshintrc
file:
- undef - catch potential mistyped or leaking variables
- unused - unused functions and variables are not allowed
- strict - all functions must start with a "use strict" statement, if not already nested inside a function that contains this
- eqeqeq - mandates the use of strict equality (=== or !==)
- curly - loops and conditionals must include curly brackets
- browser - assume that this will be running inside a browser.
- devel - assume that
console
andalert
functions may be present.
The code inside src/badCode.js
violates these rules, unlike the code inside goodCode.js
. badCode.js
is deliberately excluded from validation.
This task just takes src/goodCode.js
and minifies it. The result will be inside the dist
folder if it succeeds, and Jenkins considers this to be the build artifact.
See the attached LICENSE file.