Can I access Azure App Configuration inside a script called in a pipeline task? - powershell

Using Azure App Configuration I can make the variables available to the entire pipeline. However, if the following task is running a PowerShell script, called Script.ps1. Are the configs vars going to be available in that script or do I have to pass them as input to the script?

The key-values that are fetched from App Configuration are set as pipeline variables, which are accessible as environment variables. To use environment variables in your PowerShell script file you can refer to the following.
Use environment variables in PowerShell script file:
Use PowerShell task in pipeline:
The result in the pipeline:

Related

How to read release pipeline variable and use as environment variable in Azure DevOps in Azure CLI task?

I am using one powershell task and one Azure Cli task in release pipeline of Azure DevOps.
I have some release pipeline's variables. I want to read those variables as they will be required by my script in above two tasks. I used powershell core in Azure cli task
I tried to read them in the inline script directly as $(variableName) or $env:variableName but none of the above worked.
I tried to set read the variable in Environment variables option in the task and then use in the inline scripts using $env:variableName but it also didn't work. On printing the variableName in the script using Write-Host, the value I got is $(valueName) instead of the correct value.
How to read those variables inside these scripts?
The pipeline variables can be reference in the Azure CLI inline script by using the syntax $(variableName). I tested by adding the following in a script
write-host "The variable value: $(variableName)"

How to use AzureDevOps predefined variables in Bash/Powershell scripts

In an AzureDevOps pipeline, I have tasks written in Bash/Powershell script.
If I choose to use Inline scrpit, I can use predefined variables directly, such as
cd $(Build.SourcesDirectory)
However, if I choose to use a file path to call a script, I can't use predefined variable directly in the script file. I have to pass the predefined variable to an environment variable in the task definition, like in the example below, so I can use $SourceDirectoy in script.sh,
Is there a better way to call predefined variable direclty in the script?
I believe the variables are also made available to scripts, but the formatting to reference them in the script might depend on script type. Reference the documentation.
Notice that variables are also made available to scripts through
environment variables. The syntax for using these environment
variables depends on the scripting language.
The name is upper-cased, and the . is replaced with the _. This is
automatically inserted into the process environment. Here are some
examples:
Batch script: %VARIABLE_NAME%
PowerShell script: $env:VARIABLE_NAME
Bash script: $VARIABLE_NAME
Predefined variables that contain file paths are translated to the
appropriate styling (Windows style C:\foo\ versus Unix style /foo/)
based on agent host type and shell type. If you are running bash
script tasks on Windows, you should use the environment variable
method for accessing these variables rather than the pipeline variable
method to ensure you have the correct file path styling.

Azure DevOps ssh script file task

I'm just wondering if it's possible to use a script file in the SSH task in the Releases that will be populated with the environment variables from azure.
Script:
TEST=$(test)
I saved this script as an artifact, successfully download it, and select this script in the SSH task as a file, but the problem is the environment variables is not unwrapped, does someone have some approach?
If I put this same script as an inline script, then it's working. But if I chose script file then not.
I want to have this script in the git repo, so I can easily edit the script.
Does someone have this working?
I'm just wondering if it's possible to use a script file in the SSH task in the Releases that will be populated with the environment variables from azure
I am afraid there is no such out of way to use a script file in the SSH task to populated with the environment variables from azure.
As workaround, we could use the task Replace Tokens to update the value in the script file:
The format of variable in .sh file is #{TestVar}#.
Hope this helps.

Access System Environment Variables in Azure Pipeline Powershell Script Task

We are using powershell to do some deployment tasks in the target environment(e.g. a VM).
However, it seems the script does not have access to the system environment variables, as well as other programs run by the script.
So far, it only has access to the variables defined in the pipeline.
Also, inside the .NET program run by that script, we have:
var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", EnvironmentVariableTarget.Machine);
but it still does not have access to the desired environment variable.
Is there a way we can allow the pipeline script to access the system variable?
Or, do we have to define a stage-scoped pipeline variable and make it consistent with the targeting machine's system variable?
Thanks.
If you refer to get the system environment variables of the target machine, you just need to run the pipeline with the self-hosted agent installed on the target machine. Then use $ env: VARIABLE_NAME in the powershell script to get the system environment variables of the target machine.
The system environment variables of the machine obtained by powershell script in Azure Devops are determined by the agent that runs the pipeline. The agent on which machine is used to run the pipeline can access the system environment variables of that machine.
In addition, you can try the method mentioned by Mar Tin in the comment to define system variables as pipeline variables.

Is there a way to access TeamCity system properties in a Powershell script?

I'm trying to set up a new build configuration in TeamCity using the Powershell runner. However, I can't seem to find a way to access the TeamCity System Properties in the build script. I've seen hints that it is possible, but cannot find documentation on how to do it.
I have tried accessing the system properties using Powershell variable syntax, $variable. I have also printed out all variables in memory and see no teamcity variables to use.
Is this possible with the Powershell runner, and if so what is the syntax necessary to get it working?
TeamCity will set up environment variables, such as build.number (you can see a list of these within TeamCity).
In Powershell you can access environment variables using the env "provider", e.g.
$env:PATH
TeamCity variables are accessible by replacing the . with a _, so the build.number variable can be accessed as
$env:build_number
As it says in the TeamCity documentation, the system parameters are passed to the build script runner, but not all build script runners know what to do with them. In the case of the Powershell script runner, when using a script file, they don't propagate down to your scripts.
It's occurred to me to write a psake-optimized build runner that does, but in the meantime you can do one of the following:
explicitly map any of the TeamCity build properties to script parameters using the parameter expansion that's available within the Script Source box. eg .\build.ps1 -someParam:%build.name%
use environment parameters, which can be accessed explicitly within PowerShell using $env:NAME_IN_TEAMCITY syntax, eg $env:TEAMCITY_VERSION, or looped over and pushed into variable scope
access the build properties file that TeamCity makes available during the build. The file is available at $env:TEAMCITY_BUILD_PROPERTIES_FILE, and if you load the XML version it's fairly easy to loop through and push them all into scope (though you do get everything as a string of course). I posted a gist on how to do this (https://gist.github.com/piers7/6432985). Or, if using Psake, modify the script above to return you a hashtable which you can pass directly to Psake's -properties argument.
It is posible. Here is example how to pass system properties into PSake script:
& .\psake.ps1 -parameters #{build_number=%build.number%; personal_build=%build.is.personal%}
If you don't use Psake, you can define your variables like this:
$build_number = %build.number%
The %build.number% part will be replaced with TeamCity provided data. I think, it works only in Source code script input mode.
I created a meta-runner that will pass through System parameters to parameters declared in the Powershell script. It's not perfect (if you put '# in your source it will break) but it works for what I needed, you can find it here: https://gist.github.com/anonymous/ef60ada3f48f0fb25093