Skip to content

Commit 4e8e4bc

Browse files
committed
actions: depth: add action, gets fetch depth
Signed-off-by: Jorge Marques <jorge.marques@analog.com>
1 parent 21656cc commit 4e8e4bc

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

ci/actions/depth/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Depth
2+
=====
3+
4+
Sets the FETCH_DEPTH variable, to be passed to GitHub's checkout action.
5+
6+
Usage:
7+
8+
```
9+
on: workflow_call
10+
jobs:
11+
checks:
12+
runs-on: [self-hosted, v1]
13+
14+
steps:
15+
- uses: analogdevicesinc/doctools/range@v1
16+
- uses: actions/checkout@v4
17+
with:
18+
fetch-depth: ${{ env.FETCH_DEPTH }}
19+
```

ci/actions/depth/action.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Git depth
2+
description: Get fetch depth from GitHub context
3+
4+
# behaviour:
5+
# github.event.pull_request.head.sha is always used to detect the PR case.
6+
# Fetch depth is number of commits + 1 (base commit)
7+
8+
runs:
9+
using: composite
10+
steps:
11+
- name: Get commit depth
12+
shell: bash
13+
run: |
14+
if [[ "${{ github.event.pull_request.commits }}" ]]; then
15+
echo "set FETCH_DEPTH based on github.event.pull_request.commits"
16+
echo "FETCH_DEPTH=$(( ${{ github.event.pull_request.commits }} + 1 ))" >> $GITHUB_ENV
17+
elif [[ ! "${{ github.event.before }}" =~ "0000000000000000" ]] && [[ "${{ github.event.forced }}" == "false" ]]; then
18+
echo "set FETCH_DEPTH based on github.event.before"
19+
commits_sha='${{ toJSON(github.event.commits.*.id) }}'
20+
FETCH_DEPTH=$(( $(jq <<<"$commits_sha" length) + 1 ))
21+
echo "FETCH_DEPTH=$FETCH_DEPTH" >> $GITHUB_ENV
22+
else
23+
echo "setting FETCH_DEPTH as 6 (fallback, either force-push, push-new-branch, or unknown event type)"
24+
echo "FETCH_DEPTH=6" >> $GITHUB_ENV
25+
echo "FETCH_DEPTH_FALLBACK=true" >> $GITHUB_ENV
26+
fi
27+

0 commit comments

Comments
 (0)