Skip to content

Commit 916662d

Browse files
authored
docs: add Dockerfile auto-discovery workflow pattern (#25)
Add documentation for automatically discovering Dockerfiles in repos with multiple Dockerfile locations. Uses a two-job workflow pattern where the first job finds Dockerfiles matching common patterns (Dockerfile, Dockerfile.*, *.dockerfile) while excluding test fixtures and build artifacts, then passes discovered paths to the scan job. This approach was chosen over building discovery into Socket Basics itself for better portability and per-repo customization.
1 parent 968b113 commit 916662d

1 file changed

Lines changed: 83 additions & 0 deletions

File tree

docs/github-action.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Complete guide to integrating Socket Basics into your GitHub Actions workflows f
88
- [Basic Configuration](#basic-configuration)
99
- [Enterprise Features](#enterprise-features)
1010
- [Advanced Workflows](#advanced-workflows)
11+
- [Dockerfile Auto-Discovery](#dockerfile-auto-discovery)
1112
- [Configuration Reference](#configuration-reference)
1213
- [Troubleshooting](#troubleshooting)
1314

@@ -385,6 +386,88 @@ jobs:
385386
trivy_vuln_enabled: 'true'
386387
```
387388

389+
### Dockerfile Auto-Discovery
390+
391+
For repositories with multiple Dockerfiles across different directories, you can automatically discover them instead of manually listing each path.
392+
393+
```yaml
394+
name: Security Scan with Dockerfile Auto-Discovery
395+
on:
396+
pull_request:
397+
types: [opened, synchronize, reopened]
398+
push:
399+
branches: [main]
400+
401+
jobs:
402+
discover-dockerfiles:
403+
runs-on: ubuntu-latest
404+
outputs:
405+
dockerfiles: ${{ steps.discover.outputs.dockerfiles }}
406+
steps:
407+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
408+
409+
- name: Discover Dockerfiles
410+
id: discover
411+
run: |
412+
DOCKERFILES=$(find . -type d \( \
413+
-name node_modules -o -name vendor -o -name .git -o \
414+
-name test -o -name tests -o -name testing -o -name __tests__ -o \
415+
-name fixture -o -name fixtures -o -name testdata -o \
416+
-name example -o -name examples -o -name sample -o -name samples -o \
417+
-name dist -o -name build -o -name out -o -name target -o \
418+
-name venv -o -name .venv -o -name .cache \
419+
\) -prune -o \
420+
-type f \( -name 'Dockerfile' -o -name 'Dockerfile.*' -o -name '*.dockerfile' \) \
421+
-print | sed 's|^./||' | paste -sd ',' -)
422+
423+
echo "Discovered Dockerfiles: $DOCKERFILES"
424+
echo "dockerfiles=$DOCKERFILES" >> $GITHUB_OUTPUT
425+
426+
security-scan:
427+
needs: discover-dockerfiles
428+
if: needs.discover-dockerfiles.outputs.dockerfiles != ''
429+
permissions:
430+
issues: write
431+
contents: read
432+
pull-requests: write
433+
runs-on: ubuntu-latest
434+
steps:
435+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
436+
437+
- name: Run Socket Basics
438+
uses: SocketDev/socket-basics@1.0.26
439+
env:
440+
GITHUB_PR_NUMBER: ${{ github.event.pull_request.number || github.event.issue.number }}
441+
with:
442+
github_token: ${{ secrets.GITHUB_TOKEN }}
443+
dockerfiles: ${{ needs.discover-dockerfiles.outputs.dockerfiles }}
444+
trivy_vuln_enabled: 'true'
445+
```
446+
447+
**How it works:**
448+
449+
1. **Discovery job** uses `find` to locate Dockerfiles matching common patterns:
450+
- `Dockerfile` (exact match)
451+
- `Dockerfile.*` (e.g., `Dockerfile.prod`, `Dockerfile.dev`)
452+
- `*.dockerfile` (e.g., `backend.dockerfile`)
453+
454+
2. **Excluded directories** prevent scanning test fixtures and build artifacts:
455+
- Package managers: `node_modules`, `vendor`, `venv`
456+
- Test directories: `test`, `tests`, `__tests__`, `fixtures`
457+
- Build outputs: `dist`, `build`, `out`, `target`
458+
459+
3. **Scan job** receives discovered paths via job output and skips if none found
460+
461+
**Customizing discovery patterns:**
462+
463+
```yaml
464+
# Only scan production Dockerfiles
465+
-type f -name 'Dockerfile.prod' -print
466+
467+
# Add custom exclusions
468+
-name custom_test_dir -o -name legacy -o \
469+
```
470+
388471
### Custom Rule Configuration
389472

390473
```yaml

0 commit comments

Comments
 (0)