如何将 exe 输出分配给 gitlab ci 脚本中的变量? [英] How do I assign exe output to a variable in gitlab ci scripts?

查看:17
本文介绍了如何将 exe 输出分配给 gitlab ci 脚本中的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在运行我的 gitlab ci 时,我需要检查指定的 svn 目录是否存在.

When running my gitlab ci I need to check whether a specified svn directory exists.

我正在使用脚本:

variables:
  DIR_CHECK: "default"

stages:
  - setup
  - test
  - otherDebugJob
  
.csharp:
  only:
    changes:
      - "**/*.cs"
      - "**/*.js"

setup:
  script:
    - $DIR_CHECK = $(svn ls https://server.fsl.local:port/svn/myco/personal/TestNotReal --depth empty)
    - echo $DIR_CHECK
test:
  script:
    - echo "DIR_CHECK is blank"
    - echo $DIR_CHECK
  rules:
    - if: $DIR_CHECK == ''

otherDebugJob:
  script:
    - echo "DIR_CHECK is not blank"
    - echo $DIR_CHECK
  rules:
    - if: $DIR_CHECK != ''
    

svn 命令有效并回显正确的回复,但$DIR_CHECK 没有设置为原始默认.它不存储从 svn 命令返回的字符串.

the svn command works and echos back the correct reply but $DIR_CHECK does not get set to anything but the original default. It does not store the returned string from the svn command.

如何将 exe 返回的字符串存储在 gitlab ci 的变量中?

How do I store the returned string from an exe in a variable in gitlab ci?

试运行:

执行step_script";作业脚本阶段 00:00 $ $DIR_CHECK =$(svn ls https://server.fsl.local:port/svn/myco/personal/TestNotReal--depth empty) svn: E170000: Illegal repository URL https://server.fsl.local:port/svn/myco/personal/TestNotReal' $ echo$DIR_CHECK 清理基于文件的变量 00:01 作业成功

Executing "step_script" stage of the job script 00:00 $ $DIR_CHECK = $(svn ls https://server.fsl.local:port/svn/myco/personal/TestNotReal --depth empty) svn: E170000: Illegal repository URL https://server.fsl.local:port/svn/myco/personal/TestNotReal' $ echo $DIR_CHECK Cleaning up file based variables 00:01 Job succeeded

推荐答案

作业之间传递变量

很遗憾,您不能按照您描述的方式使用 DIR_CHECK 变量.要执行的步骤列表在步骤实际运行之前生成,这意味着对于所有步骤 DIR_CHECK 将等于 default.首先,有一些技巧可以在作业之间传递变量:

Passing variables between jobs

Unfortunately, you cannot use DIR_CHECK variable the way you described. List of steps to be executed generates before steps actually runs, that means for all of the steps DIR_CHECK will be equal to default. First of all there are few tips how you can pass variables between jobs:

第一种方式

您可以将所需的命令添加到 .csharp 模板中的 before_script 部分:

You can add desired command to the before_script section in your .csharp template:

.csharp:
  before_script:
    - export DIR_CHECK=$(svn ls https://server.fsl.local:port/svn/myco/personal/TestNotReal --depth empty)

并使用此 .csharp 扩展其他步骤.

and extend other steps with this .csharp.

第二种方式

您可以使用作业工件在作业之间传递变量:

You can pass variables between jobs with job artifacts:

setup:
  stage: setup
  script:
    - DIR_CHECK=$(svn ls https://server.fsl.local:port/svn/myco/personal/TestNotReal --depth empty)
    - echo "DIR_CHECK=$DIR_CHECK" > dotenv_file
  artifacts:
    reports:
      dotenv: 
        - dotenv_file

第三种方式你可以 trigger 或使用 父/子管道将变量传递到管道中.

Thirds way You can trigger or use parent/child pipelines to pass variables into pipelines.

staging:
  variables:
    DIR_CHECK: "you are awesome, guys!"
  stage: deploy
  trigger: my/deployment

在触发的管道中,您的变量将在一开始就存在,并且所有规则都将正确应用.

In the triggered pipeline your variable will exists at the very start moment, and all the rules will be applied correctly.

在您的情况下,如果您真的不想在管道中包含 otherDebugJob 步骤,您可以执行以下操作:

In your case, if you really don't want to include otherDebugJob step in your pipeline you can do the following:

第一种方法

这是一种非常简单的方法,并且可行,但看起来不是最佳做法.因此,我们已经知道如何从 setup 步骤传递我们的 DIR_CHECK 变量,只需在 test 步骤 script<中添加一些检查/code> 块:

This is quite easy way and this will work, but looks like not a best practice. So, we are already know how to pass our DIR_CHECK variable from setup step , just add some check in the test step script block:

script:
- |
  if [ -z "$DIR_CHECK" ]; then
    exit 0
  fi
- echo "DIR_CHECK is blank"
- echo $DIR_CHECK

otherDebugJob 执行几乎相同的操作,但使用 if [ -n "$DIR_CHECK" 检查 DIR_CHECK 是否为空].

Do the almost same thing for the otherDebugJob but check if DIR_CHECK is not empty with if [ -n "$DIR_CHECK" ].

当您的管道不包含很多步骤时,此方法很有用,但在 testotherDebugJob 之后还有几个步骤.

This approach is helpful when your pipeline not contains a lot of steps, but after the test and otherDebugJob follows another few steps.

第二种方法

您可以使 setup 步骤失败,然后在 otherDebugJob 步骤中处理此失败:

You can fail your setup step and then handle this fail in otherDebugJob step:

setup:
  script:
    - DIR_CHECK=$(svn ls https://server.fsl.local:port/svn/myco/personal/TestNotReal --depth empty)
    - |
      if [ -z "$DIR_CHECK" ]; then
        exit 1
      fi

otherDebugJob:
  script:
    - echo "DIR_CHECK is not blank"
  when: on_failure

如果您只想在此 setup 步骤之后进行一些调试,则此方法很有用.在完成所有 on_failure 作业后,管道将被标记为 Failed 并停止.

This approach is useful if you only want to make some debug stuff after this setup step. After all on_failure jobs, pipeline will be marked as Failed and stopped.

这篇关于如何将 exe 输出分配给 gitlab ci 脚本中的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