Skip to content

🎥 Make videos programmatically with React

adding cli command for deploy

Matt Mcgillivray65a003d3cfb1
diff --git a/packages/cli/src/gcp-command.ts b/packages/cli/src/gcp-command.ts
new file mode 100644
index 0000000000..fe94471731
--- /dev/null
+++ b/packages/cli/src/gcp-command.ts
@@ -0,0 +1,25 @@
+import {Log} from './log';
+import {getPackageManager} from './preview-server/get-package-manager';
+import {getRemotionVersion} from './preview-server/update-available';
+
+export const gcpCommand = async (remotionRoot: string, args: string[]) => {
+ try {
+ const path = require.resolve('@remotion/gcp', {
+ paths: [remotionRoot],
+ });
+ const {GCPInternals} = require(path);
+
+ await GCPInternals.executeCommand(args, remotionRoot);
+ process.exit(0);
+ } catch (err) {
+ const manager = getPackageManager(remotionRoot, undefined);
+ const installCommand =
+ manager === 'unknown' ? 'npm i' : manager.installCommand;
+ Log.error(err);
+ Log.error('Remotion GCP is not installed.');
+ Log.info('');
+ Log.info('You can install it using:');
+ Log.info(`${installCommand} i @remotion/gcp@${getRemotionVersion()}`);
+ process.exit(1);
+ }
+};
diff --git a/packages/cli/src/index.tsx b/packages/cli/src/index.tsx
index 460186b79d..925bab85f0 100644
--- a/packages/cli/src/index.tsx
+++ b/packages/cli/src/index.tsx
@@ -15,6 +15,7 @@ import {getImageFormat} from './image-formats';
import {initializeCli} from './initialize-cli';
import {installCommand, INSTALL_COMMAND} from './install';
import {lambdaCommand} from './lambda-command';
+import {gcpCommand} from './gcp-command';
import {Log} from './log';
import {makeProgressBar} from './make-progress-bar';
import {BooleanFlags, parsedCli, quietFlagProvided} from './parse-command-line';
@@ -58,6 +59,8 @@ export const cli = async () => {
await previewCommand(remotionRoot, args);
} else if (command === 'lambda') {
await lambdaCommand(remotionRoot, args);
+ } else if (command === 'gcp') {
+ await gcpCommand(remotionRoot, args);
} else if (command === 'render') {
await render(remotionRoot, args);
} else if (command === 'still') {
diff --git a/packages/gcp/package.json b/packages/gcp/package.json
index 89e1a135dc..a930768712 100644
--- a/packages/gcp/package.json
+++ b/packages/gcp/package.json
@@ -8,12 +8,17 @@
"start": "functions-framework --target=TypescriptFunction",
"prestart": "pnpm run build"
},
+ "bin": {
+ "remotion-gcp": "remotion-gcp-cli.mjs"
+ },
"dependencies": {
"@google-cloud/functions-framework": "^3.1.3",
"@remotion/renderer": "3.3.36",
"remotion": "3.3.36",
"@remotion/bundler": "3.3.36",
- "@google-cloud/storage": "^6.9.1"
+ "@google-cloud/storage": "^6.9.1",
+ "minimist": "1.2.6",
+ "@remotion/cli": "3.3.36"
},
"devDependencies": {
"prettier-plugin-organize-imports": "^3.2.2",
diff --git a/packages/gcp/readme.md b/packages/gcp/readme.md
index cfe00ee74a..edb760141c 100644
--- a/packages/gcp/readme.md
+++ b/packages/gcp/readme.md
@@ -50,4 +50,18 @@ Within the Cloud Run service, you can see the URL of the service. You can run th
> as far as I understand it right now, one would have to clone the monorepo to deploy it
Add firestore option for storing progress
-https://www.remotion.dev/docs/renderer/render-media#onprogress
\ No newline at end of file
+https://www.remotion.dev/docs/renderer/render-media#onprogress
+
+# To investigate
+How can people get the :latest version from artifact registry?
+How to program a CLI command that will deploy the container to Cloud Run?
+
+## Created public artifact repo in GCP
+Public artifact registry that contains the container images
+ us-docker.pkg.dev/remotion-dev/cloud-run/render
+Image is tagged with the same semver as the remotion packages
+ us-docker.pkg.dev/remotion-dev/cloud-run/render:3.3.36-alpha
+Part of the CI/CD pipeline should be to push a new container image to the artifact registry, with the right version:
+`gcloud builds submit --tag us-docker.pkg.dev/remotion-dev/cloud-run/render:${version}`
+
+## deployCloudRun()
diff --git a/packages/gcp/remotion-gcp-cli.mjs b/packages/gcp/remotion-gcp-cli.mjs
new file mode 100755
index 0000000000..72587c6077
--- /dev/null
+++ b/packages/gcp/remotion-gcp-cli.mjs
@@ -0,0 +1,33 @@
+#!/usr/bin/env node
+import { exec } from 'child_process';
+import minimist from 'minimist';
+import {CliInternals} from '@remotion/cli';
+
+const {Log} = CliInternals;
+
+// process args:
+// [2]. remotion version (this should match in the Artifact Registry)
+// [3]. project name
+// [4]. service name
+// [5]. region
+
+let args = minimist(process.argv.slice(2));
+console.log(args);
+console.log(args.project ? `project set to: ${args.project}` : 'no project set');
+// console.log(args.remotionVersion ? `remotion version requested: ${args.remotionVersion}` : 'no remotion-version passed');
+if (args.remotionVersion) {
+ console.log(`remotion version requested: ${args.remotionVersion}`)
+} else {
+ Log.error('no remotion version set!')
+}
+
+// exec(`gcloud run deploy ${process.argv[4]} --image=us-docker.pkg.dev/remotion-dev/cloud-run/render:${process.argv[2]} --project=${args.project} --region=${process.argv[5]} --allow-unauthenticated`,
+// (error, stdout, stderr) => {
+// console.log(stdout);
+// console.log(stderr);
+// if (error !== null) {
+// console.log(`exec error: ${error}`);
+// }
+// });
+
+// npx remotion-gcp --remotion-version=3.3.36-alpha --project=remotion-lambda --service-name=hello --region=australia-southeast1
\ No newline at end of file
diff --git a/packages/gcp/src/api/deploy-cloud-run.ts b/packages/gcp/src/api/deploy-cloud-run.ts
new file mode 100644
index 0000000000..2a1689600d
--- /dev/null
+++ b/packages/gcp/src/api/deploy-cloud-run.ts
@@ -0,0 +1,49 @@
+import { validateGcpRegion } from '../shared/validate-gcp-region';
+import { validateServiceName } from '../shared/validate-service-name';
+import { validateProjectID } from '../shared/validate-project-id';
+import { exec } from 'child_process';
+
+export type DeployCloudRunInput = {
+ remotionVersion: string;
+ serviceName: string;
+ projectID: string;
+ region: string;
+ allowUnauthenticated: boolean;
+};
+
+// TODO - add some meaningful output
+// export type DeployCloudRunOutput = {
+// functionName: string;
+// alreadyExisted: boolean;
+// };
+
+/**
+ * @description Creates a Cloud Run service in your project that will be able to render a video in GCP.
+ * @link https://remotion.dev/docs/lambda/deployfunction
+ * @param options.remotionVersion Which version of Remotion to use within the Cloud Run service.
+ * @param options.projectID GCP Project ID to deploy the Cloud Run service to.
+ * @param options.serviceName The name of the Cloud Run service.
+ * @param options.region The region you want to deploy your Cloud Run service to.
+ * @param options.allowUnauthenticated Defaults true. If true, allow anyone with the Cloud Run url to trigger the service. Alternatively, manage access via Cloud IAM in GCP.
+ * @returns {Promise<DeployCloudRunOutput>} An object that contains the `functionName` property
+ */
+export const deployCloudRun = async (
+ options: DeployCloudRunInput
+): Promise<undefined> => {
+ validateGcpRegion(options.region);
+ validateServiceName(options.serviceName);
+ validateProjectID(options.projectID);
+
+
+exec(`gcloud run deploy ${options.serviceName} --image=us-docker.pkg.dev/remotion-dev/cloud-run/render:${options.remotionVersion} --project=${options.projectID} --region=${options.region} ${options.allowUnauthenticated ? '--allow-unauthenticated' : '--no-allow-unauthenticated'}`,
+ (error, stdout, stderr) => {
+ console.log(stdout);
+ console.log(stderr);
+ if (error !== null) {
+ console.log(`exec error: ${error}`);
+ }
+ });
+
+ // TODO - return cloud run serve url
+ return ;
+};
diff --git a/packages/gcp/src/cli/args.ts b/packages/gcp/src/cli/args.ts
new file mode 100644
index 0000000000..5b00cd47c1
--- /dev/null
+++ b/packages/gcp/src/cli/args.ts
@@ -0,0 +1,18 @@
+import {CliInternals} from '@remotion/cli';
+
+import type {GcpRegion} from '../pricing/gcp-regions';
+
+type GCPCommandLineOptions = {
+ region: GcpRegion;
+ ['project-id']: string;
+ ['remotion-version']: string | undefined;
+ ['service-name']: string;
+ ['allow-unauthenticated']: boolean | undefined;
+};
+
+export const parsedGcpCli = CliInternals.minimist<GCPCommandLineOptions>(
+ process.argv.slice(2),
+ {
+ boolean: CliInternals.BooleanFlags,
+ }
+);
diff --git a/packages/gcp/src/cli/commands/cloud-run/deploy.ts b/packages/gcp/src/cli/commands/cloud-run/deploy.ts
new file mode 100644
index 0000000000..6114b31448
--- /dev/null
+++ b/packages/gcp/src/cli/commands/cloud-run/deploy.ts
@@ -0,0 +1,57 @@
+import { CliInternals } from '@remotion/cli';
+import { Log } from '@remotion/cli/dist/log';
+import { VERSION } from 'remotion/version';
+import { deployCloudRun } from '../../../api/deploy-cloud-run';
+import { validateServiceName } from '../../../shared/validate-service-name';
+import { validateProjectID } from '../../../shared/validate-project-id';
+import { validateRemotionVersion } from '../../../shared/validate-remotion-version';
+import { parsedGcpCli } from '../../args';
+import { getGcpRegion } from '../../get-gcp-region';
+
+export const CLOUD_RUN_DEPLOY_SUBCOMMAND = 'deploy';
+
+export const cloudRunDeploySubcommand = async () => {
+ const region = getGcpRegion();
+ const serviceName = parsedGcpCli['service-name'];
+ const projectID = parsedGcpCli['project-id'];
+ const remotionVersion = parsedGcpCli['remotion-version'] ?? VERSION;
+ const allowUnauthenticated = parsedGcpCli['allow-unauthenticated'] ?? true;
+
+ validateServiceName(serviceName);
+ validateProjectID(projectID);
+ validateRemotionVersion(remotionVersion);
+ if (!CliInternals.quietFlagProvided()) {
+ Log.info(
+ CliInternals.chalk.gray(
+ `
+Remotion Version = ${remotionVersion}
+Service Name = ${serviceName}
+Project Name = ${projectID}
+Region = ${region}
+Allow Unauthenticated Access = ${allowUnauthenticated}
+ `.trim()
+ )
+ );
+ }
+
+ const output = CliInternals.createOverwriteableCliOutput(
+ CliInternals.quietFlagProvided()
+ );
+ output.update('Deploying Cloud Run...');
+ await deployCloudRun({
+ remotionVersion,
+ serviceName,
+ projectID,
+ region,
+ allowUnauthenticated
+ });
+ if (CliInternals.quietFlagProvided()) {
+ // Log.info(functionName);
+ }
+
+ // if (alreadyExisted) {
+ // output.update(`Already exists as ${functionName}\n`);
+ // } else {
+ // output.update(`Deployed as ${functionName}\n`);
+ // }
+};
diff --git a/packages/gcp/src/cli/commands/cloud-run/index.ts b/packages/gcp/src/cli/commands/cloud-run/index.ts
new file mode 100644
index 0000000000..c459af883e
--- /dev/null
+++ b/packages/gcp/src/cli/commands/cloud-run/index.ts
@@ -0,0 +1,23 @@
+import {Log} from '@remotion/cli/dist/log';
+import {quit} from '../../helpers/quit';
+import {cloudRunDeploySubcommand, CLOUD_RUN_DEPLOY_SUBCOMMAND} from './deploy';
+
+export const CLOUD_RUN_COMMAND = 'cloud-run';
+
+const printCloudRunHelp = () => {
+ // TODO: Add help text
+};
+
+export const cloudRunCommand = (args: string[]) => {
+ if (args[0] === CLOUD_RUN_DEPLOY_SUBCOMMAND) {
+ return cloudRunDeploySubcommand();
+ }
+
+ if (args[0]) {
+ Log.error(`Subcommand ${args[0]} not found.`);
+ printCloudRunHelp();
+ quit(1);
+ }
+
+ printCloudRunHelp();
+};
diff --git a/packages/gcp/src/cli/get-gcp-region.ts b/packages/gcp/src/cli/get-gcp-region.ts
new file mode 100644
index 0000000000..dbcbdcc8dd
--- /dev/null
+++ b/packages/gcp/src/cli/get-gcp-region.ts
@@ -0,0 +1,13 @@
+import type {GcpRegion} from '../pricing/gcp-regions';
+import {DEFAULT_REGION} from '../shared/constants';
+import {validateGcpRegion} from '../shared/validate-gcp-region';
+import {parsedGcpCli} from './args';
+
+export const getGcpRegion = (): GcpRegion => {
+ if (parsedGcpCli.region) {
+ validateGcpRegion(parsedGcpCli.region);
+ return parsedGcpCli.region;
+ }
+
+ return DEFAULT_REGION;
+};
diff --git a/packages/gcp/src/cli/helpers/quit.ts b/packages/gcp/src/cli/helpers/quit.ts
new file mode 100644
index 0000000000..3d142a276e
--- /dev/null
+++ b/packages/gcp/src/cli/helpers/quit.ts
@@ -0,0 +1,3 @@
+export const quit = (exitCode: number): never => {
+ process.exit(exitCode);
+};
diff --git a/packages/gcp/src/cli/index.ts b/packages/gcp/src/cli/index.ts
new file mode 100644
index 0000000000..11b8ad84cd
--- /dev/null
+++ b/packages/gcp/src/cli/index.ts
@@ -0,0 +1,36 @@
+import {Log} from './log';
+import {quit} from './helpers/quit';
+import {parsedGcpCli} from './args';
+import {cloudRunCommand, CLOUD_RUN_COMMAND} from './commands/cloud-run';
+
+const matchCommand = (args: string[], remotionRoot: string) => {
+ if (parsedGcpCli.help || args.length === 0) {
+ // printHelp(); TODO: implement help
+ quit(0);
+ }
+
+ if (args[0] === CLOUD_RUN_COMMAND) {
+ return cloudRunCommand(args.slice(1));
+ }
+
+ if (args[0] === 'deploy') {
+ Log.info(`The "deploy" command does not exist.`);
+ Log.info(`Did you mean "cloud-run deploy"?`);
+ }
+
+ Log.error(`Command ${args[0]} not found.`);
+ // printHelp(); TODO: implement help
+ quit(1);
+};
+
+export const executeCommand = async (args: string[], remotionRoot: string) => {
+ try {
+ await matchCommand(args, remotionRoot);
+ } catch (err) {
+ const error = err as Error;
+ // catch errors and print a message. Check lambda cli for example
+
+ Log.error(error.stack);
+ quit(1);
+ }
+};
\ No newline at end of file
diff --git a/packages/gcp/src/cli/log.ts b/packages/gcp/src/cli/log.ts
new file mode 100644
index 0000000000..96a4fa994b
--- /dev/null
+++ b/packages/gcp/src/cli/log.ts
@@ -0,0 +1,3 @@
+import {CliInternals} from '@remotion/cli';
+
+export const {Log} = CliInternals;
diff --git a/packages/gcp/src/index.ts b/packages/gcp/src/index.ts
index 2717dda478..7f787f1009 100644
--- a/packages/gcp/src/index.ts
+++ b/packages/gcp/src/index.ts
@@ -1,8 +1,9 @@
import * as ff from '@google-cloud/functions-framework';
import { renderMediaSingleThread } from "./functions/render-media-single-thread";
import { renderStillSingleThread } from "./functions/render-still-single-thread";
+import {GCPInternals} from './internals';
-export const renderOnCloudRun = async (req: ff.Request, res: ff.Response) => {
+const renderOnCloudRun = async (req: ff.Request, res: ff.Response) => {
const renderType = req.body.type;
switch (renderType) {
@@ -15,4 +16,6 @@ export const renderOnCloudRun = async (req: ff.Request, res: ff.Response) => {
default:
res.status(400).send('Invalid render type, must be either "media" or "still"');
}
-};
\ No newline at end of file
+};
+
+export { renderOnCloudRun, GCPInternals }
\ No newline at end of file
diff --git a/packages/gcp/src/internals.ts b/packages/gcp/src/internals.ts
new file mode 100644
index 0000000000..4d1b466212
--- /dev/null
+++ b/packages/gcp/src/internals.ts
@@ -0,0 +1,4 @@
+import {executeCommand} from './cli/index';
+export const GCPInternals = {
+ executeCommand,
+};
diff --git a/packages/gcp/src/pricing/gcp-regions.ts b/packages/gcp/src/pricing/gcp-regions.ts
new file mode 100644
index 0000000000..23112bca3b
--- /dev/null
+++ b/packages/gcp/src/pricing/gcp-regions.ts
@@ -0,0 +1,39 @@
+export const GCP_REGIONS = [
+ 'asia-east1',
+ 'asia-east2',
+ 'asia-northeast1',
+ 'asia-northeast2',
+ 'asia-northeast3',
+ 'asia-south1',
+ 'asia-south2',
+ 'asia-southeast1',
+ 'asia-southeast2',
+ 'australia-southeast1',
+ 'australia-southeast2',
+ 'europe-central2',
+ 'europe-north1',
+ 'europe-southwest1',
+ 'europe-west1',
+ 'europe-west2',
+ 'europe-west3',
+ 'europe-west4',
+ 'europe-west6',
+ 'europe-west8',
+ 'europe-west9',
+ 'me-west1',
+ 'northamerica-northeast1',
+ 'northamerica-northeast2',
+ 'southamerica-east1',
+ 'southamerica-west1',
+ 'us-central1',
+ 'us-east1',
+ 'us-east4',
+ 'us-east5',
+ 'us-south1',
+ 'us-west1',
+ 'us-west2',
+ 'us-west3',
+ 'us-west4'
+] as const;
+
+export type GcpRegion = typeof GCP_REGIONS[number];
diff --git a/packages/gcp/src/shared/constants.ts b/packages/gcp/src/shared/constants.ts
new file mode 100644
index 0000000000..cf4d325804
--- /dev/null
+++ b/packages/gcp/src/shared/constants.ts
@@ -0,0 +1,3 @@
+import type {GcpRegion} from '../pricing/gcp-regions';
+
+export const DEFAULT_REGION: GcpRegion = 'us-east1';
\ No newline at end of file
diff --git a/packages/gcp/src/shared/validate-gcp-region.ts b/packages/gcp/src/shared/validate-gcp-region.ts
new file mode 100644
index 0000000000..1657bfa8e1
--- /dev/null
+++ b/packages/gcp/src/shared/validate-gcp-region.ts
@@ -0,0 +1,14 @@
+import type {GcpRegion} from '../pricing/gcp-regions';
+import {GCP_REGIONS} from '../pricing/gcp-regions';
+
+export function validateGcpRegion(
+ region: unknown
+): asserts region is GcpRegion {
+ if (!GCP_REGIONS.includes(region as GcpRegion)) {
+ throw new TypeError(
+ `${region} is not a valid GCP region. Must be one of: ${GCP_REGIONS.join(
+ ', '
+ )}`
+ );
+ }
+}
diff --git a/packages/gcp/src/shared/validate-project-id.ts b/packages/gcp/src/shared/validate-project-id.ts
new file mode 100644
index 0000000000..423fd5df4d
--- /dev/null
+++ b/packages/gcp/src/shared/validate-project-id.ts
@@ -0,0 +1,34 @@
+import { exec } from 'child_process';
+
+export const validateProjectID = (projectID: unknown) => {
+ if (typeof projectID === 'undefined') {
+ throw new TypeError(
+ `The 'projectID' argument must be provided, but is missing.`
+ );
+ }
+
+ if (typeof projectID !== 'string') {
+ throw new TypeError(
+ `The 'projectID' argument must be a string, but is ${JSON.stringify(
+ projectID
+ )}`
+ );
+ }
+
+ exec(`gcloud projects describe ${projectID}`,
+ (error) => {
+ if (error !== null) {
+ throw new Error(`Project ID ${projectID} not found in GCP. It either does not exist, or you do not have access to it.`)
+ } else {
+ return
+ }
+ });
+
+ if (!projectID.match(/^[a-zA-Z][a-zA-Z0-9-]{0,48}[a-zA-Z0-9]$/g)) {
+ throw new Error(
+ "The `projectID` must match the RegExp `/^[a-zA-Z][a-zA-Z0-9-]{0,48}[a-zA-Z0-9]$/g`. This means it may only start with a letter, end with a letter or number, and contain up to 49 lowercase letters, numbers or hyphens. You passed: " +
+ projectID +
+ '. Check for invalid characters.'
+ );
+ }
+};
diff --git a/packages/gcp/src/shared/validate-remotion-version.ts b/packages/gcp/src/shared/validate-remotion-version.ts
new file mode 100644
index 0000000000..7b08dd3065
--- /dev/null
+++ b/packages/gcp/src/shared/validate-remotion-version.ts
@@ -0,0 +1,4 @@
+export const validateRemotionVersion = (remotionVersion: unknown) => {
+ // TODO - ensure the image exists at us-docker.pkg.dev/remotion-dev/cloud-run/render:${remotionVersion}
+ return
+};
diff --git a/packages/gcp/src/shared/validate-service-name.ts b/packages/gcp/src/shared/validate-service-name.ts
new file mode 100644
index 0000000000..e111c92600
--- /dev/null
+++ b/packages/gcp/src/shared/validate-service-name.ts
@@ -0,0 +1,25 @@
+export const validateServiceName = (serviceName: unknown) => {
+ if (typeof serviceName === 'undefined') {
+ // TODO - have a default service name, but also check GCP to see if it's already taken, and iterate
+ // `gcloud run services list --project=projectID` can be used, but then this function needs to be aware of the project ID also
+ throw new TypeError(
+ `The 'serviceName' argument must be provided, but is missing.`
+ );
+ }
+
+ if (typeof serviceName !== 'string') {
+ throw new TypeError(
+ `The 'serviceName' argument must be a string, but is ${JSON.stringify(
+ serviceName
+ )}`
+ );
+ }
+
+ if (!serviceName.match(/^[a-zA-Z][a-zA-Z0-9-]{0,48}[a-zA-Z0-9]$/g)) {
+ throw new Error(
+ "The `serviceName` must match the RegExp `/^[a-zA-Z][a-zA-Z0-9-]{0,48}[a-zA-Z0-9]$/g`. This means it may only start with a letter, end with a letter or number, and contain up to 49 lowercase letters, numbers or hyphens. You passed: " +
+ serviceName +
+ '. Check for invalid characters.'
+ );
+ }
+};
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 4e7f2199db..f3e44dd674 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -539,8 +539,10 @@ importers:
'@google-cloud/functions-framework': ^3.1.3
'@google-cloud/storage': ^6.9.1
'@remotion/bundler': 3.3.36
+ '@remotion/cli': 3.3.36
'@remotion/compositor-linux-x64-musl': 3.3.36
'@remotion/renderer': 3.3.36
+ minimist: 1.2.6
prettier-plugin-organize-imports: ^3.2.2
remotion: 3.3.36
ts-node: ^10.8.0
@@ -549,7 +551,9 @@ importers:
'@google-cloud/functions-framework': 3.1.3
'@google-cloud/storage': 6.9.1
'@remotion/bundler': link:../bundler
+ '@remotion/cli': link:../cli
'@remotion/renderer': link:../renderer
+ minimist: 1.2.6
remotion: link:../core
devDependencies:
'@remotion/compositor-linux-x64-musl': link:../compositor-linux-x64-musl
@@ -557,13 +561,7 @@ importers:
ts-node: 10.8.0_vnj66vw6pmisrnpmmxy72mdbpa
typescript: 4.7.2
- packages/gcp/containers/renderMedia:
- specifiers:
- '@google-cloud/functions-framework': ^3.1.3
- dependencies:
- '@google-cloud/functions-framework': 3.1.3
-
- packages/gcp/containers/renderStill:
+ packages/gcp/container:
specifiers:
'@google-cloud/functions-framework': ^3.1.3
dependencies: