docker - 如何使用多个 Docker 容器在 Jenkins 管道中设置 Jenkins 代理
问题描述
以下代码片段是 Cypress 提供的示例,我正在使用的 Javascript 测试框架。这是Github 页面的链接。
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,因为我想要进行并行赛普拉斯测试,如上面的代码片段所示。在上面的示例中,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 管道中实现这一点?我已阅读此文档;但是,它没有提及使用多个 Dockerfile 来创建 Jenkins 代理。这样的事情是否可能,有人可以告诉我我的 Jenkinsfile 应该是什么样子以实现我的目标吗?
解决方案
推荐阅读
- javascript - 使用 redux 编辑列表元素
- c# - 元组
不包含接受那么多参数的构造函数 - c# - 共享访问签名 - 签名不匹配
- c++11 - 在调用函数时强制转换右值参数是个好主意吗?
- objective-c - 在块中使用弱 obj 时崩溃
- model-view-controller - 渲染局部视图而不刷新MVC中的主视图
- redis - redis 客户端列表重新运行“cmd=NULL”
- android - 提取文本视图中显示的文本行
- swift - 如何在 Xcode (swift) 中的文本上自动滚动到右侧?
- javascript - 将参数从 StackNavigator 传递到 TabNavigator