NPM Builds with Artifactory - Scripted Pipeline Syntax

JFrog Integrations Documentation

Content Type
Integrations
ft:sourceType
Paligo

NPM builds can resolve dependencies, deploy artifacts and publish build-info to Artifactory. To run NPM builds with Artifactory from your Pipeline script, you first need to create an Artifactory server instance, as described in the Creating an Artifactory Server Instance section.

Here's an example:

def server = Artifactory.server 'my-server-id'

The next step is to create an Artifactory NPM Build instance:

def rtNpm = Artifactory.newNpmBuild()

Now let's define where the NPM build should download its dependencies from. We set the Artifactory server instance we created earlier and the repository name on the resolver:

rtNpm.resolver server: server, repo: 'npm-virtual'

The build uses the npm executable to install (download the dependencies) and publish. By default, Jenkins uses the npm executable present in the agent's PATH. You can also reference a tool defined in Jenkins, and set the script to use it as follows:

// Set the name of a tool defined in Jenkins configuration
rtNpm.tool = 'nodejs-tool-name'
// or set the tool as an environment variable
env.NODEJS_HOME = "${tool 'nodejs-tool-name'}"
// or set a path to the NodeJS home directory (not the npm executable)
env.NODEJS_HOME = 'full/path/to/the/nodeJS/home'
// or
nodejs(nodeJSInstallationName: 'nodejs-tool-name') {
        // Only in this code scope, the npm defined by 'nodejs-tool-name' is used.
}

Now we can download our project's npm dependencies. The following method runs npm install behind the scenes:

def buildInfo = rtNpm.install path: 'npm-example'

You can also add npm flags or arguments as follows:

def buildInfo = rtNpm.install path: 'npm-example', args: '--verbose'

The npm ci command is also supported the same way:

def buildInfo = rtNpm.ci path: 'npm-example'

The above methods return a buildInfo instance. If we already have a buildInfo instance we'd like to reuse, we can alternatively send the buildInfo as an argument as shown below. Read thePublishing Build-Info to Artifactory section for more details.

rtNpm.install path: 'npm-example', buildInfo: my-build-info

You also have the option of customising the build-info module name associated with this build. You do this as follows:

def buildInfo = rtNpm.install path: 'npm-example', module: 'my-build-info-module-name'

Jenkins spawns a new java process during this step's execution.

You have the option of passing any java args to this new process, by passing the javaArgs argument:

def buildInfo = rtNpm.install path: 'npm-example', javaArgs: '-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005'

The action of publishing the NPM package to Artifactory is very similar. We start by defining the deployer:

rtNpm.deployer server: server, repo: 'npm-local'

The following method will do two things: package the code (by running npm pack) and publish it to Artifactory:

def buildInfo = rtNpm.publish path: 'npm-example'

Similarly to the install method, the following is also supported:

rtNpm.publish path: 'npm-example', buildInfo: my-build-info

You also have the option of customising the build-info module name associated with this operation. You do this as follows:

def buildInfo = rtNpm.publish path: 'npm-example', module: 'my-build-info-module-name'

Jenkins spawns a new java process during this step's execution.

You have the option of passing any java args to this new process, by passing the javaArgs argument:

def buildInfo = rtNpm.publish path: 'npm-example', javaArgs: '-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005' 

You can now publish the build-info to Artifactory as described in the Publishing Build-Info to Artifactory section