GitHub Actions Integration
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
GitHub Actions integration for the 2ms tool allows developers to automate the detection of secrets, authentication keys, and passwords within their CI/CD pipelines. By leveraging the 2ms Docker container, the integration enables seamless scanning of repository history and pull requests to ensure sensitive data is not leaked into the version control system.
The integration primarily focuses on the git scan command, which is designed to analyze local Git repositories. In a GitHub Actions environment, this typically involves checking out the repository with full history and running the 2ms engine against the codebase to identify potential security risks based on pre-defined or custom rules.
Sources: README.md:12-18, README.md:118-124
Workflow Architecture and Configuration
The integration is implemented by executing the 2ms Docker image within a workflow job. A critical requirement for accurate scanning in GitHub Actions is the retrieval of the complete commit history, as 2ms scans historical commits to find secrets that may have been introduced in previous versions.
Execution Flow
The standard integration follows a linear progression: environment setup, code checkout with specific parameters, and the execution of the scan via Docker.
This diagram illustrates the necessity of setting fetch-depth: 0 during the checkout phase to ensure the engine has visibility into the entire commit history.
Sources: README.md:123-145, plugins/git.go:49-55
Configuration Parameters
Integration within GitHub Actions utilizes YAML-based workflow files. The primary configuration points include the Docker image version and the specific scan commands.
| Component | Parameter | Description |
|---|---|---|
actions/checkout | fetch-depth: 0 | Mandatory. Fetches all history for all branches and tags. |
docker run | -v $(pwd):/repo | Mounts the current GitHub workspace to the container's /repo directory. |
2ms Command | git /repo | Instructs the tool to use the Git plugin on the mounted directory. |
2ms Image | checkmarx/2ms:<version> | The official Docker image containing the 2ms engine and plugins. |
Sources: README.md:130-145, README.md:99-105
Git Plugin Integration Logic
The GitHub Actions integration relies heavily on the GitPlugin defined in the codebase. This plugin manages how the repository is read and how diffs are processed during the CI run.
Scan Options and Depth
The plugin supports several flags that are particularly relevant in a CI context:
--all-branches: When enabled, the tool appends--allto the underlyinggit logcommand to scan beyond the current checked-out branch.--depth: Limits the number of historical commits scanned from theHEAD, which can be used to optimize CI runtimes if a full scan is not required.
func (p *GitPlugin) buildScanOptions() string {
options := []string{"--full-history"}
if p.scanAllBranches {
options = append(options, "--all")
}
if p.depth > 0 {
options = append(options, fmt.Sprintf("-n %d", p.depth))
}
return strings.Join(options, " ")
}
Sources: plugins/git.go:31-33, plugins/git.go:49-59
CI Pipeline Example
A typical implementation in .github/workflows/pipeline.yml utilizes the checkmarx/2ms image. The tool uses exit codes to signal success or failure to the GitHub Actions runner; a non-zero exit code (indicating secrets found or engine errors) will fail the build step.
jobs:
scan:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Run 2ms Scan
run: docker run -v $(pwd):/repo checkmarx/2ms:latest git /repo
Sources: README.md:135-145, CONTRIBUTING.md:15-18
Automated Testing and Quality Assurance
The 2ms project itself uses GitHub Actions for self-scanning and validation. This is managed through specific scripts and tests that run during the CI process.
- Self-Scanning: The project's own pipeline (e.g.,
release.yml) uses2msto scan its own repository for secrets. - Rule Validation: The
.ci/check_new_rules.goscript is used in the CI pipeline to compare local rules against the latest Gitleaks releases to ensure the tool remains up-to-date. - E2E Testing: The CI environment executes end-to-end tests located in
tests/e2e_test.go, which build the CLI and verify the detection logic against controlled secret samples.
Sources: README.md:147-148, CONTRIBUTING.md:12-21, tests/e2e_test.go:7-14
Conclusion
GitHub Actions integration provides a robust mechanism for enforcing secret-scanning policies automatically. By utilizing the git plugin via Docker and ensuring full repository history is available, 2ms can effectively identify leaked credentials within the CI/CD lifecycle, preventing security vulnerabilities from reaching production environments.