我在使用 gitlab-runner 执行任务时遇到了麻烦,不知道我做错了什么。它运行在 Kubernetes 中,但可能与 Docker 或 Kubernetes 或其他任何东西无关。
/bin/bash: line 1: /scripts-1002-1043121/prepare_script: Permission denied
在实际的 pod 中执行:
runner-qeaugn8yj-project-1002-concurrent-0-m5ikq7m3:/# ps -auxwww
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 2416 1784 ? Ss 10:21 0:00 /bin/bash
root 15 0.0 0.0 2740 2464 pts/0 Ss 10:50 0:00 bash
root 21 0.0 0.0 1740 788 pts/0 R+ 10:50 0:00 ps -auxwww
runner-qeaugn8yj-project-1002-concurrent-0-m5ikq7m3:/# ls -lahtr /scripts-1002-1043121/prepare_script
-rwxrwxrwx 1 root root 434 Oct 4 10:21 /scripts-1002-1043121/prepare_script
runner-qeaugn8yj-project-1002-concurrent-0-m5ikq7m3:/# /scripts-1002-1043121/prepare_script
bash: /scripts-1002-1043121/prepare_script: Permission denied
runner-qeaugn8yj-project-1002-concurrent-0-m5ikq7m3:/# bash /scripts-1002-1043121/prepare_script
{"script": "/scripts-1002-1043121/prepare_script"}
Running on runner-qeaugn8yj-project-1002-concurrent-0-m5ikq7m3 via gitlab-runner-7b66fddb55-tr6b5...
{"command_exit_code": 0, "script": "/scripts-1002-1043121/prepare_script"}
该脚本以 root 身份运行,上面的输出也是如此。
runner-qeaugn8yj-project-1002-concurrent-0-m5ikq7m3:/# cat /scripts-1002-1043121/prepare_script
#!/usr/bin/env bash
start_json="{\"script\": \"$0\"}"
echo "$start_json"
...
答案1
有时系统安全会阻止脚本在某些目录中运行。要解决这个问题,您可以尝试在其他位置运行脚本或在脚本名称前键入 bash。这将调用具有不同文件权限的子 shell。
bash /scripts-1002-1043121/prepare_script