是的,GitLab 会覆盖提供给容器的命令;图像中定义的CMD 不会被执行。该命令是 GitLab 设置作业容器以执行作业脚本的方式。
如何实现在gitlab作业中自动执行图片的cmd
您可以使用图像的ENTRYPOINT 来确保在 GitLab CI 中自动执行命令,但是您的入口点必须准备好运行作为命令传递的 shell 脚本(例如,在您的末尾使用 exec /bin/bash入口点,如果入口点是 bash 脚本)。
如果您在 CI 中执行时需要图像的不同行为,您可以根据您的作业中是否存在 $CI 环境变量或任何其他 pre-defined 或用户定义的环境变量来设置此条件。
#!/usr/bin/env bash
# my-entrypoint script
echo "doing something before running commands"
if [[ -n "$CI" ]]; then
echo "this block will only execute in a CI environment"
supervisorctl restart all || echo "could not restart" # or whatever you want
echo "now running script commands"
# this is how GitLab expects your entrypoint to end, if provided
# will execute scripts from stdin
exec /bin/bash
else
echo "this block will only execute in NON-CI environments"
# execute the command as if passed to the container normally
exec "$@"
fi
然后在您的 dockerfile 中,您可以执行以下操作:
COPY my-entrypoint /my-entrypoint
RUN chmod +x /my-entrypoint # Optional if you set executable bit in filesystem
ENTRYPOINT ["/my-entrypoint"]
CMD ["/usr/bin/supervisord"] # the default command, when no other is provided
请注意,您也可以在 GitLab 配置中覆盖入口点:
my_job:
image:
name: "python:3.9-slim"
entrypoint: ["/bin/bash", "-c", "echo this executed before the job; exec /bin/bash"]
script:
- echo "hello"
- "echo 123"
可以在运行器配置中禁用覆盖入口点的功能。