Chocolatey - Install Package

Octopus.Script exported 2020-08-10 by bobjwalker belongs to ‘Chocolatey’ category.

Installs or upgrades a package using the Chocolatey package manager.

Parameters

When steps based on the template are included in a project’s deployment process, the parameters below can be set.

(Required) Package Name

ChocolateyPackageId =

The name of the Chocolatey package to install. Install multiple packages by separating them with a space.

Examples:

  • git
  • git vscode notepadplusplus

(Optional) Version

ChocolateyPackageVersion =

If a specific version of the Chocolatey package is required enter it here. Otherwise, leave this field blank to use the latest version. Example: 2.3.4.

(Optional) Cache Location

ChocolateyCacheLocation =

Use a specific folder to cache the Chocolatey package.

(Optional) Package Source

ChocolateySource =

Use a package source to install Chocolatey packages from. This can be a http or https URL, a local folder or file share. When using URL’s don’t forget the trailing / at the end.

For example:

(Optional) Disable Download Progress

ChocolateyNoProgress =

Disables the download progress percentage being displayed as this can generate a lot of output for the logs.

(Optional) Other Parameters

ChocolateyOtherParameters =

Add additional Chocolatey parameters here, separated by spaces, and they will be prefixed to the end of the Chocolatey command being executed. Examples:

  • —ignorepackagecodes
  • —ignorepackagecodes —requirechecksum —allow-downgrade

Script body

Steps based on this template will execute the following PowerShell script.

