|
| 1 | +import * as apphosting from "../gcp/apphosting"; |
| 2 | +import { logger } from "../logger"; |
| 3 | +import { Command } from "../command"; |
| 4 | +import { Options } from "../options"; |
| 5 | +import { needProjectId } from "../projectUtils"; |
| 6 | + |
| 7 | +function generateId(n = 6): string { |
| 8 | + const letters = "abcdefghijklmnopqrstuvwxyz"; |
| 9 | + const allChars = "01234567890-abcdefghijklmnopqrstuvwxyz"; |
| 10 | + let id = letters[Math.floor(Math.random() * letters.length)]; |
| 11 | + for (let i = 1; i < n; i++) { |
| 12 | + const idx = Math.floor(Math.random() * allChars.length); |
| 13 | + id += allChars[idx]; |
| 14 | + } |
| 15 | + return id; |
| 16 | +} |
| 17 | + |
| 18 | +export const command = new Command("apphosting:builds:create <backendId>") |
| 19 | + .description("Create a build for an App Hosting backend") |
| 20 | + .option("-l, --location <location>", "Specify the region of the backend", "us-central1") |
| 21 | + .option("-i, --id <buildId>", "Id of the build. If not present, autogenerate a random id", "") |
| 22 | + .option("-b, --branch <branch>", "Repository branch to deploy. Defaults to 'main'", "main") |
| 23 | + .before(apphosting.ensureApiEnabled) |
| 24 | + .action(async (backendId: string, options: Options) => { |
| 25 | + const projectId = needProjectId(options); |
| 26 | + const location = options.location as string; |
| 27 | + const buildId = (options.buildId as string) || generateId(); |
| 28 | + const branch = options.branch as string; |
| 29 | + |
| 30 | + const op = await apphosting.createBuild(projectId, location, backendId, buildId, { |
| 31 | + source: { |
| 32 | + codebase: { |
| 33 | + branch: "main", |
| 34 | + }, |
| 35 | + }, |
| 36 | + }); |
| 37 | + |
| 38 | + logger.info(`Started a build for backend ${backendId} on branch ${branch}.`); |
| 39 | + logger.info("Check status by running:"); |
| 40 | + logger.info(`\tfirebase apphosting:builds:get ${backendId} ${buildId} --location ${location}`); |
| 41 | + return op; |
| 42 | + }); |
0 commit comments