【问题标题】:How to not run gitlab CI job if directory does not exist如果目录不存在,如何不运行 gitlab CI 作业
【发布时间】:2021-08-04 12:03:54
【问题描述】:

我想编写一个可从我们的全局构建模板中包含的模板,并将 repo 的静态文件夹复制到我的静态服务器。

全局构建模板(build.yml):

stages:
  - test
  - build
  - deploy
include:
  ...
  - local: '/ci-templates/publish_static.yml'

可以在所有 repos 中使用 (my-app/.gitlab-ci.yml):

include:
  - project: 'mycompany/ci-tools'
    file: '/ci-templates/build.yml'

鉴于回购结构:

my-app
│   .gitlab-ci.yml
└───myapp
    └───static

规则类似于:

publish_static:
    stage: deploy
    script:
        - export STATIC="$(find . -type d -name static)"
        - rsync -rvv $STATIC deploy@static.server.com:/www/static

不幸的是,并非所有 repos 都有静态文件夹,我找不到使用 rules:exists 的方法,因为静态文件夹不在与 $CI_PROJECT_NAME 匹配的子文件夹中。

我尝试使用 bash 逻辑:

publish_static:
    stage: deploy
    script:
        - export STATIC="$(find . -type d -name static)"
        - '[ -d $STATIC ] && rsync -rvv $STATIC deploy@static.server.com:/www/static'

[ -d .. ] 失败时导致作业失败。

有没有办法使用我忽略的rules:exists? ..或者即使$STATIC 目录不存在,是否有办法使最后一个命令“成功”? (我添加了allow_failure: true,但我们真的希望我们的管道上有全绿色的复选图标;-)

【问题讨论】:

  • not all repos have a static folder, and I can't find a way to use rules:exists since the static folder isn't in a subfolder that matches $CI_PROJECT_NAME. 我不明白那部分。你是什​​么意思“子文件夹匹配$CI_PROJECT_NAME”?你想使用通配符吗?
  • [ -d $STATIC ] && rsync... || true 呢?
  • @RenaudPacalet 可以工作:-)

标签: bash gitlab-ci


【解决方案1】:

并非所有 repos 都有静态文件夹,我找不到使用规则的方法:存在,因为静态文件夹不在与 $CI_PROJECT_NAME 匹配的子文件夹中。

做:

rules:
  - exists:
       - '*/static'

参见https://docs.gitlab.com/ee/ci/yaml/#rulesexists 中的Additional details

【讨论】:

  • 您需要在*/static 周围加上引号才能通过语法检查,但似乎rules:exists 只检查文件,而不是目录......(至少我的工作一直在运行)跨度>
猜你喜欢
  • 2021-06-07
  • 2021-11-17
  • 2021-01-20
  • 2019-03-31
  • 1970-01-01
  • 2018-12-30
  • 2021-03-08
  • 2019-05-22
  • 2022-10-13
相关资源
最近更新 更多