[英]Jenkins - communicate from Docker agent to other Docker containers
我正在使用 buildkite/puppeteer Docker 代理運行 Jenkins 作業。 據我了解,這將從該圖像創建一個容器並在其中運行作業。 該作業從各自的存儲庫中提取 React 前端和 Express 后端應用程序,安裝它們的依賴項,啟動它們並運行npm
腳本以啟動 CodeceptJS / Puppeteer 端到端測試,這些測試位於前端存儲庫中。
在運行測試之前,我需要用測試數據填充 MongoDB 實例(應刪除舊測試數據)並索引 Elasticsearch 實例(應刪除舊索引)。 為了在本地執行此操作,我運行了一個 MongoDB 和 Elasticsearch 容器,並且在我通過運行docker
命令和curl
請求運行測試之前,一個 bash 腳本完成了必要的工作; 但是,我很難理解如何在 Jenkins 中做到這一點。 Jenkins 主機有運行 Mongo 和 Elasticsearch 的 Docker 容器,但是 Jenkins 作業無法與它們通信 - 日志顯示找不到 docker 命令,並且向 ES 容器發出 curl 請求失敗。 這是我的 Jenkins 管道代碼的樣子 - 我不得不編輯一些信息:
pipeline {
agent {
docker {
image 'buildkite/puppeteer'
args '--net=[redacted]_ci --add-host=[redacted]:127.0.0.1'
}
}
environment {
HOME = "${env.WORKSPACE}"
MONGO_HOST = "[redacted]_mongo"
ELASTIC_HOST = "[redacted]_elastic"
API_ROOT_URL = "http://[redacted]:9001"
}
stages {
stage('Clone repositories') {
parallel {
stage('pull from [react repo]') {
steps {
dir('[react folder]') {
git url: '[redacted]', branch: [redacted], credentialsId: 'global_github'
}
}
}
stage('pull from [backend repo]') {
steps {
dir('[backend folder]') {
git url: '[redacted]', branch: [redacted], credentialsId: 'global_github'
}
}
}
}
}
stage('Install npm dependencies') {
parallel {
stage('npm ci (react app)') {
steps {
dir('[react folder]') {
sh 'npm ci' // clean-install
//sh 'npm install'
}
}
}
stage('npm ci (api app)') {
steps {
dir('[backend folder]') {
sh 'npm ci' // clean-install
}
}
}
}
}
stage('Start web servers') {
parallel {
stage('Start API server') {
steps {
dir('[backend folder]') {
sh 'cp server/config/environment/secrets.ci.js server/config/environment/secrets.js'
sh 'echo $PATH'
sh 'ls -la /usr/bin'
sh 'nohup npm run serve &' // Would run script here also to populate DB and create ES index
}
}
}
stage('Start React server') {
steps {
dir('[react folder]') {
sh 'nohup npm start &'
}
}
}
}
}
stage('Wait for servers to be ready') {
options {
timeout(time: 5, unit: 'MINUTES')
}
parallel {
stage('Wait for API server') {
steps {
sh 'while ! curl http://[redacted]:9001; do sleep 1; done'
}
}
stage('Wait for React server') {
steps {
sh 'while ! curl http://[redacted]:3000; do sleep 1; done'
}
}
}
}
stage('Run acceptance tests') {
steps {
dir('[react folder]') {
//sh 'npm run e2e' // Script to run CodeceptJS / Puppeteer tests
sh 'docker ps' // Failure - docker command not found
sh 'echo skipping tests'
}
}
}
}
}
是否可以與主機 Docker 和其他容器進行通信以完成我需要做的事情,還是爬出兔子洞並以不同的方法重新開始會更好?
您可以為每個階段指定不同的代理。 一個解決方案(或其中的一部分)是在階段的步驟之前聲明代理主節點('運行驗收測試')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.