Octopus - Validate Deploying User

Octopus.Script exported 2021-10-22 by handplaned belongs to ‘Octopus’ category.

Verifies current deploying user didn’t deploy previously to specified environment.

Parameters

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

Octopus Base Url

ovdu_octopusURL =

The base url of your Octopus Deploy instance. Example: https://samples.octopus.app

Octopus Api Key

ovdu_octopusAPIKey =

The API key of a user in Octopus Deploy who has permissions to manage releases.

Environment to validate deployer against

ovdu_precedingEnvironment =

The environment preceding the controlled environment where if the user deployed this release, it will fail in this environment

Script body

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

$releaseId = $OctopusParameters["Octopus.Release.Id"]
$currentDeployerId = $OctopusParameters["Octopus.Deployment.CreatedBy.Id"]
$userName = $OctopusParameters["Octopus.Deployment.CreatedBy.Username"]
$environmentId = $OctopusParameters["Octopus.Environment.Id"]
$spaceId = $OctopusParameters["Octopus.Space.Id"]
$octopusURL = $OctopusParameters["ovdu_octopusURL"]
$octopusAPIKey = $OctopusParameters["ovdu_octopusAPIKey"]
$precedingEnvironment = $OctopusParameters["ovdu_precedingEnvironment"]

$header = @{ "X-Octopus-ApiKey" = $octopusAPIKey }

$deploymentDetails = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/$($spaceId)/releases/$($releaseId)/deployments/" -Headers $header)

# Get details for deployment to preceding environment
$allEnvironments = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/$($spaceId)/environments" -Headers $header)
$environmentItem = $allEnvironments.Items | where-object { $_.Name -eq $precedingEnvironment }
$environmentId = $environmentItem.Id

# Load all deploys to the previous environment
$environmentDeploys = $deploymentDetails.Items | Where-Object {$_.EnvironmentId -eq $environmentId}

