-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
88 lines (75 loc) · 2.31 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const { getInput, setFailed } = require('@actions/core')
const AWS = require('aws-sdk')
async function run () {
try {
const repositoryName = getInput('DOCKER_REPO_NAME', { required: true })
const ecrPolicy = getInput('AWS_ECR_PERMISSION_POLICY_JSON', { required: true })
const ecr = new AWS.ECR({ apiVersion: '2015-09-21', region: process.env.AWS_REGION })
let repositoryExists = false
try {
await ecr.describeRepositories({ repositoryNames: [repositoryName] }).promise()
repositoryExists = true
} catch {}
const lifecyclePolicy = {
rules: [
{
rulePriority: 10,
description: `Expire untagged images after 30 days`,
selection: {
tagStatus: 'untagged',
countType: 'sinceImagePushed',
countUnit: 'days',
countNumber: 30
},
action: {
type: 'expire'
}
}
]
}
lifecyclePolicy.rules.push({
rulePriority: 20,
description: 'Expire test images, keep 20 last',
selection: {
tagStatus: 'tagged',
tagPrefixList: ["dev-"],
countType: 'imageCountMoreThan',
countNumber: 20
},
action: {
type: 'expire'
}
})
lifecyclePolicy.rules.push({
rulePriority: 30,
description: 'Expire unpromoted pre images, keep last 30',
selection: {
tagStatus: 'tagged',
tagPrefixList: ["pre-"],
countType: 'imageCountMoreThan',
countNumber: 30
},
action: {
type: 'expire'
}
})
const lifecyclePolicyText = JSON.stringify(lifecyclePolicy)
if (repositoryExists) {
console.log('Repository already exists, updating lifecycle only 🎉')
await Promise.all([
ecr.putLifecyclePolicy({ repositoryName, lifecyclePolicyText }).promise()
])
return
}
console.log('Repository does not exist. Creating...')
await ecr.createRepository({ repositoryName, imageScanningConfiguration: { scanOnPush: true } }).promise()
await Promise.all([
ecr.setRepositoryPolicy({ repositoryName, policyText: ecrPolicy }).promise(),
ecr.putLifecyclePolicy({ repositoryName, lifecyclePolicyText }).promise()
])
console.log('Done! 🎉')
} catch (e) {
setFailed(e.message || e)
}
}
run()