Skip to content

feat: check if the pr-XXX images are to be merged #3

feat: check if the pr-XXX images are to be merged

feat: check if the pr-XXX images are to be merged #3

Workflow file for this run

name: Check for ephemeral PR images
on:
# push:
pull_request:
jobs:
check-files:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Check files matching glob patterns for pr-\d+ pattern
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
// const glob = require('@actions/glob');
// Define glob patterns for files to check for reference to PR images
const globPatterns = [
'**/*.yml',
'**/*.yaml',
'**/Dockerfile*',
'**/.env*',
];
// Exclude files from checks
const excludePatterns = [
'!**/.git/**',
'!**/docs/**'
'!**/ansible/**'
];
// Combine include and exclude patterns
const allPatterns = [...globPatterns, ...excludePatterns];
console.log('Inspecting files matching following glob patterns:');
globPatterns.forEach(pattern => console.log(` Include: ${pattern}`));
excludePatterns.forEach(pattern => console.log(` Exclude: ${pattern}`));
console.log('');
const globber = await glob.create(allPatterns.join('\n'));
const filesToCheck = await globber.glob();
console.log(`Found ${filesToCheck.length} files matching glob patterns:`);
filesToCheck.forEach(file => console.log(` - ${file}`));
console.log('');
const prPattern = /pr-\d+/;
let hasPatternMatch = false;
const matchedFiles = [];
for (const filePath of filesToCheck) {
try {
const fileContent = fs.readFileSync(filePath, 'utf8');
if (prPattern.test(fileContent)) {
hasPatternMatch = true;
matchedFiles.push(filePath);
console.log(`❌ Ephemeral container image reference found in: ${filePath}`);
} else {
console.log(`✅ No pattern found in: ${filePath}`);
}
} catch (error) {
console.log(`Error reading file ${filePath}: ${error.message}`);
}
}
if (hasPatternMatch) {
core.setFailed(`Pattern 'pr-\\d+' found in ${matchedFiles.length} files: ${matchedFiles.join(', ')}`);
console.log(`Please switch to a different tag before merging as this container image will not be available after PR is merged`)
console.log(`Alternatively, if this is intended edit .github/workflows/no-pr-images.yaml -> excludePatterns.`)
} else {
console.log(`✅ All ${filesToCheck.length} files checked - no pr-\\d+ pattern found`);
}