Skip to content

Commit bd6b200

Browse files
committed
prepare-workspace: make actions/checkout more reliable
Make an empty detached commit for actions/checkout to properly clean the project's workspace before checking out new code revision. It makes the actions/checkout resilient to various problems related to git submodules. Part of #28 Related to tarantool/tarantool-qa#145
1 parent 1e1d91e commit bd6b200

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

cleanup/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Setup environment
1+
# Clean up the workspace
22

33
Action cleans workspace directory after previous run. The main reason to add
44
this action is [tarantool/tarantool-qa#145](https://github.com/tarantool/tarantool-qa/issues/145).

prepare-checkout/README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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.

prepare-checkout/action.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
name: 'Prepare workspace for checkout'
3+
description: 'Prepare workspace for the actions/checkout action'
4+
runs:
5+
using: 'composite'
6+
steps:
7+
- run: |
8+
set -e
9+
git checkout -f \
10+
$(git -c user.name=TarantoolBot -c [email protected] \
11+
commit-tree $(git hash-object -t tree /dev/null) -m 'Empty commit' \
12+
< /dev/null) || :
13+
shell: bash

0 commit comments

Comments
 (0)