Table of contents
- π οΈ 1. Setting Up Jenkins in a VM
- π³ 2. Setting Up Nexus Using Docker
- π¦ 3. Setting Up an npm-Private Repository in Nexus
- π 4. Installing Plugins in Jenkins
- π οΈ 5. Configuring Tools in Jenkins
- π 6. Configuring .npmrc File in Jenkins
- π» 7. Writing the Jenkins Pipeline
- β 8. Verifying the Artifact in Nexus
- π Wrapping Up
In this blog, we will walk you through publishing a Node.js artifact to a Nexus npm-private repository using Jenkins. Letβs make this journey simple and enjoyable with step-by-step instructions and some emojis to guide you! π
π οΈ 1. Setting Up Jenkins in a VM
Launch a Virtual Machine (VM) with your preferred OS (Ubuntu/Debian recommended).
Update the system:
sudo apt update && sudo apt upgrade -y
Install Jenkins:
wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add - sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list' sudo apt update sudo apt install jenkins -y
Start Jenkins:
sudo systemctl start jenkins
Access Jenkins at
http://<your-vm-ip>:8080
and complete the setup wizard.
π³ 2. Setting Up Nexus Using Docker
Launch another VM for Nexus.
Install Docker:
sudo apt update && sudo apt install docker.io -y
Run Nexus Container:
docker run -d -p 8081:8081 --name nexus sonatype/nexus3
Access Nexus at
http://<your-vm-ip>:8081
. Default credentials:Username:
admin
Password: Found in
/nexus-data/admin.password
inside the container.
π¦ 3. Setting Up an npm-Private Repository in Nexus
Log in to Nexus.
Navigate to Repositories > Create Repository.
Select npm (hosted) and configure:
Repository Name:
npm-private
Deployment Policy:
Allow redeploy
Save the repository.
π 4. Installing Plugins in Jenkins
Go to Manage Jenkins > Manage Plugins.
Install:
NodeJS Plugin
Pipeline: Config File Provider Plugin
π οΈ 5. Configuring Tools in Jenkins
Go to Manage Jenkins > Global Tool Configuration.
Under NodeJS, add a new Node.js installation:
Name:
nodejs20
Install the latest Node.js version.
π 6. Configuring .npmrc
File in Jenkins
Generate an
.npmrc
file:echo -n 'admin:ankit' | base64
Go to Manage Jenkins > Managed Files.
Add a new Config File:
Type: npmrc
ID:
npmrc
Paste the
.npmrc
content.
π» 7. Writing the Jenkins Pipeline
Here is the pipeline to publish your Node.js artifact:
pipeline {
agent any
stages {
stage('Git Checkout') {
steps {
git branch: 'main', url: 'https://github.com/Ank911007/Nodejs-JEST.git'
}
}
stage('Dependencies') {
steps {
nodejs('nodejs20') {
sh 'npm install'
}
}
}
stage('Publish') {
steps {
configFileProvider([configFile(fileId: 'npmrc', targetLocation: '.')]) {
nodejs('nodejs20') {
sh 'npm publish'
}
}
}
}
}
}
β 8. Verifying the Artifact in Nexus
Log in to Nexus and navigate to your npm-private repository.
Verify that your Node.js package is listed. π
π Wrapping Up
Congratulations! π You have successfully set up Jenkins and Nexus, configured a pipeline, and published a Node.js artifact to a private npm repository. This process ensures secure and efficient artifact management for your Node.js projects.
Feel free to experiment and enhance your CI/CD workflows further. π