简体   繁体   中英

Azure DevOps pipeline, cannot create a variable in bash task

I have a yaml pipeline in Azure DevOps. In one step I am using "bash" task to find a file and create a variable named "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 ."

Bash syntax in pipeline:

- 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:

工件名称:找不到命令

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 命令。

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":

- 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: |
    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.

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.

To meet your requirement, you need to use the command echo "##vso[task.setvariable variable=ArtifactName;]foo" to define the pipeline variable.

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.

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) .

Result:

在此处输入图像描述

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM