Skip to content

Commit 55ce68b

Browse files
authored
[ARM] az deployment: Fix issue where Bicep is not found in CI environments (#31202)
1 parent ed5fb44 commit 55ce68b

File tree

4 files changed

+54
-24
lines changed

4 files changed

+54
-24
lines changed

src/azure-cli/azure/cli/command_modules/resource/_bicep.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,14 @@ def get_bicep_latest_release_tag():
220220
raise ClientRequestError(f"Error while attempting to retrieve the latest Bicep version: {err}.")
221221

222222

223-
def bicep_version_greater_than_or_equal_to(version):
224-
system = platform.system()
225-
installation_path = _get_bicep_installation_path(system)
226-
installed_version = _get_bicep_installed_version(installation_path)
223+
def bicep_version_greater_than_or_equal_to(cli_ctx, version):
224+
if _use_binary_from_path(cli_ctx):
225+
installed_version = _get_bicep_installed_version("bicep")
226+
else:
227+
system = platform.system()
228+
installation_path = _get_bicep_installation_path(system)
229+
installed_version = _get_bicep_installed_version(installation_path)
230+
227231
parsed_version = semver.VersionInfo.parse(version)
228232
return installed_version >= parsed_version
229233

src/azure-cli/azure/cli/command_modules/resource/custom.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,11 +1069,11 @@ def _parse_bicepparam_file(cmd, template_file, parameters):
10691069
ensure_bicep_installation(cmd.cli_ctx, stdout=False)
10701070

10711071
minimum_supported_version_bicepparam_compilation = "0.14.85"
1072-
if not bicep_version_greater_than_or_equal_to(minimum_supported_version_bicepparam_compilation):
1072+
if not bicep_version_greater_than_or_equal_to(cmd.cli_ctx, minimum_supported_version_bicepparam_compilation):
10731073
raise ArgumentUsageError(f"Unable to compile .bicepparam file with the current version of Bicep CLI. Please upgrade Bicep CLI to {minimum_supported_version_bicepparam_compilation} or later.")
10741074

10751075
minimum_supported_version_supplemental_param = "0.22.6"
1076-
if _get_parameter_count(parameters) > 1 and not bicep_version_greater_than_or_equal_to(minimum_supported_version_supplemental_param):
1076+
if _get_parameter_count(parameters) > 1 and not bicep_version_greater_than_or_equal_to(cmd.cli_ctx, minimum_supported_version_supplemental_param):
10771077
raise ArgumentUsageError(f"Current version of Bicep CLI does not support supplemental parameters with .bicepparam file. Please upgrade Bicep CLI to {minimum_supported_version_supplemental_param} or later.")
10781078

10791079
bicepparam_file = _get_bicepparam_file_path(parameters)
@@ -4681,9 +4681,9 @@ def format_bicep_file(cmd, file, stdout=None, outdir=None, outfile=None, newline
46814681
minimum_supported_version = "0.12.1"
46824682
kebab_case_params_supported_version = "0.26.54"
46834683

4684-
if bicep_version_greater_than_or_equal_to(minimum_supported_version):
4684+
if bicep_version_greater_than_or_equal_to(cmd.cli_ctx, minimum_supported_version):
46854685
args = ["format", file]
4686-
use_kebab_case_params = bicep_version_greater_than_or_equal_to(kebab_case_params_supported_version)
4686+
use_kebab_case_params = bicep_version_greater_than_or_equal_to(cmd.cli_ctx, kebab_case_params_supported_version)
46874687
newline_kind = newline_kind or newline
46884688

46894689
# Auto is no longer supported by Bicep formatter v2. Use LF as default.
@@ -4719,26 +4719,26 @@ def publish_bicep_file(cmd, file, target, documentationUri=None, documentation_u
47194719
minimum_supported_version = "0.4.1008"
47204720
kebab_case_param_supported_version = "0.26.54"
47214721

4722-
if bicep_version_greater_than_or_equal_to(minimum_supported_version):
4722+
if bicep_version_greater_than_or_equal_to(cmd.cli_ctx, minimum_supported_version):
47234723
args = ["publish", file, "--target", target]
4724-
use_kebab_case_params = bicep_version_greater_than_or_equal_to(kebab_case_param_supported_version)
4724+
use_kebab_case_params = bicep_version_greater_than_or_equal_to(cmd.cli_ctx, kebab_case_param_supported_version)
47254725
documentation_uri = documentation_uri or documentationUri
47264726

47274727
if documentation_uri:
47284728
minimum_supported_version_for_documentationUri_parameter = "0.14.46"
4729-
if bicep_version_greater_than_or_equal_to(minimum_supported_version_for_documentationUri_parameter):
4729+
if bicep_version_greater_than_or_equal_to(cmd.cli_ctx, minimum_supported_version_for_documentationUri_parameter):
47304730
args += ["--documentation-uri" if use_kebab_case_params else "--documentationUri", documentation_uri]
47314731
else:
47324732
logger.error("az bicep publish with --documentationUri/-d parameter could not be executed with the current version of Bicep CLI. Please upgrade Bicep CLI to v%s or later.", minimum_supported_version_for_documentationUri_parameter)
47334733
if with_source:
47344734
minimum_supported_version_for_publish_with_source = "0.23.1"
4735-
if bicep_version_greater_than_or_equal_to(minimum_supported_version_for_publish_with_source):
4735+
if bicep_version_greater_than_or_equal_to(cmd.cli_ctx, minimum_supported_version_for_publish_with_source):
47364736
args += ["--with-source"]
47374737
else:
47384738
logger.error("az bicep publish with --with-source/-s parameter could not be executed with the current version of Bicep CLI. Please upgrade Bicep CLI to v%s or later.", minimum_supported_version_for_publish_with_source)
47394739
if force:
47404740
minimum_supported_version_for_publish_force = "0.17.1"
4741-
if bicep_version_greater_than_or_equal_to(minimum_supported_version_for_publish_force):
4741+
if bicep_version_greater_than_or_equal_to(cmd.cli_ctx, minimum_supported_version_for_publish_force):
47424742
args += ["--force"]
47434743
else:
47444744
logger.error("az bicep publish with --force parameter could not be executed with the current version of Bicep CLI. Please upgrade Bicep CLI to v%s or later.", minimum_supported_version_for_publish_force)
@@ -4752,7 +4752,7 @@ def restore_bicep_file(cmd, file, force=None):
47524752
ensure_bicep_installation(cmd.cli_ctx)
47534753

47544754
minimum_supported_version = "0.4.1008"
4755-
if bicep_version_greater_than_or_equal_to(minimum_supported_version):
4755+
if bicep_version_greater_than_or_equal_to(cmd.cli_ctx, minimum_supported_version):
47564756
args = ["restore", file]
47574757
if force:
47584758
args += ["--force"]
@@ -4772,7 +4772,7 @@ def decompileparams_bicep_file(cmd, file, bicep_file=None, outdir=None, outfile=
47724772
ensure_bicep_installation(cmd.cli_ctx)
47734773

47744774
minimum_supported_version = "0.18.4"
4775-
if bicep_version_greater_than_or_equal_to(minimum_supported_version):
4775+
if bicep_version_greater_than_or_equal_to(cmd.cli_ctx, minimum_supported_version):
47764776
args = ["decompile-params", file]
47774777
if bicep_file:
47784778
args += ["--bicep-file", bicep_file]
@@ -4803,7 +4803,7 @@ def generate_params_file(cmd, file, no_restore=None, outdir=None, outfile=None,
48034803
ensure_bicep_installation(cmd.cli_ctx, stdout=False)
48044804

48054805
minimum_supported_version = "0.7.4"
4806-
if bicep_version_greater_than_or_equal_to(minimum_supported_version):
4806+
if bicep_version_greater_than_or_equal_to(cmd.cli_ctx, minimum_supported_version):
48074807
args = ["generate-params", file]
48084808
if no_restore:
48094809
args += ["--no-restore"]
@@ -4830,7 +4830,7 @@ def lint_bicep_file(cmd, file, no_restore=None, diagnostics_format=None):
48304830
ensure_bicep_installation(cmd.cli_ctx, stdout=False)
48314831

48324832
minimum_supported_version = "0.7.4"
4833-
if bicep_version_greater_than_or_equal_to(minimum_supported_version):
4833+
if bicep_version_greater_than_or_equal_to(cmd.cli_ctx, minimum_supported_version):
48344834
args = ["lint", file]
48354835
if no_restore:
48364836
args += ["--no-restore"]

src/azure-cli/azure/cli/command_modules/resource/tests/latest/test_resource_bicep.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from knack.util import CLIError
1313
from azure.cli.command_modules.resource._bicep import (
14+
bicep_version_greater_than_or_equal_to,
1415
ensure_bicep_installation,
1516
remove_bicep_installation,
1617
run_bicep_command,
@@ -273,4 +274,29 @@ def test_get_bicep_download_url_returns_correct_urls(self):
273274
self.assertEqual(download_url, "https://downloads.bicep.azure.com/v0.26.54/bicep-win-arm64.exe")
274275

275276
with self.assertRaises(CLIError):
276-
_get_bicep_download_url("Made Up", "x64", "v0.26.54")
277+
_get_bicep_download_url("Made Up", "x64", "v0.26.54")
278+
279+
@mock.patch("azure.cli.command_modules.resource._bicep._run_command")
280+
@mock.patch("azure.cli.command_modules.resource._bicep._use_binary_from_path")
281+
def test_bicep_version_greater_than_or_equal_to_use_binary_from_path(self, use_binary_from_path_mock, run_command_mock):
282+
use_binary_from_path_mock.return_value = True
283+
run_command_mock.return_value = "Bicep CLI version 0.13.1 (e3ac80d678)"
284+
285+
result = bicep_version_greater_than_or_equal_to(self.cli_ctx, "0.13.1")
286+
287+
self.assertTrue(result)
288+
run_command_mock.assert_called_once_with("bicep", ["--version"])
289+
290+
291+
@mock.patch("azure.cli.command_modules.resource._bicep._run_command")
292+
@mock.patch("azure.cli.command_modules.resource._bicep._get_bicep_installation_path")
293+
@mock.patch("azure.cli.command_modules.resource._bicep._use_binary_from_path")
294+
def test_bicep_version_greater_than_or_equal_to_use_cli_managed_binary(self, use_binary_from_path_mock, get_bicep_installation_path_mock, run_command_mock):
295+
use_binary_from_path_mock.return_value = False
296+
get_bicep_installation_path_mock.return_value = ".azure/bin/bicep"
297+
run_command_mock.return_value = "Bicep CLI version 0.13.1 (e3ac80d678)"
298+
299+
result = bicep_version_greater_than_or_equal_to(self.cli_ctx, "0.13.2")
300+
301+
self.assertFalse(result)
302+
run_command_mock.assert_called_once_with(".azure/bin/bicep", ["--version"])

src/azure-cli/azure/cli/command_modules/resource/tests/latest/test_resource_custom.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -671,8 +671,8 @@ def test_format_bicep_file(self, mock_print, mock_run_bicep_command, mock_bicep_
671671

672672
# Assert.
673673
mock_bicep_version_greater_than_or_equal_to.assert_has_calls([
674-
mock.call("0.12.1"),
675-
mock.call("0.26.54"),
674+
mock.call(cmd.cli_ctx, "0.12.1"),
675+
mock.call(cmd.cli_ctx, "0.26.54"),
676676
])
677677
mock_run_bicep_command.assert_called_once_with(cmd.cli_ctx, ["format", file_path, "--stdout"])
678678

@@ -689,7 +689,7 @@ def test_publish_withsource(self, mock_run_bicep_command, mock_bicep_version_gre
689689

690690
# Assert.
691691
mock_bicep_version_greater_than_or_equal_to.assert_has_calls([
692-
mock.call("0.4.1008"), # Min version for 'bicep publish'
692+
mock.call(cmd.cli_ctx, "0.4.1008"), # Min version for 'bicep publish'
693693
])
694694
mock_run_bicep_command.assert_called_once_with(cmd.cli_ctx, ['publish', file_path, '--target', 'br:contoso.azurecr.io/bicep/mymodule:v1'])
695695

@@ -705,9 +705,9 @@ def test_publish_without_source(self, mock_run_bicep_command, mock_bicep_version
705705

706706
# Assert.
707707
mock_bicep_version_greater_than_or_equal_to.assert_has_calls([
708-
mock.call("0.4.1008"), # Min version for 'bicep publish'
709-
mock.call('0.26.54'),
710-
mock.call("0.23.1") # Min version for 'bicep publish --with-source'
708+
mock.call(cmd.cli_ctx, "0.4.1008"), # Min version for 'bicep publish'
709+
mock.call(cmd.cli_ctx, '0.26.54'),
710+
mock.call(cmd.cli_ctx, "0.23.1") # Min version for 'bicep publish --with-source'
711711
])
712712
mock_run_bicep_command.assert_called_once_with(cmd.cli_ctx, ['publish', file_path, '--target', 'br:contoso.azurecr.io/bicep/mymodule:v1', '--with-source'])
713713

0 commit comments

Comments
 (0)