Tag all used ECR images

Octopus.AwsRunScript exported 2019-02-06 by hakanl belongs to ‘AWS’ category.

This will apply a tag to all AWS Elastic Container Registry images/packages from the ECR feed that are used in the deployment. That way the lifecycle policies in ECR can be configured to not delete images that are in-use by deployments in various environments.

Parameters

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

Account

AwsAccount =

AWS Account to connect using

Region

AwsRegion =

AWS Region that is used

Deployment Prefix

AwsDeployPrefix = deploy-

Prefix for the image tags. The Octopus environment is then appended to this.

Script body

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

$DeployTag = $AwsDeployPrefix + $OctopusParameters["Octopus.Environment.Name"]

#{each action in Octopus.Action}
    #{each package in action.Package}
        Write-Output "Package #{package.PackageId} at version #{package.PackageVersion}"

        $Image = Get-ECRImageBatch -ImageId @{ imageTag="#{package.PackageVersion}" } -RepositoryName "#{package.PackageId}"
        $ImageDeploy = Get-ECRImageBatch -ImageId @{ imageTag=$DeployTag } -RepositoryName "#{package.PackageId}"

        if($Image.Images[0].ImageId.ImageDigest -ne $ImageDeploy.Images[0].ImageId.ImageDigest) {
            Write-Output "Setting tag $DeployTag on image $($Image.Images[0].ImageId.ImageDigest)"
            $Manifest = $Image.Images[0].ImageManifest
            Write-ECRImage -RepositoryName "#{package.PackageId}" -ImageManifest $Manifest -ImageTag $DeployTag
        }
	#{/each}
