GeezerVison is showing github.com — fork-ci: clean .git and fixed git config before checkout on self-host… · scuttletoby/t3code@c79e782
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: .github/fork-ci-tests.txt +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -45,6 +45,7 @@ fork-ci/actions/ci-demand/ci_demand.test.py ~13s | 45 | 45 | fork-ci/actions/docs-scope/docs-scope.test.py ~16s | 46 | 46 | fork-ci/actions/fleet-route/fleet_route.test.py | 47 | 47 | fork-ci/actions/fleet-watch/fleet_watch.test.py | | 48 | +fork-ci/actions/git-clean/git_clean.test.py | 48 | 49 | fork-ci/actions/gradle-cache/gradle_cache.test.py | 49 | 50 | fork-ci/actions/head-check/head_check.test.py ~12s | 50 | 51 | fork-ci/actions/head-check/head_gate.test.py | |
Collapse file Copy file name to clipboardExpand all lines: fork-ci/README.md +1Lines changed: 1 addition & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -63,6 +63,7 @@ Each action's README has a full example, every input and output, and what happen | 63 | 63 | | [gradle-cache](actions/gradle-cache/README.md) | stages the Maven files a lock lists, sha256-checked, from release assets | `lock`, `out`, `token` | `maven-repo-local` | the lock file | | 64 | 64 | | [fleet-route](actions/fleet-route/README.md) | which runner each job of a run gets: the project's self-hosted pools or hosted | `config`, `vars`, `mode`, `gates` | `runners`, `mode`, `attempt`, `fallback`, `watch` | the fleet config (e.g. `.github/fleet.json`) | | 65 | 65 | | [fleet-watch](actions/fleet-watch/README.md) | re-runs, once, a run that a dispatcher pool's runner cut off | `config`, `verb`, `run-id`, `attempt` | `rerun`, `pending` | the fleet config (its `watch` section) | | | 66 | +| [git-clean](actions/git-clean/README.md) | empties a reused self-hosted workspace, refuses a .git config it does not know | `path` | none | none | | 66 | 67 | | [head-check](actions/head-check/README.md) | fails a pull request head that is not the change its review read | `config`, `required-lenses`, `token` | `result` | the pr-queue config | | 67 | 68 | | [pr-queue](actions/pr-queue/README.md) | a merge queue for an up-to-date-required repository (also a reusable workflow) | `config`, `state-file`, `cache`, `token` | none | the pr-queue config | | 68 | 69 | | [weakened-tests](actions/weakened-tests/README.md) | reports (never fails) the existing tests a pull request deletes, skips or weakens | `config`, `token` | `status`, `count`, `missing_why` | `.github/weakened-tests.json` (optional: added patterns) | | |
Collapse file Copy file name to clipboardExpand all lines: fork-ci/actions/docs-scope/action.yml +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -31,6 +31,10 @@ outputs: | 31 | 31 | runs: | 32 | 32 | using: composite | 33 | 33 | steps: | | 34 | + # Before any git call: a .git config that actions/checkout does not write (one an earlier job left) is refused. | | 35 | + - name: Refuse a git config actions/checkout does not write (fork-ci git-clean) | | 36 | + shell: bash | | 37 | + run: exec python3 -I "$GITHUB_ACTION_PATH/../git-clean/git_clean.py" check --path . --require | 34 | 38 | # Only code from this action's folder (the caller's pinned SHA) runs; nothing from the checkout. | 35 | 39 | # The script also reads the runner's GITHUB_SHA and GITHUB_EVENT_PATH (HEAD must be the run's commit). | 36 | 40 | - id: scope | |
Collapse file Copy file name to clipboardExpand all lines: fork-ci/actions/docs-scope/docs-scope.test.py +4-3Lines changed: 4 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -889,14 +889,15 @@ class Action(CleanEnv): | 889 | 889 |
| 890 | 890 | def test_runs_only_its_own_script(self): | 891 | 891 | runs = re.findall(r'^\s*run: (.*)$', self.text, re.M) | 892 | | - self.assertEqual(runs, ['python3 -I "$GITHUB_ACTION_PATH/docs_scope.py" --config "$CONFIG" ' | | 892 | + self.assertEqual(runs, ['exec python3 -I "$GITHUB_ACTION_PATH/../git-clean/git_clean.py" check --path . --require', # the git-clean check of this pinned SHA | | 893 | + 'python3 -I "$GITHUB_ACTION_PATH/docs_scope.py" --config "$CONFIG" ' | 893 | 894 | '--event "$EVENT" --base "$BASE"']) | 894 | 895 | self.assertNotIn('uses:', self.text) | 895 | 896 | self.assertIn('using: composite', self.text) | 896 | 897 |
| 897 | 898 | def test_inputs_reach_the_script_by_env_only(self): # never ${{ }} inside run: (script injection) | 898 | | - run = re.search(r'^\s*run: (.*)$', self.text, re.M)[1] | 899 | | - self.assertNotIn('${{', run) | | 899 | + for run in re.findall(r'^\s*run: (.*)$', self.text, re.M): | | 900 | + self.assertNotIn('${{', run) | 900 | 901 |
| 901 | 902 | def test_declares_every_output_the_script_writes(self): | 902 | 903 | for name in ('docs_only', 'jobs', 'reason'): | |
Collapse file Copy file name to clipboardExpand all lines: fork-ci/actions/docs-scope/docs_scope.py +9-1Lines changed: 9 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -47,9 +47,17 @@ class Unclear(Exception): | 47 | 47 | """Anything that makes the answer unknown: the run is full and this is the reason.""" | 48 | 48 |
| 49 | 49 |
| | 50 | +# Every git call: no global or system config (on a self-hosted runner an earlier job may have written ~/.gitconfig), and | | 51 | +# no hooks, fsmonitor command, commit-graph or replace refs from the repository. The same as | | 52 | +# ../git-clean/git_clean.py's (git_clean.test.py pins them); the action's first step refused any other .git config. | | 53 | +GIT_ENV = {'GIT_CONFIG_GLOBAL': os.devnull, 'GIT_CONFIG_NOSYSTEM': '1'} | | 54 | +GIT_SAFE = ('-c', 'core.hooksPath=/dev/null', '-c', 'core.fsmonitor=false', '-c', 'core.commitGraph=false', | | 55 | + '--no-replace-objects') | | 56 | + | | 57 | + | 50 | 58 | def git(*args): | 51 | 59 | """stdout, or None when git fails. Bytes that are not UTF-8 survive as lone surrogates (never a crash).""" | 52 | | - r = subprocess.run(['git', *args], capture_output=True) | | 60 | + r = subprocess.run(['git', *GIT_SAFE, *args], capture_output=True, env={**os.environ, **GIT_ENV}) | 53 | 61 | return r.stdout.decode('utf-8', 'surrogateescape') if r.returncode == 0 else None | 54 | 62 |
| 55 | 63 |
| |
Collapse file Copy file name to clipboard +66Lines changed: 66 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# git-clean | | 2 | + | | 3 | +A composite action for the **first step of a job, before `actions/checkout`**. A self-hosted runner reuses its | | 4 | +workspace, and an earlier job may have run pull request code there. That code can write `.git/config` keys that | | 5 | +`actions/checkout` does not reset: `core.fsmonitor` (a command git runs during the checkout's own git calls), | | 6 | +`core.hooksPath`, `attr.tree` with a merge driver, an include of another file. The next job would run them before any | | 7 | +of its own steps runs pull request code. | | 8 | + | | 9 | +- **Self-hosted runner:** it empties the workspace (`GITHUB_WORKSPACE`), so the checkout starts from nothing, as on a | | 10 | + GitHub-hosted runner. Symlinks are removed as links, never followed: nothing outside the workspace is touched. | | 11 | +- **GitHub-hosted runner:** each job starts on a fresh disk, so nothing is removed. | | 12 | +- **Both:** it then refuses a `.git` at the checkout path whose config `actions/checkout` does not write (below). | | 13 | + | | 14 | +The fork-ci actions that read a checkout with git (docs-scope, job-changed, test-select, head-check, pr-queue's | | 15 | +tested-base) run the same check as their first step, and every git call of theirs runs with no global or system | | 16 | +config (`GIT_CONFIG_GLOBAL=/dev/null`, `GIT_CONFIG_NOSYSTEM=1`) and with hooks, `core.fsmonitor`, the commit-graph and | | 17 | +replace refs off. tested-base writes no note instead of failing. | | 18 | + | | 19 | +## Usage | | 20 | + | | 21 | +Pin a release commit by its full SHA, never a commit of main ([why](../../README.md#pinning)): | | 22 | + | | 23 | +```yaml | | 24 | +jobs: | | 25 | + build: | | 26 | + runs-on: [self-hosted, build] | | 27 | + steps: | | 28 | + - uses: scuttletoby/t3code/fork-ci/actions/git-clean@<release sha> # ci/vN | | 29 | + - uses: actions/checkout@<sha> | | 30 | + with: { fetch-depth: 0 } | | 31 | +``` | | 32 | +
| | 33 | +Put it in every job that runs on a self-hosted runner and checks out code, before the first checkout and before any | | 34 | +other step that writes the workspace. With several checkouts, list their paths (`path: | ...`): all of them are | | 35 | +checked. | | 36 | + | | 37 | +| Input | Default | Meaning | | | 38 | +| ------ | ------- | -------------------------------------------------------------------- | | | 39 | +| `path` | `.` | the checkout path or paths (actions/checkout's `path`), one per line | | | 40 | + | | 41 | +No outputs. A refusal fails the step, so the checkout and every later step do not run. | | 42 | + | | 43 | +## What is refused | | 44 | + | | 45 | +- `GITHUB_WORKSPACE` that is not an absolute folder, is a symlink, is not inside `RUNNER_WORKSPACE`, or holds `HOME`, | | 46 | + `RUNNER_TEMP`, `RUNNER_TOOL_CACHE` or the action itself: nothing is removed. | | 47 | +- A delete that fails (a file of another user, for example from a container): the step fails with the path. What is | | 48 | + left is removed by the next run once the file can be deleted. | | 49 | +- A checkout path that is absolute or leads outside the workspace. | | 50 | +- `<path>/.git` that is a file or a symlink (a worktree or submodule: git would read another repository), or that holds | | 51 | + `commondir`. | | 52 | +- A key in `.git/config` or `.git/config.worktree` that `actions/checkout` (v4 to v7) and `git init` do not write, or | | 53 | + one of those keys with another kind of value. Allowed: the core settings of `git init`, sparse checkout, the object | | 54 | + format, `gc.auto 0`, `remote.origin` url/fetch/partial clone, `branch.*.remote`/`merge`, the token header, the LFS | | 55 | + filter exactly as `git lfs install` writes it, submodule url/active, `user.name`/`user.email`. The include | | 56 | + checkout writes for its token (`includeIf.gitdir:...path`) only to a `git-credentials-<uuid>.config` in | | 57 | + `RUNNER_TEMP` that holds nothing but the token header. Everything else is refused by name, among it | | 58 | + `core.fsmonitor`, `core.hooksPath`, `core.sshCommand` (checkout's `ssh-key` with `persist-credentials`: use | | 59 | + `persist-credentials: false`), `core.pager`, `attr.tree`, merge/diff/filter drivers, `include.path`, other | | 60 | + includes, `url.*.insteadOf` and `alias.*`. | | 61 | + | | 62 | +## Not covered | | 63 | + | | 64 | +The runner user's own files outside the workspace (`~/.gitconfig`, the system git config) are not removed: the fork-ci | | 65 | +git calls ignore them, `actions/checkout` does not. A runner that takes pull requests from people who are not trusted | | 66 | +also needs a job home that does not outlive the job. |
Collapse file Copy file name to clipboard +19Lines changed: 19 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +name: git-clean | | 2 | +description: >- | | 3 | + The first step of a job, before actions/checkout. On a self-hosted runner it empties the reused workspace, so a git | | 4 | + config, hook or file an earlier job (pull request code) left there cannot run during this job's checkout. On a | | 5 | + GitHub-hosted runner (a fresh disk) it removes nothing. Then it refuses a .git config at the checkout path that | | 6 | + actions/checkout does not write. Needs git and python3. | | 7 | +inputs: | | 8 | + path: | | 9 | + description: The checkout path or paths (actions/checkout's path), one per line, relative to the workspace. | | 10 | + required: false | | 11 | + default: "." | | 12 | +runs: | | 13 | + using: composite | | 14 | + steps: | | 15 | + - name: Empty a reused workspace; refuse a git config actions/checkout does not write | | 16 | + shell: bash | | 17 | + env: | | 18 | + CHECKOUT_PATH: ${{ inputs.path }} | | 19 | + run: exec python3 -I "$GITHUB_ACTION_PATH/git_clean.py" prepare --path "$CHECKOUT_PATH" |
|