How to pass multiple arguments to Command line in VSTS - azure-devops

I am using Command line in VSTS in the Release pipeline. Unfortunately not sure how to pass multiple arguments like "username", "password" and "siteurl" to the command line

If you are using Command Line task, you can use the format $(variablename) to pass argument.
Such as below example:
For passing multiple variables in the Command Line task, the format is still $(variablename).
Such as, the command line script can be:
echo "Please enter your user name: $(username). Please enter to you password: $(password). Please enter the site url: $(siteurl)"

As per the documentation, and using YAML code, this can be achieved by the env argument.
# Command line
# Run a command line script using Bash on Linux and macOS and cmd.exe on Windows
- task: CmdLine#2
inputs:
script: 'echo Example of a command line task'
env:
var1: someValue
var2: $(someVariable)
...
Note that:
Secret variables are not automatically mapped. If you have a secret
variable called Foo, you can map it in as shown in the above example
Also, do not forget that you have to declare the variables you are using. For groups of variables, which on Azure DevOps you can declare in the Pipelines Menu, on the Library sub-menu. In the image below, I am declaring a group variable named Tailspin Space Game - Release
I can then reference the group variables in the YAML code as follows:
variables:
- group: 'Timeline CV - Release'

Related

How to fail a pipeline gracefully when a variable is not defined

I am using a pipeline variable to define a path for a deploy script. There is the danger that someone forgets to define the variable. What would be a good way to detect this and give the appropriate error message in the yaml script file?
I could create a PowerShell script that would fail if the variable is not defined. But I would prefer to keep it all in the yaml file.
The PowerShell script to examine the variable value can be tiny and can still live in the YAML as the inline script of the PowerShell task:
- powershell: if (!$env:MyVar) Write-Error "The variable is not set"
displayName: Check Prerequisite Variable
failOnStderr: true
errorActionPreference: stop
I might be mistaken on syntax, but it describes the idea.

In codeship, what is the syntax to use env variables in steps?

In codeship - I am trying to use the env variables. My setup looks like this:
codeship-services.yml
environment:
- ENV=my-var
codeship-steps.yml
type: parallel
steps:
- command: echo $ENV
I does not work, it just prints $ENV.
Environment variables are only available when you the command is invoked in the context of a shell. By default that's not the case (similar to how docker run operates as well).
To get access to environment variables, either extract the command to a shell script and call the script instead, or explicitly invoke a shell
- service: app
command: sh -c "echo $ENV"

How to add secret variable as task environment variable in VSTS

This documentation states that secret variables are
Not decrypted into environment variables. So scripts and programs run by your build steps are not given access by default.
One of my build tasks require that an environment variable be set that is stored in a secret variable. Does this mean it's impossible to do this using secret varaibles in VSTS? If not, how do I do this?
For further background, I'm trying to code sign my electron app using electron-builder. It requires that two environment variables be set: CSC_LINK and CSC_KEY_PASSWORD. One of these is the password to a code signing certificate so needs to be kept secure.
Set Environment Variable
Use a Command Line task, like this:
target_environment_variable now contains the value of secret_variable.
Verify
Add a subsequent Command Line task that writes all environment variables to a disk file, like this: (note: in the Arguments text box, write to a folder that both you and build agent can access):
Queue the build definition, then view the file containing the environment variables:
When using the YAML-syntax this can be achieved too:
steps:
- script: |
echo %MYSECRET%
env:
MySecret: $(Secret_Variable)
You can supply variables to pass to tasks in the Variables page of the build definition:
Then they can be passed in to a task as an input like so:

How to I set environment variables using gitversion?

I am trying gitversion /output buildserver in both powershell and command prompt and neither modify the environment variables with GitVersion.SemVer (for example). How can I use it in a script?
According to the documentation:
By default GitVersion returns a json object to stdout containing all the variables which GitVersion generates.
So if you want to run it as part of a standalone script rather than a build task, just grab the json output from stdout and convert it to an object:
$GitVersion = gitversion |ConvertFrom-Json
$GitVersion.SemVer

VSTS builds - how to define new variable in custom build step and pass to next build steps

We are using VSTS to build our VS solution.
Is there a way to define custom Build Step, for example a PowerShell script, that creates a new variable to be passed to further build steps?
There's nothing about it in MSDN:
https://msdn.microsoft.com/Library/vs/alm/Build/scripts/variables
You can use Task Logging Commands to do this.
To invoke a logging command, simply emit the command via standard
output. For example, from a PowerShell task:
"##vso[task.setvariable variable=testvar;]testvalue"
For Mac, do this :
echo '##vso[task.setvariable variable=variableName;]'$variableValue