【发布时间】:2019-06-20 08:26:27
【问题描述】:
我的目标是让 Jenkins 2 在 express js 应用程序和 postgres db 之间执行 alpha 集成测试。我将在本地启动容器化资源并使用使用docker-compose 的 bash 脚本成功测试。相关的 bash 脚本是scripts/docker/dockerRunTest.sh。
但是,当我尝试通过 Jenkins 做同样的事情时,Jenkins 声称找不到启动脚本。
Jenkinsfile
stage('Alpha Integration Tests') {
agent {
docker {
image 'tmaier/docker-compose'
args '-u root -v /var/run/docker.sock:/var/run/docker.sock --network host'
}
}
steps {
sh 'ls -lah ./scripts/docker/'
sh './scripts/docker/dockerRunTest.sh'
}
}
输出
+ ls -lah ./scripts/docker/
total 36
drwxr-xr-x 2 root root 4.0K Jan 26 21:31 .
drwxr-xr-x 6 root root 4.0K Jan 26 20:54 ..
-rwxr-xr-x 1 root root 2.2K Jan 26 21:31 docker.lib.sh
-rwxr-xr-x 1 root root 282 Jan 26 21:31 dockerBuildApp.sh
-rwxr-xr-x 1 root root 289 Jan 26 21:31 dockerBuildTestRunner.sh
-rwxr-xr-x 1 root root 322 Jan 26 21:31 dockerDown.sh
-rw-r--r-- 1 root root 288 Jan 26 21:31 dockerRestart.sh
-rwxr-xr-x 1 root root 482 Jan 26 21:31 dockerRunTest.sh
-rwxr-xr-x 1 root root 284 Jan 26 21:31 dockerUp.sh
+ ./scripts/docker/dockerRunTest.sh
/var/jenkins_home/workspace/project-name@2@tmp/durable-9ac0d23a/script.sh: line 1: ./scripts/docker/dockerRunTest.sh: not found
ERROR: script returned exit code 127
根据ls 输出,该文件显然存在。我有一些模糊的想法,shell 脚本和 bash 脚本的工作方式之间可能存在一些冲突,但我无法完全理解 Jenkins 无法执行明显存在的脚本的细微差别。
编辑(包括脚本内容):
dockerRunTest.sh
#!/bin/bash
MY_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd -P )"
MY_DIR="${MY_DIR:?}"
SCRIPTS_DIR="$(realpath "${MY_DIR}/..")"
ROOT_DIR="$(realpath "${SCRIPTS_DIR}/..")"
TEST_DIR="${ROOT_DIR}/test/integration"
SRC_DIR="${ROOT_DIR}/src"
REPORTS_DIR="${ROOT_DIR}/reports"
. "${SCRIPTS_DIR}/docker/docker.lib.sh"
dockerComposeUp
dockerExecuteTestRunner
dockerComposeDown
docker.lib.sh
#!/bin/bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd -P )"
CURRENT_DIR="${CURRENT_DIR:?}"
SCRIPTS_DIR="$(realpath "${CURRENT_DIR}/..")"
ROOT_DIR="$(realpath "${SCRIPTS_DIR}/..")"
. "${SCRIPTS_DIR}/lib.sh"
dockerComposeUp() {
docker-compose build --no-cache
docker-compose up --detach --force-recreate
DC_CODE=$?
if [ ${DC_CODE} -ne 0 ]; then
# Introspection
docker-compose logs
docker-compose ps
exit ${DC_CODE}
fi
}
dockerComposeDown() {
# docker-compose rm: Removes stopped service containers.
# -f, --force - Don't ask to confirm removal.
# -s, --stop - Stop the containers, if required, before removing.
# -v - Remove any anonymous volumes attached to containers.
docker-compose rm --force --stop -v
}
dockerComposeRestart() {
dockerComposeDown
dockerComposeUp
}
dockerBuildTestRunner() {
docker build -f test/Dockerfile -t kwhitejr/botw-test-runner .
}
dockerExecuteTestRunner() {
IMAGE_NAME="kwhitejr/botw-test-runner"
echo "Build new ${IMAGE_NAME} image..."
dockerBuildTestRunner
echo "Run ${IMAGE_NAME} executable test container..."
docker run -it --rm --network container:api_of_the_wild_app_1 kwhitejr/botw-test-runner
}
【问题讨论】:
-
你能在脚本中加入shebang吗?詹金斯上是否安装了命名解释器?我认为该错误与未找到的名称脚本本身略有不同。
-
在主帖中包含了相关的 bash 脚本。不确定什么是“命名解释器”?你是说 Bash vs vanilla shell?
-
是的。
/bin/bash必须可用才能正常工作。是吗?这将毫无疑问地证明这一点:ls -l /bin/bash -
通过 Docker Pipeline 插件,docker 命令在 Jenkins 中可能会更流畅。
-
@MattSchuchard 也许你可以提供一个答案来解释你在说什么?否则,这似乎是一种任意偏好。我很想看看这如何使它更顺畅。我喜欢 jenkinsfile 的一件事是,它可以很清楚地修改它,并且它将代码仓库与 jenkins exec 联系起来,无论如何它本质上是耦合的。
标签: bash docker jenkins docker-compose