尝试通过 GitLab CI CD 在 docker-容器中执行脚本的权限被拒绝

Permission denied trying to execute script in docker-container through GitLab CI CD

在我的本地机器上一切都运行良好。但是,当我将代码推送到 GitLab 时,出现以下错误:

来自 GitLab 作业查看器

GUI                | sh: 1: /usr/src/app/test.startup.sh: Permission denied
GUI exited with code 126

这是我的设置:

gui/Dockerfile

#cypress image is needed for automated testing, for production a simple node image is enough
FROM cypress/browsers:node14.16.0-chrome89-ff86

ENV PORT 3000

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

COPY package*.json /usr/src/app/

EXPOSE 3000

COPY test.startup.sh /usr/src/app/test.startup.sh

COPY startup.sh /usr/src/app/startup.sh

# the following 4 lines were added to try and solve the problem, they did not. On my local machine it runs fine even without them
RUN chmod 777 /usr
RUN chmod 777 /usr/src
RUN chmod 777 /usr/src/app
RUN chmod 777 /usr/src/app/test.startup.sh

ENTRYPOINT []

docker-compose.testing.yml

version: '3.7'

services:

  GUI:
    network_mode: host
    build: "./gui"
    container_name: GUI

    volumes:
      - "./gui:/usr/src/app"
      - /usr/src/app/node_modules
      - /usr/src/app/.next

    depends_on:
      - rhasspy
      - rhasspy_de
      - rhasspy_adapter

    command: sh -c "/usr/src/app/test.startup.sh"

.gitlab-ci.yml

application:
  stage: application_test
  image: docker
  services: 
    - docker:dind
  script:
    - apk add --no-cache docker-compose
    - docker-compose --file docker-compose.testing.yml build
    - docker-compose --file docker-compose.testing.yml up --abort-on-container-exit

我没有想法所以非常感谢任何帮助, 谢谢

在您的 gitlab-ci.yml 中,您使用 docker-compose(这本身很糟糕,但不是问题的主题)。

在您的 docker-compose.yml 中,您将 gui 目录挂载为 /usr/src/app。因此,该目录中的构建映像中的内容无关紧要,所有内容都将替换为 git 工作副本的 gui 目录的内容。

要使其在 CI 中运行,您需要确保您的脚本在 git 树中可执行。您需要将其作为可执行文件推送。

示例:

~/pipes $ cat gui/test.script.sh
#!/bin/sh

echo "I'm a script"

~/pipes $ ls -l gui/test.script.sh 
-rw-rw-r--. 1 test test 32 Dec 22 01:47 gui/test.script.sh

~/pipes $ chmod +x gui/test.script.sh 

~/pipes $ ls -l gui/test.script.sh 
-rwxrwxr-x. 1 test test 32 Dec 22 01:47 gui/test.script.sh

~/pipes $ git commit gui/test.script.sh -m 'Now executable.'
[feature-123 b524b0d] Now executable.
 1 file changed, 0 insertions(+), 0 deletions(-)
 mode change 100644 => 100755 gui/test.script.sh

~/pipes $ git push

我通过 运行 docker-compose 文件中的所有命令解决了这个问题

docker-compose.testing.yml

version: '3.7'

services:

  GUI:
    network_mode: host
    build: "./gui"
    container_name: GUI

    volumes:
      - "./gui:/usr/src/app"
      - /usr/src/app/node_modules
      - /usr/src/app/.next

    depends_on:
      - rhasspy
      - rhasspy_de
      - rhasspy_adapter
    
    entrypoint: ["/bin/sh","-c"]
    command: 
      - | 
        npm ci
        npm run start:ci &
        sleep 5
        npm run test:ci