Skip to content

Commit de9e476

Browse files
committed
refactoring
1 parent b682e0f commit de9e476

File tree

5 files changed

+1074
-235
lines changed

5 files changed

+1074
-235
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ This repo includes a template of starting Supabase stack on AWS via CloudFormati
7272
| 2xlarge | 8192 | 16384 |
7373
| 4xlarge | 16384 | 32768 |
7474

75-
#### IAM Policy
75+
#### IAM Policy to create CloudFormation Stack
7676

7777
```json
7878
{

src/aws-amplify-hosting.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class AmplifyHosting extends Construct {
3232

3333
const repository = new Repository(this, 'Repo', { repositoryName, description: `${this.node.path}/Repo` });
3434

35-
const repoImportJob = repository.importFromUrl('main', sourceRepo, sourceBranch);
35+
const repoImportJob = repository.importFromUrl(sourceRepo, sourceBranch);
3636

3737
const amplifySSRLoggingRole = new iam.Role(this, 'AmplifySSRLoggingRole', {
3838
description: 'The service role that will be used by AWS Amplify for SSR app logging.',
@@ -62,6 +62,10 @@ export class AmplifyHosting extends Construct {
6262
},
6363
postBuild: {
6464
commands: [
65+
'shopt -s dotglob',
66+
`if [ -d .next/standalone/node_modules ]; then mv -n .next/standalone/node_modules/* .next/standalone/${appRoot}/node_modules/.; fi`,
67+
'rm -rf .next/standalone/node_modules',
68+
`mv -f .next/standalone/${appRoot}/* .next/standalone/.`,
6569
'cp .env .env.production .next/standalone/',
6670
],
6771
},
@@ -88,6 +92,9 @@ export class AmplifyHosting extends Construct {
8892
});
8993
(this.app.node.defaultChild as cdk.CfnResource).addPropertyOverride('Platform', 'WEB_COMPUTE');
9094

95+
const outputFileTracingRoot = appRoot.split('/').map(x => x = '..').join('/') + '/';
96+
this.app.addEnvironment('NEXT_PRIVATE_OUTPUT_TRACE_ROOT', outputFileTracingRoot);
97+
9198
this.app.addEnvironment('AMPLIFY_MONOREPO_APP_ROOT', appRoot);
9299
this.app.addEnvironment('AMPLIFY_DIFF_DEPLOY', 'false');
93100
this.app.addEnvironment('_LIVE_UPDATES', JSON.stringify(liveUpdates));
@@ -136,13 +143,14 @@ export class AmplifyHosting extends Construct {
136143
}
137144

138145
export class Repository extends codecommit.Repository {
146+
readonly importFunction: lambda.Function;
139147
readonly importProvider: cr.Provider;
140148

141149
constructor(scope: Construct, id: string, props: codecommit.RepositoryProps) {
142150
super(scope, id, props);
143151

144-
const importFunction = new lambda.Function(this, 'ImportFunction', {
145-
description: 'Copy Git Repository',
152+
this.importFunction = new lambda.Function(this, 'ImportFunction', {
153+
description: 'Clone to CodeCommit from remote repo (You can execute this function manually.)',
146154
runtime: lambda.Runtime.PYTHON_3_9,
147155
code: lambda.Code.fromAsset('./src/functions/copy-git-repo', {
148156
bundling: {
@@ -167,21 +175,26 @@ export class Repository extends codecommit.Repository {
167175
memorySize: 2048,
168176
ephemeralStorageSize: cdk.Size.gibibytes(2),
169177
timeout: cdk.Duration.minutes(3),
178+
environment: {
179+
TARGET_REPO: this.repositoryCloneUrlGrc,
180+
},
170181
});
171-
this.grantPullPush(importFunction);
182+
this.grantPullPush(this.importFunction);
172183

173-
this.importProvider = new cr.Provider(this, 'ImportProvider', { onEventHandler: importFunction });
184+
this.importProvider = new cr.Provider(this, 'ImportProvider', { onEventHandler: this.importFunction });
174185
}
175186

176-
importFromUrl(targetBranch: string, sourceRepoUrlHttp: string, sourceBranch: string) {
187+
importFromUrl(sourceRepoUrlHttp: string, sourceBranch: string, targetBranch: string = 'main') {
188+
this.importFunction.addEnvironment('SOURCE_REPO', sourceRepoUrlHttp);
189+
this.importFunction.addEnvironment('SOURCE_BRANCH', sourceBranch);
190+
this.importFunction.addEnvironment('TARGET_BRANCH', targetBranch);
191+
177192
return new cdk.CustomResource(this, targetBranch, {
178193
resourceType: 'Custom::RepoImportJob',
179194
serviceToken: this.importProvider.serviceToken,
180195
properties: {
181196
SourceRepo: sourceRepoUrlHttp,
182197
SourceBranch: sourceBranch,
183-
TargetRepo: this.repositoryCloneUrlGrc,
184-
TargetBranch: targetBranch,
185198
},
186199
});
187200
}

src/functions/copy-git-repo/index.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
CLONE_DIR = '/tmp/src-repo'
66

7+
TARGET_REPO = os.environ['TARGET_REPO']
8+
TARGET_BRANCH = os.environ['TARGET_BRANCH']
9+
SOURCE_REPO = os.environ['SOURCE_REPO']
10+
SOURCE_BRANCH = os.environ['SOURCE_BRANCH']
11+
712
PATH = os.environ['PATH']
813
LD_LIBRARY_PATH = os.environ['LD_LIBRARY_PATH']
914
AWS_REGION = os.environ['AWS_REGION']
@@ -43,16 +48,15 @@ def exec(command: List[str], cwd: str = '/tmp') -> str:
4348
return stdout
4449

4550
def handler(event: dict, context: Any) -> dict:
46-
request_type: str = event['RequestType']
47-
source_repo: str = event['ResourceProperties']['SourceRepo']
48-
source_branch: str = event['ResourceProperties'].get('SourceBranch', 'main')
49-
target_repo: str = event['ResourceProperties']['TargetRepo']
50-
target_branch: str = event['ResourceProperties'].get('TargetBranch', 'main')
51+
props: dict = event.get('ResourceProperties', {})
52+
source_repo: str = props.get('SourceRepo', SOURCE_REPO)
53+
source_branch: str = props.get('SourceBranch', SOURCE_BRANCH)
54+
request_type: str = event.get('RequestType', 'ManualExecution')
5155
if request_type != 'Delete':
5256
exec(['rm', '-rf', CLONE_DIR])
5357
exec(['git', 'clone', '--depth', '1', '-b', source_branch, source_repo, CLONE_DIR])
5458
exec(['git', 'fetch', '--unshallow'], CLONE_DIR)
5559
exec(['git', 'checkout', '-b', 'local_tmp'], CLONE_DIR)
56-
exec(['git', 'remote', 'add', 'dest', target_repo], CLONE_DIR)
57-
exec(['git', 'push', '--force', 'dest', f'local_tmp:{target_branch}'], CLONE_DIR)
60+
exec(['git', 'remote', 'add', 'dest', TARGET_REPO], CLONE_DIR)
61+
exec(['git', 'push', '--force', 'dest', f'local_tmp:{TARGET_BRANCH}'], CLONE_DIR)
5862
return {}

src/supabase-stack.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ export class SupabaseStack extends FargateStack {
448448
const studioBranch = new cdk.CfnParameter(this, 'StudioBranch', {
449449
type: 'String',
450450
default: '0.22.08',
451-
description: 'https://github.com/supabase/supabase/tags',
451+
description: 'Branch or tag - https://github.com/supabase/supabase/tags',
452452
});
453453

454454
new AmplifyHosting(this, 'Studio', {
@@ -458,7 +458,10 @@ export class SupabaseStack extends FargateStack {
458458
environmentVariables: {
459459
STUDIO_PG_META_URL: `${apiExternalUrl}/pg`,
460460
POSTGRES_PASSWORD: db.secret.secretValueFromJson('password').toString(),
461+
//DEFAULT_ORGANIZATION: 'Default Organization',
462+
//DEFAULT_PROJECT: 'Default Project',
461463
SUPABASE_URL: `${apiExternalUrl}`,
464+
//SUPABASE_PUBLIC_URL: `${apiExternalUrl}`,
462465
SUPABASE_REST_URL: `${apiExternalUrl}/rest/v1/`,
463466
SUPABASE_ANON_KEY: anonKey.value,
464467
SUPABASE_SERVICE_KEY: serviceRoleKey.value,
@@ -508,6 +511,7 @@ export class SupabaseStack extends FargateStack {
508511
restImageUri.logicalId,
509512
realtimeImageUri.logicalId,
510513
storageImageUri.logicalId,
514+
imgproxyImageUri.logicalId,
511515
postgresMetaImageUri.logicalId,
512516
studioBranch.logicalId,
513517
],

0 commit comments

Comments
 (0)