Chocolatey - Pin Package

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

Allows pinning versions of packages 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

ChocolateyPackagePinId =

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

Examples:

  • git
  • git vscode notepadplusplus

(Optional) Package Version

ChocolateyPackagePinVersion =

If a specific version of the Chocolatey package is required enter it here. If you specify a version this will apply to all packages in ‘Package Name’. Otherwise, leave this field blank to use the latest version. Example: 2.3.4.

(Required) Pin Action

ChocolateyPackagePinAction = add

The Chocolatey pin action to take place. You can add a pin or remove an existing pin.

(Optional) Pin Reason

ChocolateyPackagePinReason =

The reason for the package being pinned.

NOTE: This only works with Chocolatey For Business editions 1.12.2+

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."
}

# check we have required parameters
if (-not $ChocolateyPackagePinId) {
    throw "Please specify the ID of an application package to install."
}

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

# base arguments to use with choco.exe
$chocoBaseArgs = @('pin', $ChocolateyPackagePinAction)

# this parameter only works in Chocolatey licensed editions
if ($ChocolateyPackagePinReason) {
 	# determine if this is a licensed edition
	$edition = & $choco
    if ($edition -like '*Business*' -and [version]$chocoVersion -ge [version]'1.12.2') {
    	Write-Output "Using reason '$ChocolateyPackagePinReason' when pinning packages."
    	$chocoBaseArgs += "--reason=""'$ChocolateyPackagePinReason'"""
    }
	else {
    	Write-Output "Using a reason for a package pin only works with Chocolatey For Business licensed editions. Ignoring the pin reason '$ChocolateyPackagePinReason'."
	}
}

if ($ChocolateyPackagePinVersion) {
	$chocoBaseArgs += "--version=$ChocolateyPackagePinVersion"
}

$chocoPackages = $ChocolateyPackagePinId -split ' '
ForEach ($package in $chocoPackages) {
	Write-Output "Pinning Chocolatey package $package."
    $chocoArgs = $chocoBaseArgs + @("--name=""'$package'""")
    
    # execute the command line
	Write-Output "Running the command: $choco $chocoArgs"
	& $choco $chocoArgs
}

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": "81d9b602-7969-4cb2-a9c0-b5c961937db4",
  "Name": "Chocolatey - Pin Package",
  "Description": "Allows pinning versions of packages using the Chocolatey package manager.",
  "Version": 1,
  "ExportedAt": "2020-08-10T17:30:40.400Z",
  "ActionType": "Octopus.Script",
  "Author": "pauby",
  "Packages": [],
  "Parameters": [
    {
      "Id": "2194aed6-a753-4e95-8067-b98a2a1a54bb",
      "Name": "ChocolateyPackagePinId",
      "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_",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "65b0dd7d-7643-4d55-8a63-d8506b674688",
      "Name": "ChocolateyPackagePinVersion",
      "Label": "(Optional) Package Version",
      "HelpText": "If a specific version of the Chocolatey package is required enter it here. If you specify a version this will apply to all packages in 'Package Name'. Otherwise, leave this field blank to use the latest version. Example: _2.3.4_.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "32722882-c1cc-42fe-b3c9-4405218795ba",
      "Name": "ChocolateyPackagePinAction",
      "Label": "(Required) Pin Action",
      "HelpText": "The Chocolatey pin action to take place. You can add a pin or remove an existing pin.",
      "DefaultValue": "add",
      "DisplaySettings": {
        "Octopus.ControlType": "Select",
        "Octopus.SelectOptions": "add|Add a package pin\nremove|Remove an existing package pin"
      }
    },
    {
      "Id": "00ef4776-72bc-4a1a-8a73-ee2a5c86a810",
      "Name": "ChocolateyPackagePinReason",
      "Label": "(Optional) Pin Reason",
      "HelpText": "The reason for the package being pinned. \n\n_NOTE: This only works with Chocolatey For Business editions 1.12.2+_",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptSource": "Inline",
    "Octopus.Action.Script.Syntax": "PowerShell",
    "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# check we have required parameters\nif (-not $ChocolateyPackagePinId) {\n    throw \"Please specify the ID of an application package to install.\"\n}\n\n$chocoVersion = & $choco --version\nWrite-Output \"Running Chocolatey version $chocoVersion\"\n\n# base arguments to use with choco.exe\n$chocoBaseArgs = @('pin', $ChocolateyPackagePinAction)\n\n# this parameter only works in Chocolatey licensed editions\nif ($ChocolateyPackagePinReason) {\n \t# determine if this is a licensed edition\n\t$edition = & $choco\n    if ($edition -like '*Business*' -and [version]$chocoVersion -ge [version]'1.12.2') {\n    \tWrite-Output \"Using reason '$ChocolateyPackagePinReason' when pinning packages.\"\n    \t$chocoBaseArgs += \"--reason=\"\"'$ChocolateyPackagePinReason'\"\"\"\n    }\n\telse {\n    \tWrite-Output \"Using a reason for a package pin only works with Chocolatey For Business licensed editions. Ignoring the pin reason '$ChocolateyPackagePinReason'.\"\n\t}\n}\n\nif ($ChocolateyPackagePinVersion) {\n\t$chocoBaseArgs += \"--version=$ChocolateyPackagePinVersion\"\n}\n\n$chocoPackages = $ChocolateyPackagePinId -split ' '\nForEach ($package in $chocoPackages) {\n\tWrite-Output \"Pinning Chocolatey package $package.\"\n    $chocoArgs = $chocoBaseArgs + @(\"--name=\"\"'$package'\"\"\")\n    \n    # execute the command line\n\tWrite-Output \"Running the command: $choco $chocoArgs\"\n\t& $choco $chocoArgs\n}",
    "Octopus.Action.EnabledFeatures": ""
  },
  "Category": "Chocolatey",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/chocolatey-pin-package.json",
  "Website": "/step-templates/81d9b602-7969-4cb2-a9c0-b5c961937db4",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAZhQTFRF////gLXjiEsuoG9YJCxcmcTpapS4cD8nuJOCoMjqlF1DrIFteqzbaZPBKjVkO05+k8DocJzDUnGgR1+PMD1tXoKwb5vKNUZ1daTSQVeGTGiXY4q5WHmodkIplWtXd0o0o3RdgkgsmmZOkVk+ga7YiFI5g5SqhmxnhmZbh1hFg42fhXNyj6/KhIeUs9PvgajMgpu2hXl9h19QhICJgqHBdEQtlGZQm2tVeUs1rYNvqXxosYl2roNwnW1WcEQwsId0mWpUm6m8onJbao+vmb/gkaK3oHRhs5SFfUw0nYF3fE44i6jGdkgygKfMkGJMiF5JbHSCnZSXcUYzqHtmdUYwa4Sdfpi0mrTOpXdhpHRec0Mrnm5XeYGRfqbMck4/noR8blpUhq/Ub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80d673y0sNtgzz7qVUTpPfa+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