Skip to content

Commit

Permalink
Fail action when module installation fails #202
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieWhite committed Aug 11, 2021
1 parent 061f35d commit 4714287
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ What's changed since v1.0.0:
- Bump PSRule dependency to v1.6.0. [#200](https://github.com/microsoft/PSRule-pipelines/issues/200)
- See the [change log](https://github.com/microsoft/PSRule/blob/main/docs/CHANGELOG-v1.md#v160).
- Bump azure-pipelines-task-lib to 3.1.6. [#195](https://github.com/microsoft/PSRule-pipelines/pull/195)
- Bug fixes:
- Fixed assert task to ensure it fails when a dependency module installation fails. [#202](https://github.com/microsoft/PSRule-pipelines/issues/202)

## v1.0.0

Expand Down
43 changes: 31 additions & 12 deletions tasks/ps-rule-assert/powershell.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -106,24 +106,43 @@ $moduleParams = @{

# Install each module if not already installed
foreach ($m in $moduleNames) {
$m = $m.Trim();
Write-Host "> Checking module: $m";
if ($Null -eq (Get-InstalledModule -Name $m -ErrorAction Ignore)) {
Write-Host ' - Installing module';
$Null = Install-Module -Name $m @moduleParams -AllowClobber;
}
else {
Write-Host ' - Already installed';
}
# Check
if ($Null -eq (Get-InstalledModule -Name $m)) {
Write-Host ' - Failed to install';
try {
if ($Null -eq (Get-InstalledModule -Name $m -ErrorAction Ignore)) {
Write-Host ' - Installing module';
$Null = Install-Module -Name $m @moduleParams -AllowClobber -ErrorAction Stop;
}
else {
Write-Host ' - Already installed';
}
# Check
if ($Null -eq (Get-InstalledModule -Name $m)) {
Write-Host ' - Failed to install';
}
else {
Write-Host " - Using version: $((Get-InstalledModule -Name $m).Version)";
}
}
else {
Write-Host " - Using version: $((Get-InstalledModule -Name $m).Version)";
catch {
Write-Host "`#`#vso[task.logissue type=error]$(Get-VstsLocString -Key 'DependencyFailed')";
Write-Host "`#`#vso[task.complete result=Failed;]FAILED";
$Host.SetShouldExit(1);
}
}

try {
$Null = Import-Module PSRule -ErrorAction Stop;
$version = (Get-InstalledModule PSRule).Version;
}
catch {
Write-Host "`#`#vso[task.logissue type=error]$(Get-VstsLocString -Key 'ImportFailed')";
Write-Host "`#`#vso[task.complete result=Failed;]FAILED";
$Host.SetShouldExit(1);
}

Write-Host '';
Write-Host "[info] Using Version: $version";
Write-Host "[info] Using PWD: $PWD";
Write-Host "[info] Using Path: $Path";
Write-Host "[info] Using Source: $Source";
Expand Down
4 changes: 3 additions & 1 deletion tasks/ps-rule-assert/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@
}
},
"messages": {
"AssertFailed": "One or more assertions failed."
"AssertFailed": "One or more assertions failed.",
"DependencyFailed": "An error occurred installing a dependency module.",
"ImportFailed": "An error occurred importing module 'PSRule'."
}
}

0 comments on commit 4714287

Please sign in to comment.