简体   繁体   English

GitLab CI - 添加标签时避免构建

[英]GitLab CI - avoid build when adding tag

How do I prevent a gitlab ci pipeline being triggered when I add a git tag?添加 git 标签时,如何防止触发 gitlab ci 管道? I'm running this command locally (as opposed to within a gitlab-ci job)我在本地运行这个命令(而不是在 gitlab-ci 作业中)

git tag -a "xyz"

and then pushing the tag;然后推标签; and this triggers various pipelines.这会触发各种管道。 I want to exclude some of those pipelines from running.我想排除其中一些管道的运行。

I'm trying variations on ideas from questions such as this ;我正在尝试从诸如此类的问题中改变想法 that question is using only , I'm wanting to exclude, so I'm trying except.这个问题使用 ,我想排除,所以我正在尝试除外。 The answers there have two variants, one with refs one without.那里的答案有两种变体,一种有参考文献,一种没有。

build:  
  # ... my work here ...  
  except:
    - tags


build:  
  # ... my work here ...  
  except:
    refs:
      - tags

Neither seem to have any effect;两者似乎都没有任何效果; I add a tag, the build still happens.我添加了一个标签,构建仍然发生。

My understanding may be completely awry here as there seem to be three possible meanings of the word tags and when reading docs or examples I'm not always sure which meaning is applicable:我的理解在这里可能完全错误,因为单词标签似乎有三种可能的含义,在阅读文档或示例时,我并不总是确定哪个含义适用:

  1. Git tags applied using git tag使用 git 标签应用Git 标签
  2. Gitlab CI tags used to determine which runners pick a job Gitlab CI 标签用于确定哪些跑步者选择工作
  3. The ref identifier of a commit used to trigger a pipeline via the REST API. This is usually a branch name, but could be a git tag.用于通过 REST API 触发管道的提交的ref标识符。这通常是分支名称,但也可以是 git 标记。

I'm interested in controlling what happens if the first case.我有兴趣控制第一种情况下会发生什么。 It does seem clear from comments so far that "except: -tags" is not relevant to my case, so is there any approach that does work?到目前为止,从评论中可以清楚地看出“except: -tags”与我的案例无关,那么有没有什么方法可行呢?

Except tags is exactly what you should use if you want to skip build for tags.如果你想跳过标签的构建, Except tags正是你应该使用的。

You need to be sure to understand commit vs branches vs tags您需要确保了解提交 vs 分支 vs 标签

To illustrate what happens when you push tagged commit to gitlab I did as follows:为了说明将标记的提交推送到 gitlab 时会发生什么,我执行了以下操作:

  1. Created .gitlab-ci.yml with following content:创建了.gitlab-ci.yml ,内容如下:
tests_always_run:
    script:
      - echo I should always execute
tests_except_tags:
    script:
      - echo I skip tagged triggers
    except:
      - tags
  1. Commited changes, tagged commit and pushed with --follow-tags to make sure tag is also propagated to server:提交更改,标记提交并使用--follow-tags推送以确保标记也传播到服务器:
git add .gitlab-ci.yml
git commit -m 'my great yml with except tags'
git tag -a "abc" -m "Test tag"
git push --follow-tags

Ilustrated results:图示结果: 标记的提交管道结果

If you want to skip CI for selected commit then you could use git push -o ci.skip , inspired by this article如果你想跳过选定提交的 CI,那么你可以使用git push -o ci.skip ,灵感来自这篇文章

It looks like GitLab recommends using rules instead of except as per the documentation它看起来像GitLab建议使用rules ,而不是except文件

only and except are not being actively developed. only 和 except 没有被积极开发。 rules is the preferred keyword to control when to add jobs to pipelines. rules 是控制何时向管道添加作业的首选关键字。

So it'd be所以它会

your_job:
  stage: your_stage
  script:
    - echo "Hello"
  rules:
    - if: $CI_COMMIT_TAG
      when: never 
    - when: always

(note : this a formatted comment more than an answer) (注意:这是一个格式化的评论而不是一个答案)

In order to debug the conditions that trigger your pipeline :为了调试触发管道的条件:

gitlab's doc mentions several variables which are set when running a CI job, among which : gitlab 的文档提到了在运行 CI 作业时设置的几个变量,其中:

  • CI_COMMIT_REF_NAME : The branch or tag name for which project is built CI_COMMIT_REF_NAME :为其构建项目的分支或标记名称
  • CI_COMMIT_BRANCH : The commit branch name. CI_COMMIT_BRANCH :提交分支名称。 Present only when building branches.仅在构建分支时出现。
  • CI_COMMIT_TAG : The commit tag name. CI_COMMIT_TAG :提交标签名称。 Present only when building tags.仅在构建标签时出现。

Have your build job output some of these variables (eg : echo "triggered by ref : " $CI_COMMIT_REF_NAME ) to view what triggered your job.让您的构建作业输出其中一些变量(例如: echo "triggered by ref : " $CI_COMMIT_REF_NAME )以查看触发您的作业的原因。

I was in the same situation, my solution was this:我遇到了同样的情况,我的解决方法是:

Before::前:: 前

After:后: 后

Both stages are configured in my.gitlab-ci.yml file, with different name The " Dev-UnitTests ", it only executes when someone commits to the repository, no effect on tags y the branch " test "这两个阶段都在 my.gitlab-ci.yml 文件中配置,具有不同的名称“ Dev-UnitTests ”,它仅在有人提交到存储库时执行,对分支“ test ”的标签没有影响

Dev-UnitTests:
  stage: pruebas 
  script:
    - mvn $MAVEN_CLI_OPTS test
  artifacts:
    when: always
    reports:
      junit: 
        - target/surefire-reports/*Test.xml
        - target/failsafe-reports/*Test.xml
      cobertura: target/site/jacoco/jacoco.xml
  tags:
    - shell 
  except: 
    - test 
    - tags   

The Unit Tests , only run when a merge is done on the branch test单元测试,仅在分支测试上完成合并时运行

Unit Tests:
  stage: pruebas 
  script:
    - mvn $MAVEN_CLI_OPTS test
  artifacts:
    when: always
    reports:
      junit: 
        - target/surefire-reports/*Test.xml
        - target/failsafe-reports/*Test.xml
      cobertura: target/site/jacoco/jacoco.xml
  tags:
    - shell 
  only: 
    - test

That did not run again any pipeline when creating a tag, I hope it helps you.创建标签时没有再次运行任何管道,希望对您有所帮助。

The key is:关键是:

...
 except:  
    - tags   
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM