|
| 1 | +# Prepare workspace for checkout action |
| 2 | + |
| 3 | +This action creates and checks out an empty detached commit. |
| 4 | +It helps subsequent [`actions/checkout`](https://github.com/actions/checkout) action |
| 5 | +correctly clean the workspace. |
| 6 | + |
| 7 | +## Usage |
| 8 | + |
| 9 | +Add the following line to your workflow before the `actions/checkout` action: |
| 10 | + |
| 11 | +```diff |
| 12 | ++ - uses: tarantool/actions/prepare-checkout@master |
| 13 | + - uses: actions/checkout@v3 |
| 14 | + ... |
| 15 | +``` |
| 16 | + |
| 17 | +## Explanation and rationale |
| 18 | + |
| 19 | +This action is a solution for |
| 20 | +[tarantool/tarantool-qa#145](https://github.com/tarantool/tarantool-qa/issues/145). |
| 21 | +First attempt to solve this was the `tarantool/actions/cleanup` action. |
| 22 | +The `tarantool/actions/prepare-checkout` action is a softer alternative to it. |
| 23 | + |
| 24 | +When submodules change, `actions/checkout` can fail with git errors. |
| 25 | +It's a well-known problem and the related issues are still open: |
| 26 | +actions/checkout#354, |
| 27 | +actions/checkout#385, |
| 28 | +actions/checkout#418, and |
| 29 | +actions/checkout#590. |
| 30 | + |
| 31 | +Before checking out a new revision, `actions/checkout` runs the following code: |
| 32 | + |
| 33 | +```bash |
| 34 | +# removes ignored and non-versioned files |
| 35 | +git clean -ffdx |
| 36 | +# resets workspace to the commit, on which it was left |
| 37 | +# after the last job run |
| 38 | +git reset --hard |
| 39 | +``` |
| 40 | + |
| 41 | +The problem is that when a workflow fails because of a particular commit, |
| 42 | +the repository still stays on that commit. On the next job run, |
| 43 | +`actions/checkout` will run the above code, restore that particular commit, |
| 44 | +and fail to make a proper code checkout. |
| 45 | + |
| 46 | +By creating a detached empty commit, this actions forces `actions/checkout` |
| 47 | +to clean up the project's workspace entirely, removing any files that could |
| 48 | +break checkout. Meanwhile, the `.git` directory stays intact, so full checkout |
| 49 | +isn't required and the workflow does not waste much time. |
| 50 | + |
| 51 | +If this is a first job run on a particular runner and there's no repository yet, |
| 52 | +the command in this action will silently fail, thanks to `|| :`, |
| 53 | +but the action itself will succeed. |
| 54 | + |
| 55 | +This action uses a solution proposed in a |
| 56 | +[comment](https://github.com/actions/checkout/issues/590#issuecomment-970586842) |
| 57 | +at actions/checkout#590. |
0 commit comments