Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

git - Trigger via commit message

I am trying to build a Gitlab CI yml, that triggers a certain job:

  • when the commit is tagged in the form x.y.z
  • I trigger manually via the Gitlab webapp
  • the commit message ends with a certain regex ("-build_doc")

with the bullets being logical or statements. I tried only statements and rules alike but couldn't figure it out. Web and tag already worked like so:

script:
  ...
only:
  - /^v(d+).(d+).(d+)$/ #tag
  - web

I also found this issue, which allows the commit message condition via only:variables, but now I struggled with the other two...

My trial with rules:

script:
  ...
rules:
  - if: '$CI_PIPELINE_SOURCE == "web"'
  - if: '$CI_COMMIT_MESSAGE =~ /^.*-build_doc$/'
  - if: $CI_COMMIT_TAG

Any advice? Thanks!!

question from:https://stackoverflow.com/questions/65887312/trigger-via-commit-message

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You won't be able to get your desired result using the only syntax due to the way it's evaluated. The example you have above is a simple/shorthand way of using the refs keyword, so it is the same thing as

only:
  refs:
    - /^v(d+).(d+).(d+)$/ #tag
    - web

For this alone, the job will be added to the pipeline if any of the refs matches. When combined with the other keywords (like variables or kubernetes), the keywords are combined using AND's. So with this example, one of the refs must match, AND the variable must match. There's no way to make it an OR.

only:
  refs:
    - /^v(d+).(d+).(d+)$/ #tag
    - web
  variables:
    - $CI_COMMIT_MESSAGE =~ /^my message/

You should be able to get your desired result using the newer rules syntax though. If you have multiple if statements, they are OR'ed together, not AND'ed. An example of how if's are evaluated are in the docs here: https://docs.gitlab.com/ee/ci/yaml/#rules-clauses. In a single if statement, you can combine conditions using || for OR and && for AND. You can see examples of that here: https://docs.gitlab.com/ee/ci/yaml/#rulesif

So for this use case, I'd try either

rules:
  - if: '$CI_PIPELINE_SOURCE == "web" || $CI_COMMIT_MESSAGE =~ /^my message/i || $CI_COMMIT_REF_NAME =~ /^v(d+).(d+).(d+)$/'
    when: always

You're not required to add the when clause to your if statements, and if you don't it will use the when you put on the job, or use the default on-success.

Also, all the pre-defined variables you can use are listed here: https://docs.gitlab.com/ee/ci/variables/predefined_variables.html.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...