英文:
GitLab jobs:build config key may not be used with `rules`: only
问题
我有一个GitLab CI作业,我希望只在tags或两个特殊分支(develop,main)上运行。
only:
- develop
- main
- tags
此作业具有使用rules功能设置的动态变量:
variables:
IMAGE_TAG: $CI_COMMIT_TAG # 默认为推送的提交标签名称
rules:
- if: $CI_COMMIT_REF_NAME == 'develop'
variables:
IMAGE_TAG: 'develop'
- if: $CI_COMMIT_REF_NAME == 'main'
variables:
IMAGE_TAG: 'main'
然而,linter给了我这个错误:
> jobs:build config key may not be used with rules: only
我查了一下,看起来你不能在rules中同时使用only。
如果我移除only指令,那么这个作业将在任何分支上运行。我该如何调整我的脚本,以便我可以动态设置变量,但也限制作业只在标签和特定分支上运行?
英文:
I have a GitLab CI job that I want to only run for tags or for two special branches (develop, main).
only:
- develop
- main
- tags
This job has a dynamic variable that I set using the rules feature:
variables:
IMAGE_TAG: $CI_COMMIT_TAG # defaults to the pushed commit tag name
rules:
- if: $CI_COMMIT_REF_NAME == 'develop'
variables:
IMAGE_TAG: 'develop'
- if: $CI_COMMIT_REF_NAME == 'main'
variables:
IMAGE_TAG: 'main'
The linter gives me this error though:
> jobs:build config key may not be used with rules: only
I looked around and it looks like you can't use rules together with only.
If I remove the only directive, then this job will also run on any branch. How do I adjust my script so that I can set the variable dynamically but also limit the job to only run on tags and certain branches?
答案1
得分: 2
The equivalent rules: to this would be:
rules:
- if: $CI_COMMIT_BRANCH == "develop"
# ... insert `variables:` here if you want...
- if: $CI_COMMIT_BRANCH == "main"
# ...
- if: $CI_COMMIT_TAG
In this case, you also don't need to set your IMAGE_TAG variable conditionally. You can just use $CI_COMMIT_REF_NAME which will be equal to $CI_COMMIT_BRANCH for branch pipelines or $CI_COMMIT_TAG for tag pipelines.
英文:
The equivalent rules: to this would be:
rules:
- if: $CI_COMMIT_BRANCH == "develop"
# ... insert `variables:` here if you want...
- if: $CI_COMMIT_BRANCH == "main"
# ...
- if: $CI_COMMIT_TAG
In this case, you also don't need to set your IMAGE_TAG variable conditionally. You can just use $CI_COMMIT_REF_NAME which will be equal to $CI_COMMIT_BRANCH for branch pipelines or $CI_COMMIT_TAG for tag pipelines.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论