-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
85 lines (71 loc) · 2.24 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
const core = require('@actions/core');
const github = require('@actions/github');
const {
ECR,
DescribeRepositoriesCommand,
CreateRepositoryCommand,
PutLifecyclePolicyCommand
} = require('@aws-sdk/client-ecr');
const ecr = new ECR();
const executeGitHubAction = async () => {
const ecrRepoName = core.getInput('ecr-name') || github.context.payload.repository.name;
let wantedEcrRepo;
try {
core.info(`Checking ECR repo '${ecrRepoName}'.`);
wantedEcrRepo = await ecr.send(new DescribeRepositoriesCommand({repositoryNames: [ecrRepoName]}))
wantedEcrRepo = wantedEcrRepo.repositories[0];
core.info(`ECR repo '${ecrRepoName}' already exists.`);
// TODO check repo lifecycle policy
} catch (error) {
if (error.name !== 'RepositoryNotFoundException') {
throw error;
}
core.info(`ECR repo '${ecrRepoName}' does not exist.`);
wantedEcrRepo = await createNewEcrRepo(ecrRepoName);
wantedEcrRepo = wantedEcrRepo.repository;
await setupLifecyclePolicy(ecrRepoName);
}
core.setOutput('ecr-name', wantedEcrRepo.repositoryName);
core.setOutput('ecr-arn', wantedEcrRepo.repositoryArn);
core.setOutput('ecr-uri', wantedEcrRepo.repositoryUri);
};
const createNewEcrRepo = async (repoName) => {
core.info(`Create new ECR repo '${repoName}'.`);
const params = {
"repositoryName": repoName,
"imageScanningConfiguration": {
"scanOnPush": true
}
};
return await ecr.send(new CreateRepositoryCommand(params));
};
const setupLifecyclePolicy = async (repoName) => {
core.info(`Set lifecycle to ECR repo '${repoName}'.`);
const lifecyclePolicy = {
"rules": [{
"rulePriority": 1,
"description": "Expire images older than 30 days",
"selection": {
"tagStatus": "untagged",
"countType": "sinceImagePushed",
"countUnit": "days",
"countNumber": 30
},
"action": {
"type": "expire"
}
}]
};
const params = {
"repositoryName": repoName,
"lifecyclePolicyText": JSON.stringify(lifecyclePolicy)
};
await ecr.send(new PutLifecyclePolicyCommand(params));
};
module.exports = {
executeGitHubAction: executeGitHubAction
};
/* istanbul ignore if */
if (process.env.NODE_ENV !== 'test') {
executeGitHubAction();
}