Amazon ECR 中的生命週期政策範例 - Amazon ECR

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

Amazon ECR 中的生命週期政策範例

以下是顯示語法的生命週期原則範例。

若要查看有關策略內容的詳細資訊,請參閱Amazon ECR 中的生命週期政策屬性。如需使用建立生命週期原則的指示 AWS CLI,請參閱建立生命週期政策 (AWS CLI)

生命週期政策範本

在與存放庫產生關聯之前,會先評估生命週期原則的內容。以下是生命週期政策的 JSON 語法範本。

{ "rules": [ { "rulePriority": integer, "description": "string", "selection": { "tagStatus": "tagged"|"untagged"|"any", "tagPatternList": list<string>, "tagPrefixList": list<string>, "countType": "imageCountMoreThan"|"sinceImagePushed", "countUnit": "string", "countNumber": integer }, "action": { "type": "expire" } } ] }

篩選映像存在時間

以下範例顯示政策的生命週期政策語法,該政策能尋找以 prod 標籤開頭的映像,使用 prod*tagPatternList,將存在時間同樣超過 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" } } ] }

篩選映像計數

以下範例顯示政策的生命週期政策語法,該政策能只保留一個未標記的映像,並將其他所有映像設為過期。

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

篩選多個規則

以下是在生命週期政策中使用多個規則的範例。範例儲存庫與指定的生命週期政策與結果說明同時提供。

範例 A

儲存庫內容:

  • Image A, Taglist: ["beta-1", "prod-1"], Pushed: 10 days ago

  • Image B, Taglist: ["beta-2", "prod-2"], Pushed: 9 days ago

  • Image C, Taglist: ["beta-3"], Pushed: 8 days ago

生命週期政策文字:

{ "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 為過期。但是,映像 A 已被規則 1 看到,而若映像 B 已過期,則會因違反規則 1 而被略過。

  • 結果:映像 A 已過期。

範例:B

這是與前一個範例相同的儲存庫,但是規則優先順序已變更以說明結果。

儲存庫內容:

  • Image A, Taglist: ["beta-1", "prod-1"], Pushed: 10 days ago

  • Image B, Taglist: ["beta-2", "prod-2"], Pushed: 9 days ago

  • Image C, Taglist: ["beta-3"], Pushed: 8 days ago

生命週期政策文字:

{ "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

當單一規則指定多個標籤模式,映像必須符合所有列出的標籤模式。

儲存庫內容:

  • Image A, Taglist: ["alpha-1"], Pushed: 12 days ago

  • Image B, Taglist: ["beta-1"], Pushed: 11 days ago

  • Image C, Taglist: ["alpha-2", "beta-2"], Pushed: 10 days ago

  • Image D, Taglist: ["alpha-3"], Pushed: 4 days ago

  • Image E, Taglist: ["beta-3"], Pushed: 3 days ago

  • Image F, Taglist: ["alpha-4", "beta-4"], Pushed: 2 days ago

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

生命週期政策的邏輯會是:

  • 規則 1 找出含有前綴 alphabeta 標籤的映像。看到映像 C 與 F。應標記存在時間大於五天的映像,應是映像 C。

  • 結果:映像 C 已過期。

範例:B

下列範例說明非專屬的標籤。

儲存庫內容:

  • Image A, Taglist: ["alpha-1", "beta-1", "gamma-1"], Pushed: 10 days ago

  • Image B, Taglist: ["alpha-2", "beta-2"], Pushed: 9 days ago

  • Image C, Taglist: ["alpha-3", "beta-3", "gamma-2"], Pushed: 8 days ago

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

生命週期政策的邏輯會是:

  • 規則 1 找出含有前綴 alphabeta 標籤的映像。看到所有映像。將會從最舊的映像開始標記,直到沒有或剩餘很少符合的映像。標記映像 A 與 B 為過期。

  • 結果:映像 A 與 B 皆已過期。

篩選所有映像

以下生命週期政策範例說明含有不同篩選條件的映像。範例儲存庫與指定的生命週期政策與結果說明同時提供。

範例 A

下列顯示套用到所有規則的政策之生命週期政策語法,但是只保留一個映像並將其他所有映像設為過期。

儲存庫內容:

  • Image A, Taglist: ["alpha-1"], Pushed: 4 days ago

  • Image B, Taglist: ["beta-1"], Pushed: 3 days ago

  • Image C, Taglist: [], Pushed: 2 days ago

  • Image D, Taglist: ["alpha-2"], Pushed: 1 day ago

{ "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

以下範例說明在單一規則中整合所有規則類型的生命週期政策。

儲存庫內容:

  • Image A, Taglist: ["alpha-", "beta-1", "-1"], Pushed: 4 days ago

  • Image B, Taglist: [], Pushed: 3 days ago

  • Image C, Taglist: ["alpha-2"], Pushed: 2 days ago

  • Image D, Taglist: ["git hash"], Pushed: 1 day ago

  • Image E, Taglist: [], Pushed: 1 day ago

{ "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 皆已過期。