Skip to content
Prev Previous commit
Next Next commit
fix: reorganize post actions
  • Loading branch information
SimonBriche committed Nov 18, 2025
commit c7d4148a08bd88941741ffca3a9366f0e4a2974f
2 changes: 1 addition & 1 deletion src/clean-pip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ export async function cleanPipPackages() {
]);
Comment on lines +10 to +13

Copilot AI Nov 19, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bash one-liner is complex and difficult to read. Consider breaking this into multiple steps or commands for better maintainability. The logic combines multiple conditions and commands that would be clearer if separated.

Suggested change
await exec('bash', [
'-c',
'test $(which python) != "/usr/bin/python" -a $(python -m pip freeze | wc -l) -gt 0 && python -m pip freeze | xargs python -m pip uninstall -y || true'
]);
// Step 1: Check if python is not /usr/bin/python
let pythonPath = '';
await exec('which python', [], {
listeners: {
stdout: (data: Buffer) => {
pythonPath += data.toString();
}
}
});
pythonPath = pythonPath.trim();
if (pythonPath !== '/usr/bin/python') {
// Step 2: Check if there are any installed pip packages
let pipFreezeOutput = '';
await exec('python', ['-m', 'pip', 'freeze'], {
listeners: {
stdout: (data: Buffer) => {
pipFreezeOutput += data.toString();
}
}
});
const packages = pipFreezeOutput
.split('\n')
.map(line => line.trim())
.filter(line => line.length > 0);
if (packages.length > 0) {
// Step 3: Uninstall all pip packages
await exec('python', ['-m', 'pip', 'uninstall', '-y', ...packages]);
}
}

Copilot uses AI. Check for mistakes.
Comment on lines +8 to +13

Copilot AI Nov 19, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function assumes bash is available, which may not be true on all platforms (e.g., Windows runners). Consider using platform-agnostic shell commands or checking the platform before executing bash-specific commands.

Suggested change
// uninstall all currently installed packages (if any)
// Use a shell so we can pipe the output of pip freeze into xargs
await exec('bash', [
'-c',
'test $(which python) != "/usr/bin/python" -a $(python -m pip freeze | wc -l) -gt 0 && python -m pip freeze | xargs python -m pip uninstall -y || true'
]);
// Get list of installed pip packages
let pipFreezeOutput = '';
await exec('python', ['-m', 'pip', 'freeze'], {
listeners: {
stdout: (data: Buffer) => {
pipFreezeOutput += data.toString();
}
}
});
const packages = pipFreezeOutput
.split('\n')
.map(line => line.trim())
.filter(line => line.length > 0 && !line.startsWith('#'));
if (packages.length > 0) {
for (const pkg of packages) {
// Only uninstall the package name (ignore version specifier)
const pkgName = pkg.split('==')[0];
await exec('python', ['-m', 'pip', 'uninstall', '-y', pkgName]);
}
}

Copilot uses AI. Check for mistakes.
core.info('Successfully cleaned up pip packages');
} catch (error) {
core.setFailed(`Failed to clean up pip packages.`);
core.setFailed('Failed to clean up pip packages.');
}
}
19 changes: 11 additions & 8 deletions src/post-python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,24 @@ import {State} from './cache-distributions/cache-distributor';
export async function run(earlyExit?: boolean) {
try {
const cache = core.getInput('cache');
if (cache) {
await saveCache(cache);
// Optionally clean up pip packages after the post-action if requested.
// This mirrors the `preclean` behavior used in the main action.
const postcleanPip = core.getBooleanInput('postclean');

if (cache || postcleanPip) {
if (cache) {
await saveCache(cache);
}
if (postcleanPip) {
await cleanPipPackages();
}
// Preserve early-exit behavior for the post action when requested.
// Some CI setups may want the post step to exit early to avoid long-running
// processes during cleanup. If enabled, exit with success immediately.
if (earlyExit) {
process.exit(0);
}
}
// Optionally clean up pip packages after the post-action if requested.
// This mirrors the `preclean-pip` behavior used in the main action.
const postcleanPip = core.getBooleanInput('postclean');
if (postcleanPip) {
await cleanPipPackages();
}
} catch (error) {
const err = error as Error;
core.setFailed(err.message);
Expand Down