如何让GitHub Codespaces默认安装Azure CLI?
解决GitHub Codespaces自动安装Azure CLI的问题
问题根源
你配置的ms-vscode.azurecli只是VSCode的扩展,仅能在编辑器里提供Azure CLI的集成功能,但系统终端里并没有安装实际的Azure CLI程序,所以终端执行az命令时会提示bash: az: command not found。
两种可行的自动安装方案
方案1:用Dev Container官方Feature(推荐)
Dev Container官方提供了现成的Azure CLI安装Feature,直接在devcontainer.json的features节点里添加即可,这是最规范的实现方式:
在features对象中加入以下内容:
"ghcr.io/devcontainers/features/azure-cli:1": {}
修改后的完整features节点如下:
"features": { "ghcr.io/devcontainers/features/docker-in-docker:2": {}, "ghcr.io/devcontainers/features/github-cli:1": { "version": "2" }, "ghcr.io/devcontainers/features/powershell:1": { "version": "latest" }, "ghcr.io/azure/azure-dev/azd:0": { "version": "latest" }, "ghcr.io/devcontainers/features/common-utils:2": {}, "ghcr.io/devcontainers/features/dotnet:2": { "version": "none", "dotnetRuntimeVersions": "7.0", "aspNetCoreRuntimeVersions": "7.0" }, "ghcr.io/devcontainers/features/azure-cli:1": {} }
方案2:通过postCreateCommand执行安装脚本
如果不想用官方Feature,可以修改postCreateCommand,把Azure CLI的安装命令追加到现有命令前,替换原来的postCreateCommand字段:
"postCreateCommand": "curl -L https://aka.ms/InstallAzureCli | sudo bash && cd ./SampleApp && dotnet restore"
注:脚本里的sudo已经能处理权限问题,不需要先切换到root用户。
验证步骤
重新创建Codespaces后,打开终端输入az --version,如果能正常显示Azure CLI的版本信息,就说明自动安装成功了。
内容的提问来源于stack exchange,提问作者Diskdrive
相关产品推荐
相关产品推荐

