How to split a CLI command in Azure Devops over multiple lines? (Running on Windows) - azure-devops

I am running the following in a CLI task on Azure Devops (inline)
rem Create the Service Plan
call az appservice plan create --resource-group %RESOURCE_GROUP% --name %SERVICE_NAME% --sku B1
Whch works just fine. However, I'm having to scroll to see the whole thing and I have other commands which are even longer. So I'm trying to split it over multiple lines so I can see more clearly what is going on.
Looking at Microsoft Docs it looked like the solution was to put a backslash on the end of each line. So I tried:
I've tried:
rem Create the Service Plan
call az appservice plan create \
--resource-group %RESOURCE_GROUP% \
--name %APP_SERVICE_NAME% \
--sku B1
Which didn't work. I then read something that recommended the back-tick/back-quote at the end of each line:
rem Create the Service Plan
call az appservice plan create `
--resource-group %RESOURCE_GROUP% `
--name %APP_SERVICE_NAME% `
--sku B1
This also didn't work. Any help greatly appreciated.

Never mind. Worked it out. Turns out you need to use '^'
rem Create the Service Plan
call az appservice plan create ^
--resource-group %RESOURCE_GROUP% ^
--name %APP_SERVICE_NAME% ^
--sku B1

For me the ` character is working. I posted earlier that it wasn't but I was inadvertently using the wrong character.
(That post seems to have been deleted by a moderator though I'm not sure why - perhaps because it was more of a "me too" than an answer, but it did add that the above "^" solution doesn't work for me).
In case it was the same issue as I had, ensure the "Backtick" you use is the correct one (it's immediately to the left of "1" on my microsoft keyboard). I was trying to use a ' in the failed attempt as the CLI docs were reading made it hard to distinguish.

The character that you need to use to specify that the command is split over multiple lines, depends on the environment in where the CLI command is run.
In other words, it depends on the scriptType property of the AzureCLI#2 command.
Powershell/Powershell Core: use a backtick `
bash: use backslash \
batch: use accent circonflexe ^

from azure cloud shell, type AZ, then copy paste the az command with \ for multiple line will not work.
but there is a fix, you click on the + sign like a adding a folder sign, it will bring a full page Azure Cloud Shell window, change to Bash from the pull down (default is Poweshell), then you see prompt changed to name#Azure:~$, now you can use az commend with \ for multiple lines.

Related

Automatically pass string to powershell user input

In my powershell script, I call the following azure function:
az repos import create --git-source-url https://incommunities#dev.azure.com/my-organisation/Templates/_git/$($Framework) --detect true --project $ProjectAlias --repository $ProjectAlias --requires-authorization
When running, it prompts the user for a Password/PAT token, e.g:
Git Password / PAT:
Is there a way to automatically pass the password/token to the user input without having to enter manually?
I attempted to pipe the value through, however this does not seem to work e.g
my-pat-token | az repos import create --git-source-url https://incommunities#dev.azure.com/my-organisation/Templates/_git/$($Framework) --detect true --project $ProjectAlias --repository $ProjectAlias --requires-authorization
Is this both a) possible and then if so b) how can I do this?
There are two approaches you can use, both come courtesy from this nice blog post which you'll probably want to read, as it talks about a bunch of Azure Devops tasks.
Use an environmental variable
These commands will check for the presence of an environmental variable and will use it instead of prompting.
To do this, set an environment variable called AZURE_DEVOPS_EXT_PAT to the value of your PAT. (More info on how these tokens work here from the Microsoft Docs)
# set environment variable for current process
$env:AZURE_DEVOPS_EXT_PAT = 'xxxxxxxxxx'
When you're automating things, just set this variable before running the Azure commands.
Pipe the value in
I am not as big of a fan of this sort of approach but you can "echo out" the PAT value and pipe that into a command, which might work. IMHO this is more fragile and frunky and I wouldn't advise it.
$pat | az devops login

How can i make my curl command work in gitlab-ci?

I have curl that i use in gitlab-ci job to upload an artifact to Nexus
the command is as follow (defined in .gitlab-ci.yml under script section)
cmd /c curl -v -u $env:USERREG:$env:PASSREG --upload-file $env:BINFILE $env:NEXUS_REGISTRY/$env:REPONAME$env:BINFILE
of course all the variables are declared in .gitlab-ci.yml file except for USERREG and PASSREG which i declared them using the gitlab GUI.
Notice that i am using:
- Gitlab Runner with docker-windows executor
- Windows docker container to exec the above command
PROBLEM : the job is stacked demanding for the user (defined by USERREG) password (PASSREG) until the job is terminated due to timeout.
How to fix this problem ? thank you.
I am not sure if this could be your problem, but please check and refer to the GitLab Variables which you set ( USERREG and PASSREG). If they are protected variables that means that they will be only available on a "protected" branches and in case you are pushing it from non-protected branch that could brings you to the state where you are currently because above mentioned variables are not available.
If this is the case, just make them to not be protected but masked and you should be fine.

Executing commands inside helm install

I'm writing a script that executes multiple helm install commands based on a config file.
In this config file the user needs to be able to specify additional arguments e.g. --set userPw="password".
Some of these arguments require retrieval of variables from somewhere.
In powershell, I can store this in a variable, and use this variable in the helm install --set argument as follows:
$mySecret = az keyvault secret show --name MYSECRET--vault-name MYVAULT| ConvertFrom-Json).value
helm install mydeployment repo/mychart -n my-namespace --set azureSecret=$mySecret
I can't do this dynamically however...
Different deployments ofcourse need different variables.
I want to use the config file rather than variables, since I don't want the users to edit the script that runs these commands.
Is something like this possible for helm install?:
helm install mydeployment repo/mychart -n my-namespace --set azureSecret=(az keyvault secret show --name MYSECRET--vault-name MYVAULT| ConvertFrom-Json).value)
In that case users would be able to put commands like this in the config file.
My syntax could be off, I tried some variations but it doesn't seem to work.
This script is only used by our team to easily deploy some stuff, so security is not an issue.
I think it isn't allowed according to:
https://github.com/helm/helm/issues/5145
Just want to make sure.
Helm in fact can't directly launch subcommands. Helm also disables the Sprig functions that can query the shell environment. The only input to templates is the values object built from the chart's values.yaml file and -f and --set options.
In a Unix-like environment you can use $(az keyvault ...) command-substitution syntax in your helm install command. (I know there are a couple of ways to run the GNU Bash shell on Windows and one of these might work.) The command you show is involved enough that writing a short script to run it would be beneficial, and if you have it in a script, your two-line Powershell script will work as well.
In a continuous-integration environment another approach that can work well is to write out a YAML values file, and then pass that via the helm install -f option. All valid JSON should be valid YAML, so you can set up e.g. a Jenkins pipeline to compute some values, make a Groovy map object, dump it as JSON to a file, and then use that as an input values file.
this is possible using simple bash command like EX. you want to get the password value from abc.txt file from your machine location you can simple do like this
helm install . --name abc --set userPw=userPw,[^"]*' /opt/abc.txt
use your bash command in inside `` to grep/sed to get the values from some file or get the values using command.

Closing parentheses in string dropped when creating app service app setting via Azure cli

I'm creating an Application Setting in an Azure App Service via the Azure cli. The value of the setting is a KeyVault reference which, if you're not familiar, has a special syntax:
#Microsoft.KeyVault(SecretUri=https://something.vault.azure.net/secrets/SomeKey/xxxxxxxxxx)
My powershell script creates the KeyVault secret and stores the secret id. I then construct the Application Setting value like:
$new_secret_id = "#Microsoft.KeyVault(SecretUri=$secret_id)"
I use Write-Host to verify $new_secret_id is exactly correct at this point.
Then I use the following command to create the Application Setting but the trailing paren is always missing and that causes the app setting to become a verbatim value instead of a KeyVault reference. If I hard-code a value instead of using the variable $secret_id it works, it does not strip the closing ).
az webapp config appsettings set `
--resource-group $rg `
--name $app_name `
--settings A_SECRET=$new_secret_id
Update
I've been trying with various combinations of values for $secred_id. It only seems to happen when the value is a URL.
This is already a documented problem (feature):
https://github.com/Azure/azure-cli/issues/8506
We made this change to help prevent the shell from interpreting the special characters in the setting value
As noted in the github comments, you can embed double quotes.
--settings "A_SECRET=""#Microsoft.KeyVault(SecretUri=$secret_id)"""

How do I escape a semicolon (;) character for an Azure DevOps SSH task?

I am trying to run a command like:
java -jar pathToJar/jarFile.jar "connectionstring=jdbc:impala:server:port;param1=value1"
I am running it via an Azure DevOps SSH task. The problem with this is that the semicolon character (;) is a special character (command separator). I've tried escaping with \ but that doesn't help.
With no escaping I see the following command has been run:
java -jar pathToJar/jarFile.jar "connectionstring=jdbc:impala:server:port
The ;param1=value1 suffix is missing.
Taking a look at the code on GitHub, I'm assuming that you have 'Commands' selected as the run option. By default it splits the command on ; or line break. In this case you need to switch the option to 'inline' or put it in a script file and run it that way.
Use 'inline' if you are going to have a single command that contains a semi-colon