#{/each}

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": "9894aeda-bf06-4be2-8789-b570c9050d34",
  "Name": "Tag all used ECR images",
  "Description": "This will apply a tag to all AWS Elastic Container Registry images/packages from the ECR feed that are used in the deployment. That way the lifecycle policies in ECR can be configured to not delete images that are in-use by deployments in various environments.",
  "Version": 12,
  "ExportedAt": "2019-02-06T15:41:19.708Z",
  "ActionType": "Octopus.AwsRunScript",
  "Author": "hakanl",
  "Packages": [],
  "Parameters": [
    {
      "Id": "3bed9fa6-9d8e-452e-957a-25af1bb6fa58",
      "Name": "AwsAccount",
      "Label": "Account",
      "HelpText": "AWS Account to connect using",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "AmazonWebServicesAccount"
      }
    },
    {
      "Id": "2c64b94b-deb7-4e4b-b977-ec24f3b86951",
      "Name": "AwsRegion",
      "Label": "Region",
      "HelpText": "AWS Region that is used",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "fa47f4c7-b67a-4ee7-8c8f-a890acae795a",
      "Name": "AwsDeployPrefix",
      "Label": "Deployment Prefix",
      "HelpText": "Prefix for the image tags. The Octopus environment is then appended to this.",
      "DefaultValue": "deploy-",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptSource": "Inline",
    "Octopus.Action.Script.Syntax": "PowerShell",
    "Octopus.Action.Aws.AssumeRole": "False",
    "Octopus.Action.AwsAccount.UseInstanceRole": "False",
    "Octopus.Action.AwsAccount.Variable": "#{AwsAccount}",
    "Octopus.Action.Aws.Region": "#{AwsRegion}",
    "Octopus.Action.Script.ScriptBody": "$DeployTag = $AwsDeployPrefix + $OctopusParameters[\"Octopus.Environment.Name\"]\n\n#{each action in Octopus.Action}\n    #{each package in action.Package}\n        Write-Output \"Package #{package.PackageId} at version #{package.PackageVersion}\"\n\n        $Image = Get-ECRImageBatch -ImageId @{ imageTag=\"#{package.PackageVersion}\" } -RepositoryName \"#{package.PackageId}\"\n        $ImageDeploy = Get-ECRImageBatch -ImageId @{ imageTag=$DeployTag } -RepositoryName \"#{package.PackageId}\"\n\n        if($Image.Images[0].ImageId.ImageDigest -ne $ImageDeploy.Images[0].ImageId.ImageDigest) {\n            Write-Output \"Setting tag $DeployTag on image $($Image.Images[0].ImageId.ImageDigest)\"\n            $Manifest = $Image.Images[0].ImageManifest\n            Write-ECRImage -RepositoryName \"#{package.PackageId}\" -ImageManifest $Manifest -ImageTag $DeployTag\n        }\n\t#{/each}\n#{/each}\n"
  },
  "Category": "AWS",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/aws-add-imagetag-to-all-used-ecr-packages.json",
  "Website": "/step-templates/9894aeda-bf06-4be2-8789-b570c9050d34",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAADNQTFRF////9o0R/eLD/Nu0/erS95Qg+bhr95sv/vHh+r96/vjw+bFc/NSl+KI++82W+saI+KpNeDqM1wAAA41JREFUeNrsnG2XazAURiuo0Cr//9feliIvR3DvXJFZe3+a6XpW5+xWEpyY2w0AAAAAAAAAAAAAAAAAAADgf1J0bda/9N70q83a3enzUHWVjbR1sW0xp6sd6fPI72VmUt3zA+kymD6N5vnIBMrHsxHTjsUXOX0e+iVaTNU5Q0A/Q+k+4oAp+ixMbw6A4rGVVjGHR92ulNXWuTAlBNJN/FFyr5yy3qN9rawmF9IxR4hqX4U1WMplmGtruVBDuiuswbKkzaGhX+cfXsqbZlXXv0dsYR13nw9fLenGXD7f6U5Ony4yTpzyZLNMUcpMr0xNzfwdRRMR1/LP2cqMctNqKx1LZFydm2U022ueEtLL6HbHfmSRYRn4HDXaXyzU4XRkkZWK/+JlRBBBBBFEEEEEEUQQQQQRRBBB5B9uYJc7SyuLw+nI7R2ptKWJcywd18Utza0rnM4iN66M6qzS5E93Lf1zLaviUL/ISs/Nt6W00DEyuRgiP2Yxvrd15z/Y26ncG76jy1Ta5jEy/L0p/VMWy33woVm8UYN1Y9fqKrzfZ5iedtaV34+kNxHak2Wg2SSkY7djx/bQWkNP6nkE0lH3Lyx7D1aak1Z1erWJ+U130Vz0Sude7mZqv995nW7mZxJd27Sg5XQppuMdWY3xl1XXOge8MasWjZfund0KbvrkE9fK7OPNne+2U9YEWX3nemtSbvLv6LJ7gZ9X45yBl9ZxrZ9d3vjT8rz62tOsny7jXkpYPX9jQmvF8yF55TdaslGviZy1vAmfoTobsZztGNEv7qZZSr/6HRc/0yzlb3HiKhURRBBBBBFEEEEEEUQQQQQRRBD5XSLav38tllbVzeH02Ww/UWA+6XgsHdXFKc2vK5Quoz/duVRnlrb26crpizzXOVU3l2Zb5Pfe+d1OX8ViqW7qH9gt51K44bukr2XxrW54vMaoy7mxa/cgvPRVKcQG7uOCD58HLQLt3r17Iy6AqjYeDG7TUenWW+p9Ot/IOF/lwuHV1nk6o8M469PWXhtr+0BeX/x7Ue40W3xacfb2gXFxUZcX8TYB3Kyfp+GThsjKti2zgZuMiLshxW3gpiQyrn/DXhR/i1NqIte5pkUEEUQQQQQRRBBBBBFEEEEEEUR+g4jQUZBEqjqFO9mOiyeShoXvYoukZOG4GCLpWZgu83/vTNRidhlE0rYAAAAAAAAAAAAAAAAAAACAZPkjwAAMDi+bsnPP/wAAAABJRU5ErkJggg==",
  "$Meta": {
    "Type": "ActionTemplate"
  }
}

History

Page updated on Wednesday, February 6, 2019