

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# Amazon ECR 中的生命周期策略的示例
<a name="lifecycle_policy_examples"></a>

以下是示例生命周期策略，其中显示了语法。

要查看有关策略属性的更多信息，请参阅[Amazon ECR 中的生命周期策略属性](lifecycle_policy_parameters.md)。有关使用创建生命周期策略的说明 AWS CLI，请参阅[创建生命周期策略 (AWS CLI)](lp_creation.md#lp-creation-cli)。

## 生命周期策略模板
<a name="lifecycle_policy_syntax"></a>

在与存储库关联之前评估生命周期策略的内容。以下是生命周期策略的 JSON 语法模板。

```
{
        "rules": [
            {
                "rulePriority": integer,
                "description": "string",
                "selection": {
                    "tagStatus": "tagged"|"untagged"|"any",
                    "tagPatternList": list<string>,
                    "tagPrefixList": list<string>,
                    "storageClass": "standard"|"archive",
                    "countType": "imageCountMoreThan"|"sinceImagePushed"|"sinceImagePulled"|"sinceImageTransitioned",
                    "countUnit": "string",
                    "countNumber": integer
                },
                "action": {
                    "type": "expire"|"transition",
                    "targetStorageClass": "archive"
                }
            }
        ]
    }
```

## 根据镜像使用期限筛选
<a name="lifecycle_policy_example_age"></a>

以下示例演示了一个策略的生命周期策略语法，该策略使用 `prod*` 的 `tagPatternList` 来确保具有以 `prod` 开头的标签并且存在时间超过 `14` 天的映像会过期。

```
{
    "rules": [
        {
            "rulePriority": 1,
            "description": "Expire images older than 14 days",
            "selection": {
                "tagStatus": "tagged",
                "tagPatternList": ["prod*"],
                "countType": "sinceImagePushed",
                "countUnit": "days",
                "countNumber": 14
            },
            "action": {
                "type": "expire"
            }
        }
    ]
}
```

## 筛选上次拉取时间
<a name="lifecycle_policy_example_last_pulled"></a>

以下示例显示了策略的生命周期策略语法，该策略将图像转换为`90`几天未提取的存档存储。

```
{
    "rules": [
        {
            "rulePriority": 1,
            "description": "Archive images not pulled in 90 days",
            "selection": {
                "tagStatus": "any",
                "countType": "sinceImagePulled",
                "countUnit": "days",
                "countNumber": 90
            },
            "action": {
                "type": "transition",
                "targetStorageClass": "archive"
            }
        }
    ]
}
```

**重要**  
`sinceImagePulled`计数类型必须与`transition`操作一起使用。它不能与`expire`操作一起使用。要根据拉取活动删除图像，请先使用将其过渡到存档存储`sinceImagePulled`，然后`sinceImageTransitioned`使用`expire`操作将其删除。图片必须在存档存储空间中存放至少 90 天，然后才能删除。

## 按存档过渡时间进行筛选
<a name="lifecycle_policy_example_transitioned"></a>

以下示例显示了策略的生命周期策略语法，该策略使存档存储`365`时间超过几天的存档图像过期。

```
{
    "rules": [
        {
            "rulePriority": 1,
            "description": "Expire images archived for more than 365 days",
            "selection": {
                "tagStatus": "any",
                "storageClass": "archive",
                "countType": "sinceImageTransitioned",
                "countUnit": "days",
                "countNumber": 365
            },
            "action": {
                "type": "expire"
            }
        }
    ]
}
```

**重要**  
`sinceImageTransitioned`计数类型必须与`expire`操作和`archive`存储类一起使用。图片必须在存档存储空间中存放至少 90 天，然后才能删除。

## 根据镜像计数筛选
<a name="lifecycle_policy_example_number"></a>

以下示例演示了一个策略的生命周期策略语法，该策略将仅保留一个未标记的映像并使所有其他映像过期。

```
{
    "rules": [
        {
            "rulePriority": 1,
            "description": "Keep only one untagged image, expire all others",
            "selection": {
                "tagStatus": "untagged",
                "countType": "imageCountMoreThan",
                "countNumber": 1
            },
            "action": {
                "type": "expire"
            }
        }
    ]
}
```

## 根据多个规则筛选
<a name="lp_example_multiple"></a>

以下示例在生命周期策略中使用多条规则。在此提供了一个示例存储库和生命周期策略以及结果的说明。

### 示例 A
<a name="lp_example_multiple_a"></a>

存储库内容：
+ 镜像 A，标签列表：["beta-1", "prod-1"]，已推送：10 天前
+ 镜像 B，标签列表：["beta-2", "prod-2"]，已推送：9 天前
+ 镜像 C，标签列表：["beta-3"]，已推送：8 天前

生命周期策略文本：

```
{
    "rules": [
        {
            "rulePriority": 1,
            "description": "Rule 1",
            "selection": {
                "tagStatus": "tagged",
                "tagPatternList": ["prod*"],
                "countType": "imageCountMoreThan",
                "countNumber": 1
            },
            "action": {
                "type": "expire"
            }
        },
        {
            "rulePriority": 2,
            "description": "Rule 2",
            "selection": {
                "tagStatus": "tagged",
                "tagPatternList": ["beta*"],
                "countType": "imageCountMoreThan",
                "countNumber": 1
            },
            "action": {
                "type": "expire"
            }
        }
    ]
}
```

此生命周期策略的逻辑是：
+ 规则 1 标识带有前缀 `prod` 标签的镜像。它应该从最早的镜像开始标记镜像，直到剩下一个或更少的匹配镜像。它将镜像 A 标记为过期。
+ 规则 2 标识带有前缀 `beta` 标签的镜像。它应该从最早的镜像开始标记镜像，直到剩下一个或更少的匹配镜像。它将镜像 A 和镜像 B 标记为过期。但是，规则 1 已标记镜像 A，如果镜像 B 已过期，则会违反规则 1，因此跳过。
+ 结果：镜像 A 已过期。

### 示例 B
<a name="lp_example_multiple_b"></a>

这与前面的示例相同，但规则优先级顺序会更改以展示结果。

存储库内容：
+ 镜像 A，标签列表：["beta-1", "prod-1"]，已推送：10 天前
+ 镜像 B，标签列表：["beta-2", "prod-2"]，已推送：9 天前
+ 镜像 C，标签列表：["beta-3"]，已推送：8 天前

生命周期策略文本：

```
{
    "rules": [
        {
            "rulePriority": 1,
            "description": "Rule 1",
            "selection": {
                "tagStatus": "tagged",
                "tagPatternList": ["beta*"],
                "countType": "imageCountMoreThan",
                "countNumber": 1
            },
            "action": {
                "type": "expire"
            }
        },
        {
            "rulePriority": 2,
            "description": "Rule 2",
            "selection": {
                "tagStatus": "tagged",
                "tagPatternList": ["prod*"],
                "countType": "imageCountMoreThan",
                "countNumber": 1
            },
            "action": {
                "type": "expire"
            }
        }
    ]
}
```

此生命周期策略的逻辑是：
+ 规则 1 标识带有前缀 `beta` 标签的镜像。它应该从最早的镜像开始标记镜像，直到剩下一个或更少的匹配镜像。它可以看到所有三个镜像，并将镜像 A 和镜像 B 标记为过期。
+ 规则 2 标识带有前缀 `prod` 标签的镜像。它应该从最早的镜像开始标记镜像，直到剩下一个或更少的匹配镜像。它不会看到任何镜像，因为所有可用镜像已经被规则 1 标记，因此不会标记其他镜像。
+ 结果：镜像 A 和 B 已过期。

## 在单个规则中筛选多个标签
<a name="lp_example_difftype"></a>

以下示例为单个规则中的多个标签模式指定了生命周期策略语法。在此提供了一个示例存储库和生命周期策略以及结果的说明。

### 示例 A
<a name="lp_example_difftype_a"></a>

在单个规则上指定多个标签模式时，映像必须与所有列出的标签模式匹配。

存储库内容：
+ 镜像 A，标签列表：["alpha-1"]，已推送：12 天前
+ 镜像 B，标签列表：["beta-1"]，已推送：11 天前
+ 镜像 C，标签列表：["alpha-2", "beta-2"]，已推送：10 天前
+ 镜像 D，标签列表：["alpha-3"]，已推送：4 天前
+ 图片 E，标签列表：["beta-3"]，已推送：3 天前
+ 镜像 F，标签列表：["alpha-4", "beta-4"]，已推送：2 天前

```
{
    "rules": [
        {
            "rulePriority": 1,
            "description": "Rule 1",
            "selection": {
                "tagStatus": "tagged",
                "tagPatternList": ["alpha*", "beta*"],
                "countType": "sinceImagePushed",
                "countNumber": 5,
                "countUnit": "days"
            },
            "action": {
                "type": "expire"
            }
        }
    ]
}
```

此生命周期策略的逻辑是：
+ 规则 1 用于识别标记有前缀 `alpha` 和`beta` 的映像。它可以看到镜像 C 和 F。它应该标记超过五天的镜像，镜像 C 符合此条件。
+ 结果：镜像 C 已过期。

### 示例 B
<a name="lp_example_difftype_b"></a>

以下示例说明标签不是独占的。

存储库内容：
+ 镜像 A，标签列表：["alpha-1", "beta-1", "gamma-1"]，已推送：10 天前
+ 镜像 B，标签列表：["alpha-2", "beta-2"]，已推送：9 天前
+ 镜像 C，标签列表：["alpha-3", "beta-3", "gamma-2"]，已推送：8 天前

```
{
    "rules": [
        {
            "rulePriority": 1,
            "description": "Rule 1",
            "selection": {
                "tagStatus": "tagged",
                "tagPatternList": ["alpha*", "beta*"],
                "countType": "imageCountMoreThan",
                "countNumber": 1
            },
            "action": {
                "type": "expire"
            }
        }
    ]
}
```

此生命周期策略的逻辑是：
+ 规则 1 用于识别标记有前缀 `alpha` 和`beta` 的映像。它可以看到所有的镜像。它应该从最早的镜像开始标记镜像，直到剩下一个或更少的匹配镜像。它将镜像 D 标记为过期。
+ 结果：镜像 A 和 B 已过期。

## 筛选所有镜像
<a name="lp_example_allimages"></a>

以下生命周期策略示例指定具有不同筛选条件的所有镜像。此处提供了一个示例存储库和生命周期策略以及结果的说明。

### 示例 A
<a name="lp_example_difftype_a"></a>

下面显示了应用于所有规则但仅保留一个镜像并使所有其他镜像过期的策略的生命周期策略语法。

存储库内容：
+ 镜像 A，标签列表：["alpha-1"]，已推送：4 天前
+ 镜像 B，标签列表：["beta-1"]，已推送：3 天前
+ 镜像 C，标签列表：[]，已推送：2 天前
+ 镜像 D，标签列表：["alpha-2"]，已推送：1 天前

```
{
    "rules": [
        {
            "rulePriority": 1,
            "description": "Rule 1",
            "selection": {
                "tagStatus": "any",
                "countType": "imageCountMoreThan",
                "countNumber": 1
            },
            "action": {
                "type": "expire"
            }
        }
    ]
}
```

此生命周期策略的逻辑是：
+ 规则 1 标识所有镜像。它可以看到镜像 A、B、C 和 D。它应该使最新镜像之外的所有其他镜像过期。它将镜像 A、B 和 C 标记为过期。
+ 结果：镜像 A、B 和 C 已过期。

### 示例 B
<a name="lp_example_difftype_b"></a>

以下示例说明了将所有规则类型组合在一个策略中的生命周期策略。

存储库内容：
+ 镜像 A，标签列表：["alpha-1", "beta-1"]，已推送：4 天前
+ 图片 B，标签列表：[]，已推送：3 天前
+ 镜像 C，标签列表：["alpha-2"]，已推送：2 天前
+ 镜像 D，标签列表：["git hash"]，已推送：1 天前
+ 镜像 E，标签列表：[]，已推送：1 天前

```
{
    "rules": [
        {
            "rulePriority": 1,
            "description": "Rule 1",
            "selection": {
                "tagStatus": "tagged",
                "tagPatternList": ["alpha*"],
                "countType": "imageCountMoreThan",
                "countNumber": 1
            },
            "action": {
                "type": "expire"
            }
        },
        {
            "rulePriority": 2,
            "description": "Rule 2",
            "selection": {
                "tagStatus": "untagged",
                "countType": "sinceImagePushed",
                "countUnit": "days",
                "countNumber": 1
            },
            "action": {
                "type": "expire"
            }
        },
        {
            "rulePriority": 3,
            "description": "Rule 3",
            "selection": {
                "tagStatus": "any",
                "countType": "imageCountMoreThan",
                "countNumber": 1
            },
            "action": {
                "type": "expire"
            }
        }
    ]
}
```

此生命周期策略的逻辑是：
+ 规则 1 标识带有前缀 `alpha` 标签的镜像。它识别镜像 A 和 C。它应该保留最新镜像并将其余镜像标记为过期。它将镜像 A 标记为过期。
+ 规则 2 标识未标记的镜像。它可以识别镜像 B 和 E。它应该将所有超过一天的镜像标记为过期。它将镜像 B 标记为过期。
+ 规则 3 标识所有镜像。它识别镜像 A、B、C、D 和 E。它应该保留最新镜像并将其余镜像标记为过期。但是，它无法标记镜像 A、B、C 或 E，因为它们由优先级更高的规则识别。它将镜像 D 标记为过期。
+ 结果：镜像 A、B 和 D 已过期。

## 存档示例
<a name="lp_example_archive"></a>

以下示例显示了存档图像而不是删除图像的生命周期策略。

### 存档时间超过指定天数的图像
<a name="lp_example_archive_age"></a>

以下示例显示了一个生命周期策略，该策略用于存档标签以开头`prod`且时间超过 30 天的图像：

```
{
    "rules": [
        {
            "rulePriority": 1,
            "description": "Archive production images older than 30 days",
            "selection": {
                "tagStatus": "tagged",
                "tagPatternList": ["prod*"],
                "countType": "sinceImagePushed",
                "countUnit": "days",
                "countNumber": 30
            },
            "action": {
                "type": "transition",
                "targetStorageClass": "archive"
            }
        }
    ]
}
```

### 存档未在指定天数内提取的图像
<a name="lp_example_archive_pull"></a>

以下示例显示了存档 90 天内未提取的图像的生命周期策略：

```
{
    "rules": [
        {
            "rulePriority": 1,
            "description": "Archive images not pulled in 90 days",
            "selection": {
                "tagStatus": "any",
                "countType": "sinceImagePulled",
                "countUnit": "days",
                "countNumber": 90
            },
            "action": {
                "type": "transition",
                "targetStorageClass": "archive"
            }
        }
    ]
}
```

### 合并存档和过期规则
<a name="lp_example_archive_delete"></a>

以下示例显示了一个生命周期策略，该策略用于存档 30 天以上的图像，然后将存档超过 365 天的图像永久过期：

**注意**  
存档图像的最短存储期限为 90 天。您不能配置生命周期策略来删除存档时间少于 90 天的图像。如果您必须删除存档时间少于 90 天的图片，则需要使用 **batch-delete-image** API，但您需要按照 90 天的最短存储时间付费。

```
{
    "rules": [
        {
            "rulePriority": 1,
            "description": "Archive images older than 30 days",
            "selection": {
                "tagStatus": "any",
                "countType": "sinceImagePushed",
                "countUnit": "days",
                "countNumber": 30
            },
            "action": {
                "type": "transition",
                "targetStorageClass": "archive"
            }
        },
        {
            "rulePriority": 2,
            "description": "Expire images archived for more than 365 days",
            "selection": {
                "tagStatus": "any",
                "storageClass": "archive",
                "countType": "sinceImageTransitioned",
                "countUnit": "days",
                "countNumber": 365
            },
            "action": {
                "type": "expire"
            }
        }
    ]
}
```