docker - 如何使用多个Docker容器在Jenkins管道中设置Jenkins代理

标签 docker jenkins docker-compose jenkins-pipeline dockerfile

以下代码段是Cypress提供的示例,Cypress是我正在使用的Javascript测试框架。这是Github页面的link

pipeline {
  agent {
    // this image provides everything needed to run Cypress
    docker {
      image 'cypress/base:10'
    }
  }

  stages {
    // first stage installs node dependencies and Cypress binary
    stage('build') {
      steps {
        // there a few default environment variables on Jenkins
        // on local Jenkins machine (assuming port 8080) see
        // http://localhost:8080/pipeline-syntax/globals#env
        echo "Running build ${env.BUILD_ID} on ${env.JENKINS_URL}"
        sh 'npm ci'
        sh 'npm run cy:verify'
      }
    }

    stage('start local server') {
      steps {
        // start local server in the background
        // we will shut it down in "post" command block
        sh 'nohup npm run start:ci &'
      }
    }

    // this stage runs end-to-end tests, and each agent uses the workspace
    // from the previous stage
    stage('cypress parallel tests') {
      environment {
        // we will be recording test results and video on Cypress dashboard
        // to record we need to set an environment variable
        // we can load the record key variable from credentials store
        // see https://jenkins.io/doc/book/using/using-credentials/
        CYPRESS_RECORD_KEY = credentials('cypress-example-kitchensink-record-key')
        // because parallel steps share the workspace they might race to delete
        // screenshots and videos folders. Tell Cypress not to delete these folders
        CYPRESS_trashAssetsBeforeRuns = 'false'
      }

      // https://jenkins.io/doc/book/pipeline/syntax/#parallel
      parallel {
        // start several test jobs in parallel, and they all
        // will use Cypress Dashboard to load balance any found spec files
        stage('tester A') {
          steps {
            echo "Running build ${env.BUILD_ID}"
            sh "npm run e2e:record:parallel"
          }
        }

        // second tester runs the same command
        stage('tester B') {
          steps {
            echo "Running build ${env.BUILD_ID}"
            sh "npm run e2e:record:parallel"
          }
        }
      }

    }
  }

  post {
    // shutdown the server running in the background
    always {
      echo 'Stopping local server'
      sh 'pkill -f http-server'
    }
  }
}

我的目标是要有一个与上述非常相似的Jenkinsfile,因为我想进行并行的Cypress测试,如上面的代码片段所示。在上面的示例中,Jenkins代理只是官方的 Cypress Docker镜像cypress/base:10
  agent {
    // this image provides everything needed to run Cypress
    docker {
      image 'cypress/base:10'
    }
  }

但是,要使用我自己的数据库运行所有测试,我需要启动两个单独的Docker容器。一个容器包含我的Web应用程序的前端部分,另一个容器包含我的Web应用程序的后端部分。

以下是我的前端容器的Dockerfile,该文件位于my-app/docker/combined/Dockerfile中。
FROM cypress/included:3.4.1

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 5000

RUN npm install -g history-server nodemon

RUN npm run build-test

EXPOSE 8080

以下是我的后端容器的Dockerfile,该文件位于my-app/docker/db/Dockerfile中。它所做的就是将一些本地数据复制到Docker容器中,然后使用该数据初始化我的MongoDB数据库。
FROM  mongo:3.6.14-xenial

COPY ./dump/ /tmp/dump/

COPY mongo_restore.sh /docker-entrypoint-initdb.d/

RUN chmod 777 /docker-entrypoint-initdb.d/mongo_restore.sh

通常,我将使用docker-compose和以下docker-compose.yml文件启动这两个容器。如您所见,称为“combined”的前端容器取决于称为“db”的后端容器。
version: '3'
services:
    db:
        build:
            context: .
            dockerfile: ./docker/db/Dockerfile
        container_name: b-db
        restart: unless-stopped
        volumes:     
            - dbdata:/data/db
        ports:
            - "27017:27017"
        networks:
            - app-network

    combined:
        build:
            context: .
            dockerfile: ./docker/combined/Dockerfile
        container_name: b-combined
        restart: unless-stopped
        env_file: .env
        ports:
            - "5000:5000"
            - "8080:8080"
        networks:
            - app-network
        depends_on:
            - db

以下是我将使用的docker-compose命令。
docker-compose up --build

我希望我的Jenkins代理成为combined容器;但是,我需要将combined容器连接到需要旋转的db容器。我的问题是,如何在 Jenkins 管道中实现这一目标?我读过this documentation;但是,它没有提及使用多个Dockerfile创建Jenkins代理的任何内容。这样的事情是否可能发生,有人可以告诉我实现我的目标的Jenkinsfile应该是什么样吗?

最佳答案

关于docker - 如何使用多个Docker容器在Jenkins管道中设置Jenkins代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59356957/

相关文章:

java - 如何配置Jenkins,从本地到从机运行代码?

docker - docker-compose无法识别sudoers容器文件

mysql - 无法连接到 Windows 计算机上 Docker 上托管的 Mysql

docker - Gitlab CI仿真器无法启动

docker - 如何使用 Docker Compose 设置 Dockerfile 的 "current"目录?

使用 EMMA 的 Sonar 的 Delphi 代码覆盖率

docker-compose - traefik - 将变量放入 docker 标签中

docker - “docker image ls”给出了巨大的<none>图像列表

docker - 进入由docker创建的卷

groovy - 如何在每个触发器的基础上更改 Jenkins email-ext 插件中的 FROM 电子邮件地址?