Skip to content

Commit

Permalink
Create a build command
Browse files Browse the repository at this point in the history
  • Loading branch information
pziemkowski committed Sep 6, 2023
1 parent 1cee96a commit 0778b06
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
branches: [ '**' ]

env:
BACKEND_BASE_IMAGE: python:3.11-slim-bullseye
SB_BACKEND_BASE_IMAGE: python:3.11-slim-bullseye

jobs:
test:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
branches: [ '**' ]

env:
BACKEND_BASE_IMAGE: python:3.11-slim-bullseye
SB_BACKEND_BASE_IMAGE: python:3.11-slim-bullseye

jobs:
build:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/workers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
branches: [ '**' ]

env:
WORKERS_BASE_IMAGE: python:3.9-slim-bullseye
SB_WORKERS_BASE_IMAGE: python:3.9-slim-bullseye

jobs:
build:
Expand Down
6 changes: 0 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ build:
lint:
pnpm nx run-many --output-style=stream --target=lint

deploy-components:
pnpm nx run --output-style=stream infra-shared:deploy:components

deploy-env-app: deploy-components
pnpm nx run-many --output-style=stream --target=deploy --projects=backend,workers,webapp

stop-task-scheduling-executions:
pnpm nx run --output-style=stream workers:stop-task-scheduling-executions

Expand Down
4 changes: 2 additions & 2 deletions docker-compose.ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ services:
- CI=true
build:
args:
- BACKEND_BASE_IMAGE=${BACKEND_BASE_IMAGE}
- BACKEND_BASE_IMAGE=${SB_BACKEND_BASE_IMAGE}

workers:
volumes:
Expand All @@ -27,4 +27,4 @@ services:
- AWS_SESSION_EXPIRATION=${AWS_SESSION_EXPIRATION}
build:
args:
- WORKERS_BASE_IMAGE=${WORKERS_BASE_IMAGE}
- WORKERS_BASE_IMAGE=${SB_WORKERS_BASE_IMAGE}
41 changes: 16 additions & 25 deletions packages/backend/scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ const { STSClient, GetCallerIdentityCommand } = require('@aws-sdk/client-sts');

const { runCommand } = require('./lib/runCommand');

const AWS_REGION = process.env.AWS_REGION;
const AWS_DEFAULT_REGION = process.env.AWS_DEFAULT_REGION;
const PROJECT_NAME = process.env.PROJECT_NAME;
const VERSION = process.env.VERSION;
const BACKEND_BASE_IMAGE = process.env.BACKEND_BASE_IMAGE;
const BACKEND_BASE_IMAGE = process.env.SB_BACKEND_BASE_IMAGE;

const stsClient = new STSClient();
const ecrClient = new ECRClient();
Expand All @@ -20,7 +21,8 @@ const ecrClient = new ECRClient();
const { Account: AWS_ACCOUNT_ID } = await stsClient.send(
getCallerIdentityCommand
);
const BACKEND_REPO_URI = `${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com/${PROJECT_NAME}-backend`;
const region = AWS_REGION || AWS_DEFAULT_REGION;
const BACKEND_REPO_URI = `${AWS_ACCOUNT_ID}.dkr.ecr.${region}.amazonaws.com/${PROJECT_NAME}-backend`;

try {
const getAuthorizationTokenCommand = new GetAuthorizationTokenCommand();
Expand Down Expand Up @@ -51,7 +53,7 @@ const ecrClient = new ECRClient();
'get-login',
'--no-include-email',
'--region',
AWS_DEFAULT_REGION,
region,
]);
}

Expand All @@ -61,27 +63,16 @@ const ecrClient = new ECRClient();
console.warn(`Warning: ${error.message}`);
}

if (BACKEND_BASE_IMAGE) {
await runCommand('docker', [
'build',
'--target',
'backend',
'-t',
`${BACKEND_REPO_URI}:${VERSION}`,
'--build-arg',
`BACKEND_BASE_IMAGE=${BACKEND_BASE_IMAGE}`,
'.',
]);
} else {
await runCommand('docker', [
'build',
'--target',
'backend',
'-t',
`${BACKEND_REPO_URI}:${VERSION}`,
'.',
]);
}
await runCommand('docker', [
'build',
'--target',
'backend',
'-t',
`${BACKEND_REPO_URI}:${VERSION}`,
'--build-arg',
`BACKEND_BASE_IMAGE=${BACKEND_BASE_IMAGE}`,
'.',
]);

