简体   繁体   English

Azure DevOps 管道,无法在 bash 任务中创建变量

[英]Azure DevOps pipeline, cannot create a variable in bash task

I have a yaml pipeline in Azure DevOps.我在 Azure DevOps 中有一个 yaml 管道。 In one step I am using "bash" task to find a file and create a variable named "ArtifactName".在一个步骤中,我使用“bash”任务来查找文件并创建一个名为“ArtifactName”的变量。 The task is as follows.任务如下。 The issue is it cannot create the variable and the pipeline although runs successfully does not create the variable and prints: " ArtifactName: command not found ."问题是它无法创建变量和管道,尽管成功运行不会创建变量并打印:“ ArtifactName: command not found 。”

Bash syntax in pipeline:管道中的 Bash 语法:

- bash: |
    echo $(Build.SourcesDirectory)
    ArtifactName=find $(Build.SourcesDirectory) -name '*.whl'
    echo "Artifact name value is " $(ArtifactName)
  displayName: 'Find the artifact in source directory'
  workingDirectory: $(Build.SourcesDirectory) 

The error message is as follows.错误信息如下。 As it is shown the variable ArtifactName is empty:如图所示,变量 ArtifactName 为空:

工件名称:找不到命令

I changed the code to make sure it gets some value and see if the problem was from regular expression part but again I get the same error with this code:我更改了代码以确保它获得一些值并查看问题是否来自正则表达式部分但是我再次遇到与此代码相同的错误:

- bash: |
    echo $(Build.SourcesDirectory)
    ArtifactName=$(find $(Build.SourcesDirectory) -name '*.whl')
    echo "Artifact name value is " $(ArtifactName)
  displayName: 'Find the artifact in source directory'
  workingDirectory: $(Build.SourcesDirectory) 

The error is like previous part "ArtifactName command not found":该错误类似于前一部分“找不到 ArtifactName 命令”:

找不到 ArtifactName 命令。

Even when I hard code the value of the variable ArtifactName to a string like "foo", still the same error of "ArtifactName command not found":即使当我将变量 ArtifactName 的值硬编码为“foo”之类的字符串时,仍然会出现“找不到 ArtifactName 命令”的相同错误:

- bash: |
    echo $(Build.SourcesDirectory)
    ArtifactName="foo"
    echo "Artifact name value is " $(ArtifactName)
  displayName: 'Find the artifact in source directory'
  workingDirectory: $(Build.SourcesDirectory)

找不到 ArtifactName 命令

My last try was to use the below syntax following this link to define a variable in bash, but this one also raised the same issue that ArtifactName is again empty:我最后一次尝试是使用此链接后的以下语法在 bash 中定义一个变量,但这也引发了同样的问题,即 ArtifactName 再次为空:

- bash: |
    echo $(Build.SourcesDirectory)
    echo "##vso[task.setvariable variable=ArtifactName;]foo"
    echo "Artifact name value is " $(ArtifactName)
  displayName: 'Find the artifact in source directory'
  workingDirectory: $(Build.SourcesDirectory)

The error is exactly the same:错误是完全一样的:

在此处输入图像描述

The question is how can I use bash task to create a variable inside it with some values that I can use in other tasks of the same and other stages.问题是我如何使用 bash 任务在其中创建一个变量,其中包含一些我可以在同一阶段和其他阶段的其他任务中使用的值。

The question is how can I use bash task to create a variable inside it with some values that I can use in other tasks of the same and other stages.问题是我如何使用 bash 任务在其中创建一个变量,其中包含一些我可以在同一阶段和其他阶段的其他任务中使用的值。

To meet your requirement, you need to use the command echo "##vso[task.setvariable variable=ArtifactName;]foo" to define the pipeline variable.为了满足您的要求,您需要使用命令echo "##vso[task.setvariable variable=ArtifactName;]foo"来定义管道变量。

Refer to this doc: Set variables in scripts参考这个文档: 在脚本中设置变量

A script in your pipeline can define a variable so that it can be consumed by one of the subsequent steps in the pipeline.Set variables in scripts管道中的脚本可以定义一个变量,以便它可以被管道中的后续步骤之一使用。在脚本中设置变量

It will not work on the current task, but the updated value can be used in the next tasks.它不会对当前任务起作用,但更新后的值可以在下一个任务中使用。

On the other hand, the format you used to run the find command in bash script has issues.另一方面,您用于在 bash 脚本中运行 find 命令的格式存在问题。

Refer to my sample:参考我的样本:

jobs:
- job: BuildandPublish

  steps:
    - task: Bash@3
      inputs:
        targetType: 'inline'
        script: |
          echo $(Build.SourcesDirectory)
          find $(Build.SourcesDirectory) -name '*.yml'
          ArtifactName=$(find $(Build.SourcesDirectory) -name '*.yml')
          echo "Artifact name value is " $ArtifactName
          echo "##vso[task.setvariable variable=ArtifactName;]$ArtifactName"

    - task: Bash@3
      inputs:
        targetType: 'inline'
        script: |
          echo $(ArtifactName)

In this case, it can get the file name and can be used in the next tasks with the format: $(variablename) .在这种情况下,它可以获取文件名并可以在接下来的任务中使用,格式为: $(variablename)

Result:结果:

在此处输入图像描述

在此处输入图像描述

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

相关问题 如何使 Azure devops 管道任务失败,专门针对 bash 脚本中的失败 - How to fail Azure devops pipeline task specifically for failures in bash script 无法访问 Azure Devops 管道 bash 脚本中的内部版本号 - Cannot access build number in Azure Devops pipeline bash script Azure Devops内联bash脚本子shell输出作为任务组的变量 - Azure Devops inline bash script subshell output as variable to task group 使用代理 Ubuntu-16.04 在 azure-devops 管道 bash cake 任务中获取 ##[error] Bash 以代码“1”退出 - Getting ##[error] Bash exited with code '1' in an azure-devops pipeline bash cake task with agent Ubuntu-16.04 如何使 Azure devops 管道任务失败,专门针对构建步骤中的失败。yaml 文件 - bash - How to fail Azure devops pipeline task specifically for failures in build-steps.yaml file - bash 在Azure DevOps Bash任务中使用XMLStarlet - Use XMLStarlet in Azure DevOps Bash task Azure DevOps 在 bash 任务中生成密码 - Azure DevOps generate a password inside of the bash task bash 如果 azure devops 任务中的语句未执行 - bash if statement inside the azure devops task is not executing 如何在可能未定义的 Azure Devops bash 任务中使用秘密变量 - How can I use a secret variable in Azure Devops bash task which could be undefined 如何在 azure devops 管道中使用 arguments 运行 bash 脚本? - How to run a bash script with arguments in azure devops pipeline?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM