Azure Devops: Shorten $(SourceBranchName) when used within pipeline name directive - azure-devops

Our yml-definition files for an Azure-pipeline start with
name: $(Build.DefinitionName)_$(SourceBranchName)_$(rev:rrrrr)
With that, we get very long build names that have negative consequences for display in build result pages. Therefore we would like to shorten the $(SourceBranchName) to the e.g. first 20 characters.
Is there a way of doing that?

we would like to shorten the $(SourceBranchName) to the e.g. first 20 characters.
You could try to use command line\powershell script to split long string into short.
steps:
- script: |
echo $(Build.SourceBranchName)
set TestVar=$(Build.SourceBranchName)
set MyCustomVar= %TestVar:~0,20%
echo %MyCustomVar%
echo ##vso[task.setvariable variable=CustomVar]%MyCustomVar%
displayName: 'Get the first 20 character versions of Build.SourceBranchName'
Then we could get the short string of the SourceBranchName.
Generally, SourceBranchName is different from SourceVersion, and it is generally not a very long string. If your SourceBranchName is indeed a very long string, then the above method will help you.
You could check my previous thread for some more details.
Note: If you want to shorten your build names, we need update the build number for current build through Logging Command (e.g. Write-Host "##vso[build.updatebuildnumber]buildnumber"):
Write-Host "##vso[build.updatebuildnumber]$(Build.DefinitionName).$(CustomVar).$(rev:rrrrr)"
But the $(rev:rrrrr) could not be available in the task, so we have to include $(rev:rrrrr) in default build number format (Options), for example: $(date:yyyyMMdd)-$(rev:rrrrr). And Get current build number from pre-defined variable (Build.BuildNumber/BUILD_BUILDNUMBER), then we parse the value of $(rev:rrrrr).

Related

Azure DevOps Pipeline - Updating BuildNumber

I have attempted to update the build number in Azure DevOps Pipelines for quite some time now, and looked through various stackoverflow questions and documentation, and I simply cannot figure out how to fix my problem.
The Build Number I would like to set would be: $(year:yy)$(DayOfYear)$(rev:.r).
This apparently works on pipelines using the graphical interface (The pipelines were not made by me)
What I have tried so far:
- task: UpdateBuildNumber#0
inputs:
buildNumber: '$(year:yy)$(DayOfYear)$(rev:.r)'
Write-Host "##vso[build.updatebuildnumber]$(year:yy)$(DayOfYear)$(rev:.r)"
And a couple of variations of those as well. Each time, I get the error:
##[error]TF209010: The build number format $(year:yy)$(DayOfYear)$(rev:.r) contains invalid character(s), is too
long, or ends with '.'. The maximum length of a build number is 255
characters. Characters which are not allowed include '"', '/', ':',
'<', '>', '', '|', '?', '#', and '*'.
I have tried making variables for each of the dates, and using the variables instead, but that does not work either.
Any help would be greatly appreciated.
It because all those variables are available only if you give it in the name: in your YAML, not in other tasks.
From the docs:
The following table shows how each token is resolved based on the previous example. You can use these tokens only to define a run number; they don't work anywhere else in your pipeline.
So, the only option it just to configure the name: $(year:yy)$(DayOfYear)$(rev:.r).

Is it possible to substring value in NodeFilter CommandLine Argument in rundeck

I have a Rundeck job where I'm loading the env Option values from remote file.
File has values as shown below
domain-dev
In NodeFilter CommandLine Argument, I'm loading values as environment: ${option.env}
I want to do substring of the values of env till -. Eg: I want to give domain to NodeFilter
is it possible to do something like below
NodeFilter = environment: ${option.env.substring(0, option.env.indexOf("-"))}
Is not possible in that way, but you can use regular expressions. To get values from a specific string. For domain-dev you can use this regex name: ^([^-]*).*$ to get only the string before the - character, of course, this example uses the tags attribute to apply the filter.
You can use regex101 or freeformater to test your regex patterns.

Azure DevOps variable and multiline value

I have an Azure Devops Pipeline Task for .NET Core, specifying the 'test' command.
One of the parameters is 'Path to project(s)'. You can specify multiple globbing patterns, each on its own line.
Now I want to make this value settable at queueing time by using a variable $(UnitTestPatterns). But a variable cannot have a multiline value. How can I specify 2 or more globbing patterns in a way that all are evaluated?
I have tried a pipe '|', a comma ',' and a semicolon ';' as separators, none have worked. The log of the task then shows ##[warning]Project file(s) matching the specified pattern were not found.
Example multiline value:
**/Project.*.Tests/*.csproj
!**/Project.Module2.Tests/*.csproj
I want a variable like this (with probably some secret separator, the ';' does not work):
$(UnitTestPatterns) = **/Project.*.Tests/*.csproj; !**/Project.Module2.Tests/*.csproj
I am using the UI to set the variable, not YAML.
It looks that this is not supported. Please check this topic on Developer Community. You may also check this topic where you will find workaround.

Can I use regular expression for job name dependency in IBM LSF?

I have a job which is supposed to be dependent on DONE state of multiple LSF Jobs having name prefix similar, let's say,
job 'A' depends on jobs with names 'Job-1', 'Job-2', 'Job-3',....,'Job-100'
Can I use following regex in defining job dependency for Job 'A'?
Job-*
Something like,
bsub -J 'A' -w "done(Job-*)"
I have found the answer in the documentation
Use double quotes (") around job names that begin with a number. In
the job name, specify the wildcard character asterisk (*) at the end
of a string, to indicate all jobs whose name begins with the string.
For example, if you use jobA* as the job name, it specifies jobs named
jobA, jobA1, jobA_test, jobA.log, etc.

Manage inputs from external command in a powershell script

First, I would like to apologize in case that the title is not descriptive enough, I'm having a hard time dealing with this problem. I'm trying to build an automation for a svn merge using a powershell script that will be executed for another process. The function that I'm using looks like this:
function($target){
svn merge $target
}
Now, my problem occurs when there are conflicts in the merge. The default behavior of the command is request an input from the user and proceed accordingly. I would like to automatize this process using predefined values (show the differences and then postpone the merge), but I haven't found a way to do it. In summary, the workflow that I am looking to accomplish is the following:
Detect whether the command execution requires any input to proceed
Provide a default inputs (in my particular case "df" and then "p")
Is there any way to do this in powershell? Thank you so much in advance for any help/clue that you can provide me.
Edit:
To clarify my question: I would like to automatically provide a value when a command executed within a powershell script require it, like in the following example:
Requesting user input
Edit 2:
Here is a test using the snippet provided by #mklement0. Unfortunately, It didn't work as expected, but I thought it was wort to add this edition to clarify the question per complete
Expected behavior:
Actual result:
Note:
This answer does not solve the OP's problem, because the specific target utility, svn, apparently suppresses prompts when the process' stdin input isn't coming from a terminal (console).
For utilities that do still prompt, however, the solution below should work, within the constraints stated.
Generally, before attempting to simulate user input, it's worth investigating whether the target utility offers programmatic control over the behavior, via its command-line options, which is both simpler and more robust.
While it would be far from trivial to detect whether a given external command is prompting for user input:
you can blindly send the presumptive responses,
which assumes that no situational variations are needed (except if a particular calls happens not to prompt at all, in which case the input is ignored).
Let's assume the following batch file, foo.cmd, which puts up 2 prompts and echoes the input:
#echo off
echo begin
set /p "input1=prompt 1: "
echo [%input1%]
set /p "input2=prompt 2: "
echo [%input2%]
echo end
Now let's send responses one and two to that batch file:
C: PS> Set-Content tmp.txt -Value 'one', 'two'; ./foo.cmd '<' tmp.txt; Remove-Item tmp.txt
begin
prompt 1: one
[one]
prompt 2: two
[two]
end
Note:
For reasons unknown to me, the use of an intermediate file is necessary for this approach to work on Windows - 'one', 'two' | ./foo.cmd does not work.
Note how the < must be represented as '<' to ensure that it is passed through to cmd.exe and not interpreted by PowerShell up front (where < isn't supported).
By contrast, 'one', 'two' | ./foo does work on Unix platforms (PowerShell Core).
You can store the SVN command line output into a variable and parse through that and branch as you desire. Each line of output is stored into a new enumerator (cli output stored in PS variables is in array format)
$var = & svn merge $target
$var