1

I am trying to setup variables from predefined variables through bash script using bash script. but could not get succedeed. below is my task in azure pipeline

  resources:
  pipelines:
  - pipeline: pipeline1
  project: appcom
  source: pipeline-api
  trigger:
  branches:
  - develop
  - feat/*
  - pipeline: pipeline2
  project: appcom
  source: pipeline2-api
  trigger:
  branches:
  - develop
  - feat/*
  variables:
 - name: alias
   value: $(resources.triggeringAlias)

 stages:
 - stage: ScanImage

     jobs:
     - job: ScanImage
       pool:
         vmImage: 'ubuntu-16.04'

       steps:
       - script: echo $(alias)

       - task: Bash@3
         inputs:
         targetType: 'inline'
         script: |
         if [ "$(alias)" == "pipeline1" ]; then
           echo ("##vso[task.setvariable variable=apiname]$(resources.pipeline.pipeline1.pipelineName)")
           echo ("##vso[task.setvariable variable=dockertag]$(resources.pipeline.pipeline1.sourceCommit) | cut -c -7")
           echo ("##vso[task.setvariable variable=helmpath]P02565Mallorca/pipeline1-api")
         elif [ "$(alias)" == "pipeline2" ]; then
           echo ("##vso[task.setvariable variable=apiname]$(resources.pipeline.pipeline2.pipelineName)")
           echo ("##vso[task.setvariable variable=dockertag]$(resources.pipeline.pipeline2.sourceCommit) | cut -c -7")
           echo ("##vso[task.setvariable variable=helmpath]P02565Mallorca/pipeline2")
           fi
       - script: echo $(dockertag)
       - script: echo $(helmpath)
       - script: echo $(apiname)

it giving me error with ##[error]Bash exited with code '2

1 Answer 1

1

By reference to this doc: Set variables in scripts, below yaml should work as expected.

resources:
  pipelines:
  - pipeline: pipeline1
    project: appcom
    source: pipeline-api
    trigger:
      branches:
      - develop
      - feat/*
  - pipeline: pipeline2
    project: appcom
    source: pipeline2-api
    trigger:
      branches:
      - develop
      - feat/*
    
variables:
- name: alias
  value: $(resources.triggeringAlias)

stages:
- stage: ScanImage
  jobs:
  - job: ScanImage
    pool:
      vmImage: 'ubuntu-16.04'
    steps:
    - script: echo $(alias)

    - task: Bash@3
      inputs:
        targetType: 'inline'
        script: |
          if [ "$(alias)" == "pipeline1" ]; then
            echo "##vso[task.setvariable variable=apiname]$(resources.pipeline.pipeline1.pipelineName)"
            echo "##vso[task.setvariable variable=dockertag]$(resources.pipeline.pipeline1.sourceCommit) | cut -c -7"
            echo "##vso[task.setvariable variable=helmpath]P02565Mallorca/pipeline1-api"
          elif [ "$(alias)" = "pipeline2" ]; then
            echo "##vso[task.setvariable variable=apiname]$(resources.pipeline.pipeline2.pipelineName)"
            echo "##vso[task.setvariable variable=dockertag]$(resources.pipeline.pipeline2.sourceCommit) | cut -c -7"
            echo "##vso[task.setvariable variable=helmpath]P02565Mallorca/pipeline2-api"
          fi

    - script: echo $(dockertag)
    - script: echo $(helmpath)
    - script: echo $(apiname)

See:Resources: pipelines for more details.

Sign up to request clarification or add additional context in comments.

5 Comments

It is my pleasure to help you.
Hi Edward, i followed as you mentioned and setting up branchName as variable as below echo "##vso[task.setvariable variable=branchName]$(resources.pipeline.pipeline2.sourceBranch)" but i could not use this variable in condition for as stage stages: - stage: DEV_DCR condition: and(succeeded(), or(eq(variables $(branchName), 'refs/heads/develop'), eq(variables$(branchName), 'refs/heads/release'), contains(variables$(branchName), 'refs/heads/hotfix/'), contains(variables$(branchName), 'refs/heads/bugfix/')))..... it gives error. any idea how we can use it
You should use stageDependencies.stageName.jobName.outputs['stepName.variableName'] not variables$(branchName) in condition, see: learn.microsoft.com/en-us/azure/devops/release-notes/2020/… and learn.microsoft.com/en-us/azure/devops/pipelines/process/… for details. BTW, please file a new ticket if you have more questions with detailed information. Thanks.
is it possible to tag above pipeline with $(alias), so i can have filter to list release for each api ??
or is it possible to name pipeline with something like this name: $(alias)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.