如何让GitLab Runner读取指定文件替代.gitlab-ci.yml?
Great question! The short answer is yes—you absolutely can get GitLab Runner (even when using the deprecated gitlab-runner exec docker command) to read configuration files beyond the default .gitlab-ci.yml. Here are a few practical approaches to make this work:
Specify an alternate main config file via command line
Use the--configflag to tell the runner to use a different YAML file instead of.gitlab-ci.yml. For example:sudo gitlab-runner exec docker --config ./my-custom-ci-config.yml [job name]If you want to keep using
.gitlab-ci.ymlbut add extra configs, newer Runner versions support the--includeflag to pull in additional YAML files:sudo gitlab-runner exec docker --include ./ci-shared-templates.yml [job name]Leverage the
includekeyword in .gitlab-ci.yml
The standard GitLab CIincludedirective works perfectly with localexecruns too. Just add references to your external config files directly in your main.gitlab-ci.yml:include: - local: './ci-configs/deployment-jobs.yml' - local: './ci-configs/testing-templates.yml'When you run the
exec dockercommand, the runner will parse all included files alongside the main config, just like it would on GitLab's hosted runners.Mount non-YAML config files into the Docker container
If your jobs need access to non-YAML files (like environment configs, script files, or tool settings), use the--volumeflag to mount them from your host machine into the runner's Docker container:sudo gitlab-runner exec docker --volume /home/you/app-config.ini:/app/config.ini [job name]Your job scripts can then read this external file as if it were natively inside the container.
Note: As you pointed out,
gitlab-runner execis marked as deprecated, and there's no official replacement available yet. That said, all the methods above still work with the current version of GitLab Runner, so you can rely on them for local testing until a new solution is rolled out.
内容的提问来源于stack exchange,提问作者Kalle Richter

