chore: check if HEAD is behind main before deploying agents (#4593)

chore: check if HEAD is behind main before deploying agents


![image](https://github.com/user-attachments/assets/9c2b5f9c-d037-42c7-be44-daf97fc37709)


![image](https://github.com/user-attachments/assets/df52f9f7-5962-4b60-b75b-e4cc87a9897f)
pull/4601/head
Paul Balaji 4 weeks ago committed by GitHub
parent 330b0580e2
commit bd68ae01e2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 66
      typescript/infra/scripts/agents/deploy-agents.ts

@ -1,9 +1,52 @@
import { confirm } from '@inquirer/prompts';
import chalk from 'chalk';
import { execSync } from 'child_process';
import { createAgentKeysIfNotExists } from '../../src/agents/key-utils.js';
import { HelmCommand } from '../../src/utils/helm.js';
import { getConfigsBasedOnArgs } from '../core-utils.js';
import { AgentCli } from './utils.js';
async function fetchLatestMain() {
try {
console.log(
chalk.grey.italic('Fetching latest changes from origin/main...'),
);
execSync('git fetch origin main', { stdio: 'inherit' });
console.log(chalk.grey.italic('Fetch completed successfully.'));
} catch (error) {
console.error(chalk.red('Error fetching from origin/main:', error));
process.exit(1);
}
}
async function getCommitsBehindMain(): Promise<number> {
// Fetch latest changes before checking if current branch is up-to-date
await fetchLatestMain();
try {
console.log(
chalk.grey.italic(
'Checking if current branch is up-to-date with origin/main...',
),
);
const execResult = execSync(
'git rev-list --left-right --count origin/main...HEAD',
);
// The output of the git command is something like:
// $ git rev-list --left-right --count origin/main...HEAD
// 0 2
// We only care about the first number, which is the number of commits behind origin/main.
const [behindCount] = execResult.toString().trim().split('\t');
return parseInt(behindCount);
} catch (error) {
console.error(chalk.red('Error checking git status:', error));
process.exit(1);
}
}
async function main() {
// Note the create-keys script should be ran prior to running this script.
// At the moment, `runAgentHelmCommand` has the side effect of creating keys / users
@ -15,6 +58,29 @@ async function main() {
const { agentConfig } = await getConfigsBasedOnArgs();
await createAgentKeysIfNotExists(agentConfig);
// Check if current branch is up-to-date with the main branch
const commitsBehind = await getCommitsBehindMain();
// If the current branch is not up-to-date with origin/main, prompt the user to continue
if (commitsBehind > 0) {
const shouldContinue = await confirm({
message: chalk.yellow.bold(
`Warning: Current branch is ${commitsBehind} commit${
commitsBehind === 1 ? '' : 's'
} behind origin/main. Are you sure you want to continue?`,
),
default: false,
});
if (!shouldContinue) {
console.log(chalk.red.bold('Exiting...'));
process.exit(1);
}
} else {
console.log(
chalk.green.bold('Current branch is up-to-date with origin/main.'),
);
}
await new AgentCli().runHelmCommand(HelmCommand.InstallOrUpgrade);
}

Loading…
Cancel
Save