Skip to content

Commit 03e3b36

Browse files
authored
Update setup script
1 parent 5d59a8f commit 03e3b36

File tree

4 files changed

+77
-6
lines changed

4 files changed

+77
-6
lines changed

.github/workflows/release.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,12 @@ jobs:
3737
node-version: 14
3838
- name: Publish 🚀
3939
run: |
40-
aws ecr get-login-password --region ap-southeast-2 | docker login --username AWS --password-stdin 045615149555.dkr.ecr.ap-southeast-2.amazonaws.com
40+
aws ecr get-login-password --region ap-southeast-2 | docker login --username AWS --password-stdin $REPO_REGISTRY
4141
npm ci
4242
npm run release
4343
env:
44+
REPO_NAME: "lambda-container-service"
45+
REPO_URI: "045615149555.dkr.ecr.ap-southeast-2.amazonaws.com/lambda-container-service"
46+
REPO_REGISTRY: "045615149555.dkr.ecr.ap-southeast-2.amazonaws.com"
4447
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
4548
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

publish.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#!/bin/bash
22

3-
REPO=045615149555.dkr.ecr.ap-southeast-2.amazonaws.com/lambda-container-service
3+
SERVICE_NAME=$REPO_NAME
4+
REPO=$REPO_URI
45

5-
docker tag lambda-container-service:latest $REPO:$1
6+
docker tag $SERVICE_NAME:latest $REPO:$1
67
docker push $REPO:$1
7-
docker tag lambda-container-service:latest $REPO:latest
8+
docker tag $SERVICE_NAME:latest $REPO:latest
89
docker push $REPO:latest

setup

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/bin/env node
2+
3+
const execSync = require('child_process').execSync;
4+
const { readFileSync, writeFileSync, unlinkSync } = require('fs');
5+
const POLICY = {
6+
"Version" : "2008-10-17",
7+
"Statement" : [
8+
{
9+
"Sid": "LambdaECRImageRetrievalPolicy",
10+
"Effect": "Allow",
11+
"Principal": {
12+
"Service": "lambda.amazonaws.com"
13+
},
14+
"Action": [
15+
"ecr:BatchGetImage",
16+
"ecr:GetDownloadUrlForLayer"
17+
]
18+
}
19+
]
20+
};
21+
22+
const runtimeConfig = {
23+
REPO_NAME : process.argv[2],
24+
REPO_URI: null,
25+
REPO_REGISTRY: null
26+
}
27+
try {
28+
const repos = execSync(` aws ecr describe-repositories --repository-names=${runtimeConfig.REPO_NAME}`, { stdio: 'pipe'});
29+
console.log();
30+
runtimeConfig.REPO_URI = JSON.parse(repos.toString()).repositories[0].repositoryUri;
31+
} catch (error) {
32+
console.log(error);
33+
const newRepo = execSync(`aws ecr create-repository --repository-name ${runtimeConfig.REPO_NAME}`, { stdio: 'pipe'});
34+
runtimeConfig.REPO_URI = JSON.parse(newRepo.toString()).repository.repositoryUri;
35+
}
36+
37+
if (runtimeConfig.REPO_URI) {
38+
runtimeConfig.REPO_REGISTRY = runtimeConfig.REPO_URI.replace(`/${runtimeConfig.REPO_NAME}`, "");
39+
console.log('Setting Repo Policy');
40+
writeFileSync('./lcs-policy.json', JSON.stringify(POLICY));
41+
try {
42+
execSync(`aws ecr set-repository-policy --repository-name=${runtimeConfig.REPO_NAME} --policy-text=file://lcs-policy.json`, { stdio: 'pipe'});
43+
unlinkSync('./lcs-policy.json');
44+
console.log(`Repo policy set on ${runtimeConfig.REPO_NAME} repo.`);
45+
} catch (error) {
46+
console.log('Error setting policy', error.stderr.toString());
47+
unlinkSync('./lcs-policy.json');
48+
process.exit(1);
49+
}
50+
}
51+
52+
console.log('\nRuntime Config:');
53+
console.log('===============');
54+
console.log('REPO_NAME:', runtimeConfig.REPO_NAME);
55+
console.log('REPO_URI :', runtimeConfig.REPO_URI);
56+
console.log('REPO_REGISTRY :', runtimeConfig.REPO_REGISTRY);
57+
58+
console.log("Updating package.json");
59+
let packageFile = JSON.parse(readFileSync('package.json').toString());
60+
packageFile.name = runtimeConfig.REPO_NAME;
61+
writeFileSync('package.json', JSON.stringify(packageFile, null, ' '));
62+
63+
console.log("Updating .github/workflows/release.yml");
64+
let releaseFile = readFileSync('.github/workflows/release.yml').toString();
65+
releaseFile = releaseFile.replace('REPO_NAME: "lambda-container-service"', `REPO_NAME: "${runtimeConfig.REPO_NAME}"`);
66+
releaseFile = releaseFile.replace('REPO_URI: "045615149555.dkr.ecr.ap-southeast-2.amazonaws.com/lambda-container-service"', `REPO_URI: "${runtimeConfig.REPO_URI}"`);
67+
releaseFile = releaseFile.replace('REPO_REGISTRY: "045615149555.dkr.ecr.ap-southeast-2.amazonaws.com"', `REPO_REGISTRY: "${runtimeConfig.REPO_REGISTRY}"`);
68+
writeFileSync('.github/workflows/release.yml', releaseFile);
69+
console.log('Setup Successfull. Please delete this setup script.');

setup.sh

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)