How can I get the branch name that is merged into target branch that trigger the pipeline? - azure-devops

Branch "1850" was merged into the branch "dev". And pipeline trigger when there is merge to branch "dev". When release is success or fail, there is a stage will send email to let user know the status of realease. And from email i cant tell which one it is. So how can i get the branch name of "1850" variable.
Update to Leo Liu-MSFT's answer:
Thanks for helping. so i make change and these error occurs. please explain why so.

How can i get the branch name that is merged into target branch that trigger the pipeline?
You could use the predefined variables System.PullRequest.SourceBranch, which return the branch that is being reviewed in a pull request.
As test, I add a inline powershell task to output this variable:
Write-Host "The merged target branch is:-" $(System.PullRequest.SourceBranch)
Then we could get the value is refs/heads/1805.
We could use a command line task to parse that path to get the branch name:
set BranchName=$(System.PullRequest.SourceBranch)
for %%a in (%BranchName%) do echo %%~nxa
Update:
As I test, it works fine on my side with following code:
Write-Host "##vso[task.setvariable variable=SourceBranchName;]$(System.PullRequest.SourceBranch)"
Remove the isOutput=true.
Then, I could get the variable SourceBranchName by the command line task:
echo $(SourceBranchName)
According to your error, it seems the error comes from your powershell task itself or the agent. So, please try to use hosted agent with Inline powershell task to check if you still have this issue.
Hope this helps.

Related

How to give input parameter to the concourse pipeline on the pipeline page and trigger the job similar to jenkins

I would like to know if it is possible to give inputs to a concourse pipeline from the UI.
I know we can add input details to a git repo and read from the repo, but for every tiny input I need to do a code commit.
For this scenario is Jenkins better than concourse?
I tried searching in the internet to find if it is possible to give inputs to the concourse pipeline, but I did not find a solution.
Manual inputs via UI are not a thing in Concourse.
FWIW: When I need frequent inputs and want to avoid git commits for that purpose, I use an s3 resource versioned file in my pipeline as an input, for example with a send_input.sh script like this:
#!/bin/bash
echo "$1" > /tmp/input.txt
aws s3 cp /tmp/input.txt s3://my-bucket/my-concourse-resource-file.txt
and then
./send_input.sh "this is my input"
then the pipeline picks it up and uses it in my workflow.

Is there any way to get the TriggeredBy value inside the release pipeline in Azure Devops

i want to receive the Triggered By value inside the Azure release pipeline TriggeredBy
You can use the predefined variable: Release.RequestedFor , Release.TriggeringArtifact.Alias
Release.RequestedFor: The display name of identity that triggered the release.
Example: Mateo Escobedo
Release.TriggeringArtifact.Alias: The alias of the artifact which triggered the release. This is empty when the release was scheduled or triggered manually.
Example: fabrikam_app
We could add task Bash and enter the script printenv to list all variable. And it contain a variable RELEASE_REASON and RELEASE_REQUESTEDFOR
RELEASE_REASON show the create release method
RELEASE_REQUESTEDFOR show the user display name who trigger this release.

Sanitize branch name to avoid problems with website name

I am using Azure Devops Release pipeline to automatically publish Pull Requests to test domain branchname.blablabla.com
Everything works fine, but sometimes branch name contains dot . and deployment task fails due to 4th level domain name like branch.name.blablabla.com
Is there a way to remove illegal characters from $(Build.SourceBranchName) before it binds to PARAMETERS_WEBSITENAME?
You can add one additional task before using it in website name, to remove dot from the branch name. For example, you can add Bash task to run one shell script:
a="$(echo $(Build.SourceBranchName) | tr -d .)"
echo "##vso[task.setvariable variable=BranchName]$a"
In this script, it firstly remove dot . from $(Build.SourceBranchName) value. Then create one new variable BranchName and assign the modified value to it.
Now, in your next task, you can call this modified value by using $(BranchName).

How to set new value for the environment variable in VSTS

we have used the env:msg merge but it did take the default value in variables tab. We were unable to assign a value.Even though we see the value in the previous step
The code that assigns the value
$env:msgmerge = 'Git Merge to Master After Deployment Repo:' + "$(reponame)"
Write-Output $env:msgmerge
Environment variables created with $env: are Process variables, so they're lost when the process exits and we can't access them from another process (PowerShell instance).
Ahh this solved we need to use the vsts method
$msgmerge = 'Git Merge to Master After Deployment Repo:' + "$(reponame)"
Write-Output $msgmerge
Write-Host ("##vso[task.setvariable variable=msgmerge;]$msgmerge")

How to get first character of teamcity.build.branch in a custom script of build configuration?

I am using team city with git. I am trying to take different action on the basis of the branch name from build is getting triggered. Build gets triggered from a branch in which a change gets checked in.
I have branch name like US12345, F12345, DE12345 and I want to perform different tests on these different builds.
For this I have added a command line step and try to run a custom script (Windwos batch script). Here is the script:
#ECHO OFF
echo "BRANCH Name: %teamcity.build.branch%"
SET BranchName="%teamcity.build.branch%"
if /I %%BranchName:~0,1%%==F (echo Working on a feature branch && <take action 1>)
if /I %%BranchName:~0,2%%==US (echo Working on a story branch && <take action 2>)
if /I %%BranchName:~0,2%%==DE (echo Working on a defect branch && <take action 3>)
if /I %%BranchName:~0,2%%==QA (echo Working on a QA1 branch && <take action 4>)
In the above script, first I am assigning value of current branch name to a varialbe "BranchName".
Then in the first if condition, I am taking out first character of that variable and check if that character is equal to F. If that is true then I want to perform some set to tasks.
In the second if condition, I am taking out first two characters of "BranchName" and check if it is equal to US? If it is then I perform another different set of actions.
In the same way I want to do in third and fourth if condition.
Now the problem is, I am not able to get first few characters of a variable in the custom script because teamcity treats everything inside a "%%" as a reference parameter and we have to add those parameters in the build configuration.
Has anyone worked in these use case? Any help would be greatly appreciated.
If you want to pass % to TeamCity, you should escape it with other %, i.e. for % it must be %%, for %% it must be %%%%
You are cramming all your different branch builds into a single configuration, then using a bat file to trigger what gets build...that sounds complicated, and also a good choice for VSC trigger in TeamCity. I can a suited approach:
Create one build configuration, then create a VCS trigger for your desired branch name to trigger the build.
Copy that configuration N-times, one for each branch you want to build, then reconfigure the VSC trigger to have the correct branch name.
If you want to keep doing what you are doing, use PoSh instead of BAT script. Add a PowerShell step, change the script to be "Source Code" and enter this under "Script source":
$branch = "%teamcity.build.branch%"
if ($branch.StartsWith('F')) {
echo "Working on a feature branch $branch"
}
elseif ($branch.StartsWith('US')) {
echo "Working on a feature branch $branch"
}
elseif ($branch.StartsWith('DE')) {
echo "Working on a feature branch $branch"
}
elseif ($branch.StartsWith('QA')) {
echo "Working on a feature branch $branch"
}