🌐 Publishing Node.js Artifacts to Nexus with Jenkins

🌐 Publishing Node.js Artifacts to Nexus with Jenkins

Β·

3 min read

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

  1. Launch a Virtual Machine (VM) with your preferred OS (Ubuntu/Debian recommended).

  2. Update the system:

     sudo apt update && sudo apt upgrade -y
    
  3. 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
    
  4. Start Jenkins:

     sudo systemctl start jenkins
    
  5. Access Jenkins at http://<your-vm-ip>:8080 and complete the setup wizard.


🐳 2. Setting Up Nexus Using Docker

  1. Launch another VM for Nexus.

  2. Install Docker:

     sudo apt update && sudo apt install docker.io -y
    
  3. Run Nexus Container:

     docker run -d -p 8081:8081 --name nexus sonatype/nexus3
    
  4. 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

  1. Log in to Nexus.

  2. Navigate to Repositories > Create Repository.

  3. Select npm (hosted) and configure:

    • Repository Name: npm-private

    • Deployment Policy: Allow redeploy

  4. Save the repository.


πŸ”Œ 4. Installing Plugins in Jenkins

  1. Go to Manage Jenkins > Manage Plugins.

  2. Install:

    • NodeJS Plugin

    • Pipeline: Config File Provider Plugin


πŸ› οΈ 5. Configuring Tools in Jenkins

  1. Go to Manage Jenkins > Global Tool Configuration.

  2. Under NodeJS, add a new Node.js installation:

    • Name: nodejs20

    • Install the latest Node.js version.


πŸ“ƒ 6. Configuring .npmrc File in Jenkins

  1. Generate an .npmrc file:

     echo -n 'admin:ankit' | base64
    

  2. Go to Manage Jenkins > Managed Files.

  3. 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

  1. Log in to Nexus and navigate to your npm-private repository.

  2. 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. πŸš€

Β