2

In Azure Pipleine YAML version the Build.Repository.Name predefined variable contains workspace/reponame value in my case.

I wanted to remove the workspace/ prefix and use the result in a following step.

I tried many ways but without success, but it seems the replace expression maybe be good for me.

One tried solution:

variables:
  myVariable: $[replace(variables['Build.Repository.Name'], 'workspace/', '')]

  steps:
    - bash: echo $(myVariable) # show nothing

As i read in documentation the Build.Repository.Name does not accessible in this phase The following runtime call show the correct value

steps:
  - bash: echo $(Build.Repository.Name) # shows: workspace/reponame

Can I do something similar, and what is the correct way?

- bash: echo $(replace('$(Build.Repository.Name)', 'workspace/', ''))
wia
  • 186
  • 2
  • 13
  • Hi @wia. Is there any update about this ticket? Feel free to let me know if the answer could give you some help. Just a remind of [this](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). – Kevin Lu-MSFT Mar 30 '21 at 06:18
  • 1
    Hi @KevinLu-MSFT thanks, I found a similar solution to 2. suggestion before you write (you did that: https://stackoverflow.com/questions/57817659/how-to-replace-in-variable-strings-inside-azure-pipelines-yaml ), so i implemented that, but actually waiting for a PR merge. When success I will be mark to best answer. The general problem it seems, the ```Build.Repository.Name``` does not exist on this phase, but later in tasks yes. – wia Mar 30 '21 at 06:41
  • Hi @wia . Thanks for your update. will wait for your good news – Kevin Lu-MSFT Mar 30 '21 at 06:42

1 Answers1

2

Based on my test, the replace expression could work fine.

Since my repo name does not contain /, I switched to another predefined variable($(build.sourcebranch) (e.g. refs/heads/master)) for testing.

Here are two methods:

1.Replace expression:

variables:
  myVariable: $[replace(variables['BUILD.SOURCEBRANCH'], 'refs/heads/', '')]

steps:
  - bash: echo $(myVariable) 

2.Bash script:

steps:
  - task: Bash@3
    inputs:
      targetType: 'inline'
      script: |
        SUBSTRING=$(echo $(BUILD.SOURCEBRANCH)| cut -d'/' -f 3)
        echo $SUBSTRING
        echo "##vso[task.setvariable variable=myvalue]$SUBSTRING"
  - task: Bash@3
    inputs:
      targetType: 'inline'
      script: |
       echo $(myvalue)

Result:

enter image description here

In your case, you could try the following bash script:

steps:
  - task: Bash@3
    inputs:
      targetType: 'inline'
      script: |
        SUBSTRING=$(echo $(Build.Repository.Name)| cut -d'/' -f 2)
        echo $SUBSTRING
        echo "##vso[task.setvariable variable=myvalue]$SUBSTRING"
  - task: Bash@3
    inputs:
      targetType: 'inline'
      script: |
       echo $(myvalue)
Kevin Lu-MSFT
  • 20,786
  • 3
  • 19
  • 28
  • 1
    The second one is implemented but with power shell, as I found [here](https://stackoverflow.com/questions/57817659/how-to-replace-in-variable-strings-inside-azure-pipelines-yaml#answer-57847395) – wia Mar 31 '21 at 18:43
  • $(Build.SourceBranchName) returns per documentation "The last path segment in the ref. For example, in refs/heads/main this value is main." https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml – Nick Cordova Jul 26 '23 at 08:24