[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
$chocolateyBin = [Environment]::GetEnvironmentVariable("ChocolateyInstall", "Machine") + "\bin"
if(-not (Test-Path $chocolateyBin)) {
    Write-Output "Environment variable 'ChocolateyInstall' was not found in the system variables. Attempting to find it in the user variables..."
    $chocolateyBin = [Environment]::GetEnvironmentVariable("ChocolateyInstall", "User") + "\bin"
}

$choco = "$chocolateyBin\choco.exe"

if (-not (Test-Path $choco)) {
    throw "Chocolatey was not found at $chocolateyBin."
}

$chocoArgs = @('install')
if (-not $ChocolateyPackageId) {
    throw "Please specify the ID of an application package to install."
}
else {
    $chocoArgs += $ChocolateyPackageId -split ' '
}

$chocoVersion = & $choco --version
Write-Output "Running Chocolatey version $chocoVersion"

if (-not $ChocolateyPackageVersion) {
    Write-Output "Installing package(s) $ChocolateyPackageId from the Chocolatey package repository..."
} else {
    Write-Output "Installing package $ChocolateyPackageId version $ChocolateyPackageVersion from the Chocolatey package repository..."
    $chocoArgs += @('--version', $ChocolateyPackageVersion)
}

if([System.Version]::Parse($chocoVersion) -ge [System.Version]::Parse("0.9.8.33")) {
    Write-Output "Adding --yes to arguments passed to Chocolatey"
    $chocoArgs += @("--yes")
}

if (![String]::IsNullOrEmpty($ChocolateyCacheLocation)) {
    Write-Output "Using --cache-location $ChocolateyCacheLocation"
    $chocoArgs += @("--cache-location", "`"'$ChocolateyCacheLocation'`"")
}

if (![String]::IsNullOrEmpty($ChocolateySource)) {
    Write-Output "Using package --source $ChocolateySource"
    $chocoArgs += @('--source', "`"'$ChocolateySource'`"")
}

if (($ChocolateyNoProgress -eq 'True') -and ([System.Version]::Parse($chocoVersion) -ge [System.Version]::Parse("0.10.4"))) {
    Write-Output "Disabling download progress with --no-progress"
    $chocoArgs += @('--no-progress')
}

if (![String]::IsNullOrEmpty($ChocolateyOtherParameters)) {
	$chocoArgs += $ChocolateyOtherParameters -split ' '
}

# execute the command line
Write-Output "Running the command: $choco $chocoArgs"
& $choco $chocoArgs

if ($global:LASTEXITCODE -eq 3010) { 
	# ignore reboot required exit code
    Write-Output "A restart may be required for the package to work"
    $global:LASTEXITCODE = 0
}

Provided under the Apache License version 2.0.

Report an issue

To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.

{
  "Id": "b2385b12-e5b5-440f-bed8-6598c29b2528",
  "Name": "Chocolatey - Install Package",
  "Description": "Installs or upgrades a package using the Chocolatey package manager.",
  "Version": 6,
  "ExportedAt": "2020-08-10T15:59:10.300Z",
  "ActionType": "Octopus.Script",
  "Author": "bobjwalker",
  "Parameters": [
    {
      "Id": "449e25a9-2984-4cb7-b190-ae5114cfbb19",
      "Name": "ChocolateyPackageId",
      "Label": "(Required) Package Name",
      "HelpText": "The name of the Chocolatey package to install. Install multiple packages by separating them with a space.\n\nExamples:\n\n* _git_\n* _git_ _vscode_ _notepadplusplus_\n",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "17ff15ac-f925-493c-9330-64180687f44c",
      "Name": "ChocolateyPackageVersion",
      "Label": "(Optional) Version",
      "HelpText": "If a specific version of the Chocolatey package is required enter it here. Otherwise, leave this field blank to use the latest version. Example: _2.3.4_.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "8bad47c8-9537-4e8d-8ff4-0508df29ed67",
      "Name": "ChocolateyCacheLocation",
      "Label": "(Optional) Cache Location",
      "HelpText": "Use a specific folder to cache the Chocolatey package.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "e4251d60-1a9c-4ee4-aa49-bd2ecfd5d64e",
      "Name": "ChocolateySource",
      "Label": "(Optional) Package Source",
      "HelpText": "Use a package source to install Chocolatey packages from. This can be a http or https URL, a local folder or file share. When using URL's don't forget the trailing `/` at the end.\n\nFor example: \n\n* _https://chocolatey.org/api/v2/_\n* _c:\\choco-packages_\n* _\\\\business.server.local\\chocolatey-packages_",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "7790d99f-9fb2-4cfb-bdcb-74b4d254dba7",
      "Name": "ChocolateyNoProgress",
      "Label": "(Optional) Disable Download Progress",
      "HelpText": "Disables the download progress percentage being displayed as this can generate a lot of output for the logs.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "Checkbox"
      }
    },
    {
      "Id": "e994f28e-f306-4af9-90da-0ad68ec8f30c",
      "Name": "ChocolateyOtherParameters",
      "Label": "(Optional) Other Parameters",
      "HelpText": "Add additional Chocolatey parameters here, separated by spaces, and they will be prefixed to the end of the Chocolatey command being executed. Examples:\n\n* _--ignorepackagecodes_\n* _--ignorepackagecodes --requirechecksum --allow-downgrade_",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptBody": "[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12\n$chocolateyBin = [Environment]::GetEnvironmentVariable(\"ChocolateyInstall\", \"Machine\") + \"\\bin\"\nif(-not (Test-Path $chocolateyBin)) {\n    Write-Output \"Environment variable 'ChocolateyInstall' was not found in the system variables. Attempting to find it in the user variables...\"\n    $chocolateyBin = [Environment]::GetEnvironmentVariable(\"ChocolateyInstall\", \"User\") + \"\\bin\"\n}\n\n$choco = \"$chocolateyBin\\choco.exe\"\n\nif (-not (Test-Path $choco)) {\n    throw \"Chocolatey was not found at $chocolateyBin.\"\n}\n\n$chocoArgs = @('install')\nif (-not $ChocolateyPackageId) {\n    throw \"Please specify the ID of an application package to install.\"\n}\nelse {\n    $chocoArgs += $ChocolateyPackageId -split ' '\n}\n\n$chocoVersion = & $choco --version\nWrite-Output \"Running Chocolatey version $chocoVersion\"\n\nif (-not $ChocolateyPackageVersion) {\n    Write-Output \"Installing package(s) $ChocolateyPackageId from the Chocolatey package repository...\"\n} else {\n    Write-Output \"Installing package $ChocolateyPackageId version $ChocolateyPackageVersion from the Chocolatey package repository...\"\n    $chocoArgs += @('--version', $ChocolateyPackageVersion)\n}\n\nif([System.Version]::Parse($chocoVersion) -ge [System.Version]::Parse(\"0.9.8.33\")) {\n    Write-Output \"Adding --yes to arguments passed to Chocolatey\"\n    $chocoArgs += @(\"--yes\")\n}\n\nif (![String]::IsNullOrEmpty($ChocolateyCacheLocation)) {\n    Write-Output \"Using --cache-location $ChocolateyCacheLocation\"\n    $chocoArgs += @(\"--cache-location\", \"`\"'$ChocolateyCacheLocation'`\"\")\n}\n\nif (![String]::IsNullOrEmpty($ChocolateySource)) {\n    Write-Output \"Using package --source $ChocolateySource\"\n    $chocoArgs += @('--source', \"`\"'$ChocolateySource'`\"\")\n}\n\nif (($ChocolateyNoProgress -eq 'True') -and ([System.Version]::Parse($chocoVersion) -ge [System.Version]::Parse(\"0.10.4\"))) {\n    Write-Output \"Disabling download progress with --no-progress\"\n    $chocoArgs += @('--no-progress')\n}\n\nif (![String]::IsNullOrEmpty($ChocolateyOtherParameters)) {\n\t$chocoArgs += $ChocolateyOtherParameters -split ' '\n}\n\n# execute the command line\nWrite-Output \"Running the command: $choco $chocoArgs\"\n& $choco $chocoArgs\n\nif ($global:LASTEXITCODE -eq 3010) { \n\t# ignore reboot required exit code\n    Write-Output \"A restart may be required for the package to work\"\n    $global:LASTEXITCODE = 0\n}",
    "Octopus.Action.Script.Syntax": "PowerShell",
    "Octopus.Action.Script.ScriptSource": "Inline",
    "Octopus.Action.EnabledFeatures": ""
  },
  "Category": "Chocolatey",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/chocolatey-install-package.json",
  "Website": "/step-templates/b2385b12-e5b5-440f-bed8-6598c29b2528",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAZhQTFRF////gLXjiEsuoG9YJCxcmcTpapS4cD8nuJOCoMjqlF1DrIFteqzbaZPBKjVkO05+k8DocJzDUnGgR1+PMD1tXoKwb5vKNUZ1daTSQVeGTGiXY4q5WHmodkIplWtXd0o0o3RdgkgsmmZOkVk+ga7YiFI5g5SqhmxnhmZbh1hFg42fhXNyj6/KhIeUs9PvgajMgpu2hXl9h19QhICJgqHBdEQtlGZQm2tVeUs1rYNvqXxosYl2roNwnW1WcEQwsId0mWpUm6m8onJbao+vmb/gkaK3oHRhs5SFfUw0nYF3fE44i6jGdkgygKfMkGJMiF5JbHSCnZSXcUYzqHtmdUYwa4Sdfpi0mrTOpXdhpHRec0Mrnm5XeYGRfqbMck4/noR8blpUh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1v3z80d673y0sNtgzz7qVUTpPfa+WogG+tL3W73Wg3S6123vDo/esn82omTU6x8fnN+GOQ7y6oN0vv96YuDLK6tdEEZiPpxbXHook19zdZ8XY7FhW53ZXl7cSMHufCqNQ1I747Hjl7cFd0REPX4rZID5rfwipXNeu4oPnxheWtVfeDV5+6xpgPp9f5ZnRQSm1snumUNgCidWDceOLmSX7M00Smbq9vzo+4Y1PKPljU1yDXKHEvbw6acX10becwwiF73qrp2u3zRwuoIwsvPbC+BSZYmuKPQ9XsDQWtsLWYWO7l+YsxDxoGoha8NX7u8Uaz2wbu/+PuJ/BHdteKd9YVuSyAQ42vrm3luVwMZo4VtnV2Lyg03zM3NHS8/InPXxvL4W5sC2V1VQXQhwv9HQLqLu7jjAIJkGgVZODnZHTMF0l3YmOiO2QLZXQRCIARCIARCIARCIKj7jlTVkwjyR+UbjryHIB9XvuGn6UFOn7Kq6gyAPPRi5Rusu24FkI+s2qoNUoPDev4dDfJNnfXcpkF2bm8fpA6HZZ1VIC/cWeeOW84okLes1kF2aq3K+l6BPFBvQb/MHX/75tZBdmo+4q5bj/UeqXeL8shpq22Qndqmuu3YUzXj/bnfjv/cNkh9DuuWd9+se8vZD061DLIzTeh+9mfdO75+1GoZ5MvDU+j92nd8de80zzl8iEQikUgkEolEIpFIJBKJRCKRSCQSiUQikUgkEolEIl1O+k+AAQDVNykpMZlchAAAAABJRU5ErkJggg==",
  "$Meta": {
    "Type": "ActionTemplate"
  }
}

History

Page updated on Monday, August 10, 2020