[英]How to use multiple Docker containers to set up Jenkins agent in Jenkins pipeline
以下代碼段是 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,因為我想要進行並行 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.yml
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 應該是什么樣子才能實現我的目標嗎?
考慮運行“sidecar”容器: https : //jenkins.io/doc/book/pipeline/docker/#running-sidecar-containers
我不認為您可以在並行構建之間進行通信,問題是,在 Jenkinsfile 上的不同parallel
階段中運行的階段理論上可以在不同的 Jenkins slave 上運行,因此無法相互通信。
我建議做的是使用&
在同一階段並行運行服務器和前端應用程序,然后調用wait
等待所有進程退出。
你甚至可以丟棄的使用docker
在Jenkinsfile關鍵字,而是,舞台本身內部通話泊塢窗。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.