Home > Net >  Azure DevOps pipeline, cannot create a variable in bash task
Azure DevOps pipeline, cannot create a variable in bash task

Time:01-19

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:

ArtifactName: command not found

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 command not found.

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 command not found

My last try was to use the below syntax following enter image description here

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.

CodePudding user response:

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: enter image description here

enter image description here

  • Related