Skip to content

🎥 Make videos programmatically with React

script to update Remotion version works

Matt McGillivray8867d521d6bd
diff --git a/packages/cloudrun/install.mjs b/packages/cloudrun/install.mjs
index a52b61d1d8..127242e7a4 100644
--- a/packages/cloudrun/install.mjs
+++ b/packages/cloudrun/install.mjs
@@ -113,6 +113,7 @@ switch (selection) {
break;
case 'updateRemotion':
+ await updateRemotion();
break;
case 'generateEnv':
@@ -152,7 +153,7 @@ function envPrompt() {
});
}
-async function runTerraform() {
+function checkTerraformStateFile() {
/****************************************
* Check for existing Terraform State
****************************************/
@@ -198,40 +199,63 @@ async function runTerraform() {
process.exit(1);
}
}
+}
- /****************************************
- * Prompt user for Remotion version
- ****************************************/
+function versionPrompt() {
+ return new Promise((resolve) => {
+ // regex to ensure remotionVersion is in semver format
+ const semverRegex = new RegExp(
+ /^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/,
+ 'i'
+ );
- function versionPrompt() {
- return new Promise((resolve) => {
- // regex to ensure remotionVersion is in semver format
- const semverRegex = new RegExp(
- /^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/,
- 'i'
- );
+ const rl = readline.createInterface({
+ input: process.stdin,
+ output: process.stdout,
+ });
- const rl = readline.createInterface({
- input: process.stdin,
- output: process.stdout,
- });
+ rl.question(
+ `What version of Remotion do you want to use (format: 1.0.0)? ${blueTextColor}`,
+ async (answer) => {
+ rl.write(`\n${resetTextColor}`);
+ rl.close();
- rl.question(
- `What version of Remotion do you want to use (format: 1.0.0)? ${blueTextColor}`,
- async (answer) => {
- rl.write(`\n${resetTextColor}`);
- rl.close();
+ let fileError = false;
+ if (semverRegex.test(answer.trim())) {
+ try {
+ execSync(
+ `gcloud storage ls gs://remotion-sa/${answer.trim()}/sa-permissions.json`,
+ {stdio: 'inherit'}
+ );
+ return resolve(answer.trim());
+ } catch {
+ fileError = true;
+ }
+ }
- if (semverRegex.test(answer.trim())) return resolve(answer.trim());
+ if (fileError) {
+ console.log(
+ `${redTextColor}Cannot find permissions file that matches version ${answer.trim()}.\n${resetTextColor}`
+ );
+ } else {
+ console.log(
+ `${redTextColor}${answer.trim()} is not a valid semver version number.\n${resetTextColor}`
+ );
+ }
- console.log(`${answer.trim()} is not a valid version number.\n`);
- const result = await versionPrompt();
+ const result = await versionPrompt();
- resolve(result);
- }
- );
- });
- }
+ resolve(result);
+ }
+ );
+ });
+}
+
+async function runTerraform() {
+ await checkTerraformStateFile();
+ /****************************************
+ * Prompt user for Remotion version
+ ****************************************/
const remotionVersion = await versionPrompt();
@@ -368,6 +392,100 @@ async function runTerraform() {
const applyPlan = await applyPrompt();
+ if (applyPlan) {
+ execSync('terraform apply remotion.tfplan', {stdio: 'inherit'});
+ } else {
+ console.log(
+ 'Plan not applied, deployed Remotion version remains unchanged.'
+ );
+ }
+}
+
+async function updateRemotion() {
+ await checkTerraformStateFile();
+
+ /****************************************
+ * Check the existing remotion version
+ ****************************************/
+
+ const deployedVersion = execSync(
+ `gcloud iam service-accounts describe remotion-sa@${projectID}.iam.gserviceaccount.com | grep "description:" | awk '{print $2}'`,
+ {
+ stdio: ['inherit', 'pipe', 'pipe'],
+ }
+ )
+ .toString()
+ .trim();
+
+ execSync(
+ `echo "For project ${blueTextColor}${projectID}${resetTextColor}, Remotion version ${blueTextColor}${deployedVersion}${resetTextColor} is deployed.\n"`,
+ {
+ stdio: 'inherit',
+ }
+ );
+
+ const remotionVersion = await versionPrompt();
+
+ /****************************************
+ * Terraform init, plan and apply. Will prompt user for confirmation before applying.
+ ****************************************/
+
+ console.log(
+ `\n\n${greenBackground} Running Terraform ${resetTextColor}`
+ );
+ const terraformVariables = `-var="remotion_version=${remotionVersion}" -var="project_id=${projectID}" -var="service_account_exists=${true}"`;
+
+ execSync('terraform init', {stdio: 'inherit'});
+
+ // If tfstate file exists, remove the role resource from state so that it can be imported fresh
+ try {
+ execSync('terraform state list', {stdio: 'pipe'});
+ execSync(`terraform state rm google_project_iam_custom_role.remotion_sa`, {
+ stdio: 'inherit',
+ });
+ } catch {}
+
+ execSync(
+ `terraform import ${terraformVariables} google_project_iam_custom_role.remotion_sa projects/${projectID}/roles/RemotionSA`,
+ {stdio: 'inherit'}
+ );
+
+ execSync(`terraform plan ${terraformVariables} -out=remotion.tfplan`, {
+ stdio: 'inherit',
+ });
+
+ // After the plan is complete, prompt the user to apply the plan or not
+ function applyPrompt() {
+ return new Promise((resolve) => {
+ const rl = readline.createInterface({
+ input: process.stdin,
+ output: process.stdout,
+ });
+
+ rl.question(
+ `\n${greenTextColor}Do you want to apply the above plan? ${blueTextColor}`,
+ async (answer) => {
+ // reset terminal color
+ rl.output.write(resetTextColor);
+ rl.close();
+
+ if (['yes', 'y'].indexOf(answer.trim().toLowerCase()) >= 0)
+ return resolve(true);
+
+ if (['no', 'n'].indexOf(answer.trim().toLowerCase()) >= 0)
+ return resolve(false);
+
+ console.log('Invalid response.');
+ const result = await applyPrompt();
+
+ resolve(result);
+ }
+ );
+ });
+ }
+
+ const applyPlan = await applyPrompt();
+
if (applyPlan) {
execSync('terraform apply remotion.tfplan', {stdio: 'inherit'});
// After the resources are created, prompt the user to generate the .env file or not
diff --git a/packages/cloudrun/terraform/main.tf b/packages/cloudrun/terraform/main.tf
index 352674b9d4..19e448c615 100644
--- a/packages/cloudrun/terraform/main.tf
+++ b/packages/cloudrun/terraform/main.tf
@@ -76,11 +76,6 @@ resource "google_project_service" "cloud_resource_manager" {
service = "cloudresourcemanager.googleapis.com"
}
-# Output the command to generate service account keys
-output "service_account_key_generation_command" {
- value = "gcloud iam service-accounts keys create key.json --iam-account=${local.service-account-email}"
-}
-
# For tf state diagnosis, output the project ID
output "remotion_project_id" {
value = var.project_id
diff --git a/packages/docs/docs/cloudrun/setup.md b/packages/docs/docs/cloudrun/setup.md
index a916a766b4..cb049af965 100644
--- a/packages/docs/docs/cloudrun/setup.md
+++ b/packages/docs/docs/cloudrun/setup.md
@@ -115,7 +115,11 @@ This means that you can clone a github repo, run a couple of Terraform commands,
## 5. Optional: Validate the permission setup
-- Run `npx remotion cloudrun policies validate`
+From within your code base, run the following command to validate the permissions are setup correctly in GCP.
+
+```
+npx remotion cloudrun policies validate
+```
<hr/>