现有Bash脚本的Jenkins“找不到文件”错误

时间:2019-01-26 22:13:01

标签: bash docker jenkins docker-compose

我的目标是让Jenkins 2在快速js应用程序和postgres数据库之间执行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
}

2 个答案:

答案 0 :(得分:3)

tmaier/docker-compose映像默认情况下未安装/bin/bash解释程序,因为latest标签是alpine映像[1、2]。可以通过运行以下命令来确认:

$ docker run -it --rm tmaier/docker-compose bash
/usr/local/bin/docker-entrypoint.sh: exec: line 35: bash: not found

要使脚本正常工作,请使用apk add bash在docker映像中安装bash,或者如果可以使用#!/bin/sh shell(脚本中的默认shell)运行脚本,请将shebang更改为ash。 busybox)。

[1] https://github.com/tmaier/docker-compose/blob/b740feb61fb25030101638800a609605cfd5e96a/Dockerfile#L2

[2] https://github.com/docker-library/docker/blob/d94b9832f55143f49e47d00de63589ed41f288e7/18.09/Dockerfile#L1

答案 1 :(得分:0)

我遇到了类似的问题,但就我而言,这是因为shell脚本文件的Windows格式为EOL(如果您使用vi在终端中打开文件,则会看到每行以{{ 1}})

我可以使用Notepad ++ Edit-> EOL Conversion-> Unix(LF)来解决此问题