# Iterate deployments to the previous environment to validate current deployer
foreach($prevdeployment in $environmentDeploys)
    {
    	if($prevDeployment.Id -eq $OctopusParameters["Octopus.Deployment.Id"])
        {continue}
    	else
        {
    		if($prevdeployment.DeployedById -eq $currentDeployerId )
        	{
            	Write-Highlight "$userName previously deployed this project to $precedingEnvironment - deployment cancelled."
            	Throw "$userName previously deployed this project to $precedingEnvironment - deployment cancelled."
        	}
        }
    }

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": "bbcf1894-9cf7-4968-a3e4-3bff08d8c75b",
  "Name": "Octopus - Validate Deploying User",
  "Description": "Verifies current deploying user didn't deploy previously to specified environment.",
  "Version": 1,
  "ExportedAt": "2021-10-22T15:08:53.042Z",
  "ActionType": "Octopus.Script",
  "Author": "handplaned",
  "Packages": [],
  "Parameters": [
    {
      "Id": "e99ae8d8-f63c-45b5-967c-703cc89e620c",
      "Name": "ovdu_octopusURL",
      "Label": "Octopus Base Url",
      "HelpText": "The base url of your Octopus Deploy instance.  Example: https://samples.octopus.app",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "781b3394-138e-4aa5-bd26-5a835563bf88",
      "Name": "ovdu_octopusAPIKey",
      "Label": "Octopus Api Key",
      "HelpText": "The API key of a user in Octopus Deploy who has permissions to manage releases.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "Sensitive"
      }
    },
    {
      "Id": "a05c4f99-ae6e-48ea-a11e-2cb3b538bf7e",
      "Name": "ovdu_precedingEnvironment",
      "Label": "Environment to validate deployer against",
      "HelpText": "The environment preceding the controlled environment where if the user deployed this release, it will fail in this environment",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.RunOnServer": "false",
    "Octopus.Action.Script.ScriptSource": "Inline",
    "Octopus.Action.Script.Syntax": "PowerShell",
    "Octopus.Action.Script.ScriptBody": "$releaseId = $OctopusParameters[\"Octopus.Release.Id\"]\n$currentDeployerId = $OctopusParameters[\"Octopus.Deployment.CreatedBy.Id\"]\n$userName = $OctopusParameters[\"Octopus.Deployment.CreatedBy.Username\"]\n$environmentId = $OctopusParameters[\"Octopus.Environment.Id\"]\n$spaceId = $OctopusParameters[\"Octopus.Space.Id\"]\n$octopusURL = $OctopusParameters[\"ovdu_octopusURL\"]\n$octopusAPIKey = $OctopusParameters[\"ovdu_octopusAPIKey\"]\n$precedingEnvironment = $OctopusParameters[\"ovdu_precedingEnvironment\"]\n\n$header = @{ \"X-Octopus-ApiKey\" = $octopusAPIKey }\n\n$deploymentDetails = (Invoke-RestMethod -Method Get -Uri \"$octopusURL/api/$($spaceId)/releases/$($releaseId)/deployments/\" -Headers $header)\n\n# Get details for deployment to preceding environment\n$allEnvironments = (Invoke-RestMethod -Method Get -Uri \"$octopusURL/api/$($spaceId)/environments\" -Headers $header)\n$environmentItem = $allEnvironments.Items | where-object { $_.Name -eq $precedingEnvironment }\n$environmentId = $environmentItem.Id\n\n# Load all deploys to the previous environment\n$environmentDeploys = $deploymentDetails.Items | Where-Object {$_.EnvironmentId -eq $environmentId}\n\n# Iterate deployments to the previous environment to validate current deployer\nforeach($prevdeployment in $environmentDeploys)\n    {\n    \tif($prevDeployment.Id -eq $OctopusParameters[\"Octopus.Deployment.Id\"])\n        {continue}\n    \telse\n        {\n    \t\tif($prevdeployment.DeployedById -eq $currentDeployerId )\n        \t{\n            \tWrite-Highlight \"$userName previously deployed this project to $precedingEnvironment - deployment cancelled.\"\n            \tThrow \"$userName previously deployed this project to $precedingEnvironment - deployment cancelled.\"\n        \t}\n        }\n    }"
  },
  "Category": "Octopus",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/octopus-validate-deploying-user.json",
  "Website": "/step-templates/bbcf1894-9cf7-4968-a3e4-3bff08d8c75b",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAC1QTFRFT6Tl////L5Pg8vj9Y67omsvwPJrisdfzfbzs5fL7y+T32Ov5isLucLXqvt31CJPHWwAABMJJREFUeNr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pro8432DPD/ySx8XrHfSuDAM8n6UhnjQabaiXf5Bq/lREHvEeNtn1rJ08+C/uXkQZHeguxAPC3UvtcJYUogLzZX5hhZZvS6onG5lxXtzWGaygwb79vT/IXhdlNibwlKYOR6T8xjI7W8n+xV7T+GH4tMzWwR+lZhRkJYSsC0thpmCYqyngOz3rN2FLBZ2wZflBCggUHF0Vnp88JKienzIXLSEZCZqU7IKr/gQW9yx3pzV7Y9kvWZWTRRIqDmTtRUnU7b2lLcTYmoqHqnmiO1poER0SPkAeZMAZxaJx0Y3TCdAclsIqDz03ALcyxfTCZBsthoGXWmigGyVhWPLFJJfuuKQWycoEFdXbH4dJJoJxNR1eD/kshz6yn48cF8yW8sFoitflB1w6Q8n+/15Za7oA17/pYNmYgP5fmWm8L1NOHPWgK8kuFew1/JXtOA0yJCv7ah7X8ObUuT5kObU30+fDZm8+zqP+HTIpK0xQ796b5Kv2hSIQAQiEIEIRCACEYhABCIQgQhEIAIRiEAEIpBf8UeAAQAEjtYmlDTcCgAAAABJRU5ErkJggg==",
  "$Meta": {
    "Type": "ActionTemplate"
  }
}

History

Page updated on Friday, October 22, 2021