简体   繁体   English

如何在 aws ec2 中使用 jenkins docker 容器设置部署 nodejs 应用程序

[英]How to setup deploy nodejs app using jenkins docker container in aws ec2

I'm running docker compose file in ec2 instance, this file contains mysql, jenkins images.我在 ec2 实例中运行 docker compose 文件,这个文件包含 mysql、jenkins 图像。 Also running nodejs app using pm2 command, when I run nodejs server manually in ec2-instance everything is working properly.还使用 pm2 命令运行 nodejs 应用程序,当我在 ec2-instance 中手动运行 nodejs 服务器时,一切正常。

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但是当我尝试使用 jenkins 容器部署 nodejs 应用程序时,最新的代码没有部署,我试图调试它为什么没有部署,我发现了一件有趣的事情

When i try to run pipeline all commands executed inside jenkins container workspace with jenkins user(container path: /var/jenkins_home/workspace/main)当我尝试使用 jenkins 用户(容器路径:/var/jenkins_home/workspace/main)运行在 jenkins 容器工作区内执行的所有命令时

So my question is my actual nodejs app placed in /home/ubuntu/node-app.所以我的问题是我实际的 nodejs 应用程序放在 /home/ubuntu/node-app 中。 But when try to deploy code using jenkins pipeline, pipeline is running in different path(/var/jenkins_home/workspace/main).但是当尝试使用 jenkins 管道部署代码时,管道在不同的路径(/var/jenkins_home/workspace/main)中运行。

Now i have question, is this possible to execute pipeline deployment command for /home/ubuntu/node-app path?现在我有疑问,是否可以为 /home/ubuntu/node-app 路径执行管道部署命令? not docker container path?不是 docker 容器路径?

if changing path is not possible, how to point jenkins docker container to ec2 public ip?如果无法更改路径,如何将 jenkins docker 容器指向 ec2 公共 ip?

I shared jenkinsfile script and docker compose image code for reference我分享了 jenkinsfile 脚本和 docker compose 镜像代码以供参考

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 Docker 镜像代码:

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编辑 1:我尝试按照 jenkinsfile 中的方式更改路径

cd /home/ubuntu/node-app cd /home/ubuntu/节点应用程序

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注意:此路径(/var/jenkins_home/workspace/main)仅在执行以下命令后在 ec2 机器中可见,通常此路径在 ec2 机器中不存在

docker exec -it jenkins bash

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 '
    }
}

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实际问题是我没有为 jenkins 管道创建任何从代理,这就是为什么 jenkins 管道作业在主代理位置运行,这里主代理位置是 jenkins docker 容器空间,这就是为什么管道作业被存储到 /var/jenkins_home/ workspace/main 这个路径

Now I added slave agent and mentioned the customWorkspace path( i mentioned customWorkspace path is 'home/ubuntu/node-app') in jenkinsfile.现在我添加了从属代理并在 jenkinsfile 中提到了 customWorkspace 路径(我提到 customWorkspace 路径是'home/ubuntu/node-app')。 Now my jenkins pipeline is working under custom workspace that is /home/ubuntu/node-app现在我的詹金斯管道在 /home/ubuntu/node-app 的自定义工作区下工作

My updated jenkinsfile code:我更新的 jenkinsfile 代码:

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'
            }
        }
    }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM