小编Mar*_*n W的帖子

Gitlab CI 管道 - 仅在特定条件下继续进入下一阶段

我正在尝试构建一个由 4 个作业组成的 Gitlab 管道。我的阶段是:

stages:
- compare
- build
- test
- deploy
Run Code Online (Sandbox Code Playgroud)

比较阶段从另一台服务器上的 API 获取转储,将其与上次成功管道运行的相同转储进行比较(它作为工件提供),然后比较两者。如果有任何区别,我希望管道进入下一阶段,如果没有区别,那么我希望它优雅地退出。

我让它工作,但如果没有差异,它会失败并且管道被标记为失败,而不是正常退出,这是它的外观。

在此处输入图片说明

这是我的 .gitlab-ci.yaml 中的重要代码(删除了一些识别信息)

Get_inventory_dump:
  stage: compare  
  only:
    - schedules
  script: 
    - 'curl -k --output "previous-inventory.json" --header "PRIVATE-TOKEN: $user_token" "https://url/to/get/artifact/from/last/successful/run"'
    - python3 auto_config_scripts/dump_device_inventory_api_to_json.py -p $pass -o /inventory.json -u https://url/for/inventory/dump -y
    - /usr/bin/cmp previous-inventory.json inventory.json && echo "No Change in inventory since last successful run" && exit 1 || echo "Inventory has changed since last run, continue" && exit 0
  artifacts:
    when: on_success
    expire_in: …
Run Code Online (Sandbox Code Playgroud)

gitlab gitlab-ci

9
推荐指数
2
解决办法
7894
查看次数

嵌套的`if`语句没有评估操作的结果

我正在临时目录中生成一个文件.如果要生成的文件与现有文件不同,那么我不想更新它,并运行命令.如果它是相同的,那么我什么也做不了.

我的代码看起来像这样:

errstatus = 0
if FileUtils.identical?('/var/tmp/newfile', '/var/tmp/originalfile')
  $stderr.puts "Files are the same, nothing to do"
else
  $stderr.puts "Files are different, let's update them."

  if FileUtils.cp_r '/var/tmp/newfile', '/var/tmp/originalfile'
    $stderr.puts "File copied successfully."

    if system('systemcommand1 here')
      $stderr.puts "command ran OK"

      if system('systemcommand2 here')
        $stderr.puts "next command ran ok"

      else
        $stderr.puts "command 2 failed"
        errstatus = 1
      end
    else
      $stderr.puts "command 1 failed"
      errstatus = 1
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

当我在文件不同时运行它时,我得到输出:

Files are different, let's update them.
Run Code Online (Sandbox Code Playgroud)

它运行时FileUtils.cp_r没有任何错误,但它不会告诉文件已成功复制,或运行系统命令.我使用相同的语法来评估FileUtils.identical?,但它不起作用FileUtils.cp_r …

ruby

0
推荐指数
1
解决办法
50
查看次数

标签 统计

gitlab ×1

gitlab-ci ×1

ruby ×1