Jenkins Properties for npm Commands | Generated by AI
I’ll help explain how Jenkins properties work and how they can be used with npm commands.
Jenkins Properties Overview
Jenkins properties are environment variables that can be defined in several ways:
- Build parameters - defined when configuring a job
- Environment variables - set in Jenkins configuration or pipeline
- Properties files - loaded using
readPropertiesor similar - Credentials - secure values stored in Jenkins
Using Properties with npm Commands
Yes, you can definitely use Jenkins properties for npm commands! Here’s how:
In a Pipeline:
pipeline {
agent any
environment {
NPM_AUTH_URL = 'https://registry.npmjs.org/'
NPM_TOKEN = credentials('npm-token-id')
}
stages {
stage('Install') {
steps {
sh '''
echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > .npmrc
npm install --registry=${NPM_AUTH_URL}
'''
}
}
}
}
In a Freestyle Job:
- Define environment variables in the job configuration
- Reference them in your build steps using
${VARIABLE_NAME}(Linux) or%VARIABLE_NAME%(Windows)
Common Patterns:
Using .npmrc configuration:
sh '''
npm config set registry ${NPM_AUTH_URL}
npm config set //registry.npmjs.org/:_authToken ${NPM_TOKEN}
npm install
'''
Passing as command arguments:
sh "npm install --registry=${NPM_AUTH_URL}"
Using withEnv block:
withEnv(["NPM_AUTH_URL=https://registry.npmjs.org/"]) {
sh 'npm install --registry=$NPM_AUTH_URL'
}
The key is that Jenkins properties become environment variables that are accessible in your shell commands. For sensitive data like tokens, always use Jenkins credentials rather than plain text properties.