[英]Jenkins - communicate from Docker agent to other Docker containers
I am running a Jenkins job using a buildkite/puppeteer Docker agent.我正在使用 buildkite/puppeteer Docker 代理运行 Jenkins 作业。 As I understand it, this creates a container from this image and runs the job inside it.据我了解,这将从该图像创建一个容器并在其中运行作业。 The job pulls a React frontend and Express backend app from their respective repos, installs their dependencies, starts them and runs an npm
script to kick off CodeceptJS / Puppeteer end to end tests, which reside in the front end repo.该作业从各自的存储库中提取 React 前端和 Express 后端应用程序,安装它们的依赖项,启动它们并运行npm
脚本以启动 CodeceptJS / Puppeteer 端到端测试,这些测试位于前端存储库中。
Before the tests are run I need to populate a MongoDB instance with test data (old test data should be deleted) and index an Elasticsearch instance (old index should be deleted).在运行测试之前,我需要用测试数据填充 MongoDB 实例(应删除旧测试数据)并索引 Elasticsearch 实例(应删除旧索引)。 To do this locally, I have a MongoDB and Elasticsearch container running, and a bash script does the necessaries before I run the tests by running docker
commands and curl
requests;为了在本地执行此操作,我运行了一个 MongoDB 和 Elasticsearch 容器,并且在我通过运行docker
命令和curl
请求运行测试之前,一个 bash 脚本完成了必要的工作; however, I'm having difficulty understanding how I can do this in Jenkins.但是,我很难理解如何在 Jenkins 中做到这一点。 The Jenkins host machine has Docker containers for Mongo and Elasticsearch running, but the Jenkins job is unable to communicate with them - logs show that the docker command is not found, and curl requests to the ES container fail. Jenkins 主机有运行 Mongo 和 Elasticsearch 的 Docker 容器,但是 Jenkins 作业无法与它们通信 - 日志显示找不到 docker 命令,并且向 ES 容器发出 curl 请求失败。 Here is what my Jenkins pipeline code looks like - I have had to redact some information:这是我的 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'
}
}
}
}
}
Is it possible to communicate with host Docker and other containers to do what I need to or would it be better to climb out of the rabbit hole and start again with a different approach?是否可以与主机 Docker 和其他容器进行通信以完成我需要做的事情,还是爬出兔子洞并以不同的方法重新开始会更好?
you can specify a different agent per stage.您可以为每个阶段指定不同的代理。 A solution (or part of it) would be to declare agent master just before the steps of the stage('Run acceptance tests')一个解决方案(或其中的一部分)是在阶段的步骤之前声明代理主节点('运行验收测试')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.