await runCommand('docker', ['push', `${BACKEND_REPO_URI}:${VERSION}`]);
await runCommand('docker', [
Expand All @@ -92,6 +83,6 @@ const ecrClient = new ECRClient();
await runCommand('docker', ['push', `${BACKEND_REPO_URI}:latest`]);
} catch (error) {
console.error(`Error: ${error.message}`);
process.exit(1)
process.exit(1);
}
})();
2 changes: 1 addition & 1 deletion packages/infra/infra-shared/src/stacks/ci/ciBackend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export class BackendCiConfig extends ServiceCiConfig {
type: codebuild.BuildEnvironmentVariableType.SECRETS_MANAGER,
value: 'GlobalBuildSecrets:DOCKER_PASSWORD',
},
BACKEND_BASE_IMAGE: {
SB_BACKEND_BASE_IMAGE: {
type: codebuild.BuildEnvironmentVariableType.PLAINTEXT,
value: baseImage,
},
Expand Down
4 changes: 2 additions & 2 deletions packages/infra/infra-shared/src/stacks/ci/ciDocs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class DocsCiConfig extends ServiceCiConfig {
...this.getWorkspaceSetupCommands(PnpmWorkspaceFilters.DOCS),
this.getECRLoginCommand(),
];
const baseImage = `${GlobalECR.getECRPublicCacheUrl()}/${
const baseImage = `${GlobalECR.getECRPublicCacheUrl()}/docker/library/${
props.envSettings.dockerImages.backendBaseImage
}`;

Expand Down Expand Up @@ -109,7 +109,7 @@ export class DocsCiConfig extends ServiceCiConfig {
type: codebuild.BuildEnvironmentVariableType.SECRETS_MANAGER,
value: 'GlobalBuildSecrets:DOCKER_PASSWORD',
},
BACKEND_BASE_IMAGE: {
SB_BACKEND_BASE_IMAGE: {
type: codebuild.BuildEnvironmentVariableType.PLAINTEXT,
value: baseImage,
},
Expand Down
4 changes: 2 additions & 2 deletions packages/infra/infra-shared/src/stacks/ci/ciServerless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export class ServerlessCiConfig extends ServiceCiConfig {
type: codebuild.BuildEnvironmentVariableType.PLAINTEXT,
value: dockerAssumeRole.roleArn,
},
WORKERS_BASE_IMAGE: {
SB_WORKERS_BASE_IMAGE: {
type: codebuild.BuildEnvironmentVariableType.PLAINTEXT,
value: baseImage,
},
Expand Down Expand Up @@ -238,7 +238,7 @@ export class ServerlessCiConfig extends ServiceCiConfig {
type: codebuild.BuildEnvironmentVariableType.PLAINTEXT,
value: dockerAssumeRole.roleArn,
},
WORKERS_BASE_IMAGE: {
SB_WORKERS_BASE_IMAGE: {
type: codebuild.BuildEnvironmentVariableType.PLAINTEXT,
value: baseImage,
},
Expand Down
18 changes: 18 additions & 0 deletions packages/internal/cli/src/commands/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Command, Flags } from '@oclif/core';

import { initConfig } from '../config/init';
import { runCommand } from '../lib/runCommand';

export default class Build extends Command {
static description = 'Build all deployable artifacts';

static examples = [`$ <%= config.bin %> <%= command.id %>`];

async run(): Promise<void> {
await initConfig(this, { requireAws: true });

await runCommand('pnpm', ['nx', 'run', 'backend:build']);
await runCommand('pnpm', ['nx', 'run', 'webapp:build']);
await runCommand('pnpm', ['nx', 'run', 'workers:build']);
}
}
2 changes: 1 addition & 1 deletion packages/internal/cli/src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { initConfig } from '../config/init';
import { runCommand } from '../lib/runCommand';

export default class Deploy extends Command {
static description = 'Starts both backend and frontend';
static description = 'Deploy all previously built artifacts to AWS';

static examples = [`$ <%= config.bin %> <%= command.id %>`];

Expand Down
3 changes: 2 additions & 1 deletion packages/internal/cli/src/config/aws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ async function loadStageEnv(
dotenv.populate(process.env, parsed, { override: true });

if (shouldValidate) {
await validateStageEnv();
const validationResult = await validateStageEnv();
Object.assign(process.env, validationResult);
}
}

Expand Down
6 changes: 3 additions & 3 deletions packages/internal/cli/src/config/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const exec = promisify(childProcess.exec);

type LoadDotenvOptions = {
rootPath: string;
}
export const loadDotenv = async ({ rootPath } : LoadDotenvOptions) => {
};
export const loadDotenv = async ({ rootPath }: LoadDotenvOptions) => {
dotenv.config({ path: resolve(rootPath, '.env') });
};

Expand All @@ -29,7 +29,7 @@ export async function loadVersionEnv() {
}

export async function validateStageEnv() {
envalid.cleanEnv(process.env, {
return envalid.cleanEnv(process.env, {
PROJECT_NAME: envalid.str({
desc: 'The name of your project (best if 3-5 characters to avoid AWS names being to long)',
example: 'saas',
Expand Down

0 comments on commit 0778b06

Please sign in to comment.