import { Construct } from 'constructs'; import kebabCase from 'lodash.kebabcase'; import { DataArchiveFile } from '../generated/providers/archive'; import { EcrRepository } from '../generated/providers/aws/ecr'; import { getId, getPathFromRoot } from '../utils'; type DockerFileBuilderProps = { /** * Suffix to apply to the repository for the image. Useful if needing * multiple images. */ repoSuffix?: string; /** * Where in the local repo is based on, relative to the root. */ localFileHashLocation?: string; }; /** * Builds a docker file locally and pushes it up to a repo */ export default class DockerFileBuilder extends Construct { // readonly resource: ITerraformDependable; readonly imageUrl: string; constructor(scope: Construct, id: string, options?: DockerFileBuilderProps) { super(scope, id); // setup ecr repo const imageRepo = new EcrRepository(this, 'ecr_repo', { name: kebabCase( options?.repoSuffix ? getId(options.repoSuffix) : getId() ), imageScanningConfiguration: { scanOnPush: true, }, }); const apiFolderArchive = new DataArchiveFile(this, 'api_archive', { type: 'zip', sourceDir: getPathFromRoot( `${options?.localFileHashLocation ?? 'api/src'}` ), outputPath: `src.zip`, }); const imageTag = process.env.IMAGE_TAG ?? apiFolderArchive.outputSha; const ecrRepoUrl = imageRepo.repositoryUrl; this.imageUrl = `${ecrRepoUrl}:${imageTag}`; } }