简体   繁体   中英

Azure CLI in Git Bash

I am trying to use a bash (sh) script on windows to run a test deployment. I am running the script from the gitbash console so that I have a copy of bash, but doing so means that the azure clie is not available (ie azure command is not found). Does anyone know how I can get the Azure cli working in GitBash (I am assuming I just install it somewhere else) or should I change to a different way of using bash

Sometimes commands in windows git bash need .cmd appended. Also, another way of installing the Azure-Cli is through Chocolatey https://chocolatey.org/

Try this command after Azure-Cli is installed:

az.cmd --version

Echoing mscrivo you can run the line below in CMD not PowerShell (elevated/admin)

echo "C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin\az.cmd" $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14} ${15} > "C:\Program Files\Git\mingw64\bin\az" 

Now you should be able to run in Git bash:

az --version

artberri noted the best solution in a comment:

Add the following to your %USERPROFILE%\\.bashrc or %USERPROFILE%\\.profile

alias az='az.cmd'

However, if you want to be able to use az in bash scripts, you'll need something a little more drastic, run the following from cmd prompt:

echo "C:\\Program Files (x86)\\Microsoft SDKs\\Azure\\CLI2\\wbin\\az.cmd" $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14} ${15} > "%SYSTEMROOT%\\az"

That will essentially create a passthrough az command in your windows folder that can be run from anywhere and passes parameters through to az.cmd.

In case using Git bash, navigate to the following directory:

C:\\Program Files\\Git\\etc\\profile.d

Git bash 目录的 Windows 资源管理器

Edit aliases.sh , then add a new alias for az as below:

alias az='az.cmd'

aliases.sh 示例

You have to install the CLI to your computer. There are multiple ways to do that.

I'm a friend of NodeJS so i use npm for the installation:

npm install -g azure-cli

More details here: https://www.npmjs.com/package/azure-cli

But you can do it also in other ways. A very nice way is to use docker. There are containers from Microsoft with a preinstalled version of Azure CLI.

docker run -it --name azure microsoft/azure-cli

On Windows 10 with the ubuntu bash you can use:

echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ wheezy main" | \
     sudo tee /etc/apt/sources.list.d/azure-cli.list

sudo apt-key adv --keyserver packages.microsoft.com --recv-keys 417A0893
sudo apt-get install apt-transport-https
sudo apt-get update && sudo apt-get install azure-cli

Or as a python enthusiast run

pip install --user azure-cli

Most important is that the "az"/"az.bat" or "azure" bin is available via your path variable.

In other words Azure CLI for Windows is not compatible with Git Bash for Windows

https://github.com/Azure/azure-cli/issues/3445

In your ~/bin directory (in Windows it means c:\\Users\\<username>\\bin ) I've just created a file named az with:

"C:/Program Files (x86)/Microsoft SDKs/Azure/CLI2/python.exe" -IBm azure.cli "$@"

Then make the file executable chmod a+x az

The content is "borrowed" from az.cmd.

When trying an alias approach mentioned before I had a problem with long commands, with a lot of parameters throwing an error "'C:\\Program' is not recognized as an internal or external command,operable program or batch file."

EDIT:

I've ended using Ubuntu through WLS. For all tools like az, terraform, kubectl, istioctl. The az tool runs good in interactive mode as well.

So I arrived here looking for a way to run the same az commands in a bash shell script on Azure DevOps Linux and Windows (git bash) build agents, so I could share the same code across both types of agent. This also works for a git bash shell on Windows 10. My longer answer is here: Azure DevOps Build Pipeline can't get secrets from Key Vault when secured with vnet and firewall

The gist of it is:

if [[ $(uname -s) == "Linux" ]]; then
    azcmd="az"
else
    # If we're in a bash shell on Windows, az commands don't work, but we can call the az.cmd batch file directly from git Bash if we can find it...
    azcmd=$(where az.cmd)
fi

# Remember to specify CIDR /32 for removal
"$azcmd" keyvault network-rule remove -n <MyKeyVault> --ip-address 50.1.1.1/32

Basically just substitute "$azcmd" wherever you'd normally use az once the bootstrap code has executed.

My previous approach for this was just to add the Azure CLI Scripts folder to the $PATH inside my ~/.bashrc file. But, after updating Azure CLI to 2.2.0 via the MSI, this approach started to fail with this error:

C:\\Program: can't open file 'Files': [Errno 2] No such file or directory

So, to fix this, I have my $PATH in ~/.bashrc include ~/bin and then I created a file with the following content as ~/bin/az (don't forget to chmod 0755 the new file):

#!/usr/bin/env bash

AZURE_CLI_PATH="/c/Program Files (x86)/Microsoft SDKs/Azure/CLI2"

export PATH="${PATH}:${AZURE_CLI_PATH}:${AZURE_CLI_PATH}/Scripts"
export PYTHONPATH="${AZURE_CLI_PATH}/src"
export PYTHON_CMD="$(which python)"

winpty "${PYTHON_CMD}" -m 'azure.cli' "${@}"

After closing out my GIT Bash window and re-opening it, I can now run az again:

$ az version
This command is in preview. It may be changed/removed in a future release.
{
  "azure-cli": "2.2.0",
  "azure-cli-command-modules-nspkg": "2.0.3",
  "azure-cli-core": "2.2.0",
  "azure-cli-nspkg": "3.0.4",
  "azure-cli-telemetry": "1.0.4",
  "extensions": {}
}

Don't use the MSI installer at all. Since the Azure CLI is implemented in Python, use the Python installation method as @blndev wrote. This way instead of az.cmd you get az.bat and az shell script, and the installation path will not contain spaces.

pip install --user azure-cli

More detailed info on this method can be found at https://blogs.msdn.microsoft.com/brijrajsingh/2017/03/02/installing-azure-cli-2-0-on-windows/

The symlink worked for me most of the times, but some commands are still failing, eg

az dls fs access set-entry ...
'C:\Program' is not recognized as an internal or external command, operable program or batch file

I tried @mscrivo's solution. When using the az command in a shell script however you still have problems due to the spaces in the path. Therefor I created a azproxy.cmd in %SYSTEMROOT% containing

@echo off
"C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin\az.cmd" %*

And then create the mklink to that file

mklink "%SYSTEMROOT%\az" "SYSTEMROOT%\azproxy.cmd"

PS the expanded value of %SYSTEMROOT% should not contain spaces of course

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM