Skip to content

Commit a816e4d

Browse files
authored
Merge pull request #5 from byu-oit/tagged-image-lifecycle
Allow adjusting days to retain untagged images and number of tagged images to retain
2 parents 79da813 + ef3b3fa commit a816e4d

File tree

4 files changed

+78
-5
lines changed

4 files changed

+78
-5
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,39 @@ jobs:
2929
# ...
3030
```
3131

32+
<details>
33+
<summary>Or for a more complicated example, if you want to retain a certain number of tagged images...</summary>
34+
<p>
35+
36+
```yaml
37+
on: push
38+
# ...
39+
jobs:
40+
deploy:
41+
runs-on: ubuntu-latest
42+
steps:
43+
# ...
44+
- name: Configure AWS credentials
45+
uses: aws-actions/configure-aws-credentials@v1
46+
with:
47+
aws-access-key-id: ${{ secrets.AWS_KEY }}
48+
aws-secret-access-key: ${{ secrets.AWS_SECRET }}
49+
aws-region: us-west-2
50+
- name: Log into Amazon ECR
51+
uses: aws-actions/amazon-ecr-login@v1
52+
- name: Create ECR repo if missing
53+
uses: byu-oit/github-action-create-ecr-repo-if-missing@v1
54+
with:
55+
DOCKER_REPO_NAME: ${{ env.REPO }} # Your repo name goes here
56+
NUM_DAYS_BEFORE_EXPIRING_UNTAGGED_IMAGES: 14
57+
TAG_PREFIX: 'dev-v'
58+
NUM_TAGGED_IMAGES_TO_RETAIN: 5
59+
# ...
60+
```
61+
62+
</p>
63+
</details>
64+
3265
## Contributing
3366
Hopefully this is useful to others at BYU. Feel free to ask me some questions about it, but I make no promises about being able to commit time to support it.
3467

action.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ inputs:
66
# $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/byuapi-classes-v3-app:some-tag
77
description: 'The ECR repository name'
88
required: true
9+
NUM_DAYS_BEFORE_EXPIRING_UNTAGGED_IMAGES:
10+
description: 'The number of days after which untagged images will expire'
11+
required: false
12+
default: '30'
13+
14+
# These next two go together to create the "Expire old images as new ones are built" lifecycle policy
15+
TAG_PREFIX:
16+
description: 'The image tag prefix used so that we can expire old images as new ones are built'
17+
required: false
18+
NUM_TAGGED_IMAGES_TO_RETAIN:
19+
description: 'The number of images with the given prefix to retain'
20+
required: false
21+
# Defaults to retaining all tagged images
922
runs:
1023
using: 'node12'
1124
main: 'dist/index.js'

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.js

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ const AWS = require('aws-sdk')
44
async function run () {
55
try {
66
const repositoryName = getInput('DOCKER_REPO_NAME', { required: true })
7+
const daysBeforeExpiringUntaggedImages = getInput('NUM_DAYS_BEFORE_EXPIRING_UNTAGGED_IMAGES', { required: true })
8+
const tagPrefix = getInput('TAG_PREFIX')
9+
const numImages = getInput('NUM_TAGGED_IMAGES_TO_RETAIN')
10+
if (tagPrefix && !numImages) {
11+
setFailed('If TAG_PREFIX is provided, NUM_TAGGED_IMAGES_TO_RETAIN is required')
12+
return
13+
}
14+
if (!tagPrefix && numImages) {
15+
setFailed('If NUM_TAGGED_IMAGES_TO_RETAIN is provided, TAG_PREFIX is required')
16+
return
17+
}
718

819
const ecr = new AWS.ECR({ apiVersion: '2015-09-21', region: process.env.AWS_REGION })
920

@@ -39,23 +50,39 @@ async function run () {
3950
]
4051
})
4152

42-
const lifecyclePolicyText = JSON.stringify({
53+
const lifecyclePolicy = {
4354
rules: [
4455
{
4556
rulePriority: 10,
46-
description: 'Expire untagged images after 30 days',
57+
description: `Expire untagged images after ${daysBeforeExpiringUntaggedImages} days`,
4758
selection: {
4859
tagStatus: 'untagged',
4960
countType: 'sinceImagePushed',
5061
countUnit: 'days',
51-
countNumber: 30
62+
countNumber: parseInt(daysBeforeExpiringUntaggedImages, 10)
5263
},
5364
action: {
5465
type: 'expire'
5566
}
5667
}
5768
]
58-
})
69+
}
70+
if (tagPrefix && numImages) {
71+
lifecyclePolicy.rules.push({
72+
rulePriority: 20,
73+
description: 'Expire old images as new ones are built',
74+
selection: {
75+
tagStatus: 'tagged',
76+
tagPrefixList: [tagPrefix],
77+
countType: 'imageCountMoreThan',
78+
countNumber: numImages
79+
},
80+
action: {
81+
type: 'expire'
82+
}
83+
})
84+
}
85+
const lifecyclePolicyText = JSON.stringify(lifecyclePolicy)
5986

6087
console.log('Applying repository access and lifecycle policies...')
6188
await Promise.all([

0 commit comments

Comments
 (0)