Jenkins管道withRun MySQL从ECONNREFUSED失败

时间:2020-10-08 08:19:32

标签: mysql docker jenkins jenkins-pipeline

我正在为我的应用程序设置Jenkins管道,并且该应用程序需要将MySQL服务器连接到测试代码。

我决定暂时在MySQL服务器上运行 Jenkins节点

这是我的 Jenkins文件

node {
  checkout scm
  tool name: 'node', type: 'nodejs'
  /*
    * In order to communicate with the MySQL server, this Pipeline explicitly
    * maps the port (`3306`) to a known port on the host machine.
    */

  docker.image('mysql:latest').withRun('-e "MYSQL_ROOT_PASSWORD=password" -p 3306:3306 --name mysql') { c ->
    nodejs('node') {
      stage('SETUP') {
        sh "docker ps"
        sh "docker cp ./all-databases.sql ${c.id}:./"
        sh "docker exec ${c.id} sh -c 'while ! mysqladmin ping -h0.0.0.0 --silent; do sleep 1; done'"
        sh "docker exec ${c.id} sh -c 'mysql -h127.0.0.1 -uroot -ppassword < all-databases.sql'"
      }

      withEnv(['DB_DATABASE=kimnongman',
              'DB_HOST=0.0.0.0',
             'DB_USER=anton',
             'DB_PASSWORD=password',
             ]) {
        stage('TEST') {
          try {
            sh "npm install"
            sh "docker ps"
            sh "npm run test"
          } catch (e) {
            echo "Error"
          } finally {
            echo "success"
          }
        }
      }
    }
  }
}

关于我使用sh 'docker ps'的代码,以确保MySQL临时服务器正在运行。

这是该行的结果。

enter image description here

它肯定运行良好。

这是测试代码

的结果

enter image description here

他们与MySQL服务器的连接连接错误

即使 curl 在服务器上也不起作用。

enter image description here

我不知道我现在想念什么。

0 个答案:

没有答案