如何根据不同的运行器跳过 gitlab-ci.yml 中的作业?

发布于 2025-01-15 11:36:48 字数 1102 浏览 4 评论 0原文

我使用 gitlab 作为我的 CI/CD 系统。我需要设置我自己的跑步者。 我有两个跑步者,分别是带有“powershell”的“windows”和带有“shell”的“macos”。我希望他们中的任何一个都可以运行我的构建工作。 “macos”是一台笔记本电脑,因此它并不总是在线。因此,我编写了两个版本的构建作业,并希望为不同的运行器执行其中之一。

当我使用运行者标签时,这两个作业都会执行。并且构建将执行两次。如果我的“macos”离线,build-mac 将卡住。

build-win:
  stage: build
  tags:
    - windows
  script: powershell command...

build-mac:
  stage: build
  tags:
    - macos
  script: shell command...

所以我认为我需要“跳过”这份工作,但不选择跑步者。所以我尝试使用 onlyrules 来跳过该作业。我找到链接: https://gitlab.com/gitlab-org/gitlab //问题/21860 但正如问题所述,CI_RUNNER_EXECUTABLE_ARCH 不能在 onlyexceptrules 中使用。

build-win:
  stage: build
  script: powershell command...
  rules:
    - if: $CI_RUNNER_EXECUTABLE_ARCH =~ /^windows.*/

build-mac:
  stage: build
  script: shell command...
  rules:
    - if: $CI_RUNNER_EXECUTABLE_ARCH =~ /^(darwin|linux).*/

我认为这两种解决方案都不合适。有没有更好的方法来做到这一点? 谢谢~

I am using the gitlab as my CI/CD system. And I need setup my own runner.
I have two runner both 'windows' with 'powershell' and 'macos' with 'shell'. I want either of them can run my build job. The 'macos' is a laptop so it not always online. So I write two version of build jobs, and hope just execute one of them for different runner.

When I use the runner tags, both of the job will execute. And the build will execute twice. If my 'macos' is offline, the build-mac will stuck.

build-win:
  stage: build
  tags:
    - windows
  script: powershell command...

build-mac:
  stage: build
  tags:
    - macos
  script: shell command...

So I think I need 'skip' the job but not select a runner. So I am trying to use only or rules to skip the job. I find the linkage: https://gitlab.com/gitlab-org/gitlab/-/issues/21860
But as the issues said, the CI_RUNNER_EXECUTABLE_ARCH can not use in only, except and rules.

build-win:
  stage: build
  script: powershell command...
  rules:
    - if: $CI_RUNNER_EXECUTABLE_ARCH =~ /^windows.*/

build-mac:
  stage: build
  script: shell command...
  rules:
    - if: $CI_RUNNER_EXECUTABLE_ARCH =~ /^(darwin|linux).*/

I think both of the solution is not suitable. Is there any better way to do this?
Thank you~

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萤火眠眠 2025-01-22 11:36:48

预定义变量中没有看到任何选项,例如跑步者在线状态。

如果您想根据跑步者状态建立跑步规则,您可能必须利用个人令牌(或者可能是项目令牌)来访问跑步者 api 来获取状态。你可以这样做:

stages:
  - check-runner
  - run

image: python

check runner:
  stage: check-runner
  script:
    - curl --header "PRIVATE-TOKEN:$READ_RUNNERS_TOKEN" "$CI_API_V4_URL/runners?tag_list=unreliable"
    - |
      echo RUNNER_STATUS=$(\
      curl --header "PRIVATE-TOKEN:$READ_RUNNERS_TOKEN" "$CI_API_V4_URL/runners?tag_list=macos" \
      | python -c "import sys, json; print(json.load(sys.stdin)[0]['status'])" \
      ) >> .env
  artifacts:
    reports:
      dotenv: .env

use tagged runner:
  stage: run
  script:
    - echo $RUNNER_STATUS
  dependencies:
    - check runner
  rules:
    - if: '$RUNNER_STATUS == "online"'
  tags:
    - macos

user other runner:
  stage: run
  script:
    - echo "used runner"
  dependencies:
    - check runner
  rules:
    - if: '$RUNNER_STATUS != "online"'
  tags:
    - windows

Not seeing any options in predefined variables for anything like runner online status.

If you want to base a run rule by runner status, you will probably have to leverage a personal token (or maybe a project token) to hit the runners api for status. You could do something like this:

stages:
  - check-runner
  - run

image: python

check runner:
  stage: check-runner
  script:
    - curl --header "PRIVATE-TOKEN:$READ_RUNNERS_TOKEN" "$CI_API_V4_URL/runners?tag_list=unreliable"
    - |
      echo RUNNER_STATUS=$(\
      curl --header "PRIVATE-TOKEN:$READ_RUNNERS_TOKEN" "$CI_API_V4_URL/runners?tag_list=macos" \
      | python -c "import sys, json; print(json.load(sys.stdin)[0]['status'])" \
      ) >> .env
  artifacts:
    reports:
      dotenv: .env

use tagged runner:
  stage: run
  script:
    - echo $RUNNER_STATUS
  dependencies:
    - check runner
  rules:
    - if: '$RUNNER_STATUS == "online"'
  tags:
    - macos

user other runner:
  stage: run
  script:
    - echo "used runner"
  dependencies:
    - check runner
  rules:
    - if: '$RUNNER_STATUS != "online"'
  tags:
    - windows

向日葵 2025-01-22 11:36:48

您可以使用 $CI_RUNNER_TAGS 或 $CI_RUNNER_ID 预定义变量

示例:

rules:
- if: $CI_RUNNER_TAGS == windows

或:

rules:
- if: $CI_RUNNER_TAGS == macos

You can use the $CI_RUNNER_TAGS or $CI_RUNNER_ID predefined variables.

Example:

rules:
- if: $CI_RUNNER_TAGS == windows

Or:

rules:
- if: $CI_RUNNER_TAGS == macos
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文