Home > Enterprise >  How to setup deploy nodejs app using jenkins docker container in aws ec2
How to setup deploy nodejs app using jenkins docker container in aws ec2

Time:12-23

I'm running docker compose file in ec2 instance, this file contains mysql, jenkins images. Also running nodejs app using pm2 command, when I run nodejs server manually in ec2-instance everything is working properly.

But When I try to deploy nodejs app using jenkins container, latest code is not deployed, i tried to debug why it is not deployed, I found one interesting thing

When i try to run pipeline all commands executed inside jenkins container workspace with jenkins user(container path : /var/jenkins_home/workspace/main)

So my question is my actual nodejs app placed in /home/ubuntu/node-app. But when try to deploy code using jenkins pipeline, pipeline is running in different path(/var/jenkins_home/workspace/main).

Now i have question, is this possible to execute pipeline deployment command for /home/ubuntu/node-app path? not docker container path?

if changing path is not possible, how to point jenkins docker container to ec2 public ip?

I shared jenkinsfile script and docker compose image code for reference

Jenkinsfile code:

 stages {
    stage('Build') { 
        steps {
            sh 'npm install && npm run build' 
        }
    }

    stage('Deploy') { 
        steps {
            sh "pwd"
            sh 'git pull origin main'
            sh 'pm2 stop server || true'
            sh 'npm install'
            sh 'npm run build'
            sh 'pm2 start build/server.js '
        }
    }
}

Jenkins Docker Image code:

jenkins:
image: 'jenkins/jenkins:lts'
container_name: 'jenkins'
restart: always
ports:
  - '8080:8080'
  - '50000:50000'
volumes:
  - jenkins-data:/etc/gitlab
  - /var/run/docker.sock:/var/run/docker.sock

Edit 1: I tried to change the path following ways to in jenkinsfile

cd /home/ubuntu/node-app

I'm getting following error

/var/jenkins_home/workspace/main@tmp/durable-44039790/script.sh: 1: cd: can't cd to /home/ubuntu/node-app

Note : this path(/var/jenkins_home/workspace/main) is only visible in ec2 machine after exec following command, normally this path is not exist in ec2 machine

docker exec -it jenkins bash

CodePudding user response:

Try with following fix code

stage('Deploy') { 
    steps {
        sh "cd /home/ubuntu/node-app"
        sh 'git pull origin main'
        sh 'pm2 stop server || true'
        sh 'npm install'
        sh 'npm run build'
        sh 'pm2 start build/server.js '
    }
}

CodePudding user response:

Finally I found solution for this issue.

The actual issues is I didn't create any slave agent for jenkins pipeline, that's why jenkins pipeline jobs are running in master agent location, here master agent location was jenkins docker container space, that's why pipeline jobs are strored into /var/jenkins_home/workspace/main this path

Now I added slave agent and mentioned the customWorkspace path( i mentioned customWorkspace path is 'home/ubuntu/node-app') in jenkinsfile. Now my jenkins pipeline is working under custom workspace that is /home/ubuntu/node-app

My updated jenkinsfile code:

pipeline {
  agent {
    node {  
      label 'agent1'
      customWorkspace '/home/ubuntu/node-app'
    }
  }

    stages {
        stage('Build') { 
            steps {
                sh 'npm install && npm run build' 
            }
        }

        stage('Deploy') { 
            steps {
                sh 'pm2 restart server'
            }
        }
    }
 }
  